-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdo.lua
More file actions
175 lines (169 loc) · 4.19 KB
/
do.lua
File metadata and controls
175 lines (169 loc) · 4.19 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
-- command line interfact to act api
-- you will need the act script
local historyFile = "history"
local macroFile = "macro"
function flatten(list)
if type(list) ~= "table" then return {list} end
local flat_list = {}
for _, elem in ipairs(list) do
for _, val in ipairs(flatten(elem)) do
flat_list[#flat_list + 1] = val
end
end
return flat_list
end
os.loadAPI("apis/act")
local history = act.loadFile("history") or {}
local macro = act.loadFile("macro") or {}
local tArgs = { ... }
if #tArgs == 0 then
print( "Usage: do <list of commands>" )
print( " do history" )
print( " do save <name>" )
print( " do <name>" )
print( "" )
print( "Saved Commands:" )
for key, value in pairs(macro) do
print(" "..key.." : "..act.serialize(value))
end
return
end
local cmd = tArgs[1]
if cmd == "history" then
if #history > 0 then
for i, v in ipairs(history) do
if v:find("\n") then
print("[[\n"..v.."]]")
else
print(v)
end
end
else
print("no history")
end
elseif cmd == "save" then
local macro_name = tArgs[2]
if macro_name ~= nil then
if macro[macro_name] == nil then
if #history > 0 then
macro[macro_name] = history[#history]
act.saveFile(macroFile, macro)
else
print("no history")
end
else
print("macro name is not unique)")
end
else
print("enter macro name (must be unique)")
end
elseif cmd == "macro" then
local macro_name = tArgs[2]
if macro_name ~= nil then
if macro[macro_name] == nil then
-- interactive mode
local more = ""
local lines = {}
while more ~= "]]" do
if more ~= "" then
-- macro mode commands
local ast = act.parse(more, "repeater")
if ast then
local srcTable = {"("}
local repeatlines = ast.repeatlines or 1
for i = 1, repeatlines do
table.insert(srcTable, 2, table.remove(lines))
end
more = more:gsub("^%s*%d*(.*)$", "%1")
table.insert(srcTable, more)
table.insert(lines, srcTable)
local src = table.concat(flatten(lines), "\n")
ast = act.parse(src)
if ast then
ast.count = ast.count - 1
act.interpret(ast)
end
else
local ast, error = act.parse(more)
if ast then
table.insert(lines, more)
act.interpret(ast)
else
print(error)
end
end
end
more = io.read()
end
local plan = table.concat(flatten(lines), "\n").."\n"
macro[macro_name] = plan
act.saveFile(macroFile, macro)
else
print("macro name is not unique")
end
else
print("enter macro name (must be unique)")
end
elseif macro[cmd] then
local env = {num={}}
for i = 2, #tArgs do
local num = tonumber(tArgs[i])
if num ~= nil then
env.num[string.char(63+i)] = num
else
break
end
end
local plan = macro[cmd]
print(plan)
act.act(plan, env)
elseif fs.exists(cmd..".act") then
local f = fs.open(cmd..".act", "r")
local plan = f.readAll()
f.close()
print("using script '"..cmd..".act'")
local env = {num={}}
for i = 2, #tArgs do
local num = tonumber(tArgs[i])
if num ~= nil then
env.num[string.char(63+i)] = num
else
break
end
end
act.act(plan, env)
else
local plan
if cmd:sub(1,2) == "[[" then
table.remove(tArgs, 1)
if #cmd > 2 then
table.insert(tArgs, cmd:sub(3).."\n")
end
local more = ""
while not more:find("]]$") do
if more ~= "" then
table.insert(tArgs, more.."\n")
end
more = io.read()
end
table.insert(tArgs, more:sub(1, -3))
plan = table.concat(tArgs, "")
if plan:sub(#plan) ~= "\n" then
plan = plan .. "\n"
end
else
plan = table.concat(tArgs, " ")
end
local ast, error = act.parse(plan)
if ast then
if #history >= 100 then
table.remove(history, 1)
end
table.insert(history, plan)
act.saveFile(historyFile, history)
act.interpret(ast)
else
print("Error in act script:\n")
print(error)
end
end