diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 42812cd..bb68d7e 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -85,6 +85,9 @@ func Eval(node ast.Node, env *object.Environment) object.Object { return args[0] } return apply_function(function, args) + + case *ast.StringLiteral: + return &object.String{Value: node.Value } } return nil diff --git a/evaluator/evaluator_test.go b/evaluator/evaluator_test.go index b8bf267..e42de38 100644 --- a/evaluator/evaluator_test.go +++ b/evaluator/evaluator_test.go @@ -237,6 +237,20 @@ func TestClosures(l_test *testing.T) { test_integer_object(l_test, test_eval(input), 4) } +func TestStringLiteral(l_test *testing.T){ + input := `"Hello, world!"` + + evaluated := test_eval(input) + string, ok := evaluated.(*object.String) + if !ok { + l_test.Fatalf("object is not String, got=%T (%+v)", evaluated, evaluated) + } + + if string.Value != "Hello, world!" { + l_test.Errorf("String has wrong value, got=%q", string.Value) + } +} + // Helpers func test_eval(input string) object.Object { l_lexer := lexer.New(input) diff --git a/object/object.go b/object/object.go index f81eddd..7c61ea6 100644 --- a/object/object.go +++ b/object/object.go @@ -109,4 +109,4 @@ type String struct { } func (s *String) Type() ObjectType { return STRING_OBJECT } -func (s *String) Inspec() string { return s.Value } +func (s *String) Inspect() string { return s.Value }