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") ]
# -*- 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)
def instr(self, n: AST): assert (n.tag == 'Instr') elts = n.contents _gensym_81 = elts if isinstance(_gensym_81, tuple): if (len(_gensym_81) is 2): _gensym_82 = _gensym_81[0] _gensym_83 = Token.__match__(1, _gensym_82) if (_gensym_83 is None): _gensym_2 = None elif isinstance(_gensym_83, tuple): if (len(_gensym_83) is 1): _gensym_84 = _gensym_83[0] instrname = _gensym_84 _gensym_82 = _gensym_81[1] if isinstance(_gensym_82, Token): tk = _gensym_82 _gensym_2 = () if (tk.idint == self.ID_t): return { 'load': sij.Load, 'store': sij.Store, 'deref': sij.Deref, 'deref!': sij.RefSet, 'goto': sij.Goto, 'goto-if': sij.GotoEq, 'goto-if-not': sij.GotoNEq, 'label': sij.Label, 'blockaddr': sij.BlockAddr }[instrname](tk.value) if (tk.idint == self.PY_t): assert (instrname == 'const') return sij.Extern(tk.value[1:(-1)]) if (tk.idint == self.INT_t): return { 'rot': sij.ROT, 'dup': sij.DUP, 'list': sij.BuildList, 'tuple': sij.BuildTuple, 'line': sij.Line, 'call': sij.Call }[instrname](int(tk.value)) if _: raise TypeError( 'invalid instruction {}'.format(instrname)) else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None if (_gensym_2 is None): if isinstance(_gensym_81, tuple): if (len(_gensym_81) is 1): _gensym_85 = _gensym_81[0] _gensym_86 = Token.__match__(1, _gensym_85) if (_gensym_86 is None): _gensym_2 = None elif isinstance(_gensym_86, tuple): if (len(_gensym_86) is 1): _gensym_87 = _gensym_86[0] single_instr = _gensym_87 _gensym_2 = () return { 'prj': sij.Item, 'prj!': sij.ItemSet, 'pop': sij.Pop, 'print': sij.Print, 'return': sij.Return, 'indir': sij.Indir }[single_instr]() else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None if (_gensym_2 is None): if isinstance(_gensym_81, tuple): if (len(_gensym_81) >= 1): _gensym_88 = _gensym_81[0] _gensym_89 = Token.__match__(1, _gensym_88) if (_gensym_89 is None): _gensym_2 = None elif isinstance(_gensym_89, tuple): if (len(_gensym_89) is 1): _gensym_90 = _gensym_89[0] if ('switch' == _gensym_90): _gensym_88 = _gensym_81[1:None] args = _gensym_88 _gensym_2 = () table = dict(self.jump_cases(args[(-1)])) return sij.Switch(table) else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None if (_gensym_2 is None): if isinstance(_gensym_81, tuple): if (len(_gensym_81) >= 1): _gensym_91 = _gensym_81[0] _gensym_92 = Token.__match__(1, _gensym_91) if (_gensym_92 is None): _gensym_2 = None elif isinstance(_gensym_92, tuple): if (len(_gensym_92) is 1): _gensym_93 = _gensym_92[0] if ('defun' == _gensym_93): _gensym_91 = _gensym_81[1:None] xs = _gensym_91 _gensym_2 = () doc = '' filename = '' free = [] name = '<unnamed>' args = [] _gensym_94 = xs if isinstance(_gensym_94, tuple): if (len(_gensym_94) is 3): _gensym_95 = _gensym_94[0] _gensym_95 = _gensym_94[1] instrs = _gensym_95 _gensym_95 = _gensym_94[2] _gensym_2 = () return sij.Defun( doc, filename, free, name, args, self.instrs(instrs)) else: _gensym_2 = None else: _gensym_2 = None if (_gensym_2 is None): if isinstance(_gensym_94, tuple): if (len(_gensym_94) is 4): _gensym_96 = _gensym_94[0] attrs = _gensym_96 _gensym_96 = _gensym_94[1] _gensym_96 = _gensym_94[2] instrs = _gensym_96 _gensym_96 = _gensym_94[3] _gensym_2 = () instrs = self.instrs( instrs) for ( attrname, attrvalue ) in self.attrs(attrs): _gensym_97 = attrname if ('document' == _gensym_97): _gensym_2 = () doc = attrvalue else: _gensym_2 = None if (_gensym_2 is None): if ('filename' == _gensym_97 ): _gensym_2 = () filename = attrvalue else: _gensym_2 = None if (_gensym_2 is None): if ('free' == _gensym_97 ): _gensym_2 = ( ) free = attrvalue else: _gensym_2 = None if (_gensym_2 is None): if ('name' == _gensym_97 ): _gensym_2 = ( ) name = attrvalue else: _gensym_2 = None if (_gensym_2 is None ): if ('args' == _gensym_97 ): _gensym_2 = ( ) args = attrvalue else: _gensym_2 = None if (_gensym_2 is None ): if ( 'lineno' == _gensym_97 ): _gensym_2 = ( ) instrs = [ sij . Line( attrvalue ), *instrs ] else: _gensym_2 = None if ( _gensym_2 is None ): _gensym_2 = ( ) raise TypeError( 'invalid attribute {}' . format( attrname ) ) if ( _gensym_2 is None ): raise NotExhaustive return sij.Defun( doc, filename, free, name, args, instrs) else: _gensym_2 = None else: _gensym_2 = None if (_gensym_2 is None): _gensym_2 = () raise TypeError( 'invalid args for defun, seems impossible' ) if (_gensym_2 is None): raise NotExhaustive else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None else: _gensym_2 = None if (_gensym_2 is None): raise NotExhaustive
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:
def function(self, name, filename, scope: Scope, arg_sym: Sym, instrs): freevars = list(map(self.s2n, scope.freevars.values())) return sij.Defun(name, filename, freevars, self.s2n(arg_sym), [self.s2n(arg_sym)], self.eval_many(instrs))