forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_types.py
More file actions
83 lines (65 loc) · 2.5 KB
/
custom_types.py
File metadata and controls
83 lines (65 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from pixie.vm.object import Object, Type, affirm
from pixie.vm.primitives import nil, true, false
import rpython.rlib.jit as jit
from pixie.vm.numbers import Integer
from rpython.rlib.rarithmetic import r_uint
from pixie.vm.code import as_var
from pixie.vm.symbol import Symbol
from pixie.vm.string import String
from pixie.vm.keyword import Keyword
import pixie.vm.rt as rt
class CustomType(Type):
__immutable_fields__ = ["_slots"]
def __init__(self, name, slots):
Type.__init__(self, name)
self._slots = slots
@jit.elidable_promote()
def get_slot_idx(self, nm):
return self._slots[nm]
@jit.elidable_promote()
def get_num_slots(self):
return len(self._slots)
class CustomTypeInstance(Object):
__immutable_fields__ = ["_type"]
def __init__(self, type):
affirm(isinstance(type, CustomType), u"Can't create a instance of a non custom type")
self._custom_type = type
self._fields = [None] * self._custom_type.get_num_slots()
def type(self):
return self._custom_type
def set_field(self, name, val):
idx = self._custom_type.get_slot_idx(name)
self._fields[idx] = val
return self
def get_field(self, name):
idx = self._custom_type.get_slot_idx(name)
return self._fields[idx]
def set_field_by_idx(self, idx, val):
affirm(isinstance(idx, r_uint), u"idx must be a r_uint")
self._fields[idx] = val
return self
@as_var("create-type")
def create_type(type_name, fields):
affirm(isinstance(type_name, Keyword), u"Type name must be a keyword")
field_count = rt.count(fields)
acc = {}
for i in range(rt.count(fields)):
val = rt.nth(fields, rt.wrap(i))
affirm(isinstance(val, Keyword), u"Field names must be keywords")
acc[val] = i
return CustomType(rt.name(type_name), acc)
@as_var("new")
def _new(tp):
affirm(isinstance(tp, CustomType), u"Can only create a new instance of a custom type")
return CustomTypeInstance(tp)
@as_var("set-field!")
def set_field(inst, field, val):
affirm(isinstance(inst, CustomTypeInstance), u"Can only set fields on CustomType instances")
affirm(isinstance(field, Keyword), u"Field must be a keyword")
inst.set_field(field, val)
return inst
@as_var("get-field")
def get_field(inst, field):
affirm(isinstance(inst, CustomTypeInstance), u"Can only get fields on CustomType instances")
affirm(isinstance(field, Keyword), u"Field must be a keyword")
return inst.get_field(field)