-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathkeyword.py
More file actions
81 lines (65 loc) · 1.99 KB
/
keyword.py
File metadata and controls
81 lines (65 loc) · 1.99 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
from pixie.vm.object import Object, Type
from pixie.vm.primitives import nil
from pixie.vm.string import String
import pixie.vm.stdlib as proto
from pixie.vm.code import extend, as_var
import pixie.vm.rt as rt
import pixie.vm.util as util
from rpython.rlib.rarithmetic import intmask
class Keyword(Object):
_type = Type(u"pixie.stdlib.Keyword")
def __init__(self, name):
self._str = name
self._w_name = None
self._w_ns = None
self._hash = 0
def type(self):
return Keyword._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:]))
class KeywordCache(object):
def __init__(self):
self._cache = {}
def intern(self, nm):
kw = self._cache.get(nm, None)
if kw is None:
kw = Keyword(nm)
self._cache[nm] = kw
return kw
_kw_cache = KeywordCache()
def keyword(nm, ns=None):
if ns:
nm = u"/".join([ns, nm])
return _kw_cache.intern(nm)
@extend(proto._name, Keyword)
def _name(self):
assert isinstance(self, Keyword)
self.init_names()
return self._w_name
@extend(proto._namespace, Keyword)
def _namespace(self):
assert isinstance(self, Keyword)
self.init_names()
return self._w_ns
@extend(proto._hash, Keyword)
def _hash(self):
assert isinstance(self, Keyword)
if self._hash == 0:
self._hash = util.hash_unencoded_chars(self._str)
return rt.wrap(intmask(self._hash))
@as_var("keyword")
def _keyword(s):
if not isinstance(s, String):
from pixie.vm.object import runtime_error
runtime_error(u"Symbol name must be a string")
return keyword(s._str)