|
|
@@ -6,12 +6,9 @@ import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
-/**
|
|
|
- * @Author: simon
|
|
|
- * @Author: ynwdlxm@163.com
|
|
|
- * @Date: 2022/10/2 下午8:58
|
|
|
- * @Desc:
|
|
|
- */
|
|
|
+// Node @Author: simon
|
|
|
+// @Author: ynwdlxm@163.com
|
|
|
+// @Date: 2022/10/2 下午8:58
|
|
|
type Node interface {
|
|
|
TokenLiteral() string
|
|
|
// String This will allow us to print AST nodes for debugging and to compare them with other AST nodes.
|
|
|
@@ -30,7 +27,7 @@ type Expression interface {
|
|
|
}
|
|
|
|
|
|
// ---------------------implementation of Node----------------------------------BEGIN-----------------------------------
|
|
|
-// Root Node of our parser produces.
|
|
|
+// Program Root Node of our parser produces.
|
|
|
type Program struct {
|
|
|
Statements []Statement
|
|
|
}
|
|
|
@@ -52,24 +49,7 @@ func (p *Program) String() string {
|
|
|
|
|
|
//---------------------implementation of Node-----------------------------------END-------------------------------------
|
|
|
|
|
|
-// let <identifier> = <expression>;
|
|
|
-//
|
|
|
-// let x = 5 AST
|
|
|
-// |-------------------|
|
|
|
-// | *ast.Program |
|
|
|
-// |-------------------|
|
|
|
-// | Statements |
|
|
|
-// |-------------------|
|
|
|
-// ↓
|
|
|
-// |-------------------|
|
|
|
-// | *ast.LetStatement |
|
|
|
-// |-------------------|
|
|
|
-// | Name |
|
|
|
-// |-------------------|
|
|
|
-// | Value |
|
|
|
-// |-------------------|
|
|
|
-//
|
|
|
-// *ast.Identifier *ast.Expression
|
|
|
+// LetStatement let <identifier> = <expression>;
|
|
|
type LetStatement struct {
|
|
|
Token token.Token // the token.LET token
|
|
|
Name *Identifier
|
|
|
@@ -150,6 +130,7 @@ func (es *ExpressionStatement) String() string {
|
|
|
}
|
|
|
func (es *ExpressionStatement) statementNode() {}
|
|
|
|
|
|
+// IntegerLiteral integer value e.g. 5 6 ...
|
|
|
type IntegerLiteral struct {
|
|
|
Token token.Token
|
|
|
Value int64
|
|
|
@@ -161,6 +142,7 @@ func (i *IntegerLiteral) String() string { return i.Token.Literal }
|
|
|
|
|
|
func (i *IntegerLiteral) expressionNode() {}
|
|
|
|
|
|
+// PrefixExpression 前缀表示式 e.g. !a -6
|
|
|
type PrefixExpression struct {
|
|
|
Token token.Token // The prefix token, e.g. !
|
|
|
Operator string
|
|
|
@@ -182,7 +164,7 @@ func (pe *PrefixExpression) String() string {
|
|
|
|
|
|
func (pe *PrefixExpression) expressionNode() {}
|
|
|
|
|
|
-// <expression> <infix operator> <expression>
|
|
|
+// InfixExpression <expression> <infix operator> <expression>
|
|
|
type InfixExpression struct {
|
|
|
Token token.Token // The operator token, e.g. + - * /
|
|
|
Left Expression
|
|
|
@@ -206,6 +188,7 @@ func (oe *InfixExpression) String() string {
|
|
|
|
|
|
func (oe *InfixExpression) expressionNode() {}
|
|
|
|
|
|
+// Boolean bool 表达式 e.g. true/false
|
|
|
type Boolean struct {
|
|
|
Token token.Token
|
|
|
Value bool
|
|
|
@@ -271,6 +254,7 @@ func (bs *BlockStatement) String() string {
|
|
|
|
|
|
func (bs *BlockStatement) statementNode() {}
|
|
|
|
|
|
+// FunctionLiteral fn <parameters> <block statement>
|
|
|
type FunctionLiteral struct {
|
|
|
Token token.Token // The 'fn' token
|
|
|
Parameters []*Identifier
|