Skip to content

Commit

Permalink
syntx-quote recurses into hashes and arrays. Fixes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
glycerine committed Jan 10, 2016
1 parent 8347a1b commit 9aaed84
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 126 deletions.
18 changes: 16 additions & 2 deletions interpreter/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ func (pair SexpPair) SexpString() string {
}

type SexpArray []Sexp
type SexpHash map[int][]SexpPair
type SexpHash struct {
TypeName *string
Map map[int][]SexpPair
KeyOrder *[]Sexp // must user pointers here, else hset! will fail to update.
GoStruct *interface{}
NumKeys *int
}
type SexpInt int
type SexpBool bool
type SexpFloat float64
Expand All @@ -99,7 +105,7 @@ func (arr SexpArray) SexpString() string {

func (hash SexpHash) SexpString() string {
str := "{"
for _, arr := range hash {
for _, arr := range hash.Map {
for _, pair := range arr {
str += pair.head.SexpString() + " "
str += pair.tail.SexpString() + " "
Expand Down Expand Up @@ -178,3 +184,11 @@ func IsTruthy(expr Sexp) bool {
}
return true
}

type SexpStackmark struct {
sym SexpSymbol
}

func (mark SexpStackmark) SexpString() string {
return "stackmark " + mark.sym.name
}
131 changes: 70 additions & 61 deletions interpreter/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,17 @@ func HashAccessFunction(env *Glisp, name string, args []Sexp) (Sexp, error) {
switch name {
case "hget":
if len(args) == 3 {
return HashGetDefault(hash, args[1], args[2])
return hash.HashGetDefault(args[1], args[2])
}
return HashGet(hash, args[1])
return hash.HashGet(args[1])
case "hset!":
err := HashSet(hash, args[1], args[2])
err := hash.HashSet(args[1], args[2])
return SexpNull, err
case "hdel!":
if len(args) != 2 {
return SexpNull, WrongNargs
}
err := HashDelete(hash, args[1])
err := hash.HashDelete(args[1])
return SexpNull, err
}

Expand Down Expand Up @@ -544,7 +544,7 @@ func ConstructorFunction(env *Glisp, name string, args []Sexp) (Sexp, error) {
case "list":
return MakeList(args), nil
case "hash":
return MakeHash(args)
return MakeHash(args, "hash")
}
return SexpNull, errors.New("invalid constructor")
}
Expand Down Expand Up @@ -636,60 +636,69 @@ func MakeUserFunction(name string, ufun GlispUserFunction) SexpFunction {
}

var BuiltinFunctions = map[string]GlispUserFunction{
"<": CompareFunction,
">": CompareFunction,
"<=": CompareFunction,
">=": CompareFunction,
"=": CompareFunction,
"not=": CompareFunction,
"sll": BinaryIntFunction,
"sra": BinaryIntFunction,
"srl": BinaryIntFunction,
"mod": BinaryIntFunction,
"+": NumericFunction,
"-": NumericFunction,
"*": NumericFunction,
"/": NumericFunction,
"bit-and": BitwiseFunction,
"bit-or": BitwiseFunction,
"bit-xor": BitwiseFunction,
"bit-not": ComplementFunction,
"read": ReadFunction,
"cons": ConsFunction,
"first": FirstFunction,
"rest": RestFunction,
"car": FirstFunction,
"cdr": RestFunction,
"list?": TypeQueryFunction,
"null?": TypeQueryFunction,
"array?": TypeQueryFunction,
"hash?": TypeQueryFunction,
"number?": TypeQueryFunction,
"int?": TypeQueryFunction,
"float?": TypeQueryFunction,
"char?": TypeQueryFunction,
"symbol?": TypeQueryFunction,
"string?": TypeQueryFunction,
"zero?": TypeQueryFunction,
"empty?": TypeQueryFunction,
"println": PrintFunction,
"print": PrintFunction,
"not": NotFunction,
"apply": ApplyFunction,
"map": MapFunction,
"make-array": MakeArrayFunction,
"aget": ArrayAccessFunction,
"aset!": ArrayAccessFunction,
"sget": SgetFunction,
"hget": HashAccessFunction,
"hset!": HashAccessFunction,
"hdel!": HashAccessFunction,
"slice": SliceFunction,
"len": LenFunction,
"append": AppendFunction,
"concat": ConcatFunction,
"array": ConstructorFunction,
"list": ConstructorFunction,
"hash": ConstructorFunction,
"symnum": SymnumFunction,
"<": CompareFunction,
">": CompareFunction,
"<=": CompareFunction,
">=": CompareFunction,
"=": CompareFunction,
"not=": CompareFunction,
"sll": BinaryIntFunction,
"sra": BinaryIntFunction,
"srl": BinaryIntFunction,
"mod": BinaryIntFunction,
"+": NumericFunction,
"-": NumericFunction,
"*": NumericFunction,
"/": NumericFunction,
"bit-and": BitwiseFunction,
"bit-or": BitwiseFunction,
"bit-xor": BitwiseFunction,
"bit-not": ComplementFunction,
"read": ReadFunction,
"cons": ConsFunction,
"first": FirstFunction,
"rest": RestFunction,
"car": FirstFunction,
"cdr": RestFunction,
"list?": TypeQueryFunction,
"null?": TypeQueryFunction,
"array?": TypeQueryFunction,
"hash?": TypeQueryFunction,
"number?": TypeQueryFunction,
"int?": TypeQueryFunction,
"float?": TypeQueryFunction,
"char?": TypeQueryFunction,
"symbol?": TypeQueryFunction,
"string?": TypeQueryFunction,
"zero?": TypeQueryFunction,
"empty?": TypeQueryFunction,
"println": PrintFunction,
"print": PrintFunction,
"not": NotFunction,
"apply": ApplyFunction,
"map": MapFunction,
"make-array": MakeArrayFunction,
"aget": ArrayAccessFunction,
"aset!": ArrayAccessFunction,
"sget": SgetFunction,
"hget": HashAccessFunction,
"hset!": HashAccessFunction,
"hdel!": HashAccessFunction,
"slice": SliceFunction,
"len": LenFunction,
"append": AppendFunction,
"concat": ConcatFunction,
"array": ConstructorFunction,
"list": ConstructorFunction,
"hash": ConstructorFunction,
"symnum": SymnumFunction,
"str": StringifyFunction,
}

func StringifyFunction(env *Glisp, name string, args []Sexp) (Sexp, error) {
if len(args) != 1 {
return SexpNull, WrongNargs
}

return SexpStr(args[0].SexpString()), nil
}
Loading

0 comments on commit 9aaed84

Please sign in to comment.