Skip to content

Commit

Permalink
started working on syntax tree
Browse files Browse the repository at this point in the history
  • Loading branch information
mereszeta committed Apr 15, 2018
1 parent d5ab023 commit 3a92ea7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
25 changes: 25 additions & 0 deletions ast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Node:
def __init__(self, type, children=None, leaf=None):
self.type = type
if children:
self.children = children
else:
self.children = []
self.leaf = leaf


class Expr: pass


class BinOp(Expr):
def __init__(self, left, op, right):
self.type = "binop"
self.left = left
self.right = right
self.op = op


class Number(Expr):
def __init__(self, value):
self.type = "number"
self.value = value
27 changes: 25 additions & 2 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import scanner
import ply.yacc as yacc
from ast import *

tokens = scanner.tokens

Expand Down Expand Up @@ -41,11 +42,33 @@ def p_instructions_2(p):
"""instructions : instruction """


# to finish the grammar
# ....
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 : LPAREN expression RPAREN'
p[0] = p[2]


def p_expression_number(p):
'expression : NUMBER'
p[0] = Number(p[1])


parser = yacc.yacc()

0 comments on commit 3a92ea7

Please sign in to comment.