From 4dde2320effedfc1baac0a0b2011ab2ddbc20c74 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 20 Mar 2024 21:55:09 -0500 Subject: Support dotted lists in reader --- read.lua | 17 +++++++++++++---- type.lua | 4 ++-- 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 = { ["'"] = function(chars) return pop(chars), "quote", chars end, ["`"] = function(chars) return pop(chars), "quote", chars end, [","] = function(chars) return pop(chars), "quote", chars end, + ["."] = function(chars) return pop(chars), "dot", chars end, ["\""] = consume_string, [";"] = consume_comment, -- ["#"] = @@ -161,12 +162,20 @@ end read.readmacros = { open = function (token, tokens) - local L = {} + local L, lt = {}, nil while tokens[1].type ~= "close" do - table.insert(L, read.parse(tokens)) + local nt = read.parse(tokens) + -- this isn't .. my /favorite/ implementation, + -- but it works + if nt == "." then + lt = read.parse(tokens) + break + else + table.insert(L, nt) + end end - pop(tokens) -- remove final ")" - return type.List(L) + pop(tokens) -- remove final ")" + return type.List(L, lt) end, close = 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) end -- Lists are chained Conses ending in Null -function t.List (items) +function t.List (items, last) local function tolist (base, items) if #items == 0 then return base end return tolist(t.Cons(table.remove(items), base), items) end - return tolist(t.Null, items) + return tolist(last or t.Null, items) end function t.isList (x) -- cgit 1.4.1-21-gabe81