Skip to content

2.6.5 Pratt構文解析器の実装 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified pkg/darwin_amd64/monkey/ast.a
Binary file not shown.
16 changes: 16 additions & 0 deletions src/monkey/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import (
"monkey/token"
)

type (
prefixParseFn func() ast.Expression
infixParseFn func(ast.Expression) ast.Expression
)

type Parser struct {
l *lexer.Lexer

curToken token.Token
peekToken token.Token

errors []string

prefixParseFns map[token.TokenType]prefixParseFn
infixParseFns map[token.TokenType]infixParseFn
}

func New(l *lexer.Lexer) *Parser {
Expand Down Expand Up @@ -111,3 +119,11 @@ func (p *Parser) peekError(t token.TokenType) {
msg := fmt.Sprintf("expected next token to be %s, got %s insted", t, p.peekToken.Type)
p.errors = append(p.errors, msg)
}

func (p *Parser) registerPrefix(tokenType token.TokenType, fn prefixParseFn) {
p.prefixParseFns[tokenType] = fn
}

func (p *Parser) registerInfix(tokenType token.TokenType, fn infixParseFn) {
p.infixParseFns[tokenType] = fn
}
2 changes: 1 addition & 1 deletion src/monkey/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestLetStatements(t *testing.T) {
input := `
let x 5;
let x = 5;
let y = 10;
let foobar = 383838;
`
Expand Down