def execute_evaluated(self, operands: List[Expression], frame: Frame):
     verify_min_callable_length(self, 1, len(operands))
     assert_all_numbers(operands)
     if len(operands) == 1:
         return Number(-operands[0].value)
     return Number(operands[0].value - sum(operand.value
                                           for operand in operands[1:]))
    def execute_evaluated(self, operands: List[Expression],
                          frame: Frame) -> Expression:
        verify_min_callable_length(self, 1, len(operands))
        assert_all_numbers(operands)
        if len(operands) == 1:
            return Number(1 / operands[0].value)

        out = operands[0].value
        for operand in operands[1:]:
            out /= operand.value
        return Number(out)
 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)
示例#4
0
文件: lists.py 项目: SallyPeng00/61a
 def execute_simple(self, operand: Expression) -> Expression:
     if ((not isinstance(operand, Pair)) and (operand is not Nil)):
         raise OperandDeduceError(''.join([
             'Unable to calculate length, as ', '{}'.format(operand),
             ' is not a valid list.'
         ]))
     return Number(len(pair_to_list(operand)))
示例#5
0
def get_expression(buffer: TokenBuffer) -> Union[(Expression, None)]:
    token = buffer.pop_next_token()
    if (token is None):
        return None
    elif (token in ('(', '[')):
        return get_rest_of_list(buffer, (')' if (token == '(') else ']'))
    elif (token == "'"):
        return make_list([Symbol('quote'), get_expression(buffer)])
    elif (token == ','):
        if (buffer.get_next_token() == '@'):
            buffer.pop_next_token()
            return make_list(
                [Symbol('unquote-splicing'),
                 get_expression(buffer)])
        else:
            return make_list([Symbol('unquote'), get_expression(buffer)])
    elif (token == '`'):
        return make_list([Symbol('quasiquote'), get_expression(buffer)])
    elif (token == '.'):
        if logger.dotted:
            raise ParseError(''.join(
                ["Unexpected token: '", '{}'.format(token), "'"]))
        else:
            return make_list([Symbol('variadic'), get_expression(buffer)])
    elif (token == '"'):
        return get_string(buffer)
    elif (token in SPECIALS):
        raise ParseError(''.join(
            ["Unexpected token: '", '{}'.format(token), "'"]))
    elif is_number(token.value):
        try:
            return Number(int(token.value))
        except ValueError:
            return Number(float(token.value))
    elif ((token == '#t') or (token.value.lower() == 'true')):
        return SingletonTrue
    elif ((token == '#f') or (token.value.lower() == 'false')):
        return SingletonFalse
    elif (token == 'nil'):
        return Nil
    elif is_str(token.value):
        return Symbol(token.value.lower())
    else:
        raise ParseError(''.join(
            ["Unexpected token: '", '{}'.format(token), "'"]))
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     for operand in operands:
         if not isinstance(operand, Number):
             raise MathError()
     try:
         return Number(self.func(*(operand.value for operand in operands)))
     except TypeError:
         raise OperandDeduceError(
             f"Incorrect number of arguments for #[{self.name}].")
示例#7
0
def get_expression(buffer: TokenBuffer) -> Union[Expression, None]:
    token = buffer.pop_next_token()
    if token is None:
        return None
    elif token in ("(", "["):
        return get_rest_of_list(buffer, ")" if token == "(" else "]")
    elif token == "'":
        return make_list([Symbol("quote"), get_expression(buffer)])
    elif token == ",":
        if buffer.get_next_token() == "@":
            buffer.pop_next_token()
            return make_list(
                [Symbol("unquote-splicing"),
                 get_expression(buffer)])
        else:
            return make_list([Symbol("unquote"), get_expression(buffer)])
    elif token == "`":
        return make_list([Symbol("quasiquote"), get_expression(buffer)])
    elif token == ".":
        if logger.dotted:
            raise ParseError(f"Unexpected token: '{token}'")
        else:
            return make_list([Symbol("variadic"), get_expression(buffer)])
    elif token == "\"":
        return get_string(buffer)
    elif token in SPECIALS:
        raise ParseError(f"Unexpected token: '{token}'")
    elif is_number(token.value):
        try:
            return Number(int(token.value))
        except ValueError:
            return Number(float(token.value))
    elif token == "#t" or token.value.lower() == "true":
        return SingletonTrue
    elif token == "#f" or token.value.lower() == "false":
        return SingletonFalse
    elif token == "nil":
        return Nil
    elif is_str(token.value):
        return Symbol(token.value.lower())
    else:
        raise ParseError(f"Unexpected token: '{token}'")
示例#8
0
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     for operand in operands:
         if (not isinstance(operand, Number)):
             raise MathError()
     try:
         return Number(self.func(*(operand.value for operand in operands)))
     except TypeError:
         raise OperandDeduceError(''.join([
             'Incorrect number of arguments for #[', '{}'.format(self.name),
             '].'
         ]))
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     verify_exact_callable_length(self, 2, len(operands))
     assert_all_numbers(operands)
     return Number(operands[0].value % abs(operands[1].value))
 def execute_simple(self, operand: Expression) -> Expression:
     assert_all_numbers([operand])
     return Number(abs(operand.value))
 def execute_evaluated(self, operands: List[Expression], frame: Frame):
     assert_all_numbers(operands)
     out = 1
     for operand in operands:
         out *= operand.value
     return Number(out)
 def execute_evaluated(self, operands: List[Expression], frame: Frame):
     assert_all_numbers(operands)
     return Number(sum(operand.value for operand in operands))
示例#13
0
 def execute_evaluated(self, operands: List[Expression],
                       frame: Frame) -> Expression:
     verify_exact_callable_length(self, 0, len(operands))
     return Number(log.logger.get_canvas().SIZE)
示例#14
0
 def execute_simple(self, operand: Expression) -> Expression:
     if not isinstance(operand, Pair) and operand is not Nil:
         raise OperandDeduceError(f"Unable to calculate length, as {operand} is not a valid list.")
     return Number(len(pair_to_list(operand)))