Implement strings and partial implementation of arrays (missing index expr eval)

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-01-10 17:48:54 -08:00
parent a76f47a7a3
commit 59acf6b1a1
13 changed files with 388 additions and 20 deletions

29
pkg/ast/array.go Normal file
View file

@ -0,0 +1,29 @@
package ast
import (
"bytes"
"strings"
"code.jmug.me/jmug/interpreter-in-go/pkg/token"
)
type ArrayLiteral struct {
Token token.Token // The '[' token
Elements []Expression
}
func (al *ArrayLiteral) expressionNode() {}
func (al *ArrayLiteral) TokenLiteral() string {
return al.Token.Literal
}
func (al *ArrayLiteral) String() string {
var out bytes.Buffer
elements := []string{}
for _, el := range al.Elements {
elements = append(elements, el.String())
}
out.WriteString("[")
out.WriteString(strings.Join(elements, ", "))
out.WriteString("]")
return out.String()
}

21
pkg/ast/index.go Normal file
View file

@ -0,0 +1,21 @@
package ast
import (
"fmt"
"code.jmug.me/jmug/interpreter-in-go/pkg/token"
)
type IndexExpression struct {
Token token.Token // The "[" token
Left Expression
Index Expression
}
func (ie *IndexExpression) expressionNode() {}
func (ie *IndexExpression) TokenLiteral() string {
return ie.Token.Literal
}
func (ie *IndexExpression) String() string {
return fmt.Sprintf("(%s[%s])", ie.Left.String(), ie.Index.String())
}

16
pkg/ast/string.go Normal file
View file

@ -0,0 +1,16 @@
package ast
import "code.jmug.me/jmug/interpreter-in-go/pkg/token"
type StringLiteral struct {
Token token.Token
Value string
}
func (s *StringLiteral) expressionNode() {}
func (s *StringLiteral) TokenLiteral() string {
return s.Token.Literal
}
func (s *StringLiteral) String() string {
return s.Token.Literal
}