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)
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)
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)
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))
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
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)
def builtin_null_p(*args): return Boolean(args[0] is EmptyList)
def builtin_pair_p(*args): arg = args[0] return Boolean(isinstance(arg, Pair))
def builtin_even_p(*args): return Boolean(args[0].val % 2 == 0)
def builtin_number_p(*args): return Boolean(isinstance(args[0], Number))
def builtin_zero_p(*args): return Boolean(args[0] == Number(0))
def builtin_eq_p(*args): first, second = args return Boolean(first == second)
def builtin_not(*args): return Boolean(not bool(args[0]))