def preload(self): tco = sij.Defun("base", "base", [], "tailcall", ["argf"], [ sij.Load("argf"), sij.Label("loop"), sij.Unpack(2), sij.Call(1), sij.Unpack(2), sij.GotoNEq("loop"), sij.Return() ]) to_py_callable = sij.Defun( "base", "base", [], "to_py_callable", ["urgent_func"], [ sij.Const(True), sij.Defun("base", "base", ["urgent_func"], "to_py_callable", ["x"], [ sij.Glob("tco"), sij.Load("x"), sij.Deref("urgent_func"), sij.BuildTuple(2), sij.Call(1), sij.Return() ]), sij.BuildTuple(2), sij.Return() ]) set_contents = sij.Defun("base", "base", [], "to_py_callable", ["on", "val"], [ sij.Load("val"), sij.Load("on"), sij.AttrSet("contents"), sij.Const(()), sij.Return() ]) failwith = sij.Defun("base", "base", [], "failwith", ["msg"], [ sij.Glob("Exception"), sij.Load("msg"), sij.Call(1), sij.DUP(1), sij.SimpleRaise(), sij.Return() ]) return [ # for ADTs sij.Glob("__import__"), sij.Const("collections"), sij.Call(1), sij.Attr("namedtuple"), sij.GlobSet("namedtuple"), # for tail call optimizations tco, sij.GlobSet("tco"), to_py_callable, sij.GlobSet("to_py_callable"), set_contents, sij.GlobSet("set_contents"), failwith, sij.GlobSet("failwith") ]
def var(self, s: Sym): assert isinstance(s, Sym), (s, type(s)) if s.is_global: return sij.Glob(self.s2n(s)) if s.is_cell.contents: return sij.Deref(self.s2n(s)) return sij.Load(self.s2n(s))
# 8 def f(x): pass code, _ = lowerer.lower( "name", "fname", 1, "no doc", ["x"], [], [ sij.Load("x"), sij.Switch({ 11: "a", 45: "b", 14: "c", 0: "d" }), sij.Const(5), sij.Print(), sij.Label("a"), sij.Const(6), sij.Print(), sij.Label("b"), sij.Const(7), sij.Print(), sij.Label("c"),
# -*- coding: utf-8 -*- """ Created on Thu Dec 19 18:57:40 2019 @author: twshe """ from sijuiacion_lang.lowering import sij, lower code = lower("mod", "f.txt", 1, "aa", [], [ sij.Defun("hh", "f.txt", [], "lam", ["y"], [ sij.Const(lambda x, y: x + y), sij.Const(1), sij.Load("y"), sij.Return() ]), sij.DUP(1), sij.Print(), sij.Const(2), sij.Call(1), sij.Return() ]) print(eval(code) == 2)
from timeit import timeit from sijuiacion_lang.lowering import sij, Lower defun = sij.Defun("", "", [], "", ["argf"], [ sij.Load("argf"), sij.Label("loop"), sij.Unpack(2), sij.Call(1), sij.Unpack(2), sij.GotoNEq("loop"), sij.Return() ]) code, _ = Lower({}).lower("", "", 1, "", [], [], [defun, sij.Return()]) scheduler = eval(code) print(scheduler) def schd(f, arg): while True: token, a = f(arg) if token: return a f, arg = a # # def rec1(x): if x is 0: