Change assignment into expressions
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -108,6 +108,8 @@ func TestIfElseExpression(t *testing.T) {
|
||||
{"if (1 > 2) { 10 }", nil},
|
||||
{"if (1 > 2) { 10 } else { 20 }", 20},
|
||||
{"if (1 < 2) { 10 } else { 20 }", 10},
|
||||
{"if (1 < 2) { 10 } else if (1 == 2) { 20 }", 10},
|
||||
{"if (1 > 2) { 10 } else if (1 == 2) { 20 } else { 30 }", 30},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -242,24 +244,45 @@ func TestErrorHandling(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignmentStatements(t *testing.T) {
|
||||
func TestIndexAssignmentStatements(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected int64
|
||||
}{
|
||||
{"let a = 0; a = 5;", 5},
|
||||
{"let xs = [1, 2, 3]; xs[1] = 4; xs[1];", 4},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
evaluated := testEval(tt.input)
|
||||
testIntegerObject(t, evaluated, tt.expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignmentStatements(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected interface{}
|
||||
}{
|
||||
{"let a = 0; a = 5;", nil},
|
||||
{"let a = 0; a = 5; a;", 5},
|
||||
{"let a = 0; a = 5 * 5;", 25},
|
||||
{"let a = 0; a = 5 * 5;", nil},
|
||||
{"let a = 0; a = 5 * 5; a;", 25},
|
||||
{"let a = 0; a = 5; let b = 0; b = a;", 5},
|
||||
{"let a = 0; a = 5; let b = 0; b = a;", nil},
|
||||
{"let a = 0; a = 5; let b = 0; b = a; b;", 5},
|
||||
{"let a = 0; a = 5; let b = 0; b = a; let c = 0; c = a + b + 5;", 15},
|
||||
{"let a = 0; a = 5; let b = 0; b = a; let c = 0; c = a + b + 5;", nil},
|
||||
{"let a = 0; a = 5; let b = 0; b = a; let c = 0; c = a + b + 5; c;", 15},
|
||||
{"let a = 5; let b = a; a = 0;", nil},
|
||||
{"let a = 5; let b = a; a = 0; b;", 5},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
testIntegerObject(t, testEval(tt.input), tt.expected)
|
||||
evaluated := testEval(tt.input)
|
||||
integer, ok := tt.expected.(int)
|
||||
if ok {
|
||||
testIntegerObject(t, evaluated, int64(integer))
|
||||
} else {
|
||||
testNullObject(t, evaluated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -585,8 +608,12 @@ func TestWhileExpressions(t *testing.T) {
|
||||
{"while (false) { }", nil},
|
||||
{"let n = 0; while (n < 10) { let n = n + 1 }; n", 10},
|
||||
{"let n = 10; while (n > 0) { let n = n - 1 }; n", 0},
|
||||
{"let n = 0; while (n < 10) { n = n + 1 }", 10},
|
||||
{"let n = 10; while (n > 0) { n = n - 1 }", 0},
|
||||
{"let n = 0; while (n < 10) { let n = n + 1 }", nil},
|
||||
{"let n = 10; while (n > 0) { let n = n - 1 }", nil},
|
||||
{"let n = 0; while (n < 10) { n = n + 1 }; n", 10},
|
||||
{"let n = 10; while (n > 0) { n = n - 1 }; n", 0},
|
||||
{"let n = 0; while (n < 10) { n = n + 1 }", nil},
|
||||
{"let n = 10; while (n > 0) { n = n - 1 }", nil},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user