added write, read and for statements

This commit is contained in:
hellerve
2015-07-11 17:02:45 +02:00
parent a19dd4508c
commit 140dac9cbc
3 changed files with 111 additions and 2 deletions

View File

@@ -117,13 +117,18 @@ def arithmetic_value():
return ((num ^ (lambda i: IntArithmeticExp(i))) |
(imp_id ^ (lambda v: VarArithmeticExp(v))))
def read_statement():
def internal(parsed):
return ReadStatement()
return keyword('read') ^ internal
def arithmetic_term():
"""
Matches an arithmetic term.
returns -- a parser matching an arithmetic value or group
"""
return arithmetic_value() | arithmetic_group()
return read_statement() | arithmetic_value() | arithmetic_group()
def arithmetic_exp():
"""
@@ -205,10 +210,44 @@ def while_statement():
+ keyword('do') + Lazy(statements)
+ keyword('end') ^ internal)
def for_statement():
"""
Builds a parser for for statements.
returns -- a parser for for statements
"""
def internal(parsed):
((((_, condition), _), body), _) = parsed
return ForStatement(condition, body)
return (keyword('for') + arithmetic_exp()
+ keyword('do') + Lazy(statements)
+ keyword('end') ^ internal)
def write_statement():
"""
Builds a parser for write statements.
returns -- a parser for write statements
"""
def internal(parsed):
(_, message) = parsed
return WriteStatement(message)
return keyword('write') + arithmetic_exp() ^ internal
def statement():
return assign_statement() | if_statement() | while_statement()
"""
Builds a parser for single IMP statements.
returns -- a parser for single statements in the IMP langauge
"""
return write_statement() | assign_statement() | if_statement() | while_statement() | for_statement()
def statements():
"""
Builds a parser for multiple IMP statements.
returns -- a parser for multiple statements in the IMP language
"""
sep = keyword(';') ^ (lambda x: lambda l, r: CompoundStatement(l, r))
return Exp(statement(), sep)