def declaration(self): # int i =0, k; # int s = f() #variables = [] code = [] if self.symbol.value in self.lexer.types: type = self.symbol.value self.next() while self.symbol != Lexeme(';', 'punctuation'): if self.symbol.type == 'id' and self.symbol not in self.environment: symbol = self.symbol symbol.type = type self.environment.dict[symbol.value] = (type, None) self.next() if self.symbol == Lexeme(',', 'punctuation'): self.next() #variables.append(symbol) continue if self.symbol.value == '=': self.next() expr = self.expression() etype = expr.type #Check type expression and symbol. t = Operator('=') t.add_branch(Variable(symbol.value, symbol.type, True)) self.varnum += 1 if expr.type == 'funcall': function = self.get_env_value( Lexeme(expr.name, expr.type)) etype = function.returnType if etype == symbol.type: self.environment.dict[symbol.value] = expr t.add_branch(expr) code.append(t) else: self.error() # self.next() if self.symbol == Lexeme(',', 'punctuation'): self.next() continue else: self.error(self.symbol.value + ' already declared or not id') return code
def declaration(self): # int i =0, k; # int s = f() #variables = [] code = [] if self.symbol.value in self.lexer.types: type = self.symbol.value self.next() while self.symbol != Lexeme(';', 'punctuation'): if self.symbol.type == 'id' and self.symbol not in self.environment: symbol = self.symbol symbol.type = type self.environment.dict[symbol.value] = (type, None) self.next() if self.symbol == Lexeme(',', 'punctuation'): self.next() #variables.append(symbol) continue if self.symbol.value == '=': self.next() expr = self.expression() etype = expr.type #Check type expression and symbol. t = Operator('=') t.add_branch(Variable(symbol.value, symbol.type, True)) self.varnum+=1 if expr.type == 'funcall': function = self.get_env_value(Lexeme(expr.name, expr.type)) etype = function.returnType if etype == symbol.type: self.environment.dict[symbol.value] = expr t.add_branch(expr) code.append(t) else: self.error() # self.next() if self.symbol == Lexeme(',', 'punctuation'): self.next() continue else: self.error(self.symbol.value + ' already declared or not id') return code
def factor(self): """ factor = (expression) | number """ t = Operator('') if self.symbol.value == '(': self.next('(') branch = self.expression() t.add_branch(branch) self.next(')') else: symbol = self.symbol c = self.symbol.value if symbol.type in self.lexer.types: try: t = Constant(symbol.value, symbol.type) self.next() except ValueError as e: raise ParserError(self.symbol, self.lineNum, self.symbolNum) return t if symbol.type == 'id': (local, exist) = self.exists_in_env(symbol) if symbol.value in [x.name for x in builtin]: type = 'function' symbol.type = 'function' else: if exist: type = self.get_type_in_env(symbol) symbol.type = type else: self.error('unknown symbol') # Att! If funcname == id name, it will bad else: self.error('unknown symbol type') if symbol.type == 'function': t = self.function_call() else: symbol.type = type t = Variable(symbol.value, symbol.type, local) self.next() return t
def expression(self): """ expression ::= [term+-term] """ t = Operator('') sign = 1 # I have no good idea for unary [+/-] if self.symbol.value == '+': # ignore this operator #t.add_branch(Tree(self.symbol)) self.next() if self.symbol.value == '-': t.add_branch(Node(self.symbol.value, self.symbol.type)) self.next() sign = -1 lbranch = self.term() t.add_branch(lbranch) flag = True onetime = False while self.symbol.value in ['+', '-']: flag = False if (onetime): tt = Operator('') tt.add_branch(t) else: tt = Operator('') tt.add_branch(lbranch) symbol = self.symbol if self.symbol.value == '+': rbranch = self.add() tt.name = '+' if self.symbol.value == '-': rbranch = self.sub() tt.name = '-' tt.mark = symbol.value tt.add_branch(rbranch) t = tt onetime = True if flag: t = lbranch return t
def term(self): """ term ::= [atom/*factor] """ lbranch = self.atom() t = Operator('') t.add_branch(lbranch) # flag == True ==> We don't wrap tree as term. Keep it atom. flag = True # onetime == True ==> Every iteration in while() we build new tree, where old tree = left branch of it. onetime = False while self.symbol.value in ['/', '*']: flag = False tt = t if onetime: tt = Operator('') tt.add_branch(t) tt.mark = self.symbol.value if self.symbol.value == '/': rbranch = self.div() tt.name = '/' if self.symbol.value == '*': rbranch = self.mul() tt.name = '*' tt.add_branch(rbranch) onetime = True t = tt if (flag): t = lbranch return t
def factor(self): """ factor = (expression) | number """ t = Operator('') if self.symbol.value == '(': self.next('(') branch = self.expression() t.add_branch(branch) self.next(')') else: symbol = self.symbol c = self.symbol.value if symbol.type in self.lexer.types: try: t = Constant(symbol.value, symbol.type) self.next() except ValueError as e: raise ParserError(self.symbol, self.lineNum, self.symbolNum) return t if symbol.type == 'id': (local, exist) = self.exists_in_env(symbol) if symbol.value in [x.name for x in builtin]: type = 'function' symbol.type = 'function' else: if exist: type = self.get_type_in_env(symbol) symbol.type = type else: self.error('unknown symbol') # Att! If funcname == id name, it will bad else: self.error('unknown symbol type') if symbol.type == 'function': t = self.function_call() else: symbol.type = type t = Variable(symbol.value, symbol.type, local) self.next() return t
def expression(self): """ expression ::= [term+-term] """ t = Operator( '') sign = 1 # I have no good idea for unary [+/-] if self.symbol.value == '+' : # ignore this operator #t.add_branch(Tree(self.symbol)) self.next() if self.symbol.value == '-': t.add_branch(Node(self.symbol.value, self.symbol.type)) self.next() sign = -1 lbranch = self.term() t.add_branch(lbranch) flag = True onetime = False while self.symbol.value in ['+', '-']: flag = False if(onetime): tt = Operator('') tt.add_branch(t) else: tt = Operator('') tt.add_branch(lbranch) symbol = self.symbol if self.symbol.value =='+': rbranch = self.add() tt.name = '+' if self.symbol.value =='-': rbranch = self.sub() tt.name = '-' tt.mark = symbol.value tt.add_branch(rbranch) t = tt onetime = True if flag: t = lbranch return t
def term(self): """ term ::= [atom/*factor] """ lbranch = self.atom() t = Operator('') t.add_branch(lbranch) # flag == True ==> We don't wrap tree as term. Keep it atom. flag = True # onetime == True ==> Every iteration in while() we build new tree, where old tree = left branch of it. onetime = False while self.symbol.value in ['/', '*']: flag = False tt = t if onetime: tt = Operator('') tt.add_branch(t) tt.mark = self.symbol.value if self.symbol.value == '/': rbranch = self.div() tt.name = '/' if self.symbol.value == '*': rbranch = self.mul() tt.name = '*' tt.add_branch(rbranch) onetime = True t = tt if(flag): t = lbranch return t
def atom(self): """atom ::= factor^factor """ t = Operator('') lbranch = self.factor() t.add_branch(lbranch) flag = True onetime = False while self.symbol.value == '^': flag = False tt = t if (onetime): tt = Node() tt.add_branch(t) self.next() rbranch = self.factor() tt.add_branch(rbranch) t = tt onetime = True if flag: t = lbranch return t
def atom(self): """atom ::= factor^factor """ t = Operator('') lbranch = self.factor() t.add_branch(lbranch) flag = True onetime = False while self.symbol.value == '^': flag = False tt = t if(onetime): tt = Node() tt.add_branch(t) self.next() rbranch = self.factor() tt.add_branch(rbranch) t = tt onetime = True if flag: t = lbranch return t