Add all the files from the interpreter repo
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
230fe61b12
commit
0acd1d41e8
34 changed files with 3784 additions and 0 deletions
44
pkg/parser/precedence.go
Normal file
44
pkg/parser/precedence.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package parser
|
||||
|
||||
import (
|
||||
"code.jmug.me/jmug/interpreter-in-go/pkg/token"
|
||||
)
|
||||
|
||||
const (
|
||||
_ int = iota
|
||||
LOWEST
|
||||
EQUALS // ==
|
||||
LESSGREATER // > or <
|
||||
SUM // +
|
||||
PRODUCT // *
|
||||
PREFIX // -X or !X
|
||||
CALL // myFunction(X)
|
||||
INDEX // array[index]
|
||||
)
|
||||
|
||||
var precedences = map[token.TokenType]int{
|
||||
token.EQ: EQUALS,
|
||||
token.NOT_EQ: EQUALS,
|
||||
token.GT: LESSGREATER,
|
||||
token.LT: LESSGREATER,
|
||||
token.PLUS: SUM,
|
||||
token.MINUS: SUM,
|
||||
token.ASTERISK: PRODUCT,
|
||||
token.SLASH: PRODUCT,
|
||||
token.LPAREN: CALL,
|
||||
token.LBRACKET: INDEX,
|
||||
}
|
||||
|
||||
func (p *Parser) peekPrecedence() int {
|
||||
if pr, ok := precedences[p.peekToken.Type]; ok {
|
||||
return pr
|
||||
}
|
||||
return LOWEST
|
||||
}
|
||||
|
||||
func (p *Parser) curPrecedence() int {
|
||||
if pr, ok := precedences[p.curToken.Type]; ok {
|
||||
return pr
|
||||
}
|
||||
return LOWEST
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue