From 1ebf95261b5ec5b8567c59c6d8cf46399a6fac91 Mon Sep 17 00:00:00 2001 From: mereszeta Date: Mon, 16 Apr 2018 02:11:46 +0200 Subject: [PATCH] added some stuff --- ast.py | 56 +++++++++++++++++++++++++++- parser.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 147 insertions(+), 16 deletions(-) diff --git a/ast.py b/ast.py index 379de93..834ce5c 100644 --- a/ast.py +++ b/ast.py @@ -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 @@ -88,4 +89,57 @@ def __init__(self, range): class Range(Node): def __init__(self, start, end): self.start = start - self.end = end \ No newline at end of file + 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 \ No newline at end of file diff --git a/parser.py b/parser.py index 9fe0e22..f3ac5b0 100644 --- a/parser.py +++ b/parser.py @@ -8,10 +8,10 @@ tokens = scanner.tokens precedence = ( - # to fill ... - # ("left", '+', '-'), - # ("left", '*', '/') - # to fill ... + ("left", '+', '-'), + ("left", '*', '/'), + ("left", "DOTADD", "DOTSUB"), + ("left", "DOTMUL", "DOTDIV"), ) @@ -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 @@ -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]) @@ -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 "}" @@ -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]) @@ -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()