def check_args(self, arg_names, args): res = RTResult() if len(args) > len(arg_names): return res.failure( RTError( self.pos_start, self.pos_end, f"{len(args) - len(arg_names)} too many args passed into {self}", self.context)) if len(args) < len(arg_names): return res.failure( RTError( self.pos_start, self.pos_end, f"{len(arg_names) - len(args)} too few args passed into {self}", 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, f"'{var_name}' is not defined", context )) value = value.copy().set_pos(node.pos_start, node.pos_end).set_context(context) return res.success(value)
def visit_UnaryOpNode(self, node, context): res = RTResult() number = res.register(self.visit(node.node, context)) if res.should_return(): return res error = None if node.op_tok.type == TT_MINUS: number, error = number.multed_by(Number(-1)) elif node.op_tok.matches(TT_KEYWORD, 'not'): number, error = number.notted() if error: return res.failure(error) else: return res.success(number.set_pos(node.pos_start, node.pos_end))
def visit_BinOpNode(self, node, context): res = RTResult() left = res.register(self.visit(node.left_node, context)) if res.should_return(): return res right = res.register(self.visit(node.right_node, context)) if res.should_return(): return res if node.op_tok.type == TT_PLUS: result, error = left.added_to(right) elif node.op_tok.type == TT_MINUS: result, error = left.subbed_by(right) elif node.op_tok.type == TT_MUL: result, error = left.multed_by(right) elif node.op_tok.type == TT_DIV: result, error = left.dived_by(right) elif node.op_tok.type == TT_POW: result, error = left.powed_by(right) elif node.op_tok.type == TT_EE: result, error = left.get_comparison_eq(right) elif node.op_tok.type == TT_NE: result, error = left.get_comparison_ne(right) elif node.op_tok.type == TT_LT: result, error = left.get_comparison_lt(right) elif node.op_tok.type == TT_GT: result, error = left.get_comparison_gt(right) elif node.op_tok.type == TT_LTE: result, error = left.get_comparison_lte(right) elif node.op_tok.type == TT_GTE: result, error = left.get_comparison_gte(right) elif node.op_tok.matches(TT_KEYWORD, 'and'): result, error = left.anded_by(right) elif node.op_tok.matches(TT_KEYWORD, 'or'): result, error = left.ored_by(right) if error: return res.failure(error) else: return res.success(result.set_pos(node.pos_start, node.pos_end))