def execute_Rajale(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, "Motri me tenes que dar una lista pa rajar un elemento", exec_ctx )) if not isinstance(index, Number): return RTResult().failure(RTError( self.pos_start, self.pos_end, "CAPO dame el index en el segundo argumento", exec_ctx )) try: element = list_.elements.pop(index.value) except: return RTResult().failure(RTError( self.pos_start, self.pos_end, detailsMessages["outOfIndex"], exec_ctx )) return RTResult().success(element)
def execute_Correme(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, "Dame un string papá", exec_ctx)) if fn.value[-4:] != ".che": return RTResult().failure( RTError(self.pos_start, self.pos_end, "CAPO tiene que ser un archivo .che , que te pensas?", exec_ctx)) fn = os.path.abspath(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"Que pasó perro? No arrancó? Algo va mal en \"{fn}\"\n" + str(e), exec_ctx)) from CheLang.cheLangCompiler import run _, error = run(fn, script) if error: return RTResult().failure( RTError( self.pos_start, self.pos_end, f"Che \"{fn}\" no funcó, fijate que onda.\n" + error.as_string(), exec_ctx)) return RTResult().success(Empty())
def execute_Sumate(self, exec_ctx): arg = exec_ctx.symbol_table.get("list").copy() if isinstance(arg, List): try: for i, n in enumerate(arg.elements): arg.elements[i] = n.value return RTResult().success(Number(sum(arg.elements))) except: return RTResult().failure( RTError(self.pos_start, self.pos_end, "Capo pasame una lista CON SOLO NUMEROS, dale?", exec_ctx)) else: return RTResult().failure( RTError(self.pos_start, self.pos_end, "Capo pasame una lista, dale?", exec_ctx))
def illegal_operation(self, other=None): if not other: other = self return RTError( self.pos_start, other.pos_end, 'Wait... that´s illegal', self.context )
def execute_Viborita(self, exec_ctx): ev = exec_ctx.symbol_table.get("value") if isinstance(ev, String): return RTResult().success(String(str(eval(ev.value)))) else: return RTResult().failure( RTError(self.pos_start, self.pos_end, "Capo dame un string pa evaluar, dale?", exec_ctx))
def execute_Metele(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, "Tiene que ser una lista el primer argumento crack", exec_ctx)) if not isinstance(listB, List): return RTResult().failure( RTError(self.pos_start, self.pos_end, "También tiene que ser una lista el segundo crack", exec_ctx)) listA.elements.extend(listB.elements) return RTResult().success(listA)
def execute_AFlotantito(self, exec_ctx): n = exec_ctx.symbol_table.get("value") if isinstance(n, String): try: ret = float(n.value) return RTResult().success(Number(ret)) except: return RTResult().failure( RTError( self.pos_start, self.pos_end, "Flaco no se que me pasaste pero no se puede hacer numerito", exec_ctx)) elif isinstance(n, Number): return RTResult().success(Number(float(n.value))) else: return RTResult().failure( RTError(self.pos_start, self.pos_end, "Capo pasame un numerito o un string, dale?", exec_ctx))
def dived_by(self, other): if isinstance(other, Number): if other.value == 0: return None, RTError(other.pos_start, other.pos_end, detailsMessages["zeroDiv"], self.context) return Number(self.value / other.value).set_context( self.context), None else: return None, Value.illegal_operation(self, other)
def take_item(self, other): res = RTResult() if isinstance(other, Number): try: return res.success(self.elements[other.value]) except: return res.failure( RTError(other.pos_start, other.pos_end, detailsMessages["outOfIndex"], self.context)) else: return res.failure(Value.illegal_operation(self, other))
def execute_Boludear(self, exec_ctx): from time import sleep n = exec_ctx.symbol_table.get("value") if isinstance(n, Number): sleep(n.value) else: return RTResult().failure( RTError( self.pos_start, self.pos_end, "Flaco dame una cantidad de segundos, no cualquier cosa", exec_ctx)) return RTResult().success(Empty())
def execute_TaLargo(self, exec_ctx): value = exec_ctx.symbol_table.get("value") if isinstance(value, List): return RTResult().success(Number(len(value.elements))) if isinstance(value, String): return RTResult().success(Number(len(value.value))) return RTResult().failure( RTError(self.pos_start, self.pos_end, "Che el TaLargo tiene que tener una lista o un string", exec_ctx))
def subbed_by(self, other): if isinstance(other, Number): new_list = self.copy() try: new_list.elements.pop(other.value) return new_list, None except: return None, RTError(other.pos_start, other.pos_end, detailsMessages["outOfIndex"], self.context) else: return None, Value.illegal_operation(self, other)
def execute_Agregale(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, "Motri me tenes que dar una lista pa poner un elemento", exec_ctx)) list_.elements.append(value) return RTResult().success(list_)
def check_args(self, arg_names, args): res = RTResult() final_arg_names = [] for arg_name in arg_names: if not arg_name[-1] == "?": final_arg_names.append(arg_name) if len(args) > len(arg_names): return res.failure( RTError( self.pos_start, self.pos_end, f"Che me pasaste como {len(args) - len(arg_names)} argumentos, son menos en {self} viste", self.context)) if len(args) < len(final_arg_names): return res.failure( RTError( self.pos_start, self.pos_end, f"Che me pasaste como {len(final_arg_names) - len(args)} argumentos, son más en {self} viste", self.context)) return res.success(None)
def visit_VarAccessNode(self,node,context): res = RTResult() var_name = node.var_name_tok.value value = context.symbol_table.get(var_name) if not value: return res.failure(RTError( node.pos_start, node.pos_end, detailsMessages["unknownVariable"] + var_name + "'", context )) value = value.copy().set_context(context).set_pos(node.pos_start, node.pos_end) return res.success(value)