def run(self, in_syntax_node, in_context, in_code_execution): if in_syntax_node.get_name() == MathUnaryNode.NAME: var1, _ = in_code_execution.get_math_value( in_syntax_node.get_nodes()[0]) if in_syntax_node.type == MathUnaryNode.MINUS: result = -var1.value elif in_syntax_node.type == MathUnaryNode.PLUS: result = var1.value else: raise SeriousException() elif in_syntax_node.get_name() == MathBinaryNode.NAME: var1, _ = in_code_execution.get_math_value( in_syntax_node.get_nodes()[0]) var2, _ = in_code_execution.get_math_value( in_syntax_node.get_nodes()[1]) if var1.metaname != var2.metaname: raise SyntaxException() #todo add type conversion if in_syntax_node.type == MathBinaryNode.PLUS: result = var1.value + var2.value elif in_syntax_node.type == MathBinaryNode.MINUS: result = var1.value - var2.value elif in_syntax_node.type == MathBinaryNode.DIV: result = var1.value / var2.value elif in_syntax_node.type == MathBinaryNode.MULT: result = var1.value * var2.value elif in_syntax_node.type == MathBinaryNode.INT_DIV: result = var1.value // var2.value elif in_syntax_node.type == MathBinaryNode.INT_MOD: result = var1.value % var2.value else: raise SeriousException() else: return SeriousException() return (RuntimeVariable(var1.metaname, result), runtime.type.VALUE)
def __init__(self, in_value, in_type): LeafNode.__init__(self) self._node_name = self.NAME if in_type == self.IN_WORD: try: self.value = int(in_value) self.type = self.INT except: raise SyntaxException() elif in_type == self.IN_CHARVAL: if type(in_value) != str and type(in_value) != unicode: raise SeriousException() try: self.value = float(in_value) self.type = self.FLOAT except: self.value = in_value.rstrip() self.type = self.CHARVAL elif in_type == self.IN_STRING: if type(in_value) != str and type(in_value) != unicode: raise SeriousException() self.value = in_value self.type = self.STRING else: raise SeriousException()
def get_var_metadata(self, name): if not self._variables.has_key(name): raise SyntaxException() metaname = self._variables.get(name).metaname if not self._metadatas.has_key(metaname): raise SeriousException() return self._metadatas[name]
def __init__(self, in_type): BranchNode.__init__(self) self._node_name = self.NAME self._sub_count = 1 self._subnode_list.append(None) if not in_type in [self.MINUS, self.PLUS]: raise SeriousException() self.type = in_type
def __init__(self, in_type): BaseBinaryNode.__init__(self) self._node_name = self.NAME if not in_type in [self.STATIC, self.DYNAMIC, self.FIELD]: raise SeriousException() # def get_priority(self): # if self._subnode_list[0] == None or self._subnode_list[1] == None: # return 1001 # else: # return 0
def _get_priority(self, node): if isinstance(node, MathBinaryNode): if not node.is_linked(): if node.type == MathBinaryNode.INT_DIV or node.type == MathBinaryNode.INT_MOD: return 12 elif node.type == MathBinaryNode.DIV or node.type == MathBinaryNode.MULT: return 11 elif node.type == MathBinaryNode.MINUS or node.type == MathBinaryNode.PLUS: return 10 else: raise SeriousException() elif isinstance(node, MathUnaryNode): if node.is_linked(): return 100 return 0
def run(self, in_syntax_node, in_context, in_code_execution): if in_syntax_node.type == ValueNode.INT: metadata = in_context.request_simple_type_meta('I', 0, 0) value = int(in_syntax_node.value) metaname = metadata.get_full_name() elif in_syntax_node.type == ValueNode.CHARVAL: value = str(in_syntax_node.value) metadata = in_context.request_simple_type_meta('C', len(value), 0) metaname = metadata.get_full_name() else: raise SeriousException() variable = RuntimeVariable( metaname=metaname, value=value, ) return (variable, runtime.type.VALUE)
def get_syntax_node(self): #nodes creation try: while True: self.create_node() except ParserEndException: pass if len(self._node_line) == 0: raise SyntaxException() #tree creation while True: max_priority = 0 work_list = [] for index in range(len(self._node_line)): node = self._node_line[index] # priority = node.get_priority() priority = self._get_priority(node) if priority > max_priority: max_priority = priority del work_list[:] if priority == max_priority: work_list.append(index) if max_priority == 0: break offset = 0 for index in work_list: index += offset node = self._node_line[index] left_node = None right_node = None if index > 0: left_node = self._node_line[index - 1] if node.send_left(left_node): del self._node_line[index - 1] offset -= 1 index -= 1 if index + 1 < len(self._node_line): right_node = self._node_line[index + 1] if node.send_right(right_node): del self._node_line[index + 1] offset -= 1 #tree check if len(self._node_line) != 1: raise SeriousException() return self._node_line[0]
def run(self, in_syntax_node, in_context, in_code_execution): if in_context.is_variable_exist(in_syntax_node.name): raise SyntaxException() if in_syntax_node.reftype == VariableDefinitionNode.LIKE: try: metadata = in_context.get_var_metadata(in_syntax_node.name) except: raise SyntaxException() elif in_syntax_node.reftype == VariableDefinitionNode.TYPE: typename, _ = in_code_execution.get_node_value( in_syntax_node.typename) if self.__is_type_simple(typename): if in_syntax_node.length == None: length = 0 else: length, _ = in_code_execution.get_node_value( in_syntax_node.length) if in_syntax_node.decimals == None: decimals = 0 else: decimals, _ = in_code_execution.get_node_value( in_syntax_node.decimals) metadata = in_context.request_simple_type_meta( typename=in_code_execution.get_node_value( in_syntax_node.typename)[0], length=length, decimals=decimals, ) else: metadata = in_context.get_type_metadata(typename) else: raise SeriousException() in_context.define_variable( name=in_syntax_node.name, metadata=metadata, ) return (True, None)
def parse(self, in_command): raise SeriousException()
def _check_self(self): if self._subnode_list[0] == None or self._subnode_list[1] == None: raise SeriousException()
def create_node(self): token = self._expr_source.get_next() if self._last_linked: self._last_linked = False if token['name'] == 'word': if token['body'] == '-': self._node_line.append(MathUnaryNode(MathUnaryNode.MINUS)) elif token['body'] == '+': self._node_line.append(MathUnaryNode(MathUnaryNode.PLUS)) else: try: new_node = ValueNode(token['body'], ValueNode.IN_WORD) self._node_line.append(new_node) except SyntaxException: #todo refactor this shit new_node, self._expr_source._index = NameParser( self._expr_source._token_list, self._expr_source._index - 1).parse() self._node_line.append(new_node) elif token['name'] == 'charval': self._node_line.append( ValueNode(token['body'], ValueNode.IN_CHARVAL)) elif token['name'] == 'string': self._node_line.append( ValueNode(token['body'], ValueNode.IN_STRING)) elif token['name'] == 'single_lb': subparser = ExpressionParser(self._expr_source) subparser.get_syntax_node() elif token['name'] == 'single_rb': if self._brackets: raise ParserEndException() else: raise SyntaxException() elif token['name'] == 'far_lb': pass elif token['name'] == 'far_rb': if self._brackets and self._expr_source.is_end(): raise ParserEndException() else: raise SyntaxException() elif token['name'] == 'close_lb': pass elif token['name'] == 'close_rb': raise SyntaxException() elif token['name'] == 'solid_lb': pass elif token['name'] == 'solid_rb': raise SyntaxException() else: raise SeriousException() else: if token['name'] == 'word': if token['body'] == '+': node_type = MathBinaryNode.PLUS elif token['body'] == '-': node_type = MathBinaryNode.MINUS elif token['body'] == '*': node_type = MathBinaryNode.MULT elif token['body'] == '/': node_type = MathBinaryNode.DIV elif token['body'] == 'DIV': node_type = MathBinaryNode.INT_DIV elif token['body'] == 'MOD': node_type = MathBinaryNode.INT_MOD else: raise SeriousException() self._node_line.append(MathBinaryNode(node_type)) self._last_linked = True elif token['name'] == 'static_attr': self._node_line.append(MemberNode(MemberNode.STATIC)) elif token['name'] == 'dynamic_attr': self._node_line.append(MemberNode(MemberNode.DYNAMIC)) elif token['name'] == 'field': self._node_line.append(MemberNode(MemberNode.FIELD)) elif token['name'] == 'offset': self._node_line.append(OffsetNode()) else: raise SeriousException()
def send_right(self, in_node): if self._subnode_list[0] == None and in_node != None: self._subnode_list[0] = in_node else: raise SeriousException() return True
def __init__(self, in_type): BaseBinaryNode.__init__(self) self._node_name = self.NAME if not in_type in [self.MINUS, self.PLUS, self.MULT, self.DIV, self.INT_DIV, self.INT_MOD]: raise SeriousException() self.type = in_type