From bbaff0e0c204c2fab216f6501dc8c11b4425b4bc Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 4 Mar 2024 21:01:27 -0600 Subject: Ugghhhh totally not working --- read.lua | 93 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 39 deletions(-) (limited to 'read.lua') diff --git a/read.lua b/read.lua index c89261c..00a2d2a 100644 --- a/read.lua +++ b/read.lua @@ -2,6 +2,7 @@ local read = {} local utf8 = require "utf8" +local types = require "types" table.unpack = table.unpack or unpack local string_to_table = @@ -13,7 +14,36 @@ local string_to_table = return tbl end -local bslash = { -- backslash characters +local consume_whitespace = + function (chars) + local s = {"\\"} -- accumulator for if there's no \n + while chars[1]:match("[ \t]") do + table.insert(s, util.pop(chars)) + end + if chars[1] ~= "\n" then + table.insert(s, chars[1]) + return table.concat(s), chars + end + while chars[1]:match("%s") do + util.pop(chars) + end + return chars[1], chars + end + +local consume_hexvalue = + function (chars) + local u8ch = {} + repeat + local c = util.pop(chars) + table.insert(u8ch,c) + until c == ";" + table.remove(u8ch) -- remove semicolon + return + utf8.char(tonumber(table.concat(u8ch), 16)), + chars + end + +local string_bslash = { -- backslash characters a = "\a", b = "\b", t = "\t", @@ -22,23 +52,12 @@ local bslash = { -- backslash characters ["\""] = "\"", ["\\"] = "\\", ["|"] = "|", - - -- TODO: whitespace - -- \* * : - -- nothing - - x = -- \x; : specified character - function (chars) - local u8ch = {} - repeat - local c = util.pop(chars) - table.insert(u8ch,c) - until c == ";" - table.remove(u8ch) -- remove semicolon - return - utf8.char(tonumber(table.concat(u8ch), 16)), - chars - end, + -- \* * : nothing + [" "] = consume_whitespace, + ["\t"] = consuem_whitespace, + ["\n"] = consume_whitespace, + -- \x; : specified character + x = consume_hexvalue, } local consume_string = @@ -47,17 +66,22 @@ local consume_string = repeat local c = util.pop(chars) if c == "\\" then - c = util.pop(chars) - if bslash[c] then - if type(bslash[c]) == "function" then - c, chars = bslash[c](chars) + c = chars[1] + if string_bslash[c] then + if type(string_bslash[c]) == "function" + then + c, chars = + string_bslash[c](chars) table.insert(str, c) else - table.insert(str, bslash[c]) + table.insert( + str, + string_bslash[c]) end else table.insert(str, "\\"..c) end + util.pop(chars) elseif c == "\"" then break else @@ -114,18 +138,9 @@ read.tokenize = end read.tokentable = { - string = - function (tok) - return tok.value - end, - number = - function (tok) - return tonumber(tok.value) - end, - symbol = - function (tok) -- TODO need to return a Symbol from types... - return tok.value - end, + string = function (tok) return types.String(tok.value) end, + number = function (tok) return types.Number(tok.value) end, + symbol = function (tok) return types.Symbol(tok.value) end, } read.parse = @@ -138,7 +153,7 @@ read.parse = table.insert(L, read.parse(tokens)) end util.pop(tokens) -- remove ")" - return L + return types.List(table.unpack(L)) elseif tok.value == ")" then error("Unexpected ')'") elseif read.tokentable[tok.type] then @@ -152,7 +167,7 @@ read.read = function (program) return read.parse(read.tokenize(program)) end --- return setmetatable(read, { __call = - function(_, program) - return read.read(program) - end, + function(_, program) + return read.read(program) + end, }) -- cgit 1.4.1-21-gabe81