Last active
September 26, 2019 04:43
-
-
Save 0xkohe/cbef076394a9abb26c982010b6551f2b to your computer and use it in GitHub Desktop.
[Blog][TopDownOperatorPrecedence(再帰下降構文解析)におけるExpression(式)の解析処理を追う]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (p *Parser) parseExpression(precedence int) ast.Expression { | |
prefix := p.prefixParseFns[p.curToken.Type] | |
if prefix == nil { | |
p.noPrefixParseFnError(p.curToken.Type) | |
return nil | |
} | |
leftExp := prefix() | |
for !p.peekTokenIs(token.SEMICOLON) && precedence < p.peekPrecedence() { | |
infix := p.infixParseFns[p.peekToken.Type] | |
if infix == nil { | |
return leftExp | |
} | |
p.nextToken() | |
leftExp = infix(leftExp) | |
} | |
return leftExp | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (p *Parser) parseExpressionStatement() *ast.ExpressionStatement { | |
stmt := &ast.ExpressionStatement{Token: p.curToken} | |
stmt.Expression = p.parseExpression(LOWEST) | |
if p.peekTokenIs(token.SEMICOLON) { | |
p.nextToken() | |
} | |
return stmt | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (p *Parser) parseInfixExpression(left ast.Expression) ast.Expression { | |
expression := &ast.InfixExpression{ | |
Token: p.curToken, | |
Operator: p.curToken.Literal, | |
Left: left, | |
} | |
precedence := p.curPrecedence() | |
p.nextToken() | |
expression.Right = p.parseExpression(precedence) | |
return expression | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (p *Parser) parseIntegerLiteral() ast.Expression { | |
lit := &ast.IntegerLiteral{Token: p.curToken} | |
value, err := strconv.ParseInt(p.curToken.Literal, 0, 64) | |
if err != nil { | |
msg := fmt.Sprintf("could not parse %q as integer", p.curToken.Literal) | |
p.errors = append(p.errors, msg) | |
return nil | |
} | |
lit.Value = value | |
return lit | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (p *Parser) parsePrefixExpression() ast.Expression { | |
expression := &ast.PrefixExpression{ | |
Token: p.curToken, | |
Operator: p.curToken.Literal, | |
} | |
p.nextToken() | |
expression.Right = p.parseExpression(PREFIX) | |
return expression | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
LOWEST iota + 1 | |
EQUALS // == | |
LESSGRETER // > or < | |
SUM // + | |
PRODUCT // * | |
PREFIX // -X or !X | |
CALL // myFunction(X) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment