19f22d7c2SAndrew Rist# *************************************************************
29f22d7c2SAndrew Rist#
39f22d7c2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
49f22d7c2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
59f22d7c2SAndrew Rist#  distributed with this work for additional information
69f22d7c2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
79f22d7c2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
89f22d7c2SAndrew Rist#  "License"); you may not use this file except in compliance
99f22d7c2SAndrew Rist#  with the License.  You may obtain a copy of the License at
109f22d7c2SAndrew Rist#
119f22d7c2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
129f22d7c2SAndrew Rist#
139f22d7c2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
149f22d7c2SAndrew Rist#  software distributed under the License is distributed on an
159f22d7c2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f22d7c2SAndrew Rist#  KIND, either express or implied.  See the License for the
179f22d7c2SAndrew Rist#  specific language governing permissions and limitations
189f22d7c2SAndrew Rist#  under the License.
199f22d7c2SAndrew Rist#
209f22d7c2SAndrew Rist# *************************************************************
219f22d7c2SAndrew Rist
22cdf0e10cSrcweir
23cdf0e10cSrcweirimport sys
24cdf0e10cSrcweirimport globals
25cdf0e10cSrcweir
26cdf0e10cSrcweirdef toString (node):
27cdf0e10cSrcweir
28cdf0e10cSrcweir    if node == None:
29cdf0e10cSrcweir        return ''
30cdf0e10cSrcweir
31cdf0e10cSrcweir    chars = '('
32cdf0e10cSrcweir
33cdf0e10cSrcweir    if type(node.left) == type(0):
34cdf0e10cSrcweir        chars += "%d"%node.left
35cdf0e10cSrcweir    else:
36cdf0e10cSrcweir        chars += toString(node.left)
37cdf0e10cSrcweir
38cdf0e10cSrcweir    chars += node.op
39cdf0e10cSrcweir
40cdf0e10cSrcweir    if type(node.right) == type(0):
41cdf0e10cSrcweir        chars += "%d"%node.right
42cdf0e10cSrcweir    else:
43cdf0e10cSrcweir        chars += toString(node.right)
44cdf0e10cSrcweir
45cdf0e10cSrcweir    chars += ")"
46cdf0e10cSrcweir
47cdf0e10cSrcweir    return chars
48cdf0e10cSrcweir
49cdf0e10cSrcweirclass Node(object):
50cdf0e10cSrcweir    def __init__ (self):
51cdf0e10cSrcweir        self.left = None
52cdf0e10cSrcweir        self.right = None
53cdf0e10cSrcweir        self.parent = None
54cdf0e10cSrcweir        self.op = None
55cdf0e10cSrcweir
56cdf0e10cSrcweirclass ExpParser(object):
57cdf0e10cSrcweir
58cdf0e10cSrcweir    def __init__ (self, tokens):
59cdf0e10cSrcweir        self.tokens = tokens
60cdf0e10cSrcweir
61cdf0e10cSrcweir    def jumpToRoot (self):
62cdf0e10cSrcweir        while self.ptr.parent != None:
63cdf0e10cSrcweir            self.ptr = self.ptr.parent
64cdf0e10cSrcweir
65cdf0e10cSrcweir    def build (self):
66cdf0e10cSrcweir        self.ptr = Node()
67cdf0e10cSrcweir
68cdf0e10cSrcweir        for token in self.tokens:
69cdf0e10cSrcweir
70cdf0e10cSrcweir            if token in '+-':
71cdf0e10cSrcweir                if self.ptr.left == None:
72cdf0e10cSrcweir                    raise globals.ParseError ('')
73cdf0e10cSrcweir                if self.ptr.right == None:
74cdf0e10cSrcweir                    self.ptr.op = token
75cdf0e10cSrcweir                else:
76cdf0e10cSrcweir                    self.jumpToRoot()
77cdf0e10cSrcweir                    self.ptr.parent = Node()
78cdf0e10cSrcweir                    self.ptr.parent.left = self.ptr
79cdf0e10cSrcweir                    self.ptr = self.ptr.parent
80cdf0e10cSrcweir                    self.ptr.op = token
81cdf0e10cSrcweir
82cdf0e10cSrcweir            elif token in '*/':
83cdf0e10cSrcweir                if self.ptr.left == None:
84cdf0e10cSrcweir                    raise globals.ParseError ('')
85cdf0e10cSrcweir                elif self.ptr.right == None:
86cdf0e10cSrcweir                    self.ptr.op = token
87cdf0e10cSrcweir                else:
88cdf0e10cSrcweir                    num = self.ptr.right
89cdf0e10cSrcweir                    self.ptr.right = Node()
90cdf0e10cSrcweir                    self.ptr.right.parent = self.ptr
91cdf0e10cSrcweir                    self.ptr.right.left = num
92cdf0e10cSrcweir                    self.ptr.right.op = token
93cdf0e10cSrcweir                    self.ptr = self.ptr.right
94cdf0e10cSrcweir
95cdf0e10cSrcweir            elif token == '(':
96cdf0e10cSrcweir                if self.ptr.left == None:
97cdf0e10cSrcweir                    self.ptr.left = Node()
98cdf0e10cSrcweir                    self.ptr.left.parent = self.ptr
99cdf0e10cSrcweir                    self.ptr = self.ptr.left
100cdf0e10cSrcweir                elif self.ptr.right == None:
101cdf0e10cSrcweir                    self.ptr.right = Node()
102cdf0e10cSrcweir                    self.ptr.right.parent = self.ptr
103cdf0e10cSrcweir                    self.ptr = self.ptr.right
104cdf0e10cSrcweir                else:
105cdf0e10cSrcweir                    raise globals.ParseError ('')
106cdf0e10cSrcweir
107cdf0e10cSrcweir            elif token == ')':
108cdf0e10cSrcweir                if self.ptr.left == None:
109cdf0e10cSrcweir                    raise globals.ParseError ('')
110cdf0e10cSrcweir                elif self.ptr.right == None:
111cdf0e10cSrcweir                    raise globals.ParseError ('')
112cdf0e10cSrcweir                elif self.ptr.parent == None:
113cdf0e10cSrcweir                    pass
114cdf0e10cSrcweir                else:
115cdf0e10cSrcweir                    self.ptr = self.ptr.parent
116cdf0e10cSrcweir
117cdf0e10cSrcweir            else:
118cdf0e10cSrcweir                num = int(token)
119cdf0e10cSrcweir                if self.ptr.left == None:
120cdf0e10cSrcweir                    self.ptr.left = num
121cdf0e10cSrcweir                elif self.ptr.right == None:
122cdf0e10cSrcweir                    self.ptr.right = num
123cdf0e10cSrcweir                else:
124cdf0e10cSrcweir                    raise globals.ParseError ('')
125cdf0e10cSrcweir
126cdf0e10cSrcweir    def dumpTree (self):
127cdf0e10cSrcweir        self.jumpToRoot()
128*7d9fa7c3SPedro Giffuni        print(toString(self.ptr))
129