Change assignment into expressions
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-19 20:30:30 -04:00
parent aa0582ed72
commit be81b9a6d6
12 changed files with 478 additions and 153 deletions

View File

@@ -45,20 +45,54 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
}
return &object.ReturnValue{Value: val}
case *ast.AssignmentStatement:
obj := evalIdentifier(node.Name, env)
if isError(obj) {
return obj
case *ast.AssignmentExpression:
left := Eval(node.Left, env)
if isError(left) {
return left
}
val := Eval(node.Value, env)
if isError(val) {
return val
value := Eval(node.Value, env)
if isError(value) {
return value
}
env.Set(node.Name.Value, val)
if ident, ok := node.Left.(*ast.Identifier); ok {
env.Set(ident.Value, value)
} else if ie, ok := node.Left.(*ast.IndexExpression); ok {
obj := Eval(ie.Left, env)
if isError(obj) {
return obj
}
return val
if array, ok := obj.(*object.Array); ok {
index := Eval(ie.Index, env)
if isError(index) {
return index
}
if idx, ok := index.(*object.Integer); ok {
array.Elements[idx.Value] = value
} else {
return newError("cannot index array with %#v", index)
}
} else if hash, ok := obj.(*object.Hash); ok {
key := Eval(ie.Index, env)
if isError(key) {
return key
}
if hashKey, ok := key.(object.Hashable); ok {
hashed := hashKey.HashKey()
hash.Pairs[hashed] = object.HashPair{Key: key, Value: value}
} else {
return newError("cannot index hash with %T", key)
}
} else {
return newError("object type %T does not support item assignment", obj)
}
} else {
return newError("expected identifier or index expression got=%T", left)
}
return NULL
case *ast.LetStatement:
val := Eval(node.Value, env)