-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmctext.lua
More file actions
84 lines (46 loc) · 1.47 KB
/
mctext.lua
File metadata and controls
84 lines (46 loc) · 1.47 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
local common = require("common")
local lexer = require("lexer")
local parser = require("parser")
local codegen = require("codegen")
local mctext = { CompileAccept = {}, CompileFail = {}, CompileOptions = {} }
function mctext.CompileAccept.new(result, warnings)
local self = setmetatable({}, { __index = mctext.CompileAccept })
self.kind = "CompileAccept"
self.result = result
self.warnings = warnings
return self
end
function mctext.CompileFail.new(errors)
local self = setmetatable({}, { __index = mctext.CompileFail })
self.kind = "CompileFail"
self.errors = errors
return self
end
function mctext.CompileOptions.new(useStrictMode)
local self = setmetatable({}, { __index = mctext.CompileOptions })
self.useStrictMode = useStrictMode
return self
end
function mctext.compile(text, options)
local warnings = {}
local function addWarnings(newWarnings)
for _, warning in ipairs(newWarnings) do
warnings[#warnings + 1] = warning
end
end
local tokens = lexer.tokenize(text)
addWarnings(tokens.warnings)
local ast = parser.parse(tokens.result)
if options.useStrictMode then
addWarnings(ast.strictProblems)
end
addWarnings(ast.warnings)
if options.useStrictMode then
if #warnings > 0 then
return mctext.CompileFail.new(warnings)
end
end
local result = codegen.generate(ast.result)
return mctext.CompileAccept.new(result, warnings)
end
return mctext