def importBeerXML(self): fichierBeerXML = self.s try: self.recipe = Recipe.parse(fichierBeerXML) self.currentRecipeMash = self.recipe.mash except: errors = Errors() errors.warningXml()
def quantize(input_image: str, output_image: str, r_bits: int, g_bits: int, b_bits: int): err = Errors() with open(input_image, 'rb+') as fi: # read the the first part of the header header_header = byte_list(fi, 12) image_width_raw = byte_list(fi, 2) image_width = int_from_bytes(image_width_raw) image_height_raw = byte_list(fi, 2) image_height = int_from_bytes(image_height_raw) # read the rest of the header pixel_depth = one_byte(fi) image_descriptor = one_byte(fi) with open(output_image, 'wb') as fo: # copy the header over fo.write(bytes(header_header)) fo.write(bytes(image_width_raw)) fo.write(bytes(image_height_raw)) fo.write(bytes([pixel_depth])) fo.write(bytes([image_descriptor])) # wrap the bits counts inside a convienient list colour_bits_count = [b_bits, g_bits, r_bits] # process the pixels for _ in range(0, image_height): for __ in range(0, image_width): # take three bytes for each pixel (BGR *not* RGB) pixel = byte_list(fi, 3) quantized_pixel = [] for colour in [0, 1, 2]: # how many bits have to be striped off shift = 8 - colour_bits_count[colour] q = pixel[colour] if shift > 0: # strip off unnecessary bits q = q >> shift # to make sure we've got a middle value of the range that comes from # quantizing the pixel add a one and then fill out the rest with zeroes q = q << 1 q += 1 if shift > 1: q = q << shift - 1 fo.write(bytes([q])) # add appropriate values to SNR and MSE err.register_val(pixel[colour], q, COLOURS[colour]) # copy the footer over x = fi.read(1) while x: fo.write(x) x = fi.read(1) return err
def set_points(self, input_parameters): # Gets and sets all the values* to class parameters # Values - x and y for every methods and for error approximation methods = Methods() # ------------------------------------------------ # Set values given by Numeric methods # ------------------------------------------------ methods.set_x(input_parameters, self.x_common) methods.exact_solution(input_parameters, self.x_common, self.y_exact) methods.euler_method(input_parameters, self.x_common, self.y_euler) methods.improved_euler_method(input_parameters, self.x_common, self.y_imp_euler) methods.runge_kutta_method(input_parameters, self.x_common, self.y_fk) # ------------------------------------------------ # Set values of Local approximation errors # ------------------------------------------------ errors = Errors() errors.local_error(self.diff_exact_euler, self.y_exact, self.y_euler) errors.local_error(self.diff_exact_imp_euler, self.y_exact, self.y_imp_euler) errors.local_error(self.diff_exact_rk, self.y_exact, self.y_fk) # ------------------------------------------------ # Set values of Max approximation errors # ------------------------------------------------ self.n_x, self.n_euler, self.n_euler_imp, self.n_rk = errors.max_error( input_parameters)
def handle_next_stage(result, next_stage=None): if not Errors().is_ok(): for error in Errors().list: print(error) exit(-1) if next_stage is None: return result return next_stage(result)
def res(code=Errors.SUCCESS, data=None, error=None, extra_msg=None): result = { 'code': code, } if Errors.is_succeed(code): result['success'] = True result['detail'] = data else: result['success'] = False if error: result['error'] = error else: result['error'] = Errors.error_msg(code, extra_msg) return jsonify(**result)
def parse_source_code(text: str): ast = parser.parse(text, tracking=True) if Args().stop_after_parsing and Errors().is_ok(): print(json.dumps(AstToDictVisitor().visit(ast))) exit(0) return handle_next_stage(ast, visit_ast)
def testConflict(self): st = SymbolTable() errors = Errors() st.add('x', self.x, errors) st.add('x', self.x2, errors) self.assertEquals(errors.num_errors, 1) self.assertEquals(st.get_names(), set(['x'])) self.assertEquals(st.lookup('x'), self.x)
def __init__(self, lib_path, do_type_check): self.errors = Errors() self.lib_path = lib_path self.do_type_check = do_type_check self.sem_analyzer = SemanticAnalyzer(lib_path, self.errors) self.type_checker = TypeChecker(self.errors, self.sem_analyzer.modules) self.states = [] self.module_files = {}
def auth_function(*args, **kwargs): if AUTH_ENABLED == True: user = r.get(request.headers.get('Authorization')) print(user) if user == None: return Errors.unauthorized() else: return f(*args, **kwargs)
def __init__(self): checker.types = TopTypes() checker.errors = Errors() for ast in PREDEF_CLASSES: checker.types.put(ast.type_name, self.make_class_info(ast)) for name in PREDEF_TYPES: checker.types.put(name, PrimitiveType(name)) nilType = NilType() checker.types.put(nilType.name, nilType)
def assertSuccess(self, input): errors = Errors() fl = Flatten(input, errors) self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0) inl = Inline(input, errors) self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0)
def __init__(self, threadID, dataSet, minStartWeight, maxStartWeight): Process.__init__(self) self.threadID = threadID self.weights = Weights(dataSet.getDataPoint(0).dimension()) self.weights.generateRandom(minStartWeight, maxStartWeight) weight = """*****\nThread {0} initial weights:\n{1}\n*****""" print(weight.format(self.threadID, self.weights.vector)) self.trainingErrors = Errors(dataSet.trainingDataPoints, dataSet.trainingActualValues) self.testingErrors = Errors(dataSet.testingDataPoints, dataSet.testingActualValues) self.hypothesis = Hypothesis() self.trainingErrors.updateToLower(self.weights, self.hypothesis) self.iterations = 0 self.alpha = STARTING_ALPHA
def testShadow(self): parent = SymbolTable() parent.add('x', self.x, None) st = SymbolTable(parent) errors = Errors() st.add('x', self.x2, errors) self.assertEquals(errors.num_warnings, 1) self.assertEquals(st.get_names(), set(['x'])) self.assertEquals(st.get_all_names(), set(['x'])) self.assertEquals(st.lookup('x'), self.x2)
def query(): """Processes 'query' method""" filter_by = request.args.get('filter_by') group_by = request.args.get('group_by') sort_by = request.args.get('sort_by') columns = request.args.get('columns') arguments = { "filter_by": filter_by, "group_by": group_by, "sort_by": sort_by, "columns": columns, } # returns the json dictionary try: return Handler.creating_answer(arguments) except OperationalError as e: return Errors.existing_errors(e) except IndexError: return Errors.created_errors("IndexError")
def enregistrer(self): if self.recipe.name != self.lineEditRecette.text(): self.nameChanged = True else: self.nameChanged = False self.recipe.name = self.lineEditRecette.text() self.recipe.style = self.lineEditGenre.text() self.recipe.brewer = self.lineEditBrewer.text() self.recipe.boil = self.spinBoxBoil.value() if not self.s: destination = recettes_dir + "/" + \ self.recipe.name.replace('/', ' ') + ".xml" if os.path.exists(destination): errors = Errors() errors.warningExistingPath() self.fileSaved = False else: self.s = destination self.enregistrerRecette(destination) else: self.enregistrerRecette(self.s)
def __init__(self, ast, output_name): self.output_name = output_name #if set to true, remains labels self.debug = False self.a = None self.b = None self.c = None self.d = None self.e = None self.f = None self.types = {} self.types['var'] = 'int' self.types['number'] = 'number' self.types['table'] = 'table' self.counter = 0 #access to errors self.error = Errors() self.iterators = {} # indicates next address to assign self.ptr = 4 self.load_registers() self.parse_tree = ast self.variables = self.get_variables() self.declarations = self.variables.keys() self.decoder = RegisterInstructions() self.assembler = self.decoder.get_assembler() self.commands = ast[2] # help addresses to swap or for temporary storage self.help1 = 0 self.help2 = 1 self.help3 = 2 self.help4 = 3 ############## self.storeNumberInVariables('1', '')
def api(): if request.method == 'GET': return jsonify(text='This is how you should format your POST data.') if request.method == 'POST': try: f = request.get_json() except Exception as e: # Should log this error maybe? print(e) return Errors.bad_request() else: for text in f: print( datetime.datetime.fromtimestamp(timestamp).strftime( '%Y-%m-%d %H:%M:%S')) return jsonify(Sentiment.demo_vader_instance(f[text]))
class Methods(): def __init__(self, ast, output_name): self.output_name = output_name #if set to true, remains labels self.debug = False self.a = None self.b = None self.c = None self.d = None self.e = None self.f = None self.types = {} self.types['var'] = 'int' self.types['number'] = 'number' self.types['table'] = 'table' self.counter = 0 #access to errors self.error = Errors() self.iterators = {} # indicates next address to assign self.ptr = 4 self.load_registers() self.parse_tree = ast self.variables = self.get_variables() self.declarations = self.variables.keys() self.decoder = RegisterInstructions() self.assembler = self.decoder.get_assembler() self.commands = ast[2] # help addresses to swap or for temporary storage self.help1 = 0 self.help2 = 1 self.help3 = 2 self.help4 = 3 ############## self.storeNumberInVariables('1', '') def load_registers(self): self.a = Register("a", self.ptr) self.ptr += 1 self.b = Register("b", self.ptr) self.ptr += 1 self.c = Register("c", self.ptr) self.ptr += 1 self.d = Register("d", self.ptr) self.ptr += 1 self.e = Register("e", self.ptr) self.ptr += 1 self.f = Register("f", self.ptr) self.ptr += 1 ############################################33 # load declarations / arrays/ variables ##############################################3 def get_variables(self): declarations = [] dict = {} def get_arr(t): arr = list(t) if isinstance(arr[0], str) == False: del arr[0] return arr def go_rec(tree): copy = tree declarations.append(get_arr(tree)) if isinstance(tree[0], tuple): go_rec(tree[0]) go_rec(self.parse_tree[1]) for declaration in declarations: declaration_id = declaration[1] if declaration_id in dict.keys(): if declaration[0] == self.types['table']: line = declaration[4] else: line = declaration[2] self.error.redeclaration(line) else: # array declaration if declaration[0] == self.types['table']: line = declaration[4] # wrong table declaration if int(declaration[2]) > int(declaration[3]): self.error.wrong_array_declaration(line) else: first = int(declaration[2]) last = int(declaration[3]) table = Table(declaration_id, first, last) table.first_address_id = self.ptr self.ptr += table.length - 1 table.last_address_id = self.ptr self.ptr = self.ptr + 1 dict[table.name] = table else: variable = Variable(declaration_id) variable.address = self.ptr self.ptr += 1 dict[variable.name] = variable return dict def compile(self): self.assembly() #############################################33 # UTILS methods ##############################################3 def get_assembler(self): commands = self.parse_tree[2:][0] for command in commands: self.match_action(command) self.assembler.append("HALT") self.clear_assembly_code(self.assembler, self.debug) f = open(self.output_name, "w") for a in self.assembler: f.write(str(a) + '\n') f.close() def add_var_to_variables(self, name): variable = Variable(name) self.ptr += 1 variable.address = self.ptr self.variables[variable.name] = variable def check_if_correct_tab_index(self, tab_name, idx): obj = self.variables[tab_name] start = obj.first end = obj.last if not int(start) <= idx <= int(end): self.error.wrong_table_index("") def remove_variable_from_variables(self, name): self.variables.pop(name, None) def setValueInRegister(self, register, value): self.set_number_in_register(register.name, value) #self.decoder.STORE(register.name,register.name) def setAddressInRegister(self, register, memory_cell): register.address = memory_cell self.set_number_in_register(register.name, memory_cell) def getVarAddress(self, name): obj = self.variables[str(name)] return obj.address def getNumberAddress(self, name): obj = self.variables[str(name)] return obj.get_address() def set_number_in_register(self, reg, number, bound=5): self.decoder.RESET(reg) temp = [] number = int(number) while number > bound: if number % 2: number -= 1 temp.append(self.decoder.INC(reg, False)) else: number = number // 2 temp.append(self.decoder.SHL(reg, False)) while number > 0: number -= 1 temp.append(self.decoder.INC(reg, False)) temp.reverse() self.decoder.executeList(temp) def loadValueOfAddressToRegister(self, address, register): self.setValueInRegister(self.f, int(address)) self.decoder.LOAD(register, self.f.name) def printValueOfAddress(self, address): self.loadValueOfAddressToRegister(address, self.a) self.decoder.STORE(self.a.name, self.f.name) self.decoder.PUT(self.f.name) def storeNumberInVariables(self, value, line): if value in self.variables.keys(): self.error.redeclaration(str(line)) self.ptr = self.ptr + 1 addr = self.ptr obj = Number(value) obj.value = int(value) obj.address = addr self.variables[value] = obj self.setValueInRegister(self.b, int(value)) self.setValueInRegister(self.c, addr) self.decoder.STORE(self.b.name, self.c.name) # based on index number or variable return its addr in b register def set_table_address_in_B(self, tab_name, value): obj = self.variables[tab_name] # get the address of fires tab value first_address = obj.get_first_address() first_idx = obj.first variable_address = self.getVarAddress(value) self.setValueInRegister(self.e, first_idx) self.loadValueOfAddressToRegister(variable_address, self.d.name) #off set self.decoder.SUB(self.d.name, self.e.name) # add offset and strign address for particular array # store res in b self.setValueInRegister(self.b, first_address) self.decoder.ADD(self.b.name, self.d.name) ################################################ # READ ################################################ def read(self, command): self.decoder.RESET(self.b.name) self.decoder.RESET(self.c.name) self.decoder.RESET(self.d.name) self.decoder.RESET(self.f.name) self.decoder.RESET(self.e.name) self.decoder.RESET(self.a.name) # read to a line = command[2] type = command[1][0] self.set_var_declared(command[1][1], line) if type == self.types['var']: variable_name = command[1][1] if variable_name not in self.variables.keys(): self.error.undeclared_variable(line) # ustawiam rejest a na wartosc adresu int var addr = self.getVarAddress(variable_name) self.setAddressInRegister(self.a, addr) # czytam do tego adresu self.decoder.GET(self.a) # tab elif type == self.types['table']: tab_name = command[1][1] if command[1][2][0] == self.types['number']: value = command[1][2][1] tab_name = command[1][1] if tab_name not in self.variables.keys(): self.error.undeclared_table(line) self.check_if_correct_tab_index(tab_name, int(value)) if value not in self.variables.keys(): self.storeNumberInVariables(value, line) self.set_table_address_in_B(tab_name, value) # now in b we got proper tab(n) address # so we can GET into it self.decoder.GET(self.b) self.decoder.RESET(self.b.name) #int else: line = command[2] variable_name = command[1][2][1] tab_name = command[1][1] if tab_name not in self.variables.keys(): self.error.undeclared_table(line) if variable_name not in self.variables.keys(): self.error.unknown_variable(line) self.set_table_address_in_B(tab_name, variable_name) # now in b we got proper tab(n) address # so we can GET into it self.decoder.GET(self.b) self.decoder.RESET(self.b.name) else: self.error.read_error(line) #############################################33 # WRITE ##############################################3 def write(self, command): line = int(command[2]) type = command[1][0] id = command[1][1] self.decoder.RESET(self.b.name) self.decoder.RESET(self.c.name) self.decoder.RESET(self.d.name) self.decoder.RESET(self.f.name) self.decoder.RESET(self.e.name) self.decoder.RESET(self.a.name) # int var if type == self.types['var']: self.check_var_declared(id, line) # get address of a variavle addr = self.getVarAddress(id) # set address as a value of register a self.setValueInRegister(self.a, addr) self.decoder.LOAD(self.b.name, self.a.name) # store in b self.decoder.STORE(self.b.name, self.f.name) # write self.decoder.PUT(self.f.name) #tab elif type == self.types['table']: tab_name = command[1][1] line = command[2] # tab with int variable alike tab(n) if command[1][2][0] == self.types['var']: variable_name = command[1][2][1] if variable_name not in self.variables.keys(): self.error.unknown_variable(line) #get address of particual element self.set_table_address_in_B(tab_name, variable_name) self.decoder.LOAD(self.d.name, self.b.name) self.decoder.STORE(self.d.name, self.c.name) self.decoder.PUT(self.c.name) else: index_value = command[1][2][1] if index_value not in self.variables.keys(): self.storeNumberInVariables(index_value, line) self.check_if_correct_tab_index(tab_name, int(index_value)) #get address of particual element self.set_table_address_in_B(tab_name, index_value) self.decoder.LOAD(self.d.name, self.b.name) self.decoder.STORE(self.d.name, self.c.name) self.decoder.PUT(self.c.name) elif type == self.types['number']: # set numeric value in a register b self.setValueInRegister(self.b, int(command[1][1])) # do write we have to store it in helper memory slot and then # invoke by its address # load value of helper addres into c self.setValueInRegister(self.c, self.help1) # store numeric value in help addres self.decoder.STORE(self.b.name, self.c.name) # now we can invoke put on helper address to write number self.decoder.PUT(self.c.name) self.decoder.RESET(self.b.name) else: self.error.write_error(line) ####################################################### # Expressions ####################################################### def addition(self): # left -> a register # right -> c register self.decoder.RESET(self.b.name) self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.a.name, self.c.name) self.decoder.ADD(self.b.name, self.a.name) def exp_subtraction(self): self.decoder.RESET(self.b.name) self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.a.name, self.c.name) self.decoder.SUB(self.b.name, self.a.name) def multiplication(self): labels = self.get_label_dict(4) self.decoder.LOAD(self.a.name, self.a.name) self.decoder.LOAD(self.c.name, self.c.name) self.decoder.RESET(self.b.name) self.decoder.setLabel(labels[1]) self.decoder.JZERO(self.c.name, labels[4]) self.decoder.JODD(self.c.name, labels[3]) self.decoder.setLabel(labels[2]) self.decoder.SHR(self.c.name) self.decoder.ADD(self.a.name, self.a.name) self.decoder.JUMP(labels[1]) self.decoder.setLabel(labels[3]) self.decoder.ADD(self.b.name, self.a.name) self.decoder.JUMP(labels[2]) self.decoder.setLabel(labels[4]) def division(self, register): labels = self.get_label_dict(7) self.decoder.LOAD(self.d.name, self.a.name) self.decoder.LOAD(self.c.name, self.c.name) self.decoder.RESET(register.name) self.decoder.JZERO(self.c.name, labels[7]) self.decoder.RESET(self.e.name) self.decoder.ADD(self.e.name, self.c.name) self.decoder.RESET(register.name) self.decoder.addLabel(labels[2]) self.decoder.ADD(register.name, self.e.name) self.decoder.SUB(register.name, self.d.name) self.decoder.JZERO(register.name, 2) self.decoder.JUMP(labels[3]) self.decoder.SHL(self.e.name) self.decoder.JUMP(labels[2]) self.decoder.addLabel(labels[3]) self.decoder.RESET(register.name) self.decoder.addLabel(labels[1]) self.decoder.RESET(self.f.name) self.decoder.ADD(self.f.name, self.e.name) self.decoder.SUB(self.f.name, self.d.name) self.decoder.JZERO(self.f.name, labels[6]) self.decoder.SHL(register.name) self.decoder.SHR(self.e.name) self.decoder.JUMP(labels[5]) self.decoder.addLabel(labels[6]) self.decoder.SHL(register.name) self.decoder.INC(register.name) self.decoder.SUB(self.d.name, self.e.name) self.decoder.SHR(self.e.name) self.decoder.addLabel(labels[5]) self.decoder.RESET(self.f.name) self.decoder.ADD(self.f.name, self.c.name) self.decoder.SUB(self.f.name, self.e.name) self.decoder.JZERO(self.f.name, labels[1]) self.decoder.addLabel(labels[7]) # returns dict of labels # predefined number of labels needed def get_label_dict(self, amount): labels = {} for i in range(1, amount + 1): labels[i] = self.injectAsmLabel() return labels def modulo(self): labels = self.get_label_dict(4) # by default operating data stored in a,c addresses # load them into b and d self.decoder.LOAD(self.d.name, self.a.name) self.decoder.LOAD(self.c.name, self.c.name) # by rules mod 0 is zero if right side value # is zero then jump and return 0 self.decoder.JZERO(self.c.name, labels[4]) self.decoder.RESET(self.e.name) self.decoder.ADD(self.e.name, self.c.name) self.decoder.addLabel(labels[1]) self.decoder.RESET(self.b.name) self.decoder.ADD(self.b.name, self.e.name) self.decoder.SUB(self.b.name, self.d.name) self.decoder.JZERO(self.b.name, 2) self.decoder.JUMP(3) self.decoder.ADD(self.e.name, self.e.name) self.decoder.JUMP(labels[1]) self.decoder.RESET(self.b.name) self.decoder.RESET(self.f.name) self.decoder.addLabel(labels[3]) self.decoder.ADD(self.f.name, self.e.name) self.decoder.SUB(self.f.name, self.d.name) self.decoder.JZERO(self.f.name, 4) self.decoder.ADD(self.b.name, self.b.name) self.decoder.SHR(self.e.name) self.decoder.JUMP(labels[2]) self.decoder.ADD(self.b.name, self.b.name) self.decoder.INC(self.b.name) self.decoder.SUB(self.d.name, self.e.name) self.decoder.SHR(self.e.name) self.decoder.addLabel(labels[2]) self.decoder.RESET(self.f.name) self.decoder.ADD(self.f.name, self.c.name) self.decoder.SUB(self.f.name, self.e.name) self.decoder.JZERO(self.f.name, labels[3]) self.decoder.JUMP(2) self.decoder.addLabel(labels[4]) self.decoder.RESET(self.d.name) self.decoder.RESET(self.b.name) self.decoder.ADD(self.b.name, self.d.name) def undeclared_check(self, data, register, setbound=False): type = data[0] if type == self.types['number']: line = data[2] value = data[1] if value not in self.variables.keys(): self.storeNumberInVariables(value, line) elif type == self.types['var']: line = data[2] value = data[1] self.check_var_declared(value, line) elif type == self.types['table']: line = data[3] tab_name = data[1] index_value = data[2][1] index_type = data[2][0] # check if index in variables if index_type == self.types['number']: if index_value not in self.variables.keys(): self.storeNumberInVariables(index_value, line) self.check_if_correct_tab_index(tab_name, int(index_value)) elif index_type == self.types['var']: self.check_var_declared(index_value, line) else: self.error.unknown_type(line) def store_address_in_register(self, data, register, setbound=False): self.decoder.RESET(register.name) type = data[0] if type == self.types['number']: value = data[1] line = data[2] if value not in self.variables.keys(): self.storeNumberInVariables(value, line) address = self.getNumberAddress(value) self.setValueInRegister(register, address) elif type == self.types['var']: line = data[2] value = data[1] self.check_var_declared(value, line) address = self.getVarAddress(value) self.setValueInRegister(register, address) elif type == self.types['table']: line = data[3] tab_name = data[1] index_value = data[2][1] index_type = data[2][0] # check if index in variables if index_type == self.types['number']: if index_value not in self.variables.keys(): self.storeNumberInVariables(index_value, line) self.check_if_correct_tab_index(tab_name, int(index_value)) elif index_type == self.types['var']: self.check_var_declared(index_value, line) else: self.error.unknown_type(line) # get object of array from variables obj = self.variables[tab_name] # get the address of fires tab value first_address = obj.get_first_address() first_idx = obj.first variable_address = self.getVarAddress(index_value) self.setValueInRegister(self.e, first_idx) self.loadValueOfAddressToRegister(variable_address, self.d.name) #off set self.decoder.SUB(self.d.name, self.e.name) # add offset and strign address for particular array # store res in b self.setValueInRegister(register, first_address) self.decoder.ADD(register.name, self.d.name) def calculate_expression(self, command): # set left side address in a register avlue # set right side address in c register value operator = command[0] line = command[3] left = command[1] right = command[2] # store numer as variable to have access later self.store_address_in_register(command[1], self.a) self.store_address_in_register(command[2], self.c) if operator == '+': self.addition() elif operator == '-': self.exp_subtraction() elif operator == '*': self.multiplication() elif operator == '/': self.division(self.b) elif operator == '%': self.modulo() else: self.error.unsupported_operation(line) def check_var_declared(self, name, line): if name not in self.variables.keys(): self.error.usage_undeclared_var(line) var_obj = self.variables[name] if var_obj.declared == False: self.error.usage_uninitialized_var(line) def set_var_declared(self, name, line): if name not in self.variables.keys(): self.error.usage_undeclared_var(line) var_obj = self.variables[name] var_obj.declared = True def type_check(self, name, type, line): if name not in self.variables.keys(): self.error.usage_undeclared_var(line) obj = self.variables[name] if obj.type != type: self.error.wrong_syntax(line) def assign(self, command): self.decoder.RESET(self.b.name) self.decoder.RESET(self.c.name) self.decoder.RESET(self.d.name) self.decoder.RESET(self.f.name) self.decoder.RESET(self.e.name) self.decoder.RESET(self.a.name) line = command[3] left_id = command[1][1] left_type = command[1][0] right_id = command[2][1] right_type = command[2][0] self.type_check(left_id, left_type, line) if left_type == self.types['var']: self.set_var_declared(left_id, line) # check if it is iterator if not u can try modyfying if self.variables[left_id].iterator == False: if right_type == self.types['number']: #get value to assign value = command[2][1] # get variable addres from memory addr = self.getVarAddress(left_id) # set value in b register self.setValueInRegister(self.b, value) # set address as value of c register self.setValueInRegister(self.c, addr) # store value in memory addres of variable self.decoder.STORE(self.b.name, self.c.name) elif right_type == self.types['var']: #error self.type_check(right_id, right_type, line) self.check_var_declared(right_id, line) addr = self.getVarAddress(right_id) addl = self.getVarAddress(left_id) # set value of right side addres in b register self.setValueInRegister(self.b, addr) # set value of left side addres in d register self.setValueInRegister(self.d, addl) # load right side variable value to c register self.decoder.LOAD(self.c.name, self.b.name) # store upper right side value in adres of left side stored in d self.decoder.STORE(self.c.name, self.d.name) elif right_type == self.types['table']: #2 cases first table(number) self.type_check(right_id, right_type, line) if command[2][2][0] == self.types['number']: tab_name = command[2][1] value = command[2][2][1] if value not in self.variables.keys(): self.storeNumberInVariables(value, line) variable_addr = self.getNumberAddress(value) addl = self.getVarAddress(left_id) self.set_table_address_in_B(tab_name, value) self.decoder.LOAD(self.c.name, self.b.name) self.setValueInRegister(self.d, addl) self.decoder.STORE(self.c.name, self.d.name) # case tab(n) -> tab with int variable elif command[2][2][0] == self.types['var']: variable_name = command[2][2][1] tab_name = command[2][1] self.check_var_declared(variable_name, line) addl = self.getVarAddress(left_id) # set value of left side addres in d register self.setValueInRegister(self.c, addl) self.set_table_address_in_B(tab_name, variable_name) self.decoder.LOAD(self.d.name, self.b.name) # store upper right side value in adres of left side stored in d self.decoder.STORE(self.d.name, self.c.name) else: self.error.unknown_type(line) else: addl = self.getVarAddress(left_id) self.calculate_expression(command[2]) self.setValueInRegister(self.c, addl) self.decoder.STORE(self.b.name, self.c.name) else: self.error.iterator_modification(line) elif left_type == self.types['table']: tab_name = command[1][1] index_type = command[1][2][0] index_value = command[1][2][1] line = command[3] self.type_check(left_id, left_type, line) # check if index in variables if index_type == self.types['number']: if index_value not in self.variables.keys(): self.storeNumberInVariables(index_value, line) self.check_if_correct_tab_index(tab_name, int(index_value)) elif index_type == self.types['var']: if index_value not in self.variables.keys(): self.error.unknown_type(line) else: self.error.unknown_type(line) if right_type == self.types['number']: num_value = command[2][1] # get left side address in b register self.set_table_address_in_B(tab_name, index_value) #set value in d register self.setValueInRegister(self.d, num_value) self.decoder.STORE(self.d.name, self.b.name) elif right_type == self.types['var']: self.type_check(right_id, right_type, line) var_value = command[2][1] # check if declared and initialized self.check_var_declared(var_value, line) var_address = self.getVarAddress(var_value) # get left side address in b register self.set_table_address_in_B(tab_name, index_value) #set value in d register self.loadValueOfAddressToRegister(var_address, self.d.name) self.decoder.STORE(self.d.name, self.b.name) elif right_type == self.types['table']: self.type_check(right_id, right_type, line) line = command[3] right_tab_name = command[2][1] right_type = command[2][2][0] right_index = command[2][2][1] if right_type == self.types['var']: if right_index not in self.variables.keys(): self.error.unknown_variable(line) elif right_type == self.types['number']: if right_index not in self.variables.keys(): self.storeNumberInVariables(right_index, line) self.check_if_correct_tab_index(right_tab_name, int(right_index)) else: self.error.unknown_variable(line) # get LEFT side address in b register self.set_table_address_in_B(tab_name, index_value) self.setValueInRegister(self.f, 1) self.decoder.STORE(self.b.name, self.f.name) # get RIGHT side address in b register self.set_table_address_in_B(right_tab_name, right_index) self.decoder.LOAD(self.c.name, self.b.name) self.setValueInRegister(self.f, 1) self.decoder.LOAD(self.d.name, self.f.name) self.decoder.STORE(self.c.name, self.d.name) else: # get LEFT side address in b register self.set_table_address_in_B(tab_name, index_value) self.setValueInRegister(self.f, 1) self.decoder.STORE(self.b.name, self.f.name) expression = command[2] # store result of expression in b self.calculate_expression(expression) self.setValueInRegister(self.f, 1) #we got left addres in c self.decoder.LOAD(self.c.name, self.f.name) # store result of expression in left assign address self.decoder.STORE(self.b.name, self.c.name) else: self.error.assignment_error(line) # conditions evaluated at addresses stored in a and c def conditions(self, operand): # store in B if operand == '>=': # if sub(b + 1, d) != 0 , then b >= d self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.d.name, self.c.name) self.decoder.INC(self.b.name) self.decoder.SUB(self.b.name, self.d.name) elif operand == '>': # if sub(b, d) != 0 , then b > d self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.d.name, self.c.name) self.decoder.SUB(self.b.name, self.d.name) elif operand == '<=': # reverse greater or equal self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.d.name, self.c.name) self.decoder.INC(self.d.name) self.decoder.SUB(self.d.name, self.b.name) self.decoder.STORE(self.d.name, self.f.name) self.decoder.LOAD(self.b.name, self.f.name) elif operand == '<': # reverse reverse greater self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.d.name, self.c.name) self.decoder.SUB(self.d.name, self.b.name) self.decoder.STORE(self.d.name, self.f.name) self.decoder.LOAD(self.b.name, self.f.name) elif operand == '!=': # if sub(b,d) + sub(d,b) != 0 then b != d self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.d.name, self.c.name) self.decoder.LOAD(self.e.name, self.a.name) self.decoder.SUB(self.b.name, self.d.name) self.decoder.SUB(self.d.name, self.e.name) self.decoder.ADD(self.b.name, self.d.name) elif operand == '=': labels = self.get_label_dict(2) self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.d.name, self.c.name) self.decoder.STORE(self.d.name, self.e.name) self.decoder.LOAD(self.f.name, self.e.name) self.decoder.SUB(self.f.name, self.b.name) self.decoder.SUB(self.b.name, self.d.name) self.decoder.ADD(self.b.name, self.f.name) self.decoder.JZERO(self.b.name, labels[1]) self.decoder.RESET(self.b.name) self.decoder.JUMP(labels[2]) self.decoder.setLabel(labels[1]) self.decoder.INC(self.b.name) self.decoder.addLabel(labels[2]) def clear_assembly_code(self, asembler, debug=False): if debug == False: base = "LABEL" l = len(base) storage = {} def check_if_label(instruct): #this is label if instruct[:l] == base: return True else: return False def get_label(instruct): first_idx = instruct.index(base) return instruct[first_idx:] def label_into_lineidx(instruct, label, current): return instruct.replace(label, str(storage[label] - current)).strip() k = 0 j = 0 while len(asembler) > k: if check_if_label(asembler[k]): storage[asembler[k]] = k asembler.remove(asembler[k]) else: k += 1 m = len(asembler) n = 0 while n < m: instruct = asembler[n] if base in instruct: label = get_label(instruct) asembler[j] = label_into_lineidx(instruct, label, j) j += 1 n += 1 else: print(">DEBUG") def injectAsmLabel(self): self.counter += 1 self.decoder.createLabel(self.counter) return "LABEL" + str(self.counter) def if_statement(self, command): cond = command[1] actions = command[2] op = cond[0] labels = self.get_label_dict(1) line = cond[3] left = cond[1] right = cond[2] self.store_address_in_register(left, self.a) self.store_address_in_register(right, self.c) self.conditions(op) self.decoder.JZERO(self.b.name, labels[1]) for a in actions: self.match_action(a) self.decoder.addLabel(labels[1]) def if_else_statement(self, command): line = command[4] else_inner_operations = command[3] if_inner_operations = command[2] condition_statement = command[1] labels = self.get_label_dict(2) left = condition_statement[1] right = condition_statement[2] self.store_address_in_register(left, self.a) self.store_address_in_register(right, self.c) # result of condition stored in b self.conditions(condition_statement[0]) # if condition is false jummp to label 2 and do else command # if condition is true do id command and jump to end label self.decoder.JZERO(self.b.name, labels[2]) for a in if_inner_operations: self.match_action(a) self.decoder.JUMP(labels[1]) self.decoder.addLabel(labels[2]) for a in else_inner_operations: self.match_action(a) self.decoder.addLabel(labels[1]) # generatues special variable name that will be used for # storing upper or lower bound of iterations in LOOPS def generate_bound_var_id(self, iterator_object, type, line): if type == "upper": base = str(iterator_object.name) + "_upper" while base in self.variables.keys(): base += random.choice(string.ascii_letters) elif type == "lower": base = str(iterator_object.name) + "lower" while base in self.variables.keys(): base += random.choice(string.ascii_letters) else: self.error.error_while_creating_iterator(line) return base def for_loop_with_to(self, command): line = command[5] iterator = command[1] inner_commands = command[4] labels = self.get_label_dict(2) start_value_address = command[2] end_value_address = command[3] # check declarations, case when current bounds are undeclared # or bound is the same as current iterator self.undeclared_check(end_value_address, self.a, True) self.undeclared_check(start_value_address, self.c, True) # define iterator in varialbes as long as for executes if possible if iterator in self.variables.keys(): self.error.redeclaration(line) self.add_var_to_variables(iterator) self.set_var_declared(iterator, line) iterator_address = self.getVarAddress(iterator) obj = self.variables[iterator] obj.iterator = True #to ensure that upper bound won't change define #upper fild in iterator that have same value as upper # bound of iterations in loop upper_var_id = self.generate_bound_var_id(obj, "upper", line) #we are sure that upper_ver_id not in self.vars so self.add_var_to_variables(upper_var_id) upper_var_address = self.getVarAddress(upper_var_id) # set start value to iterator self.store_address_in_register(start_value_address, self.a) self.decoder.LOAD(self.d.name, self.a.name) self.setValueInRegister(self.c, iterator_address) self.decoder.STORE(self.d.name, self.c.name) #self.decoder.PUT(self.c.name) #set lower boud value to number of iterations self.store_address_in_register(end_value_address, self.a, True) self.store_address_in_register(start_value_address, self.c, True) # condtiion to proper start the loop start fulfilled ( condition on bounds) self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.d.name, self.c.name) self.decoder.INC(self.b.name) self.decoder.SUB(self.b.name, self.d.name) self.decoder.JZERO(self.b.name, labels[2]) self.decoder.RESET(self.d.name) self.decoder.LOAD(self.d.name, self.a.name) self.decoder.LOAD(self.a.name, self.c.name) self.decoder.SUB(self.d.name, self.a.name) self.decoder.INC(self.d.name) self.setValueInRegister(self.c, upper_var_address) self.decoder.STORE(self.d.name, self.c.name) #self.decoder.PUT(self.c.name) ######################################## START self.decoder.addLabel(labels[1]) self.loadValueOfAddressToRegister(upper_var_address, self.b.name) # IF HAVE STOP ITERATING JUMP TO END self.decoder.JZERO(self.b.name, labels[2]) self.decoder.RESET(self.b.name) # make loop's inner commands for command in inner_commands: self.match_action(command) # inc iterator self.loadValueOfAddressToRegister(iterator_address, self.f.name) self.decoder.INC(self.f.name) self.setValueInRegister(self.c, iterator_address) self.decoder.STORE(self.f.name, self.c.name) #self.decoder.PUT(self.c.name) # dec couter self.loadValueOfAddressToRegister(upper_var_address, self.f.name) self.decoder.DEC(self.f.name) self.setValueInRegister(self.c, upper_var_address) self.decoder.STORE(self.f.name, self.c.name) #self.decoder.PUT(self.c.name) # JUMP TO START self.decoder.JUMP(labels[1]) ######################################## END # END LABEL self.decoder.addLabel(labels[2]) #============== # at the end remove temporary iterator variable self.remove_variable_from_variables(iterator) self.remove_variable_from_variables(upper_var_id) def while_loop(self, command): self.decoder.RESET(self.b.name) self.decoder.RESET(self.a.name) self.decoder.RESET(self.c.name) self.decoder.RESET(self.d.name) self.decoder.RESET(self.e.name) self.decoder.RESET(self.f.name) condition_statement = command[1] line = command[3] inner_actions = command[2] labels = self.get_label_dict(2) self.decoder.addLabel(labels[1]) left = condition_statement[1] right = condition_statement[2] self.store_address_in_register(left, self.a) self.store_address_in_register(right, self.c) #if condition ok then resolve inner commands self.conditions(condition_statement[0]) self.decoder.JZERO(self.b.name, labels[2]) for actions in inner_actions: self.match_action(actions) self.decoder.JUMP(labels[1]) self.decoder.addLabel(labels[2]) def repeat_until_loop(self, command): inner_commands = command[1] condition_statement = command[2] line = command[3] labels = self.get_label_dict(2) self.decoder.addLabel(labels[1]) # do inner commands for c in inner_commands: self.match_action(c) left = condition_statement[1] right = condition_statement[2] self.store_address_in_register(left, self.a) self.store_address_in_register(right, self.c) self.conditions(condition_statement[0]) self.decoder.JZERO(self.b.name, labels[1]) self.decoder.JUMP(labels[2]) self.decoder.addLabel(labels[2]) def for_loop_with_downto(self, command): line = command[5] iterator = command[1] inner_commands = command[4] labels = self.get_label_dict(2) start_value_address = command[2] end_value_address = command[3] # check declarations ic case when bounds are uncedlared # or bound is the same as current iterator self.undeclared_check(end_value_address, self.a, True) self.undeclared_check(start_value_address, self.c, True) # define iterator in varialbes as long as for executes if possible if iterator in self.variables.keys(): self.error.redeclaration(line) self.add_var_to_variables(iterator) self.set_var_declared(iterator, line) iterator_address = self.getVarAddress(iterator) #to ensure that lower bound won't change define #lower field in iterator that have same value as lower # bound of iterations in loop obj = self.variables[iterator] lower_var_id = self.generate_bound_var_id(obj, "lower", line) #we are sure that lower_ver_id not in self.vars so self.add_var_to_variables(lower_var_id) lower_var_address = self.getVarAddress(lower_var_id) # set start value to iterator self.store_address_in_register(start_value_address, self.a) self.decoder.LOAD(self.d.name, self.a.name) self.setValueInRegister(self.c, iterator_address) self.decoder.STORE(self.d.name, self.c.name) #self.decoder.PUT(self.c.name) # condtiion to proper start the loop start fulfilled ( condition on bounds) self.store_address_in_register(start_value_address, self.a) self.store_address_in_register(end_value_address, self.c) self.decoder.LOAD(self.b.name, self.a.name) self.decoder.LOAD(self.d.name, self.c.name) self.decoder.INC(self.b.name) self.decoder.SUB(self.b.name, self.d.name) self.decoder.JZERO(self.b.name, labels[2]) #set lower boud value to number of iterations self.store_address_in_register(start_value_address, self.a) self.store_address_in_register(end_value_address, self.c) self.decoder.RESET(self.d.name) self.decoder.LOAD(self.d.name, self.a.name) self.decoder.LOAD(self.a.name, self.c.name) self.decoder.SUB(self.d.name, self.a.name) self.decoder.INC(self.d.name) self.setValueInRegister(self.c, lower_var_address) self.decoder.STORE(self.d.name, self.c.name) #self.decoder.PUT(self.c.name) ######################################## START self.decoder.addLabel(labels[1]) self.loadValueOfAddressToRegister(lower_var_address, self.b.name) # IF HAVE REACHED STOP OF ITERATING JUMP TO END self.decoder.JZERO(self.b.name, labels[2]) self.decoder.RESET(self.b.name) # make loop's inner commands for command in inner_commands: self.match_action(command) # decrement iterator self.loadValueOfAddressToRegister(iterator_address, self.f.name) self.decoder.DEC(self.f.name) self.setValueInRegister(self.c, iterator_address) self.decoder.STORE(self.f.name, self.c.name) #self.decoder.PUT(self.c.name) # decrement counter self.loadValueOfAddressToRegister(lower_var_address, self.f.name) self.decoder.DEC(self.f.name) self.setValueInRegister(self.c, lower_var_address) self.decoder.STORE(self.f.name, self.c.name) #self.decoder.PUT(self.c.name) # JUMP TO START self.decoder.JUMP(labels[1]) # END LABEL self.decoder.addLabel(labels[2]) #============== # at the end remove temporary iterator variable self.remove_variable_from_variables(iterator) self.remove_variable_from_variables(lower_var_id) def match_action(self, command): command_id = command[0] if command_id == "ASSIGN": self.assign(command) elif command_id == "WRITE": self.write(command) elif command_id == "READ": self.read(command) elif command_id == "IF_STATEMENT": self.if_statement(command) elif command_id == "IF_ELSE_STATEMENT": self.if_else_statement(command) elif command_id == "FOR_LOOP_WITH_TO": self.for_loop_with_to(command) elif command_id == "WHILE_LOOP": self.while_loop(command) elif command_id == "FOR_LOOP_WITH_DOWNTO": self.for_loop_with_downto(command) elif command_id == "REPEAT_UNTIL_LOOP": self.repeat_until_loop(command)
class BuildManager: """This is the central class for building a mypy program. It coordinates parsing, import processing, semantic analysis and type checking. It manages state objects that actually perform the build steps. """ do_type_check = None # Do we perform a type check? lib_path = None # Library path for looking up modules sem_analyzer = None # Semantic analyzer type_checker = None # Type checker errors = None # For reporting all errors # States of all individual files that are being processed. Each file in a # build is always represented by a single state object (after it has been # encountered for the first time). This is the only place where states are # stored. states = None # Map from module name to source file path. There is a 1:1 mapping between # modules and source files. module_files = None def __init__(self, lib_path, do_type_check): self.errors = Errors() self.lib_path = lib_path self.do_type_check = do_type_check self.sem_analyzer = SemanticAnalyzer(lib_path, self.errors) self.type_checker = TypeChecker(self.errors, self.sem_analyzer.modules) self.states = [] self.module_files = {} def \ process(self, initial_state): """Perform a build. The argument is a state that represents the main program file. This method should only be called once per a build manager object. The return values are identical to the return values of the build function. """ self.states.append(initial_state) # Process states in a loop until all files (states) are finished. while True: # Find the next state that has all its dependencies met. next = self.next_available_state() if not next: trace('done') break # Potentially output some debug information. trace('next {} ({})'.format(next.path, next.state())) # Set the import context for reporting error messages correctly. self.errors.set_import_context(next.import_context) # Process the state. The process method is reponsible for adding a # new state object representing the new state of the file. next.process() # Raise exception if the build failed. The build can fail for # various reasons, such as parse error, semantic analysis error, # etc. if self.errors.is_errors(): self.errors.raise_error() # If there were no errors, all files should have been fully processed. for s in self.states: assert s.state() == final_state, ( '{} still unprocessed'.format(s.path)) # Collect a list of all files. trees = [] for state in self.states: trees.append((state).tree) return (trees, self.sem_analyzer.modules, self.sem_analyzer.types, self.type_checker.type_map) def next_available_state(self): """Find a ready state (one that has all its dependencies met).""" i = len(self.states) - 1 while i >= 0: if self.states[i].is_ready(): return self.states[i] i -= 1 return None def has_module(self, name): """Have we seen a module yet?""" return name in self.module_files def file_state(self, path): """Return the state of a source file. In particular, return UNSEEN_STATE if the file has no associated state. This function does not consider any dependencies. """ for s in self.states: if s.path == path: return s.state() return UNSEEN_STATE def module_state(self, name): """Return the state of a module. In particular, return UNSEEN_STATE if the file has no associated state. This considers also module dependencies. """ if not self.has_module(name): return UNSEEN_STATE state = final_state fs = self.file_state(self.module_files[name]) if earlier_state(fs, state): state = fs return state def all_imported_modules_in_file(self, file): """Return tuple (module id, line number of import) for all modules imported in a file. """ # TODO also find imports not at the top level of the file res = [] for d in file.defs: if isinstance(d, Import): for id, _ in (d).ids: res.append((id, d.line)) elif isinstance(d, ImportFrom): imp = d res.append((imp.id, imp.line)) # Also add any imported names that are submodules. for name, __ in imp.names: sub_id = imp.id + '.' + name if self.is_module(sub_id): res.append((sub_id, imp.line)) elif isinstance(d, ImportAll): res.append(((d).id, d.line)) return res def is_module(self, id): """Is there a file in the file system corresponding to the given module?""" return find_module(id, self.lib_path) is not None
from yee_method import yee_1d from yee_method import yee_3d from sympy import * from matplotlib import pyplot as plt # An analytical solution for the one-dimensional case is obtained by initializing the class with # desired initial conditions. x, t, c = symbols("x t c") speed_of_light = 299792458 f = sin(2 * pi * x) g = -2 * pi * speed_of_light * cos(2 * pi * x) m = 200 n = 200 OneDimSolutions = Solutions(f, g, speed_of_light) OneDimErrors = Errors(OneDimSolutions) init_electric = OneDimSolutions.plot_analytical(0, 1, m, 1 / speed_of_light, get_field=True) init_magnetic = OneDimSolutions.plot_analytical(0, 1, m, 1 / speed_of_light, get_field=True) init_fields = init_electric, init_magnetic # Obtaining convergence plot in 1D space using the classes. Initial conditions can changed as pleased OneDimErrors.plot_error_space(4, 10, 1, 1 / (1 * speed_of_light))
def assertSuccess(self, input_lines): errors = Errors() render = Render(input_lines, errors, indent=False) self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0) return render
def __init__(self): self.t_simbolos = Tabla_simbolos('Global') self.t_tipos = Tabla_tipos() self.t_ambitos = Tabla_ambitos() self.errors = Errors()
class EvalVisitor(decafVisitor): # declare the tables t_simbolos = 0 t_tipos = 0 t_ambitos = 0 errors = 0 def __init__(self): self.t_simbolos = Tabla_simbolos('Global') self.t_tipos = Tabla_tipos() self.t_ambitos = Tabla_ambitos() self.errors = Errors() # -------------------------------------------- METHOD DECLARATION ---------------------------------------------- def visitVarType(self, ctx): # try and get the ID if not return only gettext options = ctx.getChildCount() # this is a struct if options == 2: return ctx.getChild(1) if options == 1: return ctx.getText() def visitVarDeclarationCUSTOM(self, ctx): # tipo = ctx.varType().getText() tipo = str(self.visitVarType(ctx.varType())) nombre = ctx.ID().getText() arreglo = False len_arr = 1 if ctx.getChildCount() == 6: arreglo = True len_arr = int(ctx.NUM().getText()) return [tipo, nombre, arreglo, len_arr] # for the structs def visitVarDeclaration(self, ctx): # tipo = str(self.visitVarType(ctx.varType())) if ctx.varType().getChildCount() == 2: nombre = ctx.ID().getText() tipo_str = str(ctx.varType().getChild(1)) # buscar el tipo del struct encontrado tipo = self.t_tipos.search_type(tipo_str) # TODO: if -1 raise NON DEFINED VAR if tipo == -1: self.errors.insert_error('NON DEFINED TYPE '+str(tipo_str) + ' ' + str(nombre), [ctx.start.line, ctx.start.column]) ambito = 0 tamanio = self.t_tipos.search_size(tipo) self.t_simbolos.create_entry(nombre, tipo, ambito, tamanio, 1) if ctx.varType().getChildCount() == 1: nombre = ctx.ID().getText() tipo_str = str(ctx.varType().getText()) # buscar el tipo del struct encontrado tipo = self.t_tipos.search_type(tipo_str) # TODO: if -1 raise NON DEFINED VAR if tipo == -1: self.errors.insert_error('NON DEFINED TYPE '+str(tipo_str) + ' ' + str(nombre), [ctx.start.line, ctx.start.column]) ambito = 0 tamanio = self.t_tipos.search_size(tipo) self.t_simbolos.create_entry(nombre, tipo, ambito, tamanio, 1) def visitBlockMETHOD(self, ctx, parent): declarations = [] for value in ctx.varDeclaration(): declarations.append(self.visitVarDeclarationCUSTOM(value)) # iterate over the declarations inside the method to insert into the symbol table for declaration in declarations: number_of = 1 if declaration[2] == False: number_of = 1 if declaration[2] == True: number_of = declaration[3] # search the table for the type tipo = self.t_tipos.search_type(declaration[0]) # TODO: if -1 raise NON DEFINED VAR if tipo == -1: self.errors.insert_error('NON DEFINED TYPE '+str(declaration[0] + ' ' + str(declaration[1])), [ctx.start.line, ctx.start.column]) # serach the ambito for the block padre = self.t_ambitos.search_ambito(parent) # TODO: if -1 raise NON DEFINED PARENT if padre == -1: self.errors.insert_error('NON FOUND IN SCOPE '+str(declaration[0])+ ' ' + str(declaration[1]), [ctx.start.line, ctx.start.column]) # search the size of the type in the table tamanio = self.t_tipos.search_size(tipo) self.t_simbolos.create_entry(declaration[1], tipo, padre, tamanio, number_of) # for value in ctx.statement(): # self.visitStatement(value) # FIXME: yep c*k for value in ctx.statement(): if value != None: self.visit(value) # FIXME: RETURNS # print(self.visit(value) ) # def visitIfstmt(self, ctx): # def visitWhilestmt(self, ctx): def visitReturnstmt(self, ctx): expressions = ctx.expression() if expressions != None: return ['RETURN', self.visit(expressions)] # def visitMethodstmt(self, ctx): # def visitBlockstmt(self, ctx): # def visitLocationstmt(self, ctx): # def visitExpressionstmt(self, ctx): def visitLiteralexpr(self, ctx): return self.visit(ctx.literal()) def visitLiteral(self, ctx): if ctx.int_Literal(): return 'int' if ctx.char_Literal(): return 'char' if ctx.bool_Literal(): return 'bool' # def visitExpression(self, ctx): # return ctx.getText() def visitParameter(self, ctx): tipo = ctx.parametertype().getText() nombre = ctx.ID().getText() arreglo = False if ctx.getChildCount() == 4: arreglo = True return(tipo, nombre, arreglo) def visitMethoDeclaration(self, ctx): tipo = ctx.methodType().getText() nombre = ctx.ID().getText() parameter_list = [] parameter_context = ctx.parameter() if parameter_context != []: for value in parameter_context: parameter_list.append(self.visitParameter(value)) # search the table for the type t_tipo = self.t_tipos.search_type(tipo) # insert into table de ambitos self.t_ambitos.create_entry(nombre, 'Program', t_tipo) block_context = ctx.block() self.visitBlockMETHOD(block_context, nombre) # -------------------------------------------- STRUCT DECLARATION ---------------------------------------------- def visitStructDeclaration(self, ctx): nombre = ctx.ID().getText() vardeclar_context = ctx.varDeclaration() if vardeclar_context != []: # get the length of variables inside of the struct for memory usage calculation n_vars = len(vardeclar_context) # insert into the tokens using the values known til now self.t_tipos.create_entry(nombre, 4*n_vars, 'definido') # insert struct into the scopes (ambitos) for the insides self.t_ambitos.create_entry(nombre, 'Program', self.t_tipos.search_type('struct')) # throw a vardeclaration for each of the elements found inside the struct declarations = [] for value in vardeclar_context: declarations.append(self.visitVarDeclarationCUSTOM(value)) # iterate over the declarations inside the method to insert into the symbol table for declaration in declarations: number_of = 1 if declaration[2] == False: number_of = 1 if declaration[2] == True: number_of = declaration[3] # search the table for the type tipo = self.t_tipos.search_type(declaration[0]) # TODO: if -1 raise NON DEFINED VAR if tipo == -1: self.errors.insert_error('NON DEFINED TYPE '+str(declaration[0] + ' ' + str(declaration[1])), [ctx.start.line, ctx.start.column]) # serach the ambito for the block padre = self.t_ambitos.search_ambito(nombre) # TODO: if -1 raise NON DEFINED PARENT if padre == -1: self.errors.insert_error('NON FOUND IN SCOPE '+str(declaration[0])+ ' ' + str(declaration[1]), [ctx.start.line, ctx.start.column]) # search the size of the type in the table tamanio = self.t_tipos.search_size(tipo) self.t_simbolos.create_entry(declaration[1], tipo, padre, tamanio, number_of) else: # line = ctx.start.line # column = ctx.start.column # TODO raise an exeption EMPTY STRUCT self.errors.insert_error('EMPTY STRUCT '+str(nombre), [ctx.start.line, ctx.start.column])
def test_add_error(): e = Errors() e.add_error('_1') with e.with_path('a'): e.add_error('a_1') e.add_error('a_2') with e.with_path('aa'): e.add_error('aa_1') with e.with_path('b'): e.add_error('b_1') with e.with_path(0): e.add_error('b0_1') assert e assert e.has_errors assert e.as_dict == { '': ['_1'], 'a': { '': ['a_1', 'a_2'], 'aa': { '': ['aa_1'], } }, 'b': { '': ['b_1'], 0: { '': ['b0_1'], } } }
def __init__(self): self.errors = Errors()
def assertSuccess(self, input): errors = Errors() red = Reduce(input, errors) self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0)
def assertSuccess(self, input, expected_errors=0, expected_warnings=0): errors = Errors() vc = VarCheck(input, errors) self.assertEquals(errors.num_errors, expected_errors) self.assertEquals(errors.num_warnings, expected_warnings)
import time from uart import BaseStation from mail_sender import notify_all from errors import Errors from config import smtp_server, smtp_port, sender_email, password, error_send_interval, serial_port error1 = Errors(timeout_sec=error_send_interval) error2 = Errors(timeout_sec=error_send_interval) error3 = Errors(timeout_sec=error_send_interval) error4 = Errors(timeout_sec=error_send_interval) pc_str = '[PC program]' base_station = BaseStation(port=serial_port) base_station.start() while not base_station.is_connected: # Wait connection if base_station.is_stopped: print(pc_str, 'Stop') exit(1) print(pc_str, 'Not connected') time.sleep(1) while True: if base_station.is_stopped: break try: time.sleep(1) if base_station.errors:
"GWin32OSType":("integer(c_int)", "c_int") } # Two words types: TYPES2_DICT = { "long double": ("real(c_long_double)", "c_long_double"), "unsigned long":("integer(c_long)", "c_long"), "unsigned short":("integer(c_short)", "c_short"), "unsigned int":("integer(c_int)", "c_int") } # An instance of the Statistics class: my_stats = Statistics() # An instance of the Errors class: my_errors = Errors() #************************************************************************* # Pass 1: scan all header files to find all enum types, all pointers to # functions (funptr) and add derived GTK types #************************************************************************* print("\033[1m Pass 1: looking for enumerators, funptr and derived types...\033[0m") gtk_types = [] # These lists will be used by the iso_c_binding() function: gtk_enums = [] gtk_funptr = [] T0 = time.time() # To calculate computing time for library_path in PATH_DICT:
class Run (Process): def __init__(self, threadID, dataSet, minStartWeight, maxStartWeight): Process.__init__(self) self.threadID = threadID self.weights = Weights(dataSet.getDataPoint(0).dimension()) self.weights.generateRandom(minStartWeight, maxStartWeight) weight = """*****\nThread {0} initial weights:\n{1}\n*****""" print(weight.format(self.threadID, self.weights.vector)) self.trainingErrors = Errors(dataSet.trainingDataPoints, dataSet.trainingActualValues) self.testingErrors = Errors(dataSet.testingDataPoints, dataSet.testingActualValues) self.hypothesis = Hypothesis() self.trainingErrors.updateToLower(self.weights, self.hypothesis) self.iterations = 0 self.alpha = STARTING_ALPHA # __init__ def run(self): while(self.trainingErrors.greaterThan(ALLOWABLE_MSE_DELTA) and self.iterations < MAX_ITERATIONS and self.alpha > 0): self.iterations += 1 self.weights.update(self.alpha, self.trainingErrors) if (not self.trainingErrors.updateToLower(self.weights, self.hypothesis)): self.alpha /= ALPHA_BASE self.weights.revert() # if if (self.iterations % 10 == 0): self.alpha *= 1.1 # if # while self.print() # run def print(self): output = """ ========================================================================================================================================== RUN RESULTS for thread {0}: ALPHA and ITERATIONS: {1} {2} ---------------------------------------------------------------------------------- WEIGHT VECTOR: {3} ---------------------------------------------------------------------------------- Lowest training data MSE: {4} ---------------------------------------------------------------------------------- Test data MSE: {5} ========================================================================================================================================== """ self.testingErrors.updateToLower(self.weights, self.hypothesis) print(output.format(self.threadID, self.alpha, self.iterations, self.weights.vector, self.trainingErrors.mse, self.testingErrors.mse))
# Wojciech Wróblewski 250349 kompilator import sys from parser import OneBeerLexer, OneBeerParser from methods import Methods from errors import Errors error = Errors() argument = sys.argv length_arguments = len(argument) if length_arguments < 3: error.run_error() file_in = sys.argv[1] file_out = sys.argv[2] lexer = OneBeerLexer() parser = OneBeerParser() with open(file_in) as file: text = file.read() parse_tree = parser.parse(lexer.tokenize(text)) machine = Methods(parse_tree, file_out) machine.get_assembler()
def assertSuccess(self, input): errors = Errors() linearise = Linearise(input, errors) self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0) return linearise