12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package 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
- // and statements.
- type Statement interface {
- Node
- statementNode()
- }
- // Expression extends Node and is only a way to differentiate between
- // expressions and statements.
- type Expression interface {
- // Expression extends Node.
- Node
- expressionNode()
- }
- // Program represents the outer structure of the parser program.
- 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 {
- if len(p.Statements) > 0 {
- return p.Statements[0].TokenLiteral()
- }
- return ""
- }
|