teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.14k stars 108 forks source link

Suggestions for the structure of the project moving forward #187

Open euclidianAce opened 4 years ago

euclidianAce commented 4 years ago

I thought of some of these ideas while working on the build options (#177) and would like to know your opinions. These are definitely more abstract and long-term oriented goals, but I think they're worth discussing :)

Some of these came up when thinking about use cases for Teal and one that came to mind were the Minecraft mods ComputerCraft and OpenComputers, which embed LuaJ and Lua respectively. (I think OpenComputers falls back to LuaJ if it can't use a regular interpreter). On these restricted platforms, people write suprisingly sizable projects2 but wouldn't have access to modules like lfs due to the filesystem being handled differently internally.

I don't know how much other people would want this or if there are any glaring flaws in my logic that I'm missing, but I'd definitely have a usecase for it and would definitely be willing to write the prs.

2: I'd say OpenOS, the default OS for OpenComputers is quite impressive, and when the stars align it can run on actual hardware with a modified Linux kernel. 1: ~20 lines for a decently functional bare-bones compiler

#!/usr/bin/env lua
local input, output = ...
assert(input)
output = output or input:gsub("%.tl$", ".lua")
local tl = require("tl")
local res = assert(tl.process(input))
local function report_errors(category, errors)
    if #errors == 0 then return end
    for _, e in ipairs(errors) do
        io.stderr:write(category, ":", e.filename, ":", e.y, ":", e.x, ": ", (e.msg or ""), "\n")
    end
    os.exit(1)
end
report_errors("syntax", res.syntax_errors)
report_errors("unknowns", res.unknowns)
report_errors("type", res.type_errors)
local ofd = assert(io.open(output, "w"))
ofd:write(tl.pretty_print_ast(res.ast) .. "\n")
ofd:close()
print("Wrote: " .. output)
hishamhm commented 4 years ago

Further separate the compiler (tl.tl) from the cli (tl), as in make them separate modules

Agree! I've been thinking about this. The names are something to figure out later, but yes, I'd like to have exactly what you described: a no-dependencies pure Lua compiler that can be embedded into any kind of (compatible) environment, and a convenient top-level CLI package with all the bells and whistles.