String Implementation is done. Still has TODO that would make for good excercise.

git-svn-id: https://svn.tlawal.org/svn/monkey@57 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
This commit is contained in:
Tijani Lawal 2022-11-03 14:23:07 +00:00
parent 2791a27f1f
commit 2c2c936b29
3 changed files with 18 additions and 1 deletions

View File

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

View File

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

View File

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