def check_vars_scope(self, scope=None): if self.collection not in scope.keys(): if self.collection not in Node.global_vars.keys(): custom_exception( 'Isn\'t initialized collection: \'' + self.collection + '\'', self.line, self.column, Exceptions.NAME_ERROR) scope[self.iterator] = self.var_type
def check_params_call(self, scope=None): need_params = Node.global_funcs.get(self.func_name) if need_params is not None and len(self.children[0].children) != 0: if self.func_name != 'print' and self.func_name != 'del': if len(need_params) != len(self.children[0].children): custom_exception( "Get " + str(len(self.children[0].children)) + " param, expected " + str(len(need_params)), self.line, self.column, Exceptions.TYPE_ERROR) else: get_params = self.children[0].check_params_call(scope) for i in range(len(need_params)): if need_params[i] != get_params[i]: custom_exception( "Get " + get_params[i] + " param, expected " + need_params[i], self.line, self.column, Exceptions.TYPE_ERROR) else: get_params = self.children[0].check_params_call(scope) for i in get_params: if i not in need_params: custom_exception( "Get " + i + " param, expected " + str(need_params), self.line, self.column, Exceptions.TYPE_ERROR) elif self.func_name in Node.global_funcs.keys(): custom_exception("Get invalid params in '" + self.func_name + "'", self.line, self.column, Exceptions.TYPE_ERROR) elif self.func_name not in Node.global_funcs.keys(): custom_exception( 'Isn\'t initialized function: \'' + self.func_name + '\'', self.line, self.column, Exceptions.NAME_ERROR)
def check_vars_scope(self, scope=None): if self.var_name not in scope.keys(): if self.var_name not in Node.global_vars.keys(): custom_exception( 'Isn\'t initialized variable: \'' + self.var_name + '\'', self.line, self.column, Exceptions.NAME_ERROR) else: return self.cast_type
def check_var_init_types(self): if len(self.children) != 0: if self.var_type == "node" and self.new_flag: type_of_node = self.children[0].check_var_init_types() if type_of_node != "'tag'" and type_of_node != "'content'": custom_exception( 'Wrong type of node: got ' + type_of_node + ' expected ["tag","content"]', self.line, self.column, Exceptions.TYPE_ERROR)
def check_vars_scope(self, scope: dict = None): if self.func_name != '': if self.func_name not in Node.global_vars.keys( ) and self.func_name not in Node.global_funcs.keys(): custom_exception( 'Isn\'t initialized function: \'' + self.func_name + '\'', self.line, self.column, Exceptions.NAME_ERROR) else: for i in self.children: i.check_vars_scope(scope) return Node.global_vars[self.func_name]
def check_params_call(self, scope=None): need_params = Node.global_funcs.get(self.attribute_name) if len(self.children) != 0: get_params = self.children[0].check_params_call(scope) if need_params is None: custom_exception( "Get invalid params in '" + self.attribute_name + "'", self.line, self.column, Exceptions.TYPE_ERROR) elif len(need_params) != len(get_params): custom_exception( "Get " + str(len(get_params)) + " param, expected " + str(len(need_params)), self.line, self.column, Exceptions.TYPE_ERROR) else: index = 0 for i in get_params: if i != need_params[index]: custom_exception( "Get " + str(len(get_params)) + " param, expected " + str(len(need_params)), self.line, self.column, Exceptions.TYPE_ERROR) index += 1 elif need_params is not None: custom_exception( "Get invalid params in '" + self.attribute_name + "'", self.line, self.column, Exceptions.TYPE_ERROR) return Node.global_vars[self.attribute_name]
def generate(self, scope=None) -> str: global array_types if self.new_flag: return self.var_type + ' ' + self.var_name + '(' + self.children[ 0].generate(scope) + ')' if len( self.children) == 1 else '' + ')' else: if len(self.children) == 0: custom_exception('Invalid operation', self.line, self.column, Exceptions.COMPILE_ERROR) if self.var_type in array_types: return "vector<" + self.var_type[: -2] + "> " + self.var_name + ' = ' + self.children[ 0].generate(scope) else: return self.var_type + '& ' + self.var_name + ' = ' + self.children[ 0].generate(scope) + ''
def generate(self, scope: dict = None) -> str: result = 'auto ' + self.iterator + ' : ' collection_type = scope.get( self.collection) if self.collection in scope.keys( ) else Node.global_vars.get(self.collection) if collection_type in built_in_types: if collection_type == 'document': result += self.collection + '.root().get_children()' elif collection_type == 'node': result += self.collection + '.get_children()' elif collection_type == 'attribute': custom_exception('Invalid collection type: attribute', self.line, self.column, Exceptions.COMPILE_ERROR) exit(1) elif collection_type in array_types: result += self.collection return result
def generate(self, scope=None) -> str: if len(self.children) == 0: custom_exception('Invalid operation', self.line, self.column, Exceptions.COMPILE_ERROR) var_type = scope.get(self.var_name) if self.var_name in scope.keys( ) else Node.global_vars.get(self.var_name) if self.expression_type == 'string': if var_type == "document": return self.var_name + '.' + "set_path(" + self.children[ 0].generate(scope) + ")" elif var_type == "node": return self.var_name + '.' + "set_value(" + self.children[ 0].generate(scope) + "," + str(self.index) + ")" elif var_type == "attribute": return self.var_name + '.' + "set_value(" + self.children[ 0].generate(scope) + ")" else: return self.var_name + ' = ' + self.children[0].generate(scope)
def check_vars_scope(self, scope: dict = None): global constants for i in self.children: i.check_vars_scope(scope) expression_type = self.children[0].check_vars_scope(scope) if self.var_name not in scope.keys(): if self.var_name not in Node.global_vars.keys(): custom_exception( 'Isn\'t initialized variable: \'' + self.var_name + '\'', self.line, self.column, Exceptions.NAME_ERROR) elif scope[self.var_name] != expression_type: if scope[self.var_name] != 'node' or ( expression_type not in constants and expression_type != 'attribute'): custom_exception( 'Wrong type of expression: got \'' + expression_type + '\' expected: ' + scope[self.var_name] + ',' + str(constants), self.line, self.column, Exceptions.TYPE_ERROR)
def visitVar_init(self, ctx: EasyXMLParser.Var_initContext): new_flag = False if ctx.NEW() is not None: new_flag = True var_type = '' if ctx.TYPE() is not None and len(ctx.TYPE()) == 2: if ctx.TYPE()[0].getText() != ctx.TYPE()[1].getText(): custom_exception( ctx.TYPE()[0].getText() + ' != ' + ctx.TYPE()[1].getText(), ctx.start.line, ctx.start.column, Exceptions.TYPE_ERROR) else: var_type = ctx.TYPE()[0].getText() else: if ctx.TYPE() is not None and type(ctx.TYPE()) != list: var_type = ctx.TYPE().getText() elif type(ctx.TYPE()) == list and len(ctx.TYPE()) != 0: var_type = ctx.TYPE()[0].getText() elif ctx.ARRAY_TYPE() is not None and type( ctx.ARRAY_TYPE()) != list: var_type = ctx.ARRAY_TYPE().getText() else: var_type = ctx.ARRAY_TYPE()[0].getText() if ctx.expression() is None: expression = Expression() else: expression = self.visitExpression(ctx.expression()) if ctx.assignment() is not None: assignment = self.visitAssignment(ctx.assignment()) var_name = assignment.var_name expression = assignment.children[0] else: var_name = ctx.VARNAME().getText() var_init_node = VarInit(var_name, var_type, new_flag, line=ctx.start.line, column=ctx.start.column) var_init_node.children.append(expression) return var_init_node
def check_vars_scope(self, scope: dict = None): if self.var_name != '': if self.var_name not in scope.keys(): if self.var_name not in Node.global_vars.keys(): custom_exception( 'Isn\'t initialized variable: \'' + self.var_name + '\'', self.line, self.column, Exceptions.NAME_ERROR) if self.attribute_name != '': if self.attribute_name not in Node.global_vars.keys(): custom_exception( 'Isn\'t initialized attribute: \'' + self.attribute_name + '\'', self.line, self.column, Exceptions.NAME_ERROR) elif Node.global_vars.get( self.var_name) in Node.class_methods.keys(): if self.attribute_name not in Node.class_methods.get( Node.global_vars.get(self.var_name)): custom_exception( '\'' + Node.global_vars.get(self.var_name) + '\' has no attribute \'' + self.attribute_name + '\'', self.line, self.column, Exceptions.ATTRIBUTE_ERROR) else: for i in self.children: i.check_vars_scope(scope) return Node.global_vars[self.attribute_name]
def check_vars_scope(self, scope=None): if self.value is not None: if self.value in scope.keys(): return scope[self.value] elif self.value in Node.global_vars.keys(): return Node.global_vars[self.value] elif '\'' in self.value: return 'string' elif '.' in self.value: return 'float' elif self.value[0] in "0123456789": return 'int' else: custom_exception( 'Isn\'t initialized variable: \'' + self.value + '\'', self.line, self.column, Exceptions.NAME_ERROR) if self.operator is not None: left_value = self.left_expression.check_vars_scope(scope) right_value = self.right_expression.check_vars_scope(scope) if left_value != right_value: if (left_value != 'attribute' and left_value != 'node') or right_value not in constants: custom_exception( 'Invalid type in operation ' + self.operator + ':' + left_value + '!=' + right_value, self.line, self.column, Exceptions.TYPE_ERROR) else: return left_value if len(self.children) != 0: for i in self.children: if type(i) == TypeCast: return i.check_vars_scope(scope) elif type(i) == Get: return i.check_vars_scope(scope) elif type(i) == FuncCall: return i.check_vars_scope(scope) elif type(i) == GetArrayElement: return i.check_vars_scope(scope)
def check_type_cast(self, scope: dict = None): primordial_type = scope.get(self.var_name) if primordial_type is None: primordial_type = Node.global_vars.get(self.var_name) if primordial_type == "node": if self.cast_type != "document": custom_exception( "Can\'t reduce node to '" + self.cast_type + "'", self.line, self.column, Exceptions.VALUE_ERROR) elif primordial_type == "document": if self.cast_type != "node": custom_exception( "Can\'t reduce document to '" + self.cast_type + "'", self.line, self.column, Exceptions.VALUE_ERROR) elif primordial_type == "string": if self.cast_type != "int" and self.cast_type != "float": custom_exception( "Can\'t reduce string to '" + self.cast_type + "'", self.line, self.column, Exceptions.VALUE_ERROR) elif primordial_type == "int": if self.cast_type != "string" and self.cast_type != "float": custom_exception( "Can\'t reduce int to '" + self.cast_type + "'", self.line, self.column, Exceptions.VALUE_ERROR) elif primordial_type == "float": if self.cast_type != "string" and self.cast_type != "int": custom_exception( "Can\'t reduce float to '" + self.cast_type + "'", self.line, self.column, Exceptions.VALUE_ERROR) else: custom_exception('Irreducible type \'' + primordial_type + '\'', self.line, self.column, Exceptions.VALUE_ERROR)
def generate(self, scope=None) -> str: if len(self.children) == 0: custom_exception('Invalid operation', self.line, self.column, Exceptions.COMPILE_ERROR) return self.var_name + '.add(' + self.children[0].generate(scope) + ')'