about summary refs log tree commit diff stats
path: root/lua/chupacabra/repl.lua
blob: bed67c27bf68406025eb94aa0a2e2b618c489255 (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
local chupacabra = require("chupacabra")

-- not a great, but a passable repl
local function table_to_string(t)
    local str = "{"
    for i, v in ipairs(t) do
        if i > 1 then
            str = str .. ", "
        end
        str = str .. tostring(v)
    end
    str = str .. "}"
    return str
end

local stack = {}

local function scratch(input)
    local output = chupacabra.run(input, {})
    table.insert(stack, {input = input, output = output})
end

local function print_stack()
    for i, entry in ipairs(stack) do
        print("   input: " .. entry.input)
        if type(entry.output) == "table" then
            print("  output: " .. table_to_string(entry.output))
        else
            print("  output: " .. tostring(entry.output))
        end
    end
end

while true do
    io.write("> ")
    local input = io.read()
    if input == "bye" then
        break
    end
    scratch(input)
    print_stack()
    stack = {}
end