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)
Exemplo n.º 2
0
 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_exact_callable_length(self, 2, len(operands))
     name = operands[0]
     if not isinstance(name, Symbol):
         raise OperandDeduceError(f"Expected a Symbol, not {name}, as the first operand of set!")
     frame.mutate(name, evaluate(operands[1], frame, gui_holder.expression.children[2]))
     return Undefined
Exemplo n.º 4
0
 def execute(self, operands: List[Expression], frame: Frame,
             gui_holder: Holder):
     verify_min_callable_length(self, 2, len(operands))
     params = operands[0]
     if isinstance(params, Symbol):
         verify_exact_callable_length(self, 2, len(operands))
         frame.assign(
             params,
             evaluate(operands[1], frame,
                      gui_holder.expression.children[2]))
         return params
     elif isinstance(params, Pair):
         name = params.first
         operands[0] = params.rest
         if (not isinstance(name, Symbol)):
             raise OperandDeduceError(''.join(
                 ['Expected a Symbol, not ', '{}'.format(name), '.']))
         frame.assign(
             name,
             Lambda().execute(operands, frame, gui_holder, name.value))
         return name
     else:
         raise OperandDeduceError(''.join([
             'Expected a Pair, not ', '{}'.format(params),
             ', as the first operand of define-macro.'
         ]))
 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
Exemplo n.º 6
0
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     verify_exact_callable_length(self, 2, len(operands))
     assert_all_numbers(operands)
     negate = (operands[0].value < 0) != (operands[1].value < 0)
     negate = -1 if negate else 1
     return Number(negate * operands[0].value // operands[1].value)
Exemplo n.º 7
0
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     verify_exact_callable_length(self, 0, len(operands))
     if log.logger.fragile:
         raise IrreversibleOperationError()
     log.logger.raw_out("DISABLE_AUTODRAW[]\n")
     return Undefined
Exemplo n.º 8
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame):
     verify_exact_callable_length(self, 2, len(operands))
     for operand in operands:
         if (not isinstance(operand, Number)):
             raise OperandDeduceError(
                 ''.join(['Expected operand to be Number, not ', '{}'.format(operand)]))
     log.logger.get_canvas().move(operands[0].value, (- operands[1].value))
     return Undefined
Exemplo n.º 9
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame):
     verify_exact_callable_length(self, 2, len(operands))
     if all(isinstance(x, ValueHolder) for x in operands):
         if isinstance(operands[0], Number):
             return bools[operands[0] is operands[1]]
         else:
             return bools[operands[0].value == operands[1].value]
     return bools[operands[0] is operands[1]]
Exemplo n.º 10
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame):
     verify_exact_callable_length(self, 2, len(operands))
     for operand in operands:
         if not isinstance(operand, Number):
             raise OperandDeduceError(
                 f"Expected operand to be Number, not {operand}")
     log.logger.get_canvas().move(operands[0].value, -operands[1].value)
     return Undefined
Exemplo n.º 11
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame):
     verify_exact_callable_length(self, 2, len(operands))
     if all(isinstance(x, ValueHolder) for x in operands):
         return bools[operands[0].value == operands[1].value]
     elif all(isinstance(x, Pair) for x in operands):
         return bools[IsEqual().execute_evaluated([operands[0].first, operands[1].first], frame) is SingletonTrue and \
                      IsEqual().execute_evaluated([operands[0].rest, operands[1].rest], frame) is SingletonTrue]
     else:
         return IsEqv().execute_evaluated(operands, frame)
Exemplo n.º 12
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame) -> Expression:
     verify_exact_callable_length(self, 3, len(operands))
     (x, y, c) = operands
     for v in (x, y):
         if (not isinstance(v, Number)):
             raise OperandDeduceError(
                 ''.join(['Expected operand to be Number, not ', '{}'.format(v)]))
     log.logger.get_canvas().pixel(x.value, y.value, make_color(c))
     return Undefined
Exemplo n.º 13
0
 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)
Exemplo n.º 14
0
    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 = gui_holder.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(f"Can only splice lists, not {evaluated}.")
                            out.extend(pair_to_list(evaluated))
                        else:
                            out.append(evaluated)
                    out = make_list(out)
                else:
                    if not logger.dotted:
                        raise OperandDeduceError(f"{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
Exemplo n.º 15
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame) -> Expression:
     verify_exact_callable_length(self, 2, len(operands))
     if log.logger.fragile:
         raise IrreversibleOperationError()
     pair, val = operands
     if not isinstance(pair, Pair):
         raise OperandDeduceError(f"set-car! expected a Pair, received {pair}.")
     pair.first = val
     log.logger.raw_out("WARNING: Mutation operations on pairs are not yet supported by the debugger.")
     return Undefined
Exemplo n.º 16
0
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     verify_exact_callable_length(self, 3, len(operands))
     x, y, c, = operands
     for v in x, y:
         if not isinstance(v, Number):
             raise OperandDeduceError(
                 f"Expected operand to be Number, not {v}")
     log.logger.get_canvas().pixel(x.value, y.value, make_color(c))
     return Undefined
Exemplo n.º 17
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame) -> Expression:
     verify_exact_callable_length(self, 3, len(operands))
     for operand in operands:
         if (not isinstance(operand, Number)):
             raise OperandDeduceError(
                 ''.join(['Expected operand to be Number, not ', '{}'.format(operand)]))
         if (not (0 <= operand.value <= 1)):
             raise OperandDeduceError(
                 ''.join(['RGB values must be between 0 and 1, not ', '{}'.format(operand)]))
     return String(('#' + ''.join(('{:02X}'.format(int((x.value * 255))) for x in operands))))
