def __fun_calculate(self, fn_value, fn_name, fn_fl=False): fun_val = [] fun_ind = self.__search_fun_index(fn_name.get_value()) fun_tokens = self.functions[fun_ind][-1] fun_stack = [] for i in range(len(fn_value)): if fn_value[i].get_type() == 'VAR': e = self.__find_value(fn_value[i].get_value()) fun_val.append([self.functions[fun_ind][1][i], e]) else: fun_val.append( [self.functions[fun_ind][1][i], fn_value[i].get_value()]) if fn_fl: return [fn_name, fun_tokens, fun_val, self.functions] for i in range(len(fun_tokens) - 1): if fun_tokens[i].get_type() == 'VAR' or fun_tokens[i].get_type( ) == 'DIGIT': fun_stack.append(fun_tokens[i]) elif fun_tokens[i].get_type() == 'ARI_OP': e2 = fun_stack.pop() e1 = fun_stack.pop() if e1.get_type() == 'VAR': ind = fun_val_search(e1.get_value(), fun_val) if ind != -1: e1 = fun_val[ind][-1] else: e1 = self.__find_value(e1.get_value()) else: e1 = e1.get_value() if e2.get_type() == 'VAR': ind = fun_val_search(e2.get_value(), fun_val) if ind != -1: e2 = fun_val[ind][-1] else: e2 = self.__find_value(e2.get_value()) else: e2 = e2.get_value() fun_stack.append(operation(e1, e2, fun_tokens[i].get_value())) elif fun_tokens[i].get_type() == 'ASSIGN_OP': e2 = fun_stack.pop() e1 = fun_stack.pop() flag = True for i in range(len(fun_val)): if e1.get_value() == fun_val[i][0]: flag = False fun_val[i][-1] = e2.get_value() if flag: fun_val.append([e1.get_value(), e2.get_value()]) elif fun_tokens[i].get_type() == 'FN_VALUE': func = self.__fun_calculate(fun_tokens[i].get_value(), fun_tokens[i + 1]) fun_stack.append(func) i += 1 out = fun_stack.pop() if out.get_type() == 'VAR': index = fun_val_search(out.get_value(), fun_val) return Token('DIGIT', fun_val[index][-1]) else: return Token('DIGIT', out.get_value())
def process(self, thread_fl=False): while self.token_count < len(self.tokens): if self.tokens[self.token_count].get_type( ) == 'VAR' or self.tokens[self.token_count].get_type() == 'DIGIT': self.stack.append(self.tokens[self.token_count]) elif self.tokens[self.token_count].get_type() == 'FN_VALUE': if thread_fl: out = self.__fun_calculate( self.tokens[self.token_count].get_value(), self.tokens[self.token_count + 1], True) self.token_count += 2 return 'wait', out else: out = self.__fun_calculate( self.tokens[self.token_count].get_value(), self.tokens[self.token_count + 1]) self.stack.append(out) self.token_count += 1 elif self.tokens[self.token_count].get_type() == 'ARI_OP': self.stack.append( self.__calculate(self.tokens[self.token_count])) elif self.tokens[self.token_count].get_type() == 'ASSIGN_OP': self.__assign_op() elif self.tokens[self.token_count].get_type() == 'LOG_OP': self.stack.append( self.__calculate(self.tokens[self.token_count])) elif self.tokens[self.token_count].get_type() == 'GO_F': flag = self.stack.pop().get_value() if not flag: self.token_count = self.tokens[ self.token_count].get_value() elif self.tokens[self.token_count].get_type() == 'GO_A': self.token_count = self.tokens[self.token_count].get_value() if thread_fl and (self.tokens[self.token_count].get_type() in [ 'ARI_OP', 'ASSIGN_OP', 'LOG_OP' ]) and self.token_count + 2 < len(self.tokens): self.token_count += 1 return 'ready', [] self.token_count += 1 if thread_fl: if self.tokens[-1].get_type() != 'RETURN': return 'exit', self.value_table else: out = self.stack.pop() if out.get_type() == 'VAR': var = self.__find_value(out.get_value()) return 'exit', Token('DIGIT', var) else: return 'exit', Token('DIGIT', out.get_value()) print(self.value_table)
def __make_triads(self): e2 = self.stack.pop() e1 = self.stack.pop() self.triads.append([ '^' + str(len(self.triads)), e1, e2, self.tokens[self.token_count] ]) self.stack.append(Token('TR', self.triads[-1][0]))
def process(self, text): regex = re.compile( 'function\s[a-zA-Z]+\d*|==|!=|\d+|[a-zA-Z]+\d*|[-+*/=;<>{}()]') words = regex.findall(text) for word in words: for regex, token_type in self.rules: if re.match(re.compile(regex), word): self.tokens.append(Token(token_type, word)) break return self.tokens
def __optimize(self): count = 0 variability = True while count < len(self.triads): if self.triads[count][3].get_type() == 'GO_F': variability = False if self.triads[count][3].get_type() == 'END': variability = True if not variability: operand2 = self.triads[count][2] else: operand2 = self.__operand_processing(self.triads[count][2]) if self.triads[count][3].get_type() == 'ASSIGN_OP': if not variability: if not self.triads[count][1].get_value() in self.stop_list: self.stop_list.append( self.triads[count][1].get_value()) count += 1 continue self.triads[count][2] = operand2 if self.__find_value( self.triads[count][1].get_value()) is None: self.value_table.append( [self.triads[count][1].get_value(), operand2]) else: ind = self.__find_value(self.triads[count][1].get_value(), False) self.value_table[ind][-1] = operand2 elif self.triads[count][3].get_type() == 'ARI_OP': if not variability: operand1 = self.triads[count][1] else: operand1 = self.__operand_processing(self.triads[count][1]) if operand2.get_type() == 'DIGIT' and operand1.get_type( ) == 'DIGIT': self.triads[count][1] = self.__operation( operand1.get_value(), operand2.get_value(), self.triads[count][3].get_value()) self.triads[count][2] = Token('DIGIT', 0) self.triads[count][3] = Token('CONST', 'C') count += 1
def operation(e1, e2, op): if op == '-': return Token('DIGIT', float(e1) - float(e2)) if op == '+': return Token('DIGIT', float(e1) + float(e2)) if op == '*': return Token('DIGIT', float(e1) * float(e2)) if op == '/': return Token('DIGIT', round(float(e1) / float(e2), 3)) if op == '==': return Token('BOOL', float(e1) == float(e2)) if op == '!=': return Token('BOOL', float(e1) != float(e2)) if op == '>': return Token('BOOL', float(e1) > float(e2)) if op == '<': return Token('BOOL', float(e1) < float(e2))
def __operand_processing(self, triad): if triad.get_type() == 'VAR': op = self.__find_value(triad.get_value()) if op is not None and triad.get_value() not in self.stop_list: return Token('DIGIT', op.get_value()) else: return triad elif triad.get_type() == 'TR': t = self.triads[int(triad.get_value()[1:])] if t[3].get_type() == 'CONST': return t[1] return triad elif triad.get_type() == 'DIGIT': return triad else: return triad
def __transfer(self): ending = -1 while self.token_count < len(self.tokens): if self.token_count == ending: self.triads.append([ '^' + str(len(self.triads)), Token('END', 'END'), Token('END', 'END'), Token('END', self.token_count - 1) ]) if self.tokens[self.token_count].get_type( ) == 'VAR' or self.tokens[self.token_count].get_type() == 'DIGIT': self.stack.append(self.tokens[self.token_count]) elif self.tokens[ self.token_count].get_type() == 'ARI_OP' or self.tokens[ self.token_count].get_type() == 'ASSIGN_OP': self.__make_triads() elif self.tokens[ self.token_count].get_type() == 'ARI_OP' or self.tokens[ self.token_count].get_type() == 'LOG_OP': self.__make_triads() elif self.tokens[self.token_count].get_type() == 'GO_F': self.triads.append([ '^' + str(len(self.triads)), Token('GO_F', 'GO_F'), self.tokens[self.token_count], self.tokens[self.token_count] ]) elif self.tokens[self.token_count].get_type() == 'GO_A': self.triads.append([ '^' + str(len(self.triads)), Token('GO_A', 'GO_A'), self.tokens[self.token_count], self.tokens[self.token_count] ]) ending = self.tokens[self.token_count].get_value() + 1 elif self.tokens[self.token_count].get_type() == 'IF': self.triads.append([ '^' + str(len(self.triads)), Token('IF', 'IF'), self.tokens[self.token_count], self.tokens[self.token_count] ]) elif self.tokens[self.token_count].get_type() == 'WHILE': self.triads.append([ '^' + str(len(self.triads)), Token('WHILE', 'WHILE'), self.tokens[self.token_count], self.tokens[self.token_count] ]) elif self.tokens[self.token_count].get_type() == 'RETURN': self.triads.append([ '^' + str(len(self.triads)), self.stack.pop(), self.tokens[self.token_count], Token('RETURN', 'RETURN') ]) elif self.tokens[self.token_count].get_type() == 'F_NAME': list = [] f_ind = self.__search_fun_index( self.tokens[self.token_count].get_value()) length = len(self.function[f_ind][1]) for i in range(length): list.append(self.stack.pop()) list.reverse() self.triads.append([ '^' + str(len(self.triads)), Token('FN_VALUE', list), self.tokens[self.token_count], Token('FUN', 'FUN') ]) self.stack.append(Token('TR', self.triads[-1][0])) self.token_count += 1 if ending >= self.token_count: self.triads.append([ '^' + str(len(self.triads)), Token('END', 'END'), Token('END', 'END'), Token('END', self.token_count - 1) ])