Added support for function calls in parser.

parent 935b8061
...@@ -61,7 +61,9 @@ class Parser(BisonParser): ...@@ -61,7 +61,9 @@ class Parser(BisonParser):
('left', ('COMMA', )), ('left', ('COMMA', )),
('left', ('MINUS', 'PLUS')), ('left', ('MINUS', 'PLUS')),
('left', ('TIMES', 'DIVIDE')), ('left', ('TIMES', 'DIVIDE')),
('left', ('EQ', )),
('left', ('NEG', )), ('left', ('NEG', )),
('right', ('SIN', 'COS', 'TAN', 'SOLVE', 'INT', 'SQRT')),
('right', ('POW', )), ('right', ('POW', )),
) )
...@@ -334,6 +336,12 @@ class Parser(BisonParser): ...@@ -334,6 +336,12 @@ class Parser(BisonParser):
def on_unary(self, target, option, names, values): def on_unary(self, target, option, names, values):
""" """
unary : MINUS exp %prec NEG unary : MINUS exp %prec NEG
| SIN exp
| COS exp
| TAN exp
| INT exp
| SOLVE exp
| SQRT exp
""" """
if option == 0: # rule: NEG exp if option == 0: # rule: NEG exp
...@@ -346,6 +354,11 @@ class Parser(BisonParser): ...@@ -346,6 +354,11 @@ class Parser(BisonParser):
return values[1] return values[1]
if option < 7: # rule: SIN exp | COS exp | TAN exp | INT exp
if values[1].type == TYPE_OPERATOR and values[1].op == OP_COMMA:
return Node(values[0], *values[1])
return Node(*values)
raise BisonSyntaxError('Unsupported option %d in target "%s".' raise BisonSyntaxError('Unsupported option %d in target "%s".'
% (option, target)) # pragma: nocover % (option, target)) # pragma: nocover
...@@ -355,13 +368,14 @@ class Parser(BisonParser): ...@@ -355,13 +368,14 @@ class Parser(BisonParser):
| exp TIMES exp | exp TIMES exp
| exp DIVIDE exp | exp DIVIDE exp
| exp POW exp | exp POW exp
| exp EQ exp
| exp MINUS exp | exp MINUS exp
""" """
if 0 <= option < 4: # rule: exp {PLUS,TIMES,DIVIDES,POW} exp if 0 <= option < 5: # rule: exp {PLUS,TIMES,DIVIDES,POW,EQ} exp
return Node(values[1], values[0], values[2]) return Node(values[1], values[0], values[2])
if option == 4: # rule: exp MINUS exp if option == 5: # rule: exp MINUS exp
node = values[2] node = values[2]
# Add negation to the left-most child # Add negation to the left-most child
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment