Skip to content

Commit

Permalink
added some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mereszeta committed Apr 16, 2018
1 parent bc997fb commit 1ebf952
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 16 deletions.
56 changes: 55 additions & 1 deletion ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, variable, varFrom, varTo, instructions):
self.varTo = varTo
self.instructions = instructions


class WhileLoop(Node):
def __init__(self, condition, instructions):
self.condition = condition
Expand Down Expand Up @@ -88,4 +89,57 @@ def __init__(self, range):
class Range(Node):
def __init__(self, start, end):
self.start = start
self.end = end
self.end = end


class Variable(Expr):
def __init__(self, id):
self.id = id


class SingleMatrixRef(Expr):
def __init__(self, id, idx):
self.id = id
self.idx = idx


class DoubleMatrixRef(Expr):
def __init__(self, id, idx, idx2):
self.id = id
self.idx = idx
self.idx2 = idx2


class UnOp(Expr):
def __init__(self, op, right):
self.type = "unop"
self.op = op
self.right = right


class PrintInstruction(Node):
def __init__(self, toprint):
self.toprint = toprint


class AssignInstruction(Node):
def __init__(self, operand, left, right):
self.operand = operand
self.left = left
self.right = right


class Matrix(Node):
def __init__(self):
self.rows=[]

def append(self,row):
self.rows+=row


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

def append(self, num):
self.nums += num
107 changes: 92 additions & 15 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
tokens = scanner.tokens

precedence = (
# to fill ...
# ("left", '+', '-'),
# ("left", '*', '/')
# to fill ...
("left", '+', '-'),
("left", '*', '/'),
("left", "DOTADD", "DOTSUB"),
("left", "DOTMUL", "DOTDIV"),
)


Expand Down Expand Up @@ -42,9 +42,43 @@ def p_instructions_1(p):
def p_instructions_2(p):
"""instructions : instruction """

def p_instruction_assign(p):
'instruction : ID "=" expression'
names_dictionary[p[1]] = p[3]
def p_expression_group(p):
'expression : "(" expression ")"'
p[0] = p[2]


def p_assign_instruction(p):
'''assign_instruction : var assign_operand INTNUM
| var assign_operand FLOATNUM
| var assign_operand ID
| var assign_operand var
| var assign_operand expression
'''
p[0] = AssignInstruction(p[2], p[1], p[3])


def p_var(p):
'''var : ID
| ID '[' INTNUM ']'
| ID '[' INTNUM ',' INTNUM ']'
'''
if len(p) == 2:
p[0] = Variable(p[1])
elif len(p) == 5:
p[0] = SingleMatrixRef(p[1], p[3])
elif len(p) == 7:
p[0] = DoubleMatrixRef(p[1], p[3], p[5])


def p_assign_operand(p):
'''assign_operand : "="
| ADDASSIGN
| SUBASSIGN
| MULASSIGN
| DIVASSIGN
'''
p[0] = p[1]
print("INSTRUCTION ASSIGN")

def p_expression_binop(p):
'''expression : expression "+" expression
Expand All @@ -65,11 +99,6 @@ def p_expression_binop(p):
p[0] = BinOp(p[1], p[2], p[3])


def p_expression_group(p):
'expression : "(" expression ")"'
p[0] = p[2]


def p_expression_number(p):
'expression : INTNUM'
p[0] = Number(p[1])
Expand All @@ -82,6 +111,12 @@ def p_exp_name(p):
print("Name not defined")
p[0] = 0

def p_expression_unop(p):
'''expression : "-" expression
| "!" expression
'''
p[0] = UnOp(p[1, p[2]])


def p_expression_block(p):
'''block : "{" instructions "}"
Expand Down Expand Up @@ -122,17 +157,17 @@ def p_while_2(p):


def p_ones(p):
'expression : ONES "(" INTNUM ")"'
'ones : ONES "(" INTNUM ")"'
p[0] = Ones(p[3])


def p_zeros(p):
'expression : ZEROS "(" INTNUM ")"'
'zeros : ZEROS "(" INTNUM ")"'
p[0] = Zeros(p[3])


def p_eye(p):
'expression : EYE "(" INTNUM ")"'
'eye : EYE "(" INTNUM ")"'
p[0] = Eye(p[3])


Expand Down Expand Up @@ -165,4 +200,46 @@ def p_range(p):
'range : "[" INTNUM "," INTNUM "]" '
p[0]= Range(p[2], p[4])



def p_matrix_init_instruction(p):
'''matrix_init_instruction : matrix_init_fun
| '[' matrix_row ']'
| '[' matrix_rows ']'
'''
if len(p)==2:
p[0] = p[1]
else: p[0] = p[2]


def p_matrix_init_fun(p):
'''matrix_init_fun : zeros
| ones
| eye
'''


def p_matrix_row(p):
"""matrix_row : matrix_row ',' INTNUM
| INTNUM """
if len(p)==4:
if p[1] is None:
p[0]=Row()
else: p[0]=p[1]
p[0].append(p[3])
else:
p[0].append(p[3])


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

parser = yacc.yacc()

0 comments on commit 1ebf952

Please sign in to comment.