change Node to nodes module; use a class hierarchy for node types

This commit is contained in:
Josh Holtrop committed 2011-08-30 10:58:36 -04:00
1 parent 0b33cc7401
commit b8e617bd39
3 files changed
+20 -10

No files matched your search

-7
View File
@@ -1,7 +0,0 @@
class Node(object):
def __init__(self, type, children=None, node=None, data=None):
self.type = type
self.children = children
self.node = node
self.data = data
+17
View File
@@ -0,0 +1,17 @@
class Node(object):
def __init__(self, children=None):
self.children = children
class StatementNode(Node):
def __init__(self, children=None):
Node.__init__(self, children)
class ExprNode(Node):
def __init__(self, children=None):
Node.__init__(self, children)
class CExprNode(ExprNode):
def __init__(self, cstring):
ExprNode.__init__(self)
self.cstring = cstring
+3 -3
View File
@@ -1,10 +1,10 @@
from lexrules import tokens from lexrules import tokens
from Node import Node from nodes import *
def p_statement(p): def p_statement(p):
'statement : expr SEMICOLON' 'statement : expr SEMICOLON'
p[0] = Node('statement', [p[1]]) p[0] = StatementNode([p[1]])
def p_expr(p): def p_expr(p):
'expr : c_expr' 'expr : c_expr'
@@ -12,4 +12,4 @@ def p_expr(p):
def p_c_expr(p): def p_c_expr(p):
'c_expr : C LPAREN STRING RPAREN' 'c_expr : C LPAREN STRING RPAREN'
p[0] = Node('c_expr', data=p[3]) p[0] = CExprNode(p[3])