| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package ast
- import "github/runnignwater/monkey/token"
- /**
- * @Author: simon
- * @Author: ynwdlxm@163.com
- * @Date: 2022/10/2 下午8:58
- * @Desc:
- */
- type Node interface {
- TokenLiteral() string
- }
- // expressions produce values, statements don't
- type Statement interface {
- Node
- statementNode()
- }
- type Expression interface {
- Node
- expressionNode()
- }
- // ---------------------implementation of Node----------------------------------BEGIN-----------------------------------
- // Root Node of our parser produces.
- type Program struct {
- Statements []Statement
- }
- func (p *Program) TokenLiteral() string {
- if len(p.Statements) > 0 {
- return p.Statements[0].TokenLiteral()
- } else {
- return ""
- }
- }
- //---------------------implementation of Node-----------------------------------END-------------------------------------
- // let <identifier> = <expression>;
- //
- // let x = 5 AST
- // |-------------------|
- // | *ast.Program |
- // |-------------------|
- // | Statements |
- // |-------------------|
- // ↓
- // |-------------------|
- // | *ast.LetStatement |
- // |-------------------|
- // | Name |
- // |-------------------|
- // | Value |
- // |-------------------|
- //
- // *ast.Identifier *ast.Expression
- //
- type LetStatement struct {
- Token token.Token // the token.LET token
- Name *Identifier
- Value Expression
- }
- func (ls *LetStatement) TokenLiteral() string {
- return ls.Token.Literal
- }
- func (ls *LetStatement) statementNode() {
- panic("implement me")
- }
- type Identifier struct {
- Token token.Token // the token.IDENT token
- Value string
- }
- func (i *Identifier) expressionNode() {
- panic("implement me")
- }
- func (i *Identifier) TokenLiteral() string {
- return i.Token.Literal
- }
|