Skip to content

Commit

Permalink
matrix parsing fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Szeremeta committed May 6, 2018
1 parent 6f116ee commit 25c9ea9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
11 changes: 10 additions & 1 deletion ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def __init__(self, type, children=None, leaf=None):

class Expr: pass


class Instr(Node):
def __init__(self,instructions,instruction):
def __init__(self, instructions, instruction):
self.instructions = instructions
self.instruction = instruction

Expand Down Expand Up @@ -136,10 +137,18 @@ def __init__(self):
def append(self, row):
self.rows.append(row)

def concat(self, mtx, row):
self.rows = list(mtx.rows)
self.rows.append(row)


class Row(Node):
def __init__(self):
self.nums = []

def append(self, num):
self.nums.append(num)

def concat(self, row, num):
self.nums = list(row.nums)
self.append(num)
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 "example2.m"
filename = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
file = open(filename, "r")
except IOError:
print("Cannot open {0} file".format(filename))
Expand Down
34 changes: 19 additions & 15 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ def p_expression_unop(p):
| "!" expression
| expression "'"
'''
p[0] = UnOp(p[1], p[2])
if p[2] != "'":
p[0] = UnOp(p[1], p[2])
else:
p[0] = UnOp(p[2], p[1])


def p_expression_block(p):
Expand Down Expand Up @@ -229,14 +232,20 @@ def p_continue_instruction(p):

def p_print_instruction(p):
'''print_instruction : PRINT vars_to_print
| PRINT '"' expression '"' '''
p[0] = PrintInstruction(p[2])
| PRINT '"' vars_to_print '"' '''
if (p[2] != '"'):
p[0] = PrintInstruction(p[2])
else:
p[0] = PrintInstruction(p[3])


def p_print_list(p):
"""vars_to_print : vars_to_print ',' expression
| expression"""
pass
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[3]


def p_matrix_init_instruction(p):
Expand All @@ -261,25 +270,20 @@ def p_matrix_init_fun(p):
def p_matrix_row(p):
"""matrix_row : matrix_row ',' INTNUM
| INTNUM """
p[0] = Row()
if len(p) == 4:
if p[1] is None:
p[0] = Row()
else:
p[0] = p[1]
p[0].append(p[3])
p[0].concat(p[1],p[3])
else:
p[0].append(p[1])


def p_matrix_rows(p):
"""matrix_rows : matrix_row ';' matrix_rows
| matrix_row"""
p[0] = Matrix()
if len(p) == 4:
if p[1] is None:
p[0] = Matrix()
else:
p[0] = p[1]
p[0].append(p[3])
p[0].concat(p[3], p[1])
else:
p[0] = Matrix()
p[0].append(p[1])


Expand Down
38 changes: 28 additions & 10 deletions treeprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def printTree(self, indent=0):

@addToClass(ast.Number)
def printTree(self, indent=0):
print(createIndent(indent) + self.value)
print(createIndent(indent) + str(self.value))

@addToClass(ast.Variable)
def printTree(self, indent=0):
Expand All @@ -48,9 +48,18 @@ def printTree(self, indent=0):
@addToClass(ast.ForLoop)
def printTree(self, indent=0):
print(createIndent(indent) + 'FOR')
self.variable.printTree(indent + 1)
self.varFrom.printTree(indent + 2)
self.varTo.printTree(indent + 2)
if type(self.variable) == str:
print(createIndent(indent + 1) + self.variable)
else:
self.variable.printTree(indent + 1)
if type(self.varFrom) == str:
print(createIndent(indent + 2) + self.varFrom)
else:
self.varFrom.printTree(indent + 2)
if type(self.varTo) == str:
print(createIndent(indent + 2) + self.varTo)
else:
self.varTo.printTree(indent + 2)
self.instructions.printTree(indent + 1)

@addToClass(ast.WhileLoop)
Expand All @@ -77,7 +86,7 @@ def printTree(self, indent=0):
@addToClass(ast.PrintInstruction)
def printTree(self, indent=0):
print(createIndent(indent) + "print")
self.value.printTree(indent + 1)
print(createIndent(indent + 1)+str(self.value))

@addToClass(ast.ReturnInstruction)
def printTree(self, indent=0):
Expand Down Expand Up @@ -110,9 +119,18 @@ def printTree(self, indent=0):
@addToClass(ast.DoubleMatrixRef)
def printTree(self, indent=0):
print(createIndent(indent) + "ref")
self.id.printTree(indent + 1)
self.idx.printTree(indent + 1)
self.idx2.printTree(indent + 1)
if type(self.id) == str:
print(createIndent(indent + 1) + self.id)
else:
self.id.printTree(indent + 1)
if type(self.idx) == str or type(self.idx) == int:
print(createIndent(indent + 1) + str(self.idx))
else:
self.idx.printTree(indent + 1)
if type(self.idx2) == str or type(self.idx2) == int:
print(createIndent(indent + 1) + str(self.idx2))
else:
self.idx2.printTree(indent + 1)

@addToClass(ast.UnOp)
def printTree(self, indent=0):
Expand All @@ -121,9 +139,9 @@ def printTree(self, indent=0):
else:
self.op.printTree(indent)
if type(self.right) == str:
print(createIndent(indent+1) + self.right)
print(createIndent(indent + 1) + self.right)
else:
self.right.printTree(indent+1)
self.right.printTree(indent + 1)

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

0 comments on commit 25c9ea9

Please sign in to comment.