Skip to content

Commit 9c900fc

Browse files
Merge pull request #505 from thomasmulvaney/keyword-improve
keywords can start with numbers or true, false, nil
2 parents 12cacc6 + a6ecc89 commit 9c900fc

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

pixie/vm/reader.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pixie.vm.keyword import keyword, Keyword
1111
import pixie.vm.rt as rt
1212
from pixie.vm.persistent_vector import EMPTY as EMPTY_VECTOR
13-
from pixie.vm.libs.libedit import _readline
1413
from pixie.vm.string import Character
1514
from pixie.vm.code import wrap_fn
1615
from pixie.vm.persistent_hash_map import EMPTY as EMPTY_MAP
@@ -319,11 +318,12 @@ def fqd(self, itm):
319318
def invoke(self, rdr, ch):
320319
ch = rdr.read()
321320
if ch == u":":
322-
itm = read_inner(rdr, True)
321+
ch = rdr.read()
322+
itm = read_symbol(rdr, ch, False)
323323
return self.fqd(itm)
324324
else:
325-
rdr.unread()
326-
itm = read_inner(rdr, True)
325+
itm = read_symbol(rdr, ch, False)
326+
327327
return keyword(rt.name(itm), rt.namespace(itm))
328328

329329
class LiteralStringReader(ReaderHandler):
@@ -739,7 +739,7 @@ def read_number(rdr, ch):
739739
return parsed
740740
return Symbol(joined)
741741

742-
def read_symbol(rdr, ch):
742+
def read_symbol(rdr, ch, convert_primitives=True):
743743
acc = [ch]
744744
try:
745745
while True:
@@ -752,12 +752,14 @@ def read_symbol(rdr, ch):
752752
pass
753753

754754
sym_str = u"".join(acc)
755-
if sym_str == u"true":
756-
return true
757-
if sym_str == u"false":
758-
return false
759-
if sym_str == u"nil":
760-
return nil
755+
756+
if convert_primitives:
757+
if sym_str == u"true":
758+
return true
759+
if sym_str == u"false":
760+
return false
761+
if sym_str == u"nil":
762+
return nil
761763
return symbol(sym_str)
762764

763765
class EOF(object.Object):

tests/pixie/tests/test-keywords.pxi

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
(t/assert= (:a m) 1)
77
(t/assert= (:b m) 2)
88
(t/assert= (:c m) 3)
9-
109
(t/assert= (:d m) nil)
1110
(t/assert= (:d m :foo) :foo)))
1211

@@ -29,9 +28,40 @@
2928
(t/assert= (not= :foo/bar :cat/bar) true)
3029
(t/assert= (not= :foo/cat :foo/dog) true))
3130

31+
(t/deftest keyword-reader
32+
(t/assert= (read-string ":1") :1)
33+
(t/assert= (read-string ":1") :1)
34+
(t/assert= (read-string ":1.0") :1.0)
35+
(t/assert= (read-string ":foo") :foo)
36+
(t/assert= (read-string ":1foo") :1foo)
37+
(t/assert= (read-string ":foo/bar") :foo/bar)
38+
(t/assert= (read-string ":1foo/1bar") :1foo/1bar)
39+
(t/assert= (read-string ":nil") :nil)
40+
(t/assert= (read-string ":true") :true)
41+
(t/assert= (read-string ":false") :false)
42+
43+
;; We are reading at runtime so the namespace isn't
44+
;; going to be pixie.test.test-keywords. Its probably
45+
;; 'user but lets explicitly set it.
46+
;; The refer-ns is to initialize a new space
47+
(refer-ns 'my.other.ns 'my.fake.core 'fake)
48+
(binding [*ns* (the-ns 'my.other.ns)]
49+
(t/assert= (read-string "::1") :my.other.ns/1)
50+
(t/assert= (read-string "::1.0") :my.other.ns/1.0)
51+
(t/assert= (read-string "::foo") :my.other.ns/foo)
52+
(t/assert= (read-string "::true") :my.other.ns/true)))
53+
3254
(t/deftest string-to-keyword
55+
(t/assert= (keyword "1") :1)
56+
(t/assert= (keyword "1") :1)
57+
(t/assert= (keyword "1.0") :1.0)
3358
(t/assert= (keyword "foo") :foo)
59+
(t/assert= (keyword "1foo") :1foo)
3460
(t/assert= (keyword "foo/bar") :foo/bar)
61+
(t/assert= (keyword "1foo/1bar") :1foo/1bar)
62+
(t/assert= (keyword "nil") :nil)
63+
(t/assert= (keyword "true") :true)
64+
(t/assert= (keyword "false") :false)
3565
(t/assert-throws? (keyword 1))
3666
(t/assert-throws? (keyword :a))
3767
(t/assert-throws? (keyword 'a))

0 commit comments

Comments
 (0)