Exemplo n.º 1
0
def run_time(module, code_obj):
    """
    The Run-Time phase for a module. Evaluates the recently compiled
    code_obj in the module's environment using the module's evaluator,
    and produces the expression's resulting value.
    """

    evaluator = get_module_evaluator(module)
    return tailcall(evaluator)(code_obj)
Exemplo n.º 2
0
def run_time(module, code_obj):
    """
    The Run-Time phase for a module. Evaluates the recently compiled
    code_obj in the module's environment using the module's evaluator,
    and produces the expression's resulting value.
    """

    evaluator = get_module_evaluator(module)
    return tailcall(evaluator)(code_obj)
Exemplo n.º 3
0
def load_module_1(module, parse_time=parse_time,
                  compile_time=compile_time, run_time=run_time):

    """
    Parse a single expression from a module's source stream, compile
    it, evaluate it, and return the result.
    """

    source_expr = parse_time(module)
    if source_expr is None:
        return None

    code_obj = compile_time(module, source_expr)
    return tailcall(run_time)(module, code_obj)
Exemplo n.º 4
0
def load_module_1(module,
                  parse_time=parse_time,
                  compile_time=compile_time,
                  run_time=run_time):
    """
    Parse a single expression from a module's source stream, compile
    it, evaluate it, and return the result.
    """

    source_expr = parse_time(module)
    if source_expr is None:
        return None

    code_obj = compile_time(module, source_expr)
    return tailcall(run_time)(module, code_obj)
Exemplo n.º 5
0
    def complete_apply(self, args, declared_at, tc, cont):

        params = gather_parameters(args)

        pos, keywords, values, vargs, vkwds = params

        assert (len(keywords) == len(values)), "mismatched keyword, values"

        if tc:
            self.helper_tailcall_tos(args, declared_at)

        for expr in pos:
            self.add_expression(expr)

        if vargs:
            self.add_expression(vargs)

        for key, val in zip(keywords, values):
            self.pseudop_const(str(key))
            self.add_expression(val)

        if vkwds:
            self.add_expression(vkwds)

        if vargs:
            if vkwds:
                # CALL_FUNCTION_VAR_KW
                pseu = self.pseudop_call_var_kw
            else:
                # CALL_FUNCTION_VAR
                pseu = self.pseudop_call_var
        else:
            if vkwds:
                # CALL_FUNCTION_KW
                pseu = self.pseudop_call_kw
            else:
                # CALL_FUNCTION
                pseu = self.pseudop_call

        if declared_at:
            self.pseudop_position(*declared_at)

        pseu(len(pos), len(keywords))
        return tailcall(cont)(None, False)
Exemplo n.º 6
0
    def complete_apply(self, args, declared_at, tc, cont):

        params = gather_parameters(args)

        pos, keywords, values, vargs, vkwds = params

        assert (len(keywords) == len(values)), "mismatched keyword, values"

        if tc:
            self.helper_tailcall_tos(args, declared_at)

        for expr in pos:
            self.add_expression(expr)

        if vargs:
            self.add_expression(vargs)

        for key, val in zip(keywords, values):
            self.pseudop_const(str(key))
            self.add_expression(val)

        if vkwds:
            self.add_expression(vkwds)

        if vargs:
            if vkwds:
                # CALL_FUNCTION_VAR_KW
                pseu = self.pseudop_call_var_kw
            else:
                # CALL_FUNCTION_VAR
                pseu = self.pseudop_call_var
        else:
            if vkwds:
                # CALL_FUNCTION_KW
                pseu = self.pseudop_call_kw
            else:
                # CALL_FUNCTION
                pseu = self.pseudop_call

        if declared_at:
            self.pseudop_position(*declared_at)

        pseu(len(pos), len(keywords))
        return tailcall(cont)(None, False)
Exemplo n.º 7
0
def tco_fibonacci(num, accu=0, carry=1):
    if num < 1:
        return accu
    else:
        return tailcall(tco_fibonacci)(num - 1, carry, carry + accu)
Exemplo n.º 8
0
def tco_factorial(num, accu=1):
    if num <= 1:
        return accu
    else:
        return tailcall(tco_factorial)(num - 1, accu * num)
Exemplo n.º 9
0
 def evaluator(code):
     return tailcall(teval)(code, mod_globals)
Exemplo n.º 10
0
    def complete_apply(self, args, declared_at, tc, cont):
        params = gather_parameters(args)

        pos, keywords, values, vargs, vkwds = params

        assert (len(keywords) == len(values)), "mismatched keyword, values"

        if tc:
            self.helper_tailcall_tos(args, declared_at)

        arg_tuple = 0

        # step one, evaluate the positionals that we have
        for expr in pos:
            self.add_expression(expr)

        if not (vargs or keywords or vkwds):
            # easy mode, nothing fancy, just a plain 'ol call
            self.pseudop_call(len(pos))
            return tailcall(cont)(None, False)

        elif pos:
            # it's going to get complicated. collect the positionals
            # we've got into a tuple for later.
            self.pseudop_build_tuple(len(pos))
            arg_tuple += 1

        if vargs:
            # another tuple of positional arguments onto the pile
            self.add_expression(vargs)
            arg_tuple += 1

        if arg_tuple > 1:
            # if we have more than one positional tuple, join them
            # together into a single tuple (or if we have none, create
            # an empty tuple)
            self.pseudop_build_tuple_unpack(arg_tuple)

        if not (keywords or vkwds):
            # just positionals, so invoke CALL_FUNCTION_EX 0x00
            self.pseudop_call_var(0)
            return tailcall(cont)(None, False)

        elif not arg_tuple:
            # in order to support CALL_FUNCTION_EX later on, we're
            # going to push an empty tuple on, to represent our lack
            # of positionals
            self.pseudop_build_tuple(0)

        kwd_tuple = 0
        if keywords:
            # build a map out of all the keyword:value entries
            for key, val in zip(keywords, values):
                self.pseudop_const(str(key))
                self.add_expression(val)
            self.pseudop_build_map(len(keywords))
            kwd_tuple += 1

        if vkwds:
            # if we also have a variadic kwd, grab that.
            self.add_expression(vkwds)
            kwd_tuple += 1

        if kwd_tuple > 1:
            # if we have both keywords and variadic kwds, join' em together
            self.pseudop_build_map_unpack(kwd_tuple)

        if declared_at:
            self.pseudop_position(*declared_at)

        # even if we had no positionals, we've created an empty
        # positionals tuple, and now we can CALL_FUNCTION_EX 0x01
        self.pseudop_call_var_kw(0)

        return tailcall(cont)(None, False)
Exemplo n.º 11
0
def tco_even(num):
    return True if num == 0 else tailcall(tco_odd)(num - 1)
Exemplo n.º 12
0
def tco_fibonacci(num, accu=0, carry=1):
    if num < 1:
        return accu
    else:
        return tailcall(tco_fibonacci)(num - 1, carry, carry + accu)
Exemplo n.º 13
0
def tco_factorial(num, accu=1):
    if num <= 1:
        return accu
    else:
        return tailcall(tco_factorial)(num - 1, accu * num)
Exemplo n.º 14
0
def tco_even(num):
    return True if num == 0 else tailcall(tco_odd)(num - 1)
Exemplo n.º 15
0
def tco_odd(num):
    return False if num == 0 else tailcall(tco_even)(num - 1)
Exemplo n.º 16
0
def tco_odd(num):
    return False if num == 0 else tailcall(tco_even)(num - 1)
Exemplo n.º 17
0
 def evaluator(code):
     return tailcall(teval)(code, mod_globals)