Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added some more expressions just as +, -, * and /

git-svn-id: file:///Users/brian/Backups/S3/backups/Wednesday/brian/svn/pyphp/trunk@5 46ed67a7-99ac-4510-895c-a9dfc75a6d38
  • Loading branch information...
commit 8e9d1ed46c20cb0ed0225cedcbd81dceb8071de1 1 parent 39f7587
brian authored
Showing with 25 additions and 1 deletion.
  1. +23 −1 pyphp/interpreter.py
  2. +2 −0  tests/test001.php
View
24 pyphp/interpreter.py
@@ -56,7 +56,12 @@ def _get_banner(self):
)
tokens = ('ECHO', 'DOLLAR', 'SEMI', 'LABEL', 'SINGLE_QUOTE', 'DOUBLE_QUOTE',
- 'INT', 'RPARA', 'LPARA', 'COMMA',)
+ 'INT', 'RPARA', 'LPARA', 'COMMA', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE',)
+
+ precedence = (
+ ('left', 'PLUS', 'MINUS'),
+ ('left', 'TIMES', 'DIVIDE'),
+ )
t_php_ignore = ' \t'
@@ -78,6 +83,10 @@ def t_php_NEWLINE(self, t):
t_php_LPARA = r'\('
t_php_RPARA = r'\)'
t_php_COMMA = r','
+ t_php_PLUS = r'\+'
+ t_php_MINUS = r'-'
+ t_php_TIMES = r'\*'
+ t_php_DIVIDE = r'/'
t_php_ECHO = r'echo'
def t_php_INT(self, t):
@@ -125,6 +134,19 @@ def p_expr_double_quote(self, p):
'''expr : DOUBLE_QUOTE DOUBLE_QUOTE'''
if len(p) == 3:
p[0] = ''
+ def p_expr_binop(self, p):
+ '''expr : expr PLUS expr
+ | expr MINUS expr
+ | expr TIMES expr
+ | expr DIVIDE expr'''
+ if p[2] == '+':
+ p[0] = p[1] + p[3]
+ elif p[2] == '-':
+ p[0] = p[1] - p[3]
+ elif p[2] == '*':
+ p[0] = p[1] * p[3]
+ elif p[2] == '/':
+ p[0] = p[1] / p[3]
def p_function_call(self, p):
'''function_call : LABEL function_params'''
View
2  tests/test001.php
@@ -1,5 +1,7 @@
<?php
echo test_func();
+echo 6 + 9;
+echo 5 * 6;
?>
Please sign in to comment.
Something went wrong with that request. Please try again.