-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
67 lines (54 loc) · 1.62 KB
/
main.lua
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
--// Checking if you are running a file or in the shell
local shell = arg[1] == nil
--// Require all libraries
local L = require("libs.lex")
local P = require("libs.parse")
local I = require("libs.inter")
require('libs.rebind')
--// Create the global scope
local s_G = require("libs.scope").new()
--// Run a section of code
local function run(code)
--// If the code is to quit, return the exit code
if code == "q" then return -1 end
--// Get all the tokens from the code
local Lexer = L.new(code)
local tokens, err = Lexer:Tokenize()
--// If there is an error, print it and break
if err then
print(err:GenerateStackTrace())
return -1
end
--// Parse the tokens into the AST
local Parser = P.new(tokens)
local AST, parseErr = Parser:Parse()
--// If there is an error, print it and break
if parseErr then
print(parseErr:GenerateStackTrace())
return -1
end
--// Interpret the AST with the global scope
local Interpreter = I.new(AST, s_G)
local res = Interpreter:Interpret()
--// Print out the result
print(res)
end
--// If it's running in a shell
if shell then
--// Get input forever
while true do
--// Get the command input
io.write("$ ")
local line = io.read()
--// If the line through an error, break out
if run(line) == -1 then break end
end
--// If the user ran a file
else
--// Get the code from the file
local file = io.open(arg[1], "r")
if not file then error("Couldn't open file " .. arg[1]) end
local data = file:read("*a")
--// Run the file
run(data)
end