Пример #1
0
def _preform_comparison(func, *args):
    if len(args) == 1:
        return Boolean(True)

    prev, rest = args[0], args[1:]
    for arg in rest:
        if not func(prev, arg):
            return Boolean(False)
        prev = arg

    return Boolean(True)
Пример #2
0
    def _eval_and(self, expr, env):
        if procedure.no_operands(procedure.operands(expr)):
            return Boolean(True)

        def inner(expr, env):
            if sequence.is_last_expr(expr):
                return self._eval(sequence.first_expr(expr), env)
            if not self._eval(sequence.first_expr(expr), env):
                return Boolean(False)
            return inner(sequence.rest_exprs(expr), env)

        return inner(sequence.rest_exprs(expr), env)
Пример #3
0
    def _eval_or(self, expr, env):
        if procedure.no_operands(procedure.operands(expr)):
            return Boolean(False)

        def inner(expr, env):
            if sequence.is_last_expr(expr):
                return self._eval(sequence.first_expr(expr), env)
            first_value = self._eval(sequence.first_expr(expr), env)
            if first_value:
                return first_value
            return inner(sequence.rest_exprs(expr), env)

        return inner(sequence.rest_exprs(expr), env)
Пример #4
0
def expand_clauses(clauses):
    if clauses is EmptyList:
        return Boolean(False)

    first = car(clauses)
    rest = cdr(clauses)

    if is_cond_else_clause(first):
        if rest is EmptyList:
            return sequence_expr(cond_actions(first))
        else:
            raise ValueError('ELSE caluse isn\'t last: %s' % clauses)
    else:
        return make_if(cond_predicate(first), 
                sequence_expr(cond_actions(first)), expand_clauses(rest))
Пример #5
0
    def _simple_datum(self):
        token = self._lookahead_token(0)

        if token.type == tokens.NUMBER:
            expr = Number(int(token.text))
        elif token.type == tokens.BOOLEAN:
            expr = Boolean(True if token.text == '#t' else False)
        elif token.type == tokens.CHARACTER:
            expr = Character(token.text[2:])
        elif token.type == tokens.STRING:
            expr = String(token.text.strip('"'))
        elif token.type == tokens.ID:
            expr = Symbol(token.text)
        else:
            raise ParserException('No viable alternative')

        self._match(token.type)
        return expr
Пример #6
0
 def inner(expr, env):
     if sequence.is_last_expr(expr):
         return self._eval(sequence.first_expr(expr), env)
     if not self._eval(sequence.first_expr(expr), env):
         return Boolean(False)
     return inner(sequence.rest_exprs(expr), env)
Пример #7
0
def builtin_null_p(*args):
    return Boolean(args[0] is EmptyList)
Пример #8
0
def builtin_pair_p(*args):
    arg = args[0]
    return Boolean(isinstance(arg, Pair))
Пример #9
0
def builtin_even_p(*args):
    return Boolean(args[0].val % 2 == 0)
Пример #10
0
def builtin_number_p(*args):
    return Boolean(isinstance(args[0], Number))
Пример #11
0
def builtin_zero_p(*args):
    return Boolean(args[0] == Number(0))
Пример #12
0
def builtin_eq_p(*args):
    first, second = args
    return Boolean(first == second)
Пример #13
0
def builtin_not(*args):
    return Boolean(not bool(args[0]))