Skip to content

Commit

Permalink
Another part of partisan changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mblaszkiewicz committed Apr 15, 2018
1 parent 6790c77 commit bc997fb
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 42 deletions.
65 changes: 63 additions & 2 deletions ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,67 @@ def __init__(self, value):
self.value = value


class IfElse(Node):
def __init__(self, condition, instTrue, instFalse):
self.condition = condition
self.instTrue = instTrue
self.instFalse = instFalse


class ForLoop(Node):
def __init__(self, variable, varFrom, varTo, instructions):
self.variable = variable
self.varFrom = varFrom
self.varTo = varTo
self.instructions = instructions

class WhileLoop(Node):
def __init__(self, condition, instructions):
self.condition = condition
self.instructions = instructions


class Ones(Node):
def __init__(self, num):
self.num = num


class Zeros(Node):
def __init__(self, num):
self.num = num


class Eye(Node):
def __init__(self, num):
self.num = num


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


class ReturnInstruction(Node):
def __init__(self):
pass


class ContinueInstruction(Node):
def __init__(self):
pass


class BreakInstruction(Node):
def __init__(self):
pass


class Array(Node):
def __init__(self, range):
self.range = range


class Range(Node):
def __init__(self, start, end):
self.start = start
self.end = end
142 changes: 102 additions & 40 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

def p_error(p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, scanner.find_column(p),
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, "1",
p.type, p.value))
else:
print("Unexpected end of input")
Expand Down Expand Up @@ -45,62 +45,124 @@ def p_instructions_2(p):
def p_instruction_assign(p):
'instruction : ID "=" expression'
names_dictionary[p[1]] = p[3]
print("INSTRUCTION ASSIGN")

# def p_expression_binop(p):
# '''expression : expression "+" expression
# | expression "-" expression
# | expression "*" expression
# | expression "/" expression
# | expression "<" expression
# | expression ">" expression
# | expression DOTADD expression
# | expression DOTSUB expression
# | expression DOTMUL expression
# | expression DOTDIV expression
# | expression EQ expression
# | expression LTE expression
# | expression GTE expression
# | expression NE expression '''
#
# p[0] = BinOp(p[1], p[2], p[3])


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

def p_expression_binop(p):
'''expression : expression "+" expression
| expression "-" expression
| expression "*" expression
| expression "/" expression
| expression "<" expression
| expression ">" expression
| expression DOTADD expression
| expression DOTSUB expression
| expression DOTMUL expression
| expression DOTDIV expression
| expression EQ expression
| expression LTE expression
| expression GTE expression
| expression NE expression '''

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])

def p_exp_name(p):
'expression : ID'
try:
p[0] = names_dictionary[p[1]]
except LookupError:
print("Name not defined")
p[0] = 0


def p_expression_block(p):
'''block : "{" instructions "}"
| instruction '''
if len(p) == 3:
p[0] = p[2]
else:
p[0] = p[1]


def p_if_else(p):
'instruction : IF "(" expression ")" block ELSE block'
p[0] = IfElse(p[3], p[5], p[7])

def p_if(p):
'instruction : IF "(" expression ")" block'
p[0] = IfElse(p[3], p[5], None)


def p_for_1(p):
'instruction : FOR ID "=" INTNUM ":" INTNUM "{" instructions "}"'
p[0] = ForLoop(p[2], p[4], p[6], p[8])


def p_for_2(p):
'instruction : FOR ID "=" INTNUM ":" INTNUM instruction '
p[0] = ForLoop(p[2], p[4], p[6], p[7])


def p_while_1(p):
'instruction : WHILE "(" expression ")" instruction'
p[0] = WhileLoop(p[3], p[5])


def p_while_2(p):
'instruction : WHILE "(" expression ")" "{" instructions "}"'
p[0] = WhileLoop(p[3], p[6])


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


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


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


def p_break(p):
'instruction : BREAK ";"'
p[0] = BreakInstruction()


def p_return(p):
'instruction : RETURN ";"'
p[0] = ReturnInstruction()

def p_while_for(p):
'''instruction : WHILE "(" expression ")" "{" instructions "}"
| FOR ID "=" INTNUM ":" INTNUM "{" instructions "}"
| WHILE "(" expression ")" instruction
| FOR ID "=" INTNUM ":" INTNUM instruction '''
print("WHILE/FOR")

def p_break_cont_ret(p):
'''instruction : BREAK ";"
| CONTINUE ";"
| RETURN ";"'''
print("BREAK/CONT/RETURN")
def p_continue(p):
'instruction : CONTINUE ";"'
p[0] = ContinueInstruction()


def p_print(p):
'instruction : PRINT ID ";"'
'instruction : PRINT expression ";"'
p[0] = PrintInstruction(p[2])


def p_array_range(p):
'arrayrange : ID range'
'expression : ID range'
p[0] = Array(p[2])


def p_range(p):
'range : "[" INTNUM "," INTNUM "]" '
p[0]= Range(p[2], p[4])

parser = yacc.yacc()

0 comments on commit bc997fb

Please sign in to comment.