-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoblock.lua
More file actions
executable file
·110 lines (95 loc) · 2.86 KB
/
oblock.lua
File metadata and controls
executable file
·110 lines (95 loc) · 2.86 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env lua
package.path = package.path..";lib/?.lua"
local Lexer = require "oblock.Lexer"
local Parser = require "oblock.Parser"
local Interpreter = require "oblock.Interpreter"
local stdlib = require "oblock.stdlib"
local has_tc, tc = pcall(require, "terminalcolours")
local pretty = require("pretty").new { deep = 2, multiline = true}
local version = "0.5"
local args = {...}
local function hasArg(name)
for _, arg in ipairs(args) do
if arg == name then return true end
end
return false
end
local path = args[1]
local doParse = not hasArg("--lex")
local doInterpret = doParse and not hasArg("--parse")
local isInteractive = hasArg("--interactive") or hasArg("-i")
local file = (path and not isInteractive) and io.open(path) or io.stdin
local filename = (file == io.stdin) and "stdin" or string.match(path or "", "/([^/]+)$") or path
if file == io.stdin then
if has_tc then
local function withGradient(text, gradient)
local str = {}
for _, code in utf8.codes(text) do
table.insert(str, gradient[#str / 2 + 1])
table.insert(str, utf8.char(code))
end
return table.concat(str, "")..tc.colour(tc.reset)
end
print(" "..withGradient(" ", {
tc.colour(tc.bg.rgb(42, 79, 177)),
tc.colour(tc.bg.rgb(47, 72, 168)),
tc.colour(tc.bg.rgb(50, 65, 158)),
tc.colour(tc.bg.rgb(53, 57, 149)),
tc.colour(tc.bg.rgb(54, 50, 140)),
tc.colour(tc.bg.rgb(55, 43, 131)),
tc.colour(tc.bg.rgb(55, 35, 121)),
tc.colour(tc.bg.rgb(55, 27, 112)),
})..tc.parse(" {reset}{bg.grey} "..version.." {reset}"))
print(withGradient("░░░ oblock ", {
tc.colour(tc.fg.rgb(0, 102, 204)),
tc.colour(tc.fg.rgb(23, 94, 195)),
tc.colour(tc.fg.rgb(34, 87, 186)),
tc.colour(tc.reset, tc.bg.rgb(42, 79, 177)),
tc.colour(tc.bg.rgb(47, 72, 168)),
tc.colour(tc.bg.rgb(50, 65, 158)),
tc.colour(tc.bg.rgb(53, 57, 149)),
tc.colour(tc.bg.rgb(54, 50, 140)),
tc.colour(tc.bg.rgb(55, 43, 131)),
tc.colour(tc.bg.rgb(55, 35, 121)),
tc.colour(tc.bg.rgb(55, 27, 112)),
}))
else
print("oblock "..version)
end
end
local interpreter = Interpreter()
if isInteractive then
interpreter.environment.block:set("_G", interpreter.environment.block)
end
function perform(content)
-- Lex
local tokens = Lexer(content, filename):lex()
if not tokens then return end
if not doParse then
print(pretty(tokens))
return
end
-- Parse
local program = Parser(tokens, filename):parse(isInteractive)
if not program then return end
if not doInterpret then
print(program)
print( (program:debug()) )
return
end
-- Interpret
local values = {interpreter:interpret(program)}
if #values > 0 then print(table.unpack(values)) end
end
if isInteractive then
while true do
io.write("> ")
local content = file:read("l")
if content == nil then print() break end
perform(content)
end
else
local content = file:read("a")
perform(content)
end
file:close()