diff options
| author | Bobby <[email protected]> | 2024-04-04 16:25:56 +0000 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-04-04 16:25:56 +0000 |
| commit | 77d0ea46b3f1de99357c7706c4c05eb44c237412 (patch) | |
| tree | d6fa8ca0a89233b26591eaed72c1017ad6190dba /evaluator/evaluator.go | |
| parent | 3d4f24f7c4ea05471109c0b13abbc95e70c6924b (diff) | |
| download | mana-77d0ea46b3f1de99357c7706c4c05eb44c237412.tar.xz mana-77d0ea46b3f1de99357c7706c4c05eb44c237412.zip | |
arrays and array indexes
Diffstat (limited to 'evaluator/evaluator.go')
| -rw-r--r-- | evaluator/evaluator.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 86d1889..913138b 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -83,6 +83,30 @@ func Eval(node ast.Node, env *object.Environment) object.Object { return &object.Function{Parameters: params, Body: body, Env: env} + case *ast.ArrayLiteral: + elements := evalExpressions(node.Elements, env) + + if len(elements) == 1 && isError(elements[0]) { + return elements[0] + } + + return &object.Array{Elements: elements} + + case *ast.IndexExpression: + left := Eval(node.Left, env) + + if isError(left) { + return left + } + + index := Eval(node.Index, env) + + if isError(index) { + return index + } + + return evalIndexExpression(left, index) + case *ast.StringLiteral: return &object.String{Value: node.Value} @@ -291,6 +315,27 @@ func evalIfExpression(ie *ast.IfExpression, env *object.Environment) object.Obje } } +func evalIndexExpression(left, index object.Object) object.Object { + switch { + case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ: + return evalArrayIndexExpression(left, index) + default: + return newError("index operator not supported: %s", left.Type()) + } +} + +func evalArrayIndexExpression(array, index object.Object) object.Object { + arrayObject := array.(*object.Array) + idx := index.(*object.Integer).Value + max := int64(len(arrayObject.Elements) - 1) + + if idx < 0 || idx > max { + return NULL + } + + return arrayObject.Elements[idx] +} + func evalIdentifier(node *ast.Identifier, env *object.Environment) object.Object { if val, ok := env.Get(node.Value); ok { return val |
