def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder, eval_operands=True): new_frame = Frame(self.name, self.frame if self.lexically_scoped else frame) if eval_operands and self.evaluates_operands: operands = evaluate_all(operands, frame, gui_holder.expression.children[1:]) if self.var_param: verify_min_callable_length(self, len(self.params), len(operands)) else: verify_exact_callable_length(self, len(self.params), len(operands)) if len(self.body) > 1: body = [Pair(Symbol("begin"), make_list(self.body))] else: body = self.body for param, value in zip(self.params, operands): new_frame.assign(param, value) if self.var_param: new_frame.assign(self.var_param, make_list(operands[len(self.params):])) out = None gui_holder.expression.set_entries([ VisualExpression(expr, gui_holder.expression.display_value) for expr in body ]) gui_holder.apply() for i, expression in enumerate(body): out = evaluate(expression, new_frame, gui_holder.expression.children[i], self.evaluates_operands and i == len(body) - 1, log_stack=len(self.body) == 1) new_frame.assign(return_symbol, out) if not self.evaluates_operands: gui_holder.expression.set_entries( [VisualExpression(out, gui_holder.expression.display_value)]) out = evaluate(out, frame, gui_holder.expression.children[i], True) return out
def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder, eval_operands=True): verify_exact_callable_length(self, 1, len(operands)) operand = operands[0] if eval_operands: operand = evaluate_all(operands, frame, gui_holder.expression.children[1:])[0] if not isinstance(operand, Promise): raise OperandDeduceError( f"Force expected a Promise, received {operand}") if operand.forced: return operand.expr if logger.fragile: raise IrreversibleOperationError() gui_holder.expression.set_entries([ VisualExpression(operand.expr, gui_holder.expression.display_value) ]) gui_holder.apply() evaluated = evaluate(operand.expr, operand.frame, gui_holder.expression.children[0]) if not logger.dotted and not isinstance(evaluated, (Pair, NilType)): raise TypeMismatchError( f"Unable to force a Promise evaluating to {operand.expr}, expected another Pair or Nil" ) operand.expr = evaluated operand.force() return operand.expr
def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder, eval_operands=True): verify_exact_callable_length(self, 1, len(operands)) if eval_operands: operands = evaluate_all(operands, frame, gui_holder.expression.children[1:]) if not isinstance(operands[0], Symbol): raise OperandDeduceError( f"Load expected a Symbol, received {operands[0]}.") if logger.fragile: raise IrreversibleOperationError() try: with open(f"{operands[0].value}.scm") as file: code = "(begin-noexcept" + "\n".join(file.readlines()) + "\n)" buffer = TokenBuffer([code]) expr = get_expression(buffer) gui_holder.expression.set_entries([ VisualExpression(expr, gui_holder.expression.display_value) ]) gui_holder.apply() return evaluate(expr, frame, gui_holder.expression.children[0], True) except OSError as e: raise LoadError(e)
def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder, eval_operands=True): verify_exact_callable_length(self, 1, len(operands)) if eval_operands: operands = evaluate_all(operands, frame, gui_holder.expression.children[1:]) if (not isinstance(operands[0], Symbol)): raise OperandDeduceError(''.join([ 'Load expected a Symbol, received ', '{}'.format(operands[0]), '.' ])) if logger.fragile: raise IrreversibleOperationError() try: with open(''.join(['{}'.format(operands[0].value), '.scm'])) as file: code = (('(begin' + '\n'.join(file.readlines())) + '\n)') buffer = TokenBuffer([code]) expr = get_expression(buffer) gui_holder.expression.set_entries([ VisualExpression(expr, gui_holder.expression.display_value) ]) gui_holder.apply() return evaluate(expr, frame, gui_holder.expression.children[0], True) except OSError as e: raise LoadError(e)
def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder): verify_min_callable_length(self, 1, len(operands)) for (cond_i, cond) in enumerate(operands): if (not isinstance(cond, Pair)): raise OperandDeduceError(''.join([ 'Unable to evaluate clause of cond, as ', '{}'.format(cond), ' is not a Pair.' ])) expanded = pair_to_list(cond) cond_holder = gui_holder.expression.children[(cond_i + 1)] cond_holder.link_visual(VisualExpression(cond)) eval_condition = SingletonTrue if ((not isinstance(expanded[0], Symbol)) or (expanded[0].value != 'else')): eval_condition = evaluate(expanded[0], frame, cond_holder.expression.children[0]) if ((isinstance(expanded[0], Symbol) and (expanded[0].value == 'else')) or (eval_condition is not SingletonFalse)): out = eval_condition for (i, expr) in enumerate(expanded[1:]): out = evaluate(expr, frame, cond_holder.expression.children[(i + 1)], (i == (len(expanded) - 2))) return out return Undefined
def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder, eval_operands=True): verify_exact_callable_length(self, 1, len(operands)) if eval_operands: operand = evaluate(operands[0], frame, gui_holder.expression.children[1]) else: operand = operands[0] gui_holder.expression.set_entries([VisualExpression(operand, gui_holder.expression.display_value)]) gui_holder.apply() return evaluate(operand, frame, gui_holder.expression.children[0], True)
def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder): verify_min_callable_length(self, 2, len(operands)) bindings = operands[0] if ((not isinstance(bindings, Pair)) and (bindings is not Nil)): raise OperandDeduceError(''.join([ 'Expected first argument of let to be a Pair, not ', '{}'.format(bindings), '.' ])) new_frame = Frame('anonymous let', frame) gui_holder.expression.children[1].link_visual( VisualExpression(bindings)) bindings_holder = gui_holder.expression.children[1] bindings = pair_to_list(bindings) for (i, binding) in enumerate(bindings): if (not isinstance(binding, Pair)): raise OperandDeduceError(''.join([ 'Expected binding to be a Pair, not ', '{}'.format(binding), '.' ])) binding_holder = bindings_holder.expression.children[i] binding_holder.link_visual(VisualExpression(binding)) binding = pair_to_list(binding) if (len(binding) != 2): raise OperandDeduceError(''.join([ 'Expected binding to be of length 2, not ', '{}'.format(len(binding)), '.' ])) (name, expr) = binding if (not isinstance(name, Symbol)): raise OperandDeduceError(''.join([ 'Expected first element of binding to be a Symbol, not ', '{}'.format(name), '.' ])) new_frame.assign( name, evaluate(expr, frame, binding_holder.expression.children[1])) value = None for (i, (operand, holder)) in enumerate( zip(operands[1:], gui_holder.expression.children[2:])): value = evaluate(operand, new_frame, holder, (i == (len(operands) - 2))) new_frame.assign(return_symbol, value) return value
def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder, eval_operands=True): verify_exact_callable_length(self, 2, len(operands)) if eval_operands: operands = evaluate_all(operands, frame, gui_holder.expression.children[1:]) func, args = operands if not isinstance(func, Applicable): raise OperandDeduceError(f"Unable to apply {func}.") gui_holder.expression.set_entries([VisualExpression(Pair(func, args), gui_holder.expression.display_value)]) gui_holder.expression.children[0].expression.children = [] gui_holder.apply() args = pair_to_list(args) return func.execute(args, frame, gui_holder.expression.children[0], False)
def quasiquote_evaluate(cls, expr: Expression, frame: Frame, gui_holder: Holder, splicing=False): is_well_formed = False if isinstance(expr, Pair): try: lst = pair_to_list(expr) except OperandDeduceError: pass else: is_well_formed = (not any( map((lambda x: (isinstance(x, Symbol) and (x.value in ['unquote', 'quasiquote', 'unquote-splicing']))), lst))) visual_expression = VisualExpression(expr) gui_holder.link_visual(visual_expression) if (not is_well_formed): visual_expression.children[2:] = [] if isinstance(expr, Pair): if (isinstance(expr.first, Symbol) and (expr.first.value in ('unquote', 'unquote-splicing'))): if ((expr.first.value == 'unquote-splicing') and (not splicing)): raise OperandDeduceError( 'Unquote-splicing must be in list template.') gui_holder.evaluate() verify_exact_callable_length(expr.first, 1, (len(pair_to_list(expr)) - 1)) out = evaluate(expr.rest.first, frame, visual_expression.children[1]) visual_expression.value = out gui_holder.complete() return out elif (isinstance(expr.first, Symbol) and (expr.first.value == 'quasiquote')): visual_expression.value = expr gui_holder.complete() return expr else: if is_well_formed: out = [] for (sub_expr, holder) in zip(pair_to_list(expr), visual_expression.children): splicing = (isinstance(sub_expr, Pair) and isinstance(sub_expr.first, Symbol) and (sub_expr.first.value == 'unquote-splicing')) evaluated = Quasiquote.quasiquote_evaluate( sub_expr, frame, holder, splicing) if splicing: if (not isinstance(evaluated, (Pair, NilType))): raise TypeMismatchError(''.join([ 'Can only splice lists, not ', '{}'.format(evaluated), '.' ])) out.extend(pair_to_list(evaluated)) else: out.append(evaluated) out = make_list(out) else: if (not logger.dotted): raise OperandDeduceError(''.join([ '{}'.format(expr), ' is an ill-formed quasiquotation.' ])) out = Pair( Quasiquote.quasiquote_evaluate( expr.first, frame, visual_expression.children[0]), Quasiquote.quasiquote_evaluate( expr.rest, frame, visual_expression.children[1])) visual_expression.value = out gui_holder.complete() return out else: visual_expression.value = expr gui_holder.complete() return expr