-
Notifications
You must be signed in to change notification settings - Fork 1
/
units.lua
91 lines (73 loc) · 2.34 KB
/
units.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function prereq_handler(state, params)
-- all of our arguments should be words
for i, v in ipairs(params) do
if (v.type ~= "WORD") then
error("error: .PREREQ only accepts words as parameters.")
end
end
if (#params == 0) then
error("error: .PREREQ must have at least one argument.")
end
-- create prereqs list
local prereqs = ""
for i, v in ipairs(params) do
prereqs = prereqs .. ":" .. v.value
end
prereqs = prereqs:sub(2)
-- output a symbol for the prerequisite.
state:add_symbol("unit:prerequisite:" .. prereqs);
end
local current_unit = nil
function unit_handler(state, params)
-- we should have a single argument that is a word
if (#params ~= 1 or params[1].type ~= "WORD") then
error("error: unit name must be a single word")
end
-- make sure we're not inside another unit
if (current_unit ~= nil) then
error("error: encountered another .UNIT before .ENDUNIT")
end
-- define the unit name
local name = params[1].value
-- output a symbol that effectively terminates
-- any prerequisites that have been defined before
-- here
state:add_symbol("unit:jump")
-- output a jump that will skip over the unit
-- during normal execution.
state:print_line("SET PC, __unit_test_end_" .. name)
-- output a symbol for the unit.
state:add_symbol("unit:definition:" .. name)
-- set the current unit
current_unit = name
end
function endunit_handler(state, params)
-- we should have no arguments
if (#params ~= 0) then
error("error: .ENDUNIT accepts no parameters")
end
-- make sure we're actually in a unit.
if (current_unit == nil) then
error("error: encountered .ENDUNIT before .UNIT")
end
-- print the label to skip over the unit test.
state:print_line(":__unit_test_end_" .. current_unit)
-- output a termination symbol so that the debugger
-- knows when the unit ends.
state:add_symbol("unit:terminate")
-- reset current_unit variable
current_unit = nil
end
function setup()
-- perform setup
add_preprocessor_directive("PREREQ", prereq_handler)
add_preprocessor_directive("UNIT", unit_handler)
add_preprocessor_directive("ENDUNIT", endunit_handler)
end
MODULE = {
Type = "Preprocessor",
Name = "Unit Testing",
Version = "1.0",
SDescription = "The .UNIT and associated directives",
URL = "http://dcputoolcha.in/docs/modules/list/units.html"
};