Parsing identifiers, integer literals and prefix expressions (all as expression statements)
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
577fad2da6
commit
f286a88039
5 changed files with 341 additions and 10 deletions
|
|
@ -1,9 +1,14 @@
|
|||
package ast
|
||||
|
||||
import "code.jmug.me/jmug/interpreter-in-go/pkg/token"
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"code.jmug.me/jmug/interpreter-in-go/pkg/token"
|
||||
)
|
||||
|
||||
type Node interface {
|
||||
TokenLiteral() string
|
||||
String() string
|
||||
}
|
||||
|
||||
type Statement interface {
|
||||
|
|
@ -27,6 +32,14 @@ func (p *Program) TokenLiteral() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (p *Program) String() string {
|
||||
var out bytes.Buffer
|
||||
for _, stmt := range p.Statements {
|
||||
out.WriteString(stmt.String())
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
||||
type LetStatement struct {
|
||||
Token token.Token // TODO: This is a little redundant, figure out if I can get rid of it.
|
||||
Name *Identifier
|
||||
|
|
@ -37,6 +50,16 @@ func (ls *LetStatement) statementNode() {}
|
|||
func (ls *LetStatement) TokenLiteral() string {
|
||||
return ls.Token.Literal
|
||||
}
|
||||
func (ls *LetStatement) String() string {
|
||||
var out bytes.Buffer
|
||||
out.WriteString(ls.TokenLiteral() + " ")
|
||||
out.WriteString(ls.Name.String() + " = ")
|
||||
if ls.Value != nil {
|
||||
out.WriteString(ls.Value.String())
|
||||
}
|
||||
out.WriteString(";")
|
||||
return out.String()
|
||||
}
|
||||
|
||||
type ReturnStatement struct {
|
||||
Token token.Token // TODO: This is a little redundant, figure out if I can get rid of it.
|
||||
|
|
@ -47,7 +70,40 @@ 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()
|
||||
}
|
||||
|
||||
// ExpressionStatement is a simple wrapper of an expression in a statement
|
||||
// This is common in scripting languages and allows you to have a source line
|
||||
// that is solely an expression, think of the Python REPL and how you can
|
||||
// type `1 + 1` and get a result.
|
||||
type ExpressionStatement struct {
|
||||
Token token.Token // The first token in the expression.
|
||||
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 ""
|
||||
}
|
||||
|
||||
// Identifier is treated as an expression because in certain
|
||||
// circumstances they can return values (think `let some = other` where `other`
|
||||
// is actually an expression returning a value) and this makes them easier to
|
||||
// handle (according to the author).
|
||||
type Identifier struct {
|
||||
Token token.Token
|
||||
Value string
|
||||
|
|
@ -57,3 +113,33 @@ func (i *Identifier) expressionNode() {}
|
|||
func (i *Identifier) TokenLiteral() string {
|
||||
return i.Token.Literal
|
||||
}
|
||||
func (i *Identifier) String() string {
|
||||
return i.Value
|
||||
}
|
||||
|
||||
type IntegerLiteral struct {
|
||||
Token token.Token
|
||||
Value int64
|
||||
}
|
||||
|
||||
func (il *IntegerLiteral) expressionNode() {}
|
||||
func (il *IntegerLiteral) TokenLiteral() string {
|
||||
return il.Token.Literal
|
||||
}
|
||||
func (il *IntegerLiteral) String() string {
|
||||
return il.Token.Literal
|
||||
}
|
||||
|
||||
type PrefixExpression struct {
|
||||
Token token.Token
|
||||
Operator string
|
||||
Right Expression
|
||||
}
|
||||
|
||||
func (pe *PrefixExpression) expressionNode() {}
|
||||
func (pe *PrefixExpression) TokenLiteral() string {
|
||||
return pe.Token.Literal
|
||||
}
|
||||
func (pe *PrefixExpression) String() string {
|
||||
return "(" + pe.Operator + pe.Right.String() + ")"
|
||||
}
|
||||
|
|
|
|||
29
pkg/ast/ast_test.go
Normal file
29
pkg/ast/ast_test.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package ast
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.jmug.me/jmug/interpreter-in-go/pkg/token"
|
||||
)
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
program := &Program{
|
||||
Statements: []Statement{
|
||||
&LetStatement{
|
||||
Token: token.Token{Type: token.LET, Literal: "let"},
|
||||
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())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue