|
|
|
@ -277,6 +277,19 @@ func TestInterpretBinaryExprInvalidOperatorType(t *testing.T) {
|
|
|
|
i.VisitBinaryExpr(binary)
|
|
|
|
i.VisitBinaryExpr(binary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestInterpretErrorStatement(t *testing.T) {
|
|
|
|
|
|
|
|
i := New(errors.NewMockErrorLogger())
|
|
|
|
|
|
|
|
errorStmt := &ast.ErrorStmt{Value: "error"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
if r := recover(); r != "error" {
|
|
|
|
|
|
|
|
t.Errorf("expected panic with 'error', got %v", r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i.VisitErrorStmt(errorStmt)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestInterpretExprStatement(t *testing.T) {
|
|
|
|
func TestInterpretExprStatement(t *testing.T) {
|
|
|
|
i := New(errors.NewMockErrorLogger())
|
|
|
|
i := New(errors.NewMockErrorLogger())
|
|
|
|
literal := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
literal := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
@ -338,3 +351,40 @@ func TestInterpretVarStatement(t *testing.T) {
|
|
|
|
t.Errorf("expected 42, got %v", result)
|
|
|
|
t.Errorf("expected 42, got %v", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestInterpretVarStatementNoInitializer(t *testing.T) {
|
|
|
|
|
|
|
|
i := New(errors.NewMockErrorLogger())
|
|
|
|
|
|
|
|
varStmt := &ast.VarStmt{
|
|
|
|
|
|
|
|
Name: token.Token{Type: token.IDENTIFIER, Lexeme: "foo"},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i.VisitVarStmt(varStmt)
|
|
|
|
|
|
|
|
result := i.env.get("foo")
|
|
|
|
|
|
|
|
if result != nil {
|
|
|
|
|
|
|
|
t.Errorf("expected nil, got %v", result)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestInterpretAssignment(t *testing.T) {
|
|
|
|
|
|
|
|
i := New(errors.NewMockErrorLogger())
|
|
|
|
|
|
|
|
varStmt := &ast.VarStmt{
|
|
|
|
|
|
|
|
Name: token.Token{Type: token.IDENTIFIER, Lexeme: "foo"},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i.VisitVarStmt(varStmt)
|
|
|
|
|
|
|
|
result := i.env.get("foo")
|
|
|
|
|
|
|
|
if result != nil {
|
|
|
|
|
|
|
|
t.Errorf("expected nil, got %v", result)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assign := &ast.AssignExpr{
|
|
|
|
|
|
|
|
Name: token.Token{Type: token.IDENTIFIER, Lexeme: "foo"},
|
|
|
|
|
|
|
|
Value: &ast.LiteralExpr{Value: 42.0},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i.VisitAssignExpr(assign)
|
|
|
|
|
|
|
|
result = i.env.get("foo")
|
|
|
|
|
|
|
|
if result != 42.0 {
|
|
|
|
|
|
|
|
t.Errorf("expected 42, got %v", result)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|