aboutsummaryrefslogtreecommitdiff
path: root/lua/step1_read_print.lua
blob: 46d71f5cacd2a582835538a10f273a6f2888eb41 (plain)
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
#!/usr/bin/env lua

local readline = require('readline')
local utils = require('utils')
local reader = require('reader')
local printer = require('printer')

-- read
function READ(str)
    return reader.read_str(str)
end

-- eval
function EVAL(ast, env)
    return ast
end

-- print
function PRINT(exp)
    return printer._pr_str(exp, true)
end

-- repl
function rep(str)
    return PRINT(EVAL(READ(str),""))
end

if #arg > 0 and arg[1] == "--raw" then
    readline.raw = true
end

while true do
    line = readline.readline("user> ")
    if not line then break end
    xpcall(function()
        print(rep(line))
    end, function(exc)
        if exc then
            if types._malexception_Q(exc) then
                exc = printer._pr_str(exc.val, true)
            end
            print("Error: " .. exc)
            print(debug.traceback())
        end
    end)
end