How to use _get_primary method in lisa

Best Python code snippet using lisa_python

virtualMachineLibrary.py

Source:virtualMachineLibrary.py Github

copy

Full Screen

2class VirtualMachineLibrary:3 """4 Main class to map the Virtual Machine intermediate language to Hack machine language5 """6 def _get_primary(operation, a=None, b=None, treat_a_as_pointer=True, treat_b_as_pointer=True):7 """8 Define primary operations, which are going to be main building "blocks" of higher instructions9 """10 bytecode_dictionary = {11 "sp++": ["@SP", "M=M+1"],12 "sp--": ["@SP", "M=M-1"],13 }14 if operation == "*a=*b":15 load_b_into_d = [f"@{b}", "D=M"]16 if treat_b_as_pointer:17 load_b_into_d.insert(1, "A=M")18 load_d_into_a = [f"@{a}", "M=D"]19 if treat_a_as_pointer:20 load_d_into_a.insert(1, "A=M")21 load_b_into_a = load_b_into_d + load_d_into_a22 return load_b_into_a23 else:24 return bytecode_dictionary[operation]25 def get_arithmetic(instruction, function_block, file_name, total_instructions):26 """27 Returns bytecode for arithmetic instructions28 add | x + y29 sub | x - y30 neg | - y31 eq | x == y32 gt | x > y33 lt | y < x34 and | x && y35 or | x || y36 not | !y37 """38 direct_arithmetic_commands = {"add": "+", "sub": "-", "and": "&", "or": "|"}39 conditional_arithmetic_commands = {"eq": "JEQ", "gt": "JGT", "lt": "JLT"} # I can just preppend "J" to the type and .upper(), because they match, but the symmetry would be ruined40 unary_commands = ["neg", "not"]41 final_bytecode = []42 if instruction in direct_arithmetic_commands:43 final_bytecode.extend(VirtualMachineLibrary._get_primary("sp--")) # sp--44 final_bytecode.extend(["@SP", "A=M", "D=M"]) # D=*sp45 final_bytecode.extend(VirtualMachineLibrary._get_primary("sp--")) # sp--46 final_bytecode.extend(47 ["@SP", "A=M", f"D=M{direct_arithmetic_commands[instruction]}D"]) # D=*sp (operand) D48 final_bytecode.extend(["@SP", "A=M", "M=D"]) # *sp = D49 final_bytecode.extend(VirtualMachineLibrary._get_primary("sp++")) # sp++50 elif instruction in conditional_arithmetic_commands:51 final_bytecode.extend(VirtualMachineLibrary._get_primary("sp--")) # sp--52 final_bytecode.extend(["@SP", "A=M", "D=M"]) # D=*sp53 final_bytecode.extend(VirtualMachineLibrary._get_primary("sp--")) # sp--54 final_bytecode.extend(["@SP", "A=M", "D=M-D"]) # D=*sp - D55 write_none_label = ":".join([file_name, function_block, str(total_instructions + 17), "WRITENONE"])56 increment_sp_label = ":".join([file_name, function_block, str(total_instructions + 20), "INCREMENTSP"])57 final_bytecode.extend([f"@{write_none_label}",58 f"D;{conditional_arithmetic_commands[instruction]}"]) # @WRITENONE, jump if the corresponding condition matches with D"s (x-y) value59 final_bytecode.extend(["@SP", "A=M", "M=0"]) # (WRITEZERO) block, *sp=0 (false)60 final_bytecode.extend(61 [f"@{increment_sp_label}", "0;JMP"]) # Jump instantly to sp++ part, skipping write -162 final_bytecode.extend([f"({write_none_label})", "@SP", "A=M", "M=-1"]) # (WRITENONE) block, *sp=-1 (true)63 64 final_bytecode.extend([f"({increment_sp_label})"])65 final_bytecode.extend(VirtualMachineLibrary._get_primary("sp++")) # sp++66 else: # unary command67 final_bytecode.extend(VirtualMachineLibrary._get_primary("sp--")) # sp--68 final_bytecode.extend(["@SP", "A=M", "D=M"]) # D=*sp69 if instruction == "not":70 final_bytecode.extend(["@SP", "A=M", "M=!D"]) # *sp = !D71 else:72 final_bytecode.extend(["@SP", "A=M", "M=-D"])73 final_bytecode.extend(VirtualMachineLibrary._get_primary("sp++")) # sp++74 return final_bytecode75 def get_memory(instruction, file_name):76 """77 Returns the full memory access bytecode, which consists of:78 1. Loading address calculation in R1379 2. Decrementing SP, if pop else saving R13"s content into current SP available location80 3. Saving SP value in R13, if pop else incrementing SP81 """82 instruction_structure = instruction.split()83 instruction_type = instruction_structure[0]84 segment = instruction_structure[1]85 86 index = instruction_structure[2]87 88 calculated_address_bytecode = VirtualMachineLibrary._get_address_calculation(segment, index, file_name)89 if instruction_type == "push":90 treat_b_as_pointer = segment != "constant" # If we don"t have a constant segment, then b (R13 in this case) must be treated as a pointer91 save_R13_into_stack_bytecode = VirtualMachineLibrary._get_primary("*a=*b", a="SP", b="R13",92 treat_b_as_pointer=treat_b_as_pointer)93 increment_sp = VirtualMachineLibrary._get_primary("sp++")94 return calculated_address_bytecode + save_R13_into_stack_bytecode + increment_sp95 else:96 decrement_sp = VirtualMachineLibrary._get_primary("sp--")97 save_stack_into_R13 = VirtualMachineLibrary._get_primary("*a=*b", a="R13", b="SP")98 return calculated_address_bytecode + decrement_sp + save_stack_into_R1399 def _get_address_calculation(segment, index, file_name):100 """101 Returns bytecode that loads address calculation (segment base address + index) in R13102 """103 if segment == "constant": # Temp starts at 5104 load_bytecode = [f"@{index}", "D=A"]105 elif segment == "temp":106 load_bytecode = [f"@{int(index) + 5}", "D=A"]107 elif segment == "static":108 variable_name = file_name + "." + index109 load_bytecode = [f"@{variable_name}", "D=A"]110 elif segment == "pointer":111 if index == "0":112 register = "THIS"113 else:114 register = "THAT"115 load_bytecode = [f"@{register}", "D=A"]116 else:117 load_bytecode = [f"@{VirtualMachineLibrary._get_symbolic_symbol(segment)}", "D=M", f"@{index}", "D=D+A"]118 full_address_bytecode = load_bytecode + ["@R13", "M=D"]119 return full_address_bytecode120 def _get_symbolic_symbol(segment):121 """122 Returns Hack symbolic symbol equivalents123 """124 bytecode_dictionary = {125 "local": "LCL",126 "argument": "ARG",127 "this": "THIS",128 "that": "THAT",129 }130 try:131 return bytecode_dictionary[segment]132 except: # If the segment is not available, it is most likely a variable, so just return it133 return segment134 def get_program_flow(instruction, label, function_block):135 """136 Returns full program flow instruction bytecode137 1. goto label138 2. label139 3. if-goto label140 """141 if instruction == "label": # Set a label142 bytecode = [f"({function_block}{'$' if function_block else ''}{label})"]143 elif instruction == "goto": # Unconditional jumping144 bytecode = [f"@{label}", "0;JMP"]145 else: # Conditional jumping146 bytecode = []147 # Pop into D148 bytecode.extend(VirtualMachineLibrary._get_primary("sp--"))149 bytecode.extend(["@SP", "A=M", "D=M"])150 # Jump if D is not 0151 bytecode.extend([f"@{function_block}{('$' if function_block else '')}{label}", "D;JNE"])152 return bytecode153 def get_function(instruction_structure, total_instructions, file_name):154 """155 Returns full function instruction bytecode156 function function_name lVars157 call function_name nArgs158 return159 """160 state = ["LCL", "ARG", "THIS", "THAT"]161 instruction = instruction_structure[0]162 if instruction == "function":163 function_name = instruction_structure[1]164 vars_count = int(instruction_structure[2])165 166 bytecode = []167 168 # Start a function block169 bytecode.extend([f"({function_name})"])170 for _ in range(vars_count):171 bytecode.extend(VirtualMachineLibrary.get_memory("push constant 0", file_name)) 172 elif instruction == "call": 173 function_name = instruction_structure[1]174 args_count = instruction_structure[2]175 176 bytecode = []177 178 return_label = ":".join([file_name, function_name, str(total_instructions), "RETURN"])179 # Push return address180 bytecode.extend([f"@{return_label}"])181 bytecode.extend(["D=A", "@SP", "A=M", "M=D"])182 bytecode.extend(VirtualMachineLibrary._get_primary("sp++"))183 # Save state184 for address in state:185 bytecode.extend([f"@{address}", "D=M", "@R13", "M=D"])186 bytecode.extend(VirtualMachineLibrary._get_primary("*a=*b", a="SP", b="R13", treat_b_as_pointer=False))187 bytecode.extend(VirtualMachineLibrary._get_primary("sp++"))188 # Set ARG to point to new base address (sp - 5 - args_count)189 bytecode.extend(["@SP", "D=M", "@5", "D=D-A", f"@{args_count}", "D=D-A", "@ARG", "M=D"])190 191 # Set LCL to point to current SP192 bytecode.extend(["@SP", "D=M", "@LCL", "M=D"])193 194 # Jump to function_name195 bytecode.extend([f"@{function_name}", "0;JMP"])196 197 # Set return label198 bytecode.extend([f"({return_label})"])199 bytecode = bytecode200 else:201 bytecode = []202 # Set R13 to point to callee"s LCL203 bytecode.extend(["@LCL", "D=M", "@R13", "M=D"])204 # Set R14 to return address205 bytecode.extend(["@R13", "D=M", "@5", "D=D-A", "A=D", "D=M", "@R14", "M=D"])206 # Set first callee"s argument to be return value207 bytecode.extend(VirtualMachineLibrary._get_primary("sp--"))208 bytecode.extend(VirtualMachineLibrary._get_primary("*a=*b", a="ARG", b="SP"))209 # Reposition SP to be after first callee"s argument210 bytecode.extend(["@ARG", "D=M+1", "@SP", "M=D"])211 212 # Restore registers213 for index, address in enumerate(reversed(state)):214 bytecode.extend(["@R13", "D=M", f"@{int(index) + 1}", "D=D-A", "A=D", "D=M", f"@{address}", "M=D"])215 216 # Return jump217 bytecode.extend(["@R14", "A=M", "0;JMP"])218 ...