Exemplo n.º 18
0
 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:])
     raise SchemeError(operands[0])
Exemplo n.º 19
0
 def execute(self, operands: List[Expression], frame: Frame, gui_holder: Holder):
     verify_min_callable_length(self, 2, len(operands))
     if len(operands) > 3:
         verify_exact_callable_length(self, 3, len(operands))
     if evaluate(operands[0], frame, gui_holder.expression.children[1]) is SingletonFalse:
         if len(operands) == 2:
             return Undefined
         else:
             return evaluate(operands[2], frame, gui_holder.expression.children[3], True)
     else:
         return evaluate(operands[1], frame, gui_holder.expression.children[2], True)
Exemplo n.º 20
0
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     verify_exact_callable_length(self, 3, len(operands))
     for operand in operands:
         if not isinstance(operand, Number):
             raise OperandDeduceError(
                 f"Expected operand to be Number, not {operand}")
         if not 0 <= operand.value <= 1:
             raise OperandDeduceError(
                 f"RGB values must be between 0 and 1, not {operand}")
     return String("#" + "".join('{:02X}'.format(int(x.value * 255))
                                 for x in operands))
Exemplo n.º 21
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame) -> Expression:
     verify_exact_callable_length(self, 2, len(operands))
     if log.logger.fragile:
         raise IrreversibleOperationError()
     (pair, val) = operands
     if (not isinstance(pair, Pair)):
         raise OperandDeduceError(
             ''.join(['set-car! expected a Pair, received ', '{}'.format(pair), '.']))
     pair.first = val
     log.logger.raw_out(
         'WARNING: Mutation operations on pairs are not yet supported by the debugger.')
     return Undefined
Exemplo n.º 22
0
 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)
Exemplo n.º 23
0
    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
Exemplo n.º 24
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame) -> Expression:
     verify_min_callable_length(self, 1, len(operands))
     if (len(operands) > 2):
         verify_exact_callable_length(self, 2, len(operands))
     if (not isinstance(operands[0], Number)):
         raise OperandDeduceError(
             ''.join(['Expected radius to be Number, not ', '{}'.format(operands[0])]))
     if ((len(operands) > 2) and (not isinstance(operands[1], Number))):
         raise OperandDeduceError(
             ''.join(['Expected angle to be Number, not ', '{}'.format(operands[1])]))
     degs = (360 if (len(operands) == 1) else operands[1].value)
     log.logger.get_canvas().arc(operands[0].value, degs)
     return Undefined
Exemplo n.º 25
0
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     verify_min_callable_length(self, 1, len(operands))
     if len(operands) > 2:
         verify_exact_callable_length(self, 2, len(operands))
     if not isinstance(operands[0], Number):
         raise OperandDeduceError(
             f"Expected radius to be Number, not {operands[0]}")
     if len(operands) > 2 and not isinstance(operands[1], Number):
         raise OperandDeduceError(
             f"Expected angle to be Number, not {operands[1]}")
     degs = 360 if len(operands) == 1 else operands[1].value
     log.logger.get_canvas().arc(operands[0].value, degs)
     return Undefined
Exemplo n.º 26
0
 def execute(self, operands: List[Expression], frame: Frame,
             gui_holder: Holder):
     verify_exact_callable_length(self, 2, len(operands))
     case = operands[0]
     operands[0] = evaluate(operands[0], frame,
                            gui_holder.expression.children[1])
     if not IsEqual().execute_evaluated(operands, frame).value:
         log.logger.raw_out(
             f"Evaluated {case}, expected {operands[1]}, got {operands[0]}.\n"
         )
     else:
         log.logger.raw_out(
             f"Evaluated {case}, got {operands[0]}, as expected.\n")
     return Undefined
Exemplo n.º 27
0
 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], String):
         raise OperandDeduceError(f"Load expected a String, received {operands[0]}.")
     if logger.fragile:
         raise IrreversibleOperationError()
     from os import listdir
     from os.path import join
     directory = operands[0].value
     try:
         targets = sorted(listdir(directory))
         targets = [join(directory, target) for target in targets if target.endswith(".scm")]
         exprs = [make_list([Symbol("load"), make_list([Symbol("quote"), Symbol(x[:-4])])]) for x in targets]
         equiv = make_list([Symbol("begin-noexcept")] + exprs)
         gui_holder.expression.set_entries([equiv])
         gui_holder.apply()
         return evaluate(equiv, frame, gui_holder.expression.children[0], True)
     except Exception as e:
         raise SchemeError(e)
Exemplo n.º 28
0
 def execute_evaluated(self, operands: List[Expression], frame: Frame) -> Expression:
     verify_exact_callable_length(self, 1, len(operands))
     operand = operands[0]
     return self.execute_simple(operand)
Exemplo n.º 29
0
 def execute(self, operands: List[Expression], frame: Frame,
             gui_holder: Holder):
     verify_exact_callable_length(self, 2, len(operands))
     operands[0] = evaluate(operands[0], frame,
                            gui_holder.expression.children[1])
     return Pair(operands[0], Promise(operands[1], frame))
Exemplo n.º 30
0
 def execute(self, operands: List[Expression], frame: Frame,
             gui_holder: Holder):
     verify_exact_callable_length(self, 1, len(operands))
     return Promise(operands[0], frame)