summary refs log tree commit diff stats
path: root/cratera.lua
blob: fabb371bc9e5029a2495058a3825c1e12ea8892b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
--[[
    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 "compiler"

local CRATERA_SEED = nil -- TODO

local function cratera_load(reader)
    local f, s, i = parser.stream(luatokens.defs, reader)
    local nl = 1
    local otherstate = {}
    local f, s, i = parser.stream(compiler.defs, function()
        local tokens
        repeat
            local pos, state, transemsg, etoken, estate = f(s, i)
            otherstate.line = 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, CRATERA_SEED = CRATERA_SEED}