This commit is contained in:
2024-10-31 22:38:59 +01:00
parent 2f4e606fbf
commit e71ba34371
12 changed files with 304 additions and 35 deletions

View File

@ -1,7 +1,7 @@
from rply import ParserGenerator
from lexer import TOKENS
from ast_nodes import Exp, ExpInt, ExpBinop, ExpLet, ExpVar, ExpMin, ExpMax, ExpRoll, Roll, RollKeepHighest, RollKeepLowest, RollMin, RollMax, RollExplode, ComparePoint, ExpIf, ExpTest, ExpApply, ExpLambda, ExpNeg
from ast_nodes import Exp, ExpInt, ExpBinop, ExpLet, ExpVar, ExpMin, ExpMax, ExpRoll, Roll, RollKeepHighest, RollKeepLowest, RollMin, RollMax, RollExplode, ComparePoint, ExpIf, ExpTest, ExpApply, ExpLambda, ExpNeg, RollReroll
class Parser():
@ -11,11 +11,11 @@ class Parser():
precedence=[
('left', ["SYMBOL_BACKSLASH","SYMBOL_ARROW"]),
('left', ["KEYWORD_LET", "KEYWORD_IN", "KEYWORD_IF", "KEYWORD_THEN", "KEYWORD_ELSE"]),
('left', ["SYMBOL_EQUALS", "SYMBOL_LE"]),
('left', ["SYMBOL_EQUALS", "SYMBOL_LT", "SYMBOL_LE", "SYMBOL_GT", "SYMBOL_GE"]),
('left', ["SYMBOL_PLUS", "SYMBOL_MINUS"]),
('left', ["SYMBOL_TIMES", "SYMBOL_DIVIDE"]),
('left', ["ROLL_DIE"]),
('right', ["ROLL_KEEP_HIGHEST","ROLL_KEEP_LOWEST","ROLL_MIN","ROLL_MAX","ROLL_EXPLODE"])
('right', ["ROLL_KEEP_HIGHEST","ROLL_KEEP_LOWEST","ROLL_MIN","ROLL_MAX","ROLL_EXPLODE","ROLL_REROLL","ROLL_REROLL_ONCE"])
]
)
self._get_parser()
@ -97,6 +97,22 @@ class Parser():
def roll_explode_comp(tokens):
return RollExplode(tokens[0], tokens[2])
@self.pg.production('roll : roll ROLL_REROLL ')
def roll_reroll(tokens):
return RollReroll(tokens[0])
@self.pg.production('roll : roll ROLL_REROLL comp')
def roll_reroll_comp(tokens):
return RollReroll(tokens[0], tokens[2])
@self.pg.production('roll : roll ROLL_REROLL_ONCE')
def roll_reroll_once(tokens):
return RollReroll(tokens[0], None, True)
@self.pg.production('roll : roll ROLL_REROLL_ONCE comp')
def roll_reroll_once_comp(tokens):
return RollReroll(tokens[0], tokens[2], True)
@self.pg.production('roll : atom ROLL_DIE atom')
def roll(tokens):
return Roll(tokens[0], tokens[2])
@ -108,7 +124,10 @@ class Parser():
# Compare Points
@self.pg.production("comp : SYMBOL_EQUALS atom")
@self.pg.production("comp : SYMBOL_LT atom")
@self.pg.production("comp : SYMBOL_LE atom")
@self.pg.production("comp : SYMBOL_GT atom")
@self.pg.production("comp : SYMBOL_GE atom")
def comp_point(tokens):
return ComparePoint(tokens[0].value,tokens[1])