Full Screen

Full Screen

screen.py

Source:screen.py Github

copy

Full Screen

...102 'd': self.double_page,103 'f': self.justify_full,104 'e': self.hyphenation105 }106 def _get_primary(self):107 if self.dark_mode:108 return curses.color_pair(6)109 else:110 return curses.color_pair(1)111 def _get_modes_info(self):112 modes_info = '['113 for mode in self.modes.keys():114 if self.modes[mode]:115 modes_info += mode116 else:117 modes_info += '-'118 modes_info += ']'119 return modes_info120 def get_screen(self):121 return self.screen122 def _shorten(self, text, bracer='', text_after=''):123 text_end = '...' + bracer124 if len(text) >= self.max_x - self.padding * 2 - len(text_after):125 return text[:self.max_x - self.padding * 2 - len(text_end) - len(text_after)] + text_end126 else:127 return text128 def _print_info(self):129 app_text = Constant.APP + ' ' + Constant.VERSION130 title_text = '[' + self.title + ']'131 if not self.is_dict_installed:132 language_text = '[*' + self.language + ']'133 else:134 language_text = '[' + self.language + ']'135 keys = 'modes:' + self._get_modes_info() + ' quit:[q] help:[?]'136 if self.max_x <= len(keys) + len(app_text) + 4:137 app_text = Constant.SHORT_APP + ' ' + Constant.VERSION138 if self.max_x <= len(keys) + len(app_text) + 4:139 keys = self._get_modes_info() + '[?]'140 if self.max_x > len(keys) + len(app_text) + 4:141 self.screen.addstr(0, 2, self._shorten(app_text), self._get_primary())142 self.screen.addstr(143 0,144 self.max_x - len(keys) - self.padding,145 keys,146 self._get_primary()147 )148 else:149 self.screen.addstr(150 0,151 self.max_x - len(keys) - self.padding,152 keys,153 self._get_primary()154 )155 self.screen.addstr(156 self.max_y - 1,157 self.padding,158 self._shorten(title_text, ']', language_text),159 self._get_primary()160 )161 self.screen.addstr(162 self.max_y - 1,163 self.max_x - len(language_text) - self.padding,164 language_text,165 self._get_primary()166 )167 def redraw(self):168 self.screen.erase()169 self.screen.bkgd(' ', self._get_primary())170 try:171 self._print_info()172 except:173 pass...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run lisa automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful