Skip to content

Commit

Permalink
comparitors
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Mar 7, 2016
1 parent 51f5f60 commit 7c287ae
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyluac/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def t_multilines_STRING(t):
'def' : 'DEF',
'class' : 'CLASS',
'return': 'RETURN',
'range' : 'RANGE',
'import': 'IMPORT',
}


Expand Down
60 changes: 60 additions & 0 deletions pyluac/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pyluac.lexer import tokens # pylint: disable=W0611

precedence = (
('left', 'EQUALS', 'NEQUALS', 'LECOMP', 'GECOMP', 'LCOMP', 'GCOMP'),
('left', 'ID'),
('right', ','),
('left', '+', '-'),
Expand All @@ -18,6 +19,45 @@
('right', 'UMINUS'),
)

#def p_controllist(p):
#'''
#controllist : controllist control
#| control
#'''
#if len(p) == 2:
#p[0] = [p[1]]
#else:
#p[0] = p[1]
#p[0].append(p[2])

#def p_control(p):
#'''
#control : if
#| while
#| for
#| block
#'''
#p[0] = p[1]

#def p_if(p):
#'''
#if : IF
#'''
#pass

#def p_while(p):
#'''
#while : WHILE
#'''
#pass

#def p_for(p):
#'''
#for : FOR
#'''
#pass


def p_block(p):
'''
block : block statement
Expand All @@ -34,34 +74,40 @@ def p_statement(p):
'''
statement : assignment
| expression
| comparison
| return
'''
p[0] = p[1]


def p_return(p):
'''
return : RETURN expression
'''
p[0] = ('return', p[2])


def p_assignment(p):
'''
assignment : IDASSIGN expression
'''
p[0] = ('assign', p[1], p[2])


def p_function(p):
'''
function : ID '(' expressionlist assignmentlist ')'
'''
p[0] = ('func', p[1], p[3], p[4])


def p_tuple(p):
'''
tuple : '(' expressionlist ')'
'''
p[0] = ('tuple', p[2])


def p_expressionlist(p):
'''
expressionlist : expressionlist ',' expression
Expand All @@ -79,6 +125,7 @@ def p_expressionlist(p):
p[0] = p[1]
p[0].append(p[3])


def p_assignmentlist(p):
'''
assignmentlist : assignmentlist ',' assignment
Expand All @@ -96,6 +143,19 @@ def p_assignmentlist(p):
p[0] = p[1]
p[0].append(p[3])


def p_comparison(p):
'''
comparison : expression EQUALS expression
| expression NEQUALS expression
| expression LECOMP expression
| expression GECOMP expression
| expression LCOMP expression
| expression GCOMP expression
'''
p[0] = (p[2], p[1], p[3])


def p_expression(p):
'''
expression : expression '+' expression
Expand Down
27 changes: 27 additions & 0 deletions pyluac/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,33 @@ def test_funcparam(self):
('func', 'f', [1, 2], [('assign', 'a', 1), ('assign', 'b', 2)]),
])

def test_comparison(self):
'Expression parsing test'
data = '1 < 2 + 3'
self.assertEqual(
parser.parse(data),
[('<',
1,
('+',
2,
3
)
)])

@unittest.expectedFailure
def test_comparison_chain(self):
'Expression parsing test'
data = '1 < 2 + 3 < 4 * 5'
self.assertEqual(
parser.parse(data),
[('<',
1,
('+',
2,
3
)
)])

if __name__ == '__main__': # pragma: no cover
unittest.main()

0 comments on commit 7c287ae

Please sign in to comment.