Environment and identifiers

This commit is contained in:
Chuck Smith
2024-01-20 08:30:34 -05:00
parent 44d20ba7a0
commit e3be13cb71
4 changed files with 80 additions and 19 deletions

View File

@@ -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 {