Skip to content

Commit

Permalink
add ast for expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
54L1M committed Jan 30, 2024
1 parent a24334e commit 7731813
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
66 changes: 63 additions & 3 deletions ast/ast.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ast

import (
"bytes"

"github.com/54L1m/mil-lang/token"
)

type Node interface {
TokenLiteral() string
String() string
}

type Statement interface {
Expand All @@ -30,6 +33,16 @@ func (p *Program) TokenLiteral() string {
}
}

func (p *Program) String() string {
var out bytes.Buffer

for _, s := range p.Statements {
out.WriteString(s.String())
}

return out.String()
}

type Identifier struct {
Token token.Token
Value string
Expand All @@ -39,16 +52,49 @@ func (i *Identifier) expressionNode() {}
func (i *Identifier) TokenLiteral() string {
return i.Token.Literal
}
func (i *Identifier) String() string { return i.Value }

type VarStatement struct {
Token token.Token
Name *Identifier
Value Expression
}

func (ls *VarStatement) statementNode() {}
func (ls *VarStatement) TokenLiteral() string {
return ls.Token.Literal
func (vr *VarStatement) statementNode() {}
func (vr *VarStatement) TokenLiteral() string {
return vr.Token.Literal
}

func (vr *VarStatement) String() string {
var out bytes.Buffer

out.WriteString(vr.TokenLiteral() + " ")
out.WriteString(vr.Name.String())
out.WriteString(" = ")

if vr.Value != nil {
out.WriteString(vr.Value.String())
}

out.WriteString(";")

return out.String()
}

type ExpressionStatement struct {
Token token.Token
Expression Expression
}

func (es *ExpressionStatement) statementNode() {}
func (es *ExpressionStatement) TokenLiteral() string {
return es.Token.Literal
}
func (es *ExpressionStatement) String() string {
if es.Expression != nil {
return es.Expression.String()
}
return ""
}

type ReturnStatement struct {
Expand All @@ -60,3 +106,17 @@ func (rs *ReturnStatement) statementNode() {}
func (rs *ReturnStatement) TokenLiteral() string {
return rs.Token.Literal
}

func (rs *ReturnStatement) String() string {
var out bytes.Buffer

out.WriteString(rs.TokenLiteral() + " ")

if rs.ReturnValue != nil {
out.WriteString(rs.ReturnValue.String())
}

out.WriteString(";")

return out.String()
}
28 changes: 28 additions & 0 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ast

import (
"testing"

"github.com/54L1m/mil-lang/token"
)

func TestString(t *testing.T) {
program := &Program{
Statements: []Statement{
&VarStatement{
Token: token.Token{Type: token.VAR, Literal: "var"},
Name: &Identifier{
Token: token.Token{Type: token.IDENT, Literal: "myVar"},
Value: "myVar",
},
Value: &Identifier{
Token: token.Token{Type: token.IDENT, Literal: "anotherVar"},
Value: "anotherVar",
},
},
},
}
if program.String() != "let myVar = anotherVar;" {
t.Errorf("program.String() wrong. got=%q", program.String())
}
}

0 comments on commit 7731813

Please sign in to comment.