about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCase Duckworth2024-03-20 21:55:09 -0500
committerCase Duckworth2024-03-20 21:55:09 -0500
commit4dde2320effedfc1baac0a0b2011ab2ddbc20c74 (patch)
tree0fa7d06410633b2f0e715e96670735e85e3299e9
parentAdd luarepl target (diff)
downloadlam-4dde2320effedfc1baac0a0b2011ab2ddbc20c74.tar.gz
lam-4dde2320effedfc1baac0a0b2011ab2ddbc20c74.zip
Support dotted lists in reader
-rw-r--r--read.lua17
-rw-r--r--type.lua4
2 files changed, 15 insertions, 6 deletions
diff --git a/read.lua b/read.lua index d21e4cb..064be61 100644 --- a/read.lua +++ b/read.lua
@@ -121,6 +121,7 @@ read.readtable = {
121 ["'"] = function(chars) return pop(chars), "quote", chars end, 121 ["'"] = function(chars) return pop(chars), "quote", chars end,
122 ["`"] = function(chars) return pop(chars), "quote", chars end, 122 ["`"] = function(chars) return pop(chars), "quote", chars end,
123 [","] = function(chars) return pop(chars), "quote", chars end, 123 [","] = function(chars) return pop(chars), "quote", chars end,
124 ["."] = function(chars) return pop(chars), "dot", chars end,
124 ["\""] = consume_string, 125 ["\""] = consume_string,
125 [";"] = consume_comment, 126 [";"] = consume_comment,
126 -- ["#"] = 127 -- ["#"] =
@@ -161,12 +162,20 @@ end
161read.readmacros = { 162read.readmacros = {
162 open = 163 open =
163 function (token, tokens) 164 function (token, tokens)
164 local L = {} 165 local L, lt = {}, nil
165 while tokens[1].type ~= "close" do 166 while tokens[1].type ~= "close" do
166 table.insert(L, read.parse(tokens)) 167 local nt = read.parse(tokens)
168 -- this isn't .. my /favorite/ implementation,
169 -- but it works
170 if nt == "." then
171 lt = read.parse(tokens)
172 break
173 else
174 table.insert(L, nt)
175 end
167 end 176 end
168 pop(tokens) -- remove final ")" 177 pop(tokens) -- remove final ")"
169 return type.List(L) 178 return type.List(L, lt)
170 end, 179 end,
171 close = 180 close =
172 function (token, tokens) 181 function (token, tokens)
diff --git a/type.lua b/type.lua index 0d3b776..0a0c62d 100644 --- a/type.lua +++ b/type.lua
@@ -115,12 +115,12 @@ function t.isNull (x)
115end 115end
116 116
117-- Lists are chained Conses ending in Null 117-- Lists are chained Conses ending in Null
118function t.List (items) 118function t.List (items, last)
119 local function tolist (base, items) 119 local function tolist (base, items)
120 if #items == 0 then return base end 120 if #items == 0 then return base end
121 return tolist(t.Cons(table.remove(items), base), items) 121 return tolist(t.Cons(table.remove(items), base), items)
122 end 122 end
123 return tolist(t.Null, items) 123 return tolist(last or t.Null, items)
124end 124end
125 125
126function t.isList (x) 126function t.isList (x)