Environment and identifiers
This commit is contained in:
@@ -180,6 +180,10 @@ func TestErrorHandling(t *testing.T) {
|
||||
`,
|
||||
"unknown operator: BOOLEAN + BOOLEAN",
|
||||
},
|
||||
{
|
||||
"foobar",
|
||||
"identifier not found: foobar",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -199,12 +203,29 @@ func TestErrorHandling(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLetStatements(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected int64
|
||||
}{
|
||||
{"let a = 5; a;", 5},
|
||||
{"let a = 5 * 5; a;", 25},
|
||||
{"let a = 5; let b = a; b;", 5},
|
||||
{"let a = 5; let b = a; let c = a + b + 5; c;", 15},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
testIntegerObject(t, testEval(tt.input), tt.expected)
|
||||
}
|
||||
}
|
||||
|
||||
func testEval(input string) object.Object {
|
||||
l := lexer.New(input)
|
||||
p := parser.New(l)
|
||||
program := p.ParseProgram()
|
||||
env := object.NewEnvironment()
|
||||
|
||||
return Eval(program)
|
||||
return Eval(program, env)
|
||||
}
|
||||
|
||||
func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool {
|
||||
|
||||
Reference in New Issue
Block a user