diff options
author | Sunil Shetye | 2018-06-14 12:41:47 +0530 |
---|---|---|
committer | Sunil Shetye | 2018-06-14 12:41:47 +0530 |
commit | fafd1daf14c93e250555709056ad710bc392f9ce (patch) | |
tree | 6ce175641c4496f77dc80ec7f5ed8d562b5ab61d | |
parent | caafd891713bfb1f0df708737b18a8a18d542325 (diff) | |
download | sci2js-fafd1daf14c93e250555709056ad710bc392f9ce.tar.gz sci2js-fafd1daf14c93e250555709056ad710bc392f9ce.tar.bz2 sci2js-fafd1daf14c93e250555709056ad710bc392f9ce.zip |
add if block statement
-rwxr-xr-x | sci2jsyacc.py | 27 |
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]) |