def execute_pop(self, exec_ctx): list_ = exec_ctx.symbol_table.get("list") index = exec_ctx.symbol_table.get("index") if not isinstance(list_, List): return RTResult().failure( RTError( self.pos_start, self.pos_end, "First argument must be list", exec_ctx, )) if not isinstance(index, Number): return RTResult().failure( RTError( self.pos_start, self.pos_end, "Second argument must be number", exec_ctx, )) try: element = list_.elements.pop(index.value) except: return RTResult().failure( RTError( self.pos_start, self.pos_end, "Element at this index could not be removed from list because index is out of bounds", exec_ctx, )) return RTResult().success(element)
def execute_len(self, exec_ctx): list_ = exec_ctx.symbol_table.get("list") if not isinstance(list_, List): return RTResult().failure( RTError(self.pos_start, self.pos_end, "Argument must be list", exec_ctx)) return RTResult().success(Number(len(list_.elements)))
def execute_sum(self, exec_ctx): data = exec_ctx.symbol_table.get("value") if not isinstance(data, List): return RTResult().failure( RTError(self.pos_start, self.pos_end, f"Expected a list" + "", exec_ctx)) try: return RTResult().success(sum(data.elements)) except: return RTResult().failure( RTError(self.pos_start, self.pos_end, f"Sum function failed" + "", exec_ctx))
def execute_append(self, exec_ctx): list_ = exec_ctx.symbol_table.get("list") value = exec_ctx.symbol_table.get("value") if not isinstance(list_, List): return RTResult().failure( RTError( self.pos_start, self.pos_end, "First argument must be list", exec_ctx, )) list_.elements.append(value) return RTResult().success(Number.null)
def execute_input_int(self, exec_ctx): while True: text = input() try: number = int(text) break except ValueError: print(f"'{text}' must be an integer. Try again!") return RTResult().success(Number(number))
def execute_input(self, exec_ctx): while True: try: text = input() break except KeyboardInterrupt: print("Your keyboard interrupted") return RTResult().success(String(text))
def execute_run(self, exec_ctx): fn = exec_ctx.symbol_table.get("fn") if not isinstance(fn, String): return RTResult().failure( RTError( self.pos_start, self.pos_end, "Second argument must be string", exec_ctx, )) fn = fn.value try: with open(fn, "r") as f: script = f.read() except Exception as e: return RTResult().failure( RTError( self.pos_start, self.pos_end, f'Failed to load script "{fn}"\n' + str(e), exec_ctx, )) _, error = run(fn, script) if error: return RTResult().failure( RTError( self.pos_start, self.pos_end, f'Failed to finish executing script "{fn}"\n' + error.as_string(), exec_ctx, )) return RTResult().success(Number.null)
def execute(self, args): res = RTResult() exec_ctx = self.generate_new_context() method_name = f"execute_{self.name}" method = getattr(self, method_name, self.no_visit_method) res.register( self.check_and_populate_args(method.arg_names, args, exec_ctx)) if res.should_return(): return res return_value = res.register(method(exec_ctx)) if res.should_return(): return res return res.success(return_value)
def execute_extend(self, exec_ctx): listA = exec_ctx.symbol_table.get("listA") listB = exec_ctx.symbol_table.get("listB") if not isinstance(listA, List): return RTResult().failure( RTError( self.pos_start, self.pos_end, "First argument must be list", exec_ctx, )) if not isinstance(listB, List): return RTResult().failure( RTError( self.pos_start, self.pos_end, "Second argument must be list", exec_ctx, )) listA.elements.extend(listB.elements) return RTResult().success(Number.null)
def execute_type(self, data_values): data = data_values.symbol_table.get("value") type_value = None if isinstance(data, String): type_value = "string" if len(str(data)) == 1: type_value = "char" elif isinstance(data, List): type_value = "list" elif isinstance(data, Number): type_value = "int" if str(data).count(".") == 1: type_value = "float" type_value = "unknown" if type_value == None else type_value print(type_value) return RTResult().success(Number.null)
def execute_is_string(self, exec_ctx): is_number = isinstance(exec_ctx.symbol_table.get("value"), String) return RTResult().success(Number.true if is_number else Number.false)
def execute_clear(self, exec_ctx): os.system("cls" if os.name == "nt" else "clear") return RTResult().success(Number.null)
def execute_print_ret(self, exec_ctx): return RTResult().success( String(str(exec_ctx.symbol_table.get("value"))))
def execute_print_end(self, exec_ctx): print(str(exec_ctx.symbol_table.get("value")), end="") return RTResult().success(Number.null)
def execute_is_function(self, exec_ctx): is_number = isinstance(exec_ctx.symbol_table.get("value"), BaseFunction) return RTResult().success(Number.true if is_number else Number.false)