12 if type(node
.left
) == type(0):
13 chars
+= "%d"%node
.left
15 chars
+= toString(node
.left
)
19 if type(node
.right
) == type(0):
20 chars
+= "%d"%node
.right
22 chars
+= toString(node
.right
)
35 class ExpParser(object):
37 def __init__ (self
, tokens
):
40 def jumpToRoot (self
):
41 while self
.ptr
.parent
!= None:
42 self
.ptr
= self
.ptr
.parent
47 for token
in self
.tokens
:
50 if self
.ptr
.left
== None:
51 raise globals.ParseError ('')
52 if self
.ptr
.right
== None:
56 self
.ptr
.parent
= Node()
57 self
.ptr
.parent
.left
= self
.ptr
58 self
.ptr
= self
.ptr
.parent
62 if self
.ptr
.left
== None:
63 raise globals.ParseError ('')
64 elif self
.ptr
.right
== None:
68 self
.ptr
.right
= Node()
69 self
.ptr
.right
.parent
= self
.ptr
70 self
.ptr
.right
.left
= num
71 self
.ptr
.right
.op
= token
72 self
.ptr
= self
.ptr
.right
75 if self
.ptr
.left
== None:
76 self
.ptr
.left
= Node()
77 self
.ptr
.left
.parent
= self
.ptr
78 self
.ptr
= self
.ptr
.left
79 elif self
.ptr
.right
== None:
80 self
.ptr
.right
= Node()
81 self
.ptr
.right
.parent
= self
.ptr
82 self
.ptr
= self
.ptr
.right
84 raise globals.ParseError ('')
87 if self
.ptr
.left
== None:
88 raise globals.ParseError ('')
89 elif self
.ptr
.right
== None:
90 raise globals.ParseError ('')
91 elif self
.ptr
.parent
== None:
94 self
.ptr
= self
.ptr
.parent
98 if self
.ptr
.left
== None:
100 elif self
.ptr
.right
== None:
103 raise globals.ParseError ('')
107 print toString(self
.ptr
)