<commit_before>from pycket        import values
from pycket        import vector
from pycket.prims  import prim_env
from pycket.error import SchemeException
from rpython.rlib  import jit, debug
from small_list import *

class Env(object):
    _immutable_fields_ = ["toplevel_env"]
    pass

class Version(object):
    pass

class ToplevelEnv(Env):
    _immutable_fields_ = ["version?"]
    def __init__(self):
        self.bindings = {}
        self.version = Version()
        self.toplevel_env = self # bit silly
    def lookup(self, sym):
        raise SchemeException("variable %s is unbound"%sym.value)

    def toplevel_lookup(self, sym):
        jit.promote(self)
        w_res = self._lookup(sym, jit.promote(self.version))
        if isinstance(w_res, values.W_Cell):
            w_res = w_res.value
        return w_res

    @jit.elidable
    def _lookup(self, sym, version):
        try:
            return self.bindings[sym]
        except KeyError:
            raise SchemeException("toplevel variable %s not found" % sym.value)

    def toplevel_set(self, sym, w_val):
        if sym in self.bindings:
            self.bindings[sym].value = w_val
        else:
            self.bindings[sym] = values.W_Cell(w_val)
            self.version = Version()

class ConsEnv(Env):
    _immutable_fields_ = ["args", "prev"]
    @jit.unroll_safe
    def __init__ (self, args, prev, toplevel):
        self.toplevel_env = toplevel
        self.args = args
        self.prev = prev
    @jit.unroll_safe
    def lookup(self, sym):
        jit.promote(self.args)
        for i, s in enumerate(self.args.elems):
            if s is sym:
                v = self._get_list(i)
                assert v is not None
                return v
        return self.prev.lookup(sym)
    def set(self, sym, val):
        jit.promote(self.args)
        for i, s in enumerate(self.args.elems):
            if s is sym:
                self._set_list(i, val)
                return
        return self.prev.set(sym, val)
inline_small_list(ConsEnv, immutable=True, attrname="vals")

class Cont(object):
    def tostring(self):
        if self.prev:
            return "%s(%s)"%(self.__class__.__name__,self.prev.tostring())
        else:
            return "%s()"%(self.__class__.__name__)

def check_one_val(vals):
    if vals._get_size_list() != 1:
        raise SchemeException("expected 1 value but got %s"%(vals._get_size_list()))
    w_val = vals._get_list(0)
    return w_val

class CWVCont(Cont):
    _immutable_fields_ = ["consumer", "env", "prev"] # env is pointless
    def __init__(self, consumer, env, prev):
        self.consumer = consumer
        self.prev = prev
        self.env = env
    def plug_reduce (self, vals):
        val_list = vals._get_full_list()
        self.consumer.call(val_list, self.env, self.prev)<commit_msg>Stupid statement-based languages.<commit_after>from pycket        import values
from pycket        import vector
from pycket.prims  import prim_env
from pycket.error import SchemeException
from rpython.rlib  import jit, debug
from small_list import *

class Env(object):
    _immutable_fields_ = ["toplevel_env"]
    pass

class Version(object):
    pass

class ToplevelEnv(Env):
    _immutable_fields_ = ["version?"]
    def __init__(self):
        self.bindings = {}
        self.version = Version()
        self.toplevel_env = self # bit silly
    def lookup(self, sym):
        raise SchemeException("variable %s is unbound"%sym.value)

    def toplevel_lookup(self, sym):
        jit.promote(self)
        w_res = self._lookup(sym, jit.promote(self.version))
        if isinstance(w_res, values.W_Cell):
            w_res = w_res.value
        return w_res

    @jit.elidable
    def _lookup(self, sym, version):
        try:
            return self.bindings[sym]
        except KeyError:
            raise SchemeException("toplevel variable %s not found" % sym.value)

    def toplevel_set(self, sym, w_val):
        if sym in self.bindings:
            self.bindings[sym].value = w_val
        else:
            self.bindings[sym] = values.W_Cell(w_val)
            self.version = Version()

class ConsEnv(Env):
    _immutable_fields_ = ["args", "prev"]
    @jit.unroll_safe
    def __init__ (self, args, prev, toplevel):
        self.toplevel_env = toplevel
        self.args = args
        self.prev = prev
    @jit.unroll_safe
    def lookup(self, sym):
        jit.promote(self.args)
        for i, s in enumerate(self.args.elems):
            if s is sym:
                v = self._get_list(i)
                assert v is not None
                return v
        return self.prev.lookup(sym)
    def set(self, sym, val):
        jit.promote(self.args)
        for i, s in enumerate(self.args.elems):
            if s is sym:
                self._set_list(i, val)
                return
        return self.prev.set(sym, val)
inline_small_list(ConsEnv, immutable=True, attrname="vals")

class Cont(object):
    def tostring(self):
        if self.prev:
            return "%s(%s)"%(self.__class__.__name__,self.prev.tostring())
        else:
            return "%s()"%(self.__class__.__name__)

def check_one_val(vals):
    if vals._get_size_list() != 1:
        raise SchemeException("expected 1 value but got %s"%(vals._get_size_list()))
    w_val = vals._get_list(0)
    return w_val

class CWVCont(Cont):
    _immutable_fields_ = ["consumer", "env", "prev"] # env is pointless
    def __init__(self, consumer, env, prev):
        self.consumer = consumer
        self.prev = prev
        self.env = env
    def plug_reduce (self, vals):
        val_list = vals._get_full_list()
        return self.consumer.call(val_list, self.env, self.prev)
{'fraction_non_alphanumeric': 0.10718678615357582, 'fraction_numerical': 0.0010542962572482868, 'mean_word_length': 2.3131548311990686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6305367', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1817, 'n_words': 444}