summary refs log tree commit diff stats
path: root/dirtycratera.lua
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2019-07-30 21:12:16 -0300
committerSoniEx2 <endermoneymod@gmail.com>2019-07-30 21:12:16 -0300
commit4b365cdab1296bc88509c6f8610318adefb0ef0e (patch)
tree528741562b21cc0d81d7c71aca3eaee5e8433934 /dirtycratera.lua
parentaf3acfbb80bca7447af9fe0d4a34cf860163b218 (diff)
It... kinda works?
Diffstat (limited to 'dirtycratera.lua')
-rw-r--r--dirtycratera.lua84
1 files changed, 84 insertions, 0 deletions
diff --git a/dirtycratera.lua b/dirtycratera.lua
new file mode 100644
index 0000000..1a45709
--- /dev/null
+++ b/dirtycratera.lua
@@ -0,0 +1,84 @@
+--[[
+    cratera.lua - pure-Lua Cratera-to-Lua transpiler
+    Copyright (C) 2019  Soni L.
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+--]]
+
+-- This code is highly experimental and not very good
+
+local parser = require "parser"
+local luatokens = require "luatokens"
+local compiler = require "dirtycompiler"
+
+local LUA_SIGNATURE = string.dump(function() end):sub(1,1)
+
+local function cratera_load(reader, ...)
+    local chunkname, mode, env = ...
+    if type(reader) == "string" and reader:sub(1,1) == LUA_SIGNATURE then
+        -- bytecode
+        return (loadstring or load)(reader, ...)
+    end
+    local f, s, i = parser.stream(luatokens.defs, reader)
+    if type(s[parser.DATA]) == "string" and s[parser.DATA]:sub(1,1) == LUA_SIGNATURE then
+        -- bytecode
+        local function fn()
+            fn = reader
+            return s[parser.DATA]
+        end
+        return (loadstring or load)(function() return fn() end, ...)
+    end
+    local nl = 1
+    local otherstate = {source=chunkname} -- FIXME
+    local f, s, i = parser.stream(compiler.defs, function()
+        local tokens
+        repeat
+            local pos, state, transemsg, etoken, estate = f(s, i)
+            otherstate.linenumber = state.line
+            i = pos
+            if not i then return nil end
+            if not state then error(transemsg) end
+            tokens = {}
+            for i,v in ipairs(state) do
+                state[i] = nil
+                tokens[i] = v
+            end
+        until #tokens > 0 or not transemsg
+        return tokens
+    end, otherstate)
+    local function fn()
+        function fn()
+            local tokens
+            repeat
+                local pos, state, transemsg, etoken, estate, est = f(s, i)
+                i = pos
+                if not i then return nil end
+                if not state then error(transemsg .. " " .. tostring(etoken)) end
+                tokens = {""}
+                for i,v in ipairs(state) do
+                    state[i] = nil
+                    tokens[i+1] = v
+                end
+            until #tokens > 1 or not transemsg
+            return table.concat(tokens, " ")
+        end
+        local ret = fn()
+        return string.sub(ret, 2)
+    end
+    return load(function()
+        return fn()
+    end)
+end
+
+return {load = cratera_load}