Przeglądaj źródła

parse let statement -- let <identifier> = <expression>;

runningwater 3 lat temu
rodzic
commit
82dc513676
3 zmienionych plików z 252 dodań i 0 usunięć
  1. 84 0
      ast/ast.go
  2. 97 0
      parser/parser.go
  3. 71 0
      parser/parser_test.go

+ 84 - 0
ast/ast.go

@@ -0,0 +1,84 @@
+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
+}

+ 97 - 0
parser/parser.go

@@ -0,0 +1,97 @@
+package parser
+
+import (
+    "github/runnignwater/monkey/ast"
+    "github/runnignwater/monkey/lexer"
+    "github/runnignwater/monkey/token"
+)
+
+/**
+ * @Author: simon
+ * @Author: ynwdlxm@163.com
+ * @Date: 2022/10/2 下午9:55
+ * @Desc:
+ */
+type Parser struct {
+    l *lexer.Lexer // point to the instance of the lexer
+
+    curToken  token.Token // point to the current token
+    peekToken token.Token // point to the next token
+}
+
+func New(l *lexer.Lexer) *Parser {
+    p := &Parser{l: l}
+
+    // Read two tokens, so curToken and peekToken are both set
+    p.nextToken()
+    p.nextToken()
+
+    return p
+}
+
+func (p *Parser) nextToken() {
+    p.curToken = p.peekToken
+    p.peekToken = p.l.NextToken()
+}
+
+func (p *Parser) ParseProgram() *ast.Program {
+    program := &ast.Program{}
+    program.Statements = []ast.Statement{}
+
+    for p.curToken.Type != token.EOF {
+        stmt := p.parseStatement()
+        if stmt != nil {
+            program.Statements = append(program.Statements, stmt)
+        }
+        p.nextToken()
+
+    }
+    return program
+}
+
+func (p *Parser) parseStatement() ast.Statement {
+    switch p.curToken.Type {
+    case token.LET:
+        return p.parseLetStatement()
+    default:
+        return nil
+    }
+}
+
+// let <identifier> = <expression>;
+func (p *Parser) parseLetStatement() *ast.LetStatement {
+    stmt := &ast.LetStatement{Token: p.curToken}
+
+    if !p.expectPeek(token.IDENT) {
+        return nil
+    }
+
+    stmt.Name = &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal}
+    if !p.expectPeek(token.ASSIGN) {
+        return nil
+    }
+
+    // TODO: we're skipping the expression until we
+    // we encounter a semicolon
+    for !p.curTokenIs(token.SEMICOLON) {
+        p.nextToken()
+    }
+    return stmt
+}
+
+func (p *Parser) curTokenIs(t token.TypeToken) bool {
+    return p.curToken.Type == t
+}
+
+func (p *Parser) peekTokenIs(t token.TypeToken) bool {
+    return p.peekToken.Type == t
+}
+
+func (p *Parser) expectPeek(t token.TypeToken) bool {
+    if p.peekTokenIs(t) {
+        p.nextToken()
+        return true
+    } else {
+        return false
+    }
+}

+ 71 - 0
parser/parser_test.go

@@ -0,0 +1,71 @@
+package parser
+
+import (
+    "github/runnignwater/monkey/ast"
+    "github/runnignwater/monkey/lexer"
+    "testing"
+)
+
+/**
+ * @Author: simon
+ * @Author: ynwdlxm@163.com
+ * @Date: 2022/10/2 下午10:40
+ * @Desc: LetStatement test case
+ */
+func TestLetStatements(t *testing.T) {
+    input := `
+        let x = 5;
+        let y = 10;
+        let foo = 838383;
+        `
+    l := lexer.New(input)
+    p := New(l)
+
+    program := p.ParseProgram()
+    if program == nil {
+        t.Fatalf("ParseProgram() return nil")
+    }
+    if len(program.Statements) != 3 {
+        t.Fatalf("Program.Statements does not contain 3 statements. got=%d", len(program.Statements))
+    }
+
+    tests := []struct {
+        expectedIdentifies string
+    }{
+        {"x"},
+        {"y"},
+        {"foo"},
+    }
+
+    for i, tt := range tests {
+        stmt := program.Statements[i]
+        if !testLetStatement(t, stmt, tt.expectedIdentifies) {
+            return
+        }
+    }
+}
+
+func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
+    if s.TokenLiteral() != "let" {
+        t.Errorf("s.TokenLiteral() not 'let'. got =%q", s.TokenLiteral())
+        return false
+    }
+
+    letStmt, ok := s.(*ast.LetStatement)
+    if !ok {
+        t.Errorf("s is not *ast.LetStatement. got=%T", s)
+        return false
+    }
+
+    if letStmt.Name.Value != name {
+        t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
+        return false
+    }
+
+    if letStmt.Name.TokenLiteral() != name {
+        t.Errorf("s.name not '%s. got=%s", name, letStmt.Name)
+        return false
+    }
+
+    return true
+}