forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol.py
More file actions
96 lines (77 loc) · 2.38 KB
/
symbol.py
File metadata and controls
96 lines (77 loc) · 2.38 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pixie.vm.object as object
from pixie.vm.primitives import nil, true, false
import pixie.vm.stdlib as proto
from pixie.vm.code import extend, as_var
from pixie.vm.string import String
import pixie.vm.rt as rt
import pixie.vm.util as util
from rpython.rlib.rarithmetic import intmask
class Symbol(object.Object):
_type = object.Type(u"pixie.stdlib.Symbol")
def __init__(self, s, meta=nil):
#assert isinstance(s, unicode)
self._str = s
self._w_name = None
self._w_ns = None
self._hash = 0
self._meta = meta
def type(self):
return Symbol._type
def init_names(self):
if self._w_name is None:
s = self._str.split(u"/")
if len(s) == 2:
self._w_ns = rt.wrap(s[0])
self._w_name = rt.wrap(s[1])
elif len(s) == 1:
self._w_name = rt.wrap(s[0])
self._w_ns = nil
else:
self._w_ns = rt.wrap(s[0])
self._w_name = rt.wrap(u"/".join(s[1:]))
def with_meta(self, meta):
return Symbol(self._str, meta)
def meta(self):
return self._meta
def symbol(s):
return Symbol(s)
@extend(proto._eq, Symbol)
def _eq(self, other):
assert isinstance(self, Symbol)
if not isinstance(other, Symbol):
return false
return true if self._str == other._str else false
@extend(proto._str, Symbol)
def _str(self):
assert isinstance(self, Symbol)
return rt.wrap(self._str)
@extend(proto._name, Symbol)
def _name(self):
assert isinstance(self, Symbol)
self.init_names()
return self._w_name
@extend(proto._namespace, Symbol)
def _namespace(self):
assert isinstance(self, Symbol)
self.init_names()
return self._w_ns
@extend(proto._hash, Symbol)
def _hash(self):
assert isinstance(self, Symbol)
if self._hash == 0:
self._hash = util.hash_unencoded_chars(self._str)
return rt.wrap(intmask(self._hash))
@as_var("symbol")
def _symbol(s):
if not isinstance(s, String):
from pixie.vm.object import runtime_error
runtime_error(u"Symbol name must be a string")
return symbol(s._str)
@extend(proto._meta, Symbol)
def _meta(self):
assert isinstance(self, Symbol)
return self.meta()
@extend(proto._with_meta, Symbol)
def _with_meta(self, meta):
assert isinstance(self, Symbol)
return self.with_meta(meta)