|
|
@@ -1,6 +1,9 @@
|
|
|
package ast
|
|
|
|
|
|
-import "github/runnignwater/monkey/token"
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "github/runnignwater/monkey/token"
|
|
|
+)
|
|
|
|
|
|
/**
|
|
|
* @Author: simon
|
|
|
@@ -10,6 +13,9 @@ import "github/runnignwater/monkey/token"
|
|
|
*/
|
|
|
type Node interface {
|
|
|
TokenLiteral() string
|
|
|
+ // This will allow us to print AST nodes for debugging and to compare them with other AST nodes.
|
|
|
+ // This is going to be really handy in tests!
|
|
|
+ String() string
|
|
|
}
|
|
|
|
|
|
// expressions produce values, statements don't
|
|
|
@@ -35,6 +41,13 @@ func (p *Program) TokenLiteral() string {
|
|
|
return ""
|
|
|
}
|
|
|
}
|
|
|
+func (p *Program) String() string {
|
|
|
+ var out bytes.Buffer
|
|
|
+ for _, s := range p.Statements {
|
|
|
+ out.WriteString(s.String())
|
|
|
+ }
|
|
|
+ return out.String()
|
|
|
+}
|
|
|
|
|
|
//---------------------implementation of Node-----------------------------------END-------------------------------------
|
|
|
|
|
|
@@ -66,7 +79,19 @@ type LetStatement struct {
|
|
|
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())
|
|
|
+ out.WriteString(" = ")
|
|
|
+
|
|
|
+ if ls.Value != nil {
|
|
|
+ out.WriteString(ls.Value.String())
|
|
|
+ }
|
|
|
+ out.WriteString(";")
|
|
|
+ return out.String()
|
|
|
+}
|
|
|
func (ls *LetStatement) statementNode() {
|
|
|
panic("implement me")
|
|
|
}
|
|
|
@@ -79,6 +104,9 @@ type Identifier struct {
|
|
|
func (i *Identifier) expressionNode() {
|
|
|
panic("implement me")
|
|
|
}
|
|
|
+func (i *Identifier) String() string {
|
|
|
+ return i.Value
|
|
|
+}
|
|
|
func (i *Identifier) TokenLiteral() string {
|
|
|
return i.Token.Literal
|
|
|
}
|
|
|
@@ -92,7 +120,34 @@ type ReturnStatement struct {
|
|
|
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()
|
|
|
+}
|
|
|
func (rs *ReturnStatement) statementNode() {
|
|
|
panic("implement me")
|
|
|
}
|
|
|
+
|
|
|
+type ExpressionStatement struct {
|
|
|
+ Token token.Token // the first token of the expression
|
|
|
+ Expression Expression
|
|
|
+}
|
|
|
+
|
|
|
+func (es *ExpressionStatement) TokenLiteral() string {
|
|
|
+ return es.Token.Literal
|
|
|
+}
|
|
|
+func (es *ExpressionStatement) String() string {
|
|
|
+ if es.Expression != nil {
|
|
|
+ return es.Expression.String()
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+}
|
|
|
+func (es *ExpressionStatement) expressionNode() {
|
|
|
+ panic("implement me")
|
|
|
+}
|