Skip to content

Commit

Permalink
print parsing fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Szeremeta committed May 6, 2018
1 parent 25c9ea9 commit bca3835
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
11 changes: 11 additions & 0 deletions ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ def __init__(self, value):
self.value = value


class PrintExpressions(Node):
def __init__(self, expression, expressions):
self.expression = expression
self.expressions = expressions


class PrintExpression(Node):
def __init__(self, expression):
self.expression = expression


class ReturnInstruction(Node):
def __init__(self):
pass
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
if __name__ == '__main__':

try:
filename = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
filename = sys.argv[1] if len(sys.argv) > 1 else "example3.txt"
file = open(filename, "r")
except IOError:
print("Cannot open {0} file".format(filename))
Expand Down
6 changes: 3 additions & 3 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ def p_print_list(p):
"""vars_to_print : vars_to_print ',' expression
| expression"""
if len(p) == 2:
p[0] = p[1]
p[0] = PrintExpression(p[1])
else:
p[0] = p[3]
p[0] = PrintExpressions(p[1], p[3])


def p_matrix_init_instruction(p):
Expand All @@ -272,7 +272,7 @@ def p_matrix_row(p):
| INTNUM """
p[0] = Row()
if len(p) == 4:
p[0].concat(p[1],p[3])
p[0].concat(p[1], p[3])
else:
p[0].append(p[1])

Expand Down
11 changes: 10 additions & 1 deletion treeprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ def printTree(self, indent=0):
@addToClass(ast.PrintInstruction)
def printTree(self, indent=0):
print(createIndent(indent) + "print")
print(createIndent(indent + 1)+str(self.value))
self.value.printTree(indent+1)

@addToClass(ast.PrintExpressions)
def printTree(self, indent=0):
self.expression.printTree(indent)
self.expressions.printTree(indent)

@addToClass(ast.PrintExpression)
def printTree(self, indent=0):
self.expression.printTree(indent)

@addToClass(ast.ReturnInstruction)
def printTree(self, indent=0):
Expand Down

0 comments on commit bca3835

Please sign in to comment.