From bebcbdd000b55fa86df4c3c30b6ec7c85ff2dfab Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 5 Apr 2024 13:38:22 -0500 Subject: Fix reading from files ... i'm PRETTY sure --- read.lua | 56 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'read.lua') diff --git a/read.lua b/read.lua index 4fb9b70..35f5b57 100644 --- a/read.lua +++ b/read.lua @@ -7,10 +7,31 @@ local pop = require("util").pop -- TODO: -- - string reading --- - # syntax --- - comments -- - probably more +m.eof = setmetatable({}, { + __type = "EOF", + __tostring = function () return "#" end, +}) + +local function inport_next_token (port) + local tok, toktype + while true do + if #port.line == 0 then + if port.file then + local ln = port.file:read() + if ln == nil then return m.eof end + port.line = m.tochars(ln) + else + return nil + end + end + tok, toktype, port.line = m.scan(port.line)() + port.line = port.line or {} + if tok ~= nil then return tok, toktype end + end +end + function m.inport (source, kind) -- KIND can be one of "file", "string"; defaults to "file" -- SOURCE is the name of the file or the string to read, or nil; if nil, @@ -18,9 +39,9 @@ function m.inport (source, kind) local f, l local k = kind or "file" if source then - if kind == "file" then + if k == "file" then f = io.open(source, "r") - elseif kind == "string" then + elseif k == "string" then l = m.tochars(source) end else @@ -29,25 +50,10 @@ function m.inport (source, kind) end local t = { file = f, + filename = source, + kind = kind, line = l or {}, - next_token = - function (self) - local tok, toktype - while true do - if #self.line == 0 and self.file then - self.line = m.tochars( - self.file:read("*l")) - end - if not self.line or #self.line == 0 then - return nil - end - tok, toktype, self.line = - m.scan(self.line)() - if tok ~= nil then - return tok, toktype - end - end - end, + next_token = inport_next_token, } if t.file then t.close = function (self) self.file:close() end; end local mt = { @@ -232,12 +238,12 @@ m.readmacros = { table.insert(Q, m.read(port)) return t.list(Q) end, - comment = idf(nil), + comment = idf(nil) } function m.read (port) local function read_ahead (tok, toktype) - if tok == nil then error("Unexpected EOF") end + if tok == m.eof then error("Unexpected EOF") end if toktype == "open" then local L = {} while true do @@ -262,7 +268,7 @@ function m.read (port) end -- body of read local tok1, toktype1 = port:next_token() - if tok1 == nil then return nil + if tok1 == m.eof then return m.eof else return read_ahead(tok1, toktype1) end end -- cgit 1.4.1-21-gabe81