summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Shetye2018-06-14 12:41:47 +0530
committerSunil Shetye2018-06-14 12:41:47 +0530
commitfafd1daf14c93e250555709056ad710bc392f9ce (patch)
tree6ce175641c4496f77dc80ec7f5ed8d562b5ab61d
parentcaafd891713bfb1f0df708737b18a8a18d542325 (diff)
downloadsci2js-fafd1daf14c93e250555709056ad710bc392f9ce.tar.gz
sci2js-fafd1daf14c93e250555709056ad710bc392f9ce.tar.bz2
sci2js-fafd1daf14c93e250555709056ad710bc392f9ce.zip
add if block statement
-rwxr-xr-xsci2jsyacc.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/sci2jsyacc.py b/sci2jsyacc.py
index 3dd1904f..5aea43e8 100755
--- a/sci2jsyacc.py
+++ b/sci2jsyacc.py
@@ -6,8 +6,11 @@ import ply.yacc as yacc
from sci2jslex import tokens
precedence = (
+ ('left', 'LOGICAL'),
+ ('left', 'COMPARISON'),
('left', 'ADDITION'),
('left', 'MULTIPLICATION'),
+ ('right', 'NOT'),
('right', 'UNARYADDITION'),
)
@@ -46,6 +49,10 @@ def p_statement_assignment(p):
| function EOL'''
p[0] = str(p[1]) + '\n'
+def p_statement_if(p):
+ 'statement : IF expression THEN EOL statementblock END EOL'
+ p[0] = 'if (' + p[2] + ') {\n' + p[5] + '}\n'
+
def p_statement_eol(p):
'statement : EOL'
p[0] = ''
@@ -136,10 +143,30 @@ def p_expression_expression_addition_expression(p):
'expression : expression ADDITION expression'
p[0] = str(p[1]) + str(p[2]) + str(p[3])
+def p_expression_expression_comparison_expression(p):
+ 'expression : expression COMPARISON expression'
+ o = p[2]
+ if (o == '<>' or o == '~='):
+ o = '!='
+ p[0] = str(p[1]) + o + str(p[3])
+
+def p_expression_expression_logical_expression(p):
+ 'expression : expression LOGICAL expression'
+ o = p[2]
+ if (o == '&'):
+ o = '&&'
+ elif (o == '|'):
+ o = '||'
+ p[0] = str(p[1]) + o + str(p[3])
+
def p_expression_addition_term(p):
'expression : ADDITION term %prec UNARYADDITION'
p[0] = str(p[1]) + str(p[2])
+def p_expression_not_expression(p):
+ 'expression : NOT expression'
+ p[0] = '!' + str(p[2])
+
def p_expression_term(p):
'expression : term'
p[0] = str(p[1])