|
@@ -1,9 +1,13 @@
|
|
|
package ast
|
|
|
|
|
|
-// Node is the interface implemented by every node in the AST.
|
|
|
+import "bytes"
|
|
|
+
|
|
|
+// Node is the interface implemented by every node in the AST. It extends the
|
|
|
+// fmt.Stringer interface, allowing simple printing of nodes.
|
|
|
type Node interface {
|
|
|
// TokenLiteral returns the literal value of the token it's associated to.
|
|
|
TokenLiteral() string
|
|
|
+ String() string
|
|
|
}
|
|
|
|
|
|
// Statement extends Node and is only a way to differentiate between expressions
|
|
@@ -26,6 +30,17 @@ type Program struct {
|
|
|
Statements []Statement
|
|
|
}
|
|
|
|
|
|
+// String satisfies the Node and fmt.Stringer interfaces.
|
|
|
+func (p *Program) String() string {
|
|
|
+ var out bytes.Buffer
|
|
|
+
|
|
|
+ for _, s := range p.Statements {
|
|
|
+ out.WriteString(s.String())
|
|
|
+ }
|
|
|
+
|
|
|
+ return out.String()
|
|
|
+}
|
|
|
+
|
|
|
// TokenLiteral returns the string contents of the first statement token, which
|
|
|
// can be an empty string if the program is empty.
|
|
|
func (p *Program) TokenLiteral() string {
|