|
|
@@ -1,10 +1,10 @@
|
|
|
package parser
|
|
|
|
|
|
import (
|
|
|
- "fmt"
|
|
|
- "github/runnignwater/monkey/ast"
|
|
|
- "github/runnignwater/monkey/lexer"
|
|
|
- "github/runnignwater/monkey/token"
|
|
|
+ "fmt"
|
|
|
+ "github/runnignwater/monkey/ast"
|
|
|
+ "github/runnignwater/monkey/lexer"
|
|
|
+ "github/runnignwater/monkey/token"
|
|
|
)
|
|
|
|
|
|
/**
|
|
|
@@ -14,100 +14,117 @@ import (
|
|
|
* @Desc:
|
|
|
*/
|
|
|
type Parser struct {
|
|
|
- l *lexer.Lexer // point to the instance of the lexer
|
|
|
+ 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
|
|
|
+ curToken token.Token // point to the current token
|
|
|
+ peekToken token.Token // point to the next token
|
|
|
|
|
|
- errors []string
|
|
|
+ errors []string
|
|
|
}
|
|
|
|
|
|
func New(l *lexer.Lexer) *Parser {
|
|
|
- p := &Parser{
|
|
|
- l: l,
|
|
|
- errors: []string{},
|
|
|
- }
|
|
|
+ p := &Parser{
|
|
|
+ l: l,
|
|
|
+ errors: []string{},
|
|
|
+ }
|
|
|
|
|
|
- // Read two tokens, so curToken and peekToken are both set
|
|
|
- p.nextToken()
|
|
|
- p.nextToken()
|
|
|
+ // Read two tokens, so curToken and peekToken are both set
|
|
|
+ p.nextToken()
|
|
|
+ p.nextToken()
|
|
|
|
|
|
- return p
|
|
|
+ return p
|
|
|
}
|
|
|
|
|
|
func (p *Parser) Errors() []string {
|
|
|
- return p.errors
|
|
|
+ return p.errors
|
|
|
}
|
|
|
|
|
|
func (p *Parser) peekError(t token.TypeToken) {
|
|
|
- msg := fmt.Sprintf("exepected next token to be %s, got %s instead.", t, p.peekToken.Type)
|
|
|
- p.errors = append(p.errors, msg)
|
|
|
+ msg := fmt.Sprintf("exepected next token to be %s, got %s instead.", t, p.peekToken.Type)
|
|
|
+ p.errors = append(p.errors, msg)
|
|
|
}
|
|
|
|
|
|
func (p *Parser) nextToken() {
|
|
|
- p.curToken = p.peekToken
|
|
|
- p.peekToken = p.l.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.curTokenIs(token.EOF) {
|
|
|
- stmt := p.parseStatement()
|
|
|
- if stmt != nil {
|
|
|
- program.Statements = append(program.Statements, stmt)
|
|
|
- }
|
|
|
- p.nextToken()
|
|
|
-
|
|
|
- }
|
|
|
- return program
|
|
|
+ program := &ast.Program{}
|
|
|
+ program.Statements = []ast.Statement{}
|
|
|
+
|
|
|
+ for !p.curTokenIs(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
|
|
|
- }
|
|
|
+ switch p.curToken.Type {
|
|
|
+ case token.LET:
|
|
|
+ return p.parseLetStatement()
|
|
|
+ case token.RETURN:
|
|
|
+ return p.parseReturnStatement()
|
|
|
+ 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
|
|
|
+ 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
|
|
|
+}
|
|
|
+
|
|
|
+// return <expression>;
|
|
|
+func (p *Parser) parseReturnStatement() ast.Statement {
|
|
|
+ stmt := &ast.ReturnStatement{Token: p.curToken}
|
|
|
+
|
|
|
+ p.nextToken()
|
|
|
+
|
|
|
+ // TODO: we're skipping the expressions until 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
|
|
|
+ return p.curToken.Type == t
|
|
|
}
|
|
|
|
|
|
func (p *Parser) peekTokenIs(t token.TypeToken) bool {
|
|
|
- return p.peekToken.Type == t
|
|
|
+ return p.peekToken.Type == t
|
|
|
}
|
|
|
|
|
|
func (p *Parser) expectPeek(t token.TypeToken) bool {
|
|
|
- if p.peekTokenIs(t) {
|
|
|
- p.nextToken()
|
|
|
- return true
|
|
|
- } else {
|
|
|
- p.peekError(t)
|
|
|
- return false
|
|
|
- }
|
|
|
+ if p.peekTokenIs(t) {
|
|
|
+ p.nextToken()
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ p.peekError(t)
|
|
|
+ return false
|
|
|
+ }
|
|
|
}
|