about summary refs log tree commit diff stats
path: root/repl.lua
blob: a89fd2c65a0de0e82027b20d44b835ef2ada845c (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
--- lam.repl

local repl = {}
local eval = require "eval"
local read = require "read"
local util = require "util"
table.unpack = table.unpack or unpack

function schemestr(x)
	if type(x) == "table" then
		local ts = "(" .. schemestr(util.pop(x))
		for i,v in ipairs(x) do
			ts = string.format("%s %s", ts, schemestr(v))
		end
		ts = ts .. ")"
		return ts
	elseif x == true then
		return "#t"
	elseif x == false then
		return "#f"
	else
		return tostring(x)
	end
end

function repl.repl (prompt)
	prompt = prompt or "lam> "
	repeat
		io.write(prompt)
		io.output():flush()
		input = io.read()
		if input == ",q" or input == ",quit" then
			break
		else
			val = eval(read(input))
			if val then print(schemestr(val)) end
		end
	until false
end

---
return repl