-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluasm.lua
More file actions
494 lines (406 loc) · 15.2 KB
/
luasm.lua
File metadata and controls
494 lines (406 loc) · 15.2 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
--[[
_ _ ____ __ __
| | _ _ / \ / ___|| \/ |
| | | | | | / _ \ \___ \| |\/| |
| |__| |_| |/ ___ \ ___) | | | |
|_____\__,_/_/ \_\____/|_| |_|
A library to parse and execute custom ASM.
--]]
--------------------------------------------------------------------
--- Helper functions
--------------------------------------------------------------------
--- Trim leading and trailing whitespace from a string.
--- @param s string The string to trim.
--- @return string The trimmed string.
local function trim(s)
return s:match('^%s*(.-)%s*$')
end
--------------------------------------------------------------------
--- LuASM object (public API entry point)
--------------------------------------------------------------------
local LuASM = {}
--- Current version of the library.
LuASM.version = "0.0.1"
--- Creates a new LuASM runner with the specific instructions and settings
--- @param instructions table List of instruction objects (created with `LuASM.instruction`).
--- @param settings table Optional table that overrides the default parsing settings.
--- @return table A new LuASM instance.
function LuASM:new(instructions, settings)
-- Default settings
setmetatable(settings, { __index = {
separator = "[^,%s]+",
label = "^([%a]+):%s*(.*)",
comment = "[;#].*$",
syntax = {
imm = "^[%d]+",
reg = "^%a[%w]*",
label = "^[%a]+"
}
}})
local obj = {}
setmetatable(obj, self)
self.__index = self
obj.instructions = instructions
obj.settings = settings
return obj
end
--------------------------------------------------------------------
--- Instructions
--------------------------------------------------------------------
local instruction = {}
--- Default executor that raises an error when an instruction has no implementation.
--- @param _instruction any Ignored (placeholder for the instruction object).
--- @param _interpreter any Ignored (placeholder for the interpreter instance).
--- @return nil
local function invalidExecutor(_instruction, _interpreter)
error("The executor is not implemented and such the instruction cannot be executed!")
return nil
end
--- Creates an instruction that is being used for parsing the input
---
--- Example:
--- ```
--- LuASM:instruction("jmp", {"label"}, {})
--- ```
--- @param name string Mnemonic of the instruction (e.g. `"jmp"`).
--- @param structure table Ordered list of operand types (e.g. `{"label"}`).
--- @param settings table Optional per‑instruction settings (e.g. a custom executor).
--- @return table An instruction object.
function LuASM.instruction(name, structure, settings)
local obj = {}
obj.name = name
obj.structure = structure
-- Default settings
setmetatable(settings, { __index = {
executor = invalidExecutor -- every instruction must provide an executor to run
}})
obj.settings = settings
setmetatable(obj, instruction)
instruction.__index = instruction
return obj
end
--------------------------------------------------------------------
--- Parser
--------------------------------------------------------------------
local Tokenizer = {}
--- Abstract method that must be overridden by a concrete tokenizer.
function Tokenizer.get_next_line()
error("This function has to be implemented!")
return false
end
--- Creates a new tokenizer without a specific implementation.
--- @return table A tokenizer instance (needs a concrete `get_next_line` implementation).
function Tokenizer:new()
local obj = {}
setmetatable(obj, self)
self.__index = self
return obj
end
--- Reads in a file and returns a tokenizer for that file.
--- @param path string Path to the file to read.
--- @return table|nil Tokenizer instance or `nil` if the file cannot be opened.
function LuASM.file_tokenizer(path)
local file = io.open(path, "r")
if file == nil then
return nil
end
local tokenizer = LuASM.string_tokenizer(file:read("*a"))
file:close()
return tokenizer
end
--- Reads in a string of the asm and returns a tokenizer for that file.
--- @param input string The complete ASM source as a string.
--- @return table Tokenizer instance.
function LuASM.string_tokenizer(input)
local tokenizer = Tokenizer:new()
tokenizer.input = input
tokenizer.cursor = 1 -- byte index inside `input`
tokenizer.current_line = 1 -- line counter (1‑based)
-- Concrete implementation of `get_next_line` for a string source.
tokenizer.get_next_line = function()
if #tokenizer.input <= tokenizer.cursor then
return nil -- EOF
end
local _, endIndex = string.find(tokenizer.input, "[^\r\n]+", tokenizer.cursor)
local line = trim(string.sub(tokenizer.input, tokenizer.cursor, endIndex))
tokenizer.cursor = endIndex + 1
tokenizer.current_line = tokenizer.current_line + 1
return line
end
return tokenizer
end
--- Parses the instruction and returns an object with the structure:
---
--- `{ op = opcode, args = args, line = current line }`
---
--- If the parsing has errored out, it returns a string with the error message.
--- @param elements table Token list where `elements[1]` is the mnemonic.
--- @param luasm table The LuASM instance (provides settings, etc.).
--- @return table|string On success a table `{op, args, line, run}`; on failure a string error message.
function instruction:parse(elements, luasm)
-- `elements[1]` is the mnemonic, the rest are raw operands
local opcode = self.name
local expected = self.structure -- e.g. {"imm","reg"}
if #elements - 1 ~= #expected then
local err = string.format(
"Wrong number of operands for %s (expected %d, got %d)",
opcode, #expected, #elements - 1)
return err
end
local args = {}
for i = 2, #elements do
local pattern = luasm.settings.syntax[expected[i - 1]]
if pattern == nil then
error("The pattern with the name of '" .. expected[i - 1] .. "' does not exist.", 2)
return "Pattern not found"
end
local arg = elements[i]:match(pattern)
if arg == nil then
local err = string.format(
"Could not match argument '%s' (expected %s)",
elements[i], expected[i - 1])
return err
end
args[i - 1] = arg
end
return {
op = opcode,
args = args,
line = luasm.current_line,
run = self.settings.executor -- executor function (may be the default error)
}
end
--- Parses the inputted source and returns a list of instructions.
--- @param tokenizer table Tokenizer instance that yields trimmed source lines.
--- @return table parsed_data Table with fields `instructions`, `labels`, `parsed_lines`.
--- @return table|nil error `nil` on success, otherwise a table `{errors = {...}, line = N}`.
function LuASM:parse(tokenizer)
local parse_data = {
instructions = {},
labels = {},
parsed_lines = 0
}
local token
repeat
token = tokenizer:get_next_line()
parse_data.parsed_lines = parse_data.parsed_lines + 1
if token ~= nil then
-- Remove comments
if self.settings.comment ~= nil then
token = token:gsub(self.settings.comment, "")
end
--[[
This is very basic label processing as labels could be
nested and there could be priorities assigned with labels.
But here all the labels are just a simple reference to a line.
]]
-- -------------------------------------------------
-- LABEL PROCESSING (very basic, one‑label per line)
-- -------------------------------------------------
if self.settings.label ~= nil then
local label, rest = token:match(self.settings.label)
if label ~= nil then
-- Detect duplicate label definitions.
if parse_data.labels[label] ~= nil then
return parse_data, {
errors = { "The label '" .. label .. "' was found twice." },
line = parse_data.parsed_lines
}
end
parse_data.labels[label] = {
name = label,
location = parse_data.parsed_lines
}
token = trim(rest)
end
end
local elements = {}
string.gsub(token, self.settings.separator,
function(value) elements[#elements + 1] = value end)
if #elements == 0 then
goto continue -- empty line (or comment)
end
local errors = {}
for _, instr in ipairs(self.instructions) do
if instr.name ~= elements[1] then
goto inline_continue
end
local result = instr:parse(elements, self)
if type(result) == "table" then
parse_data.instructions[#parse_data.instructions + 1] = result
goto continue -- go to the outer `continue` label
else
errors[#errors + 1] = result
end
::inline_continue::
end
-------------------------------------------------
-- NO INSTRUCTION MATCHED
-------------------------------------------------
if #errors == 0 then
-- No instruction with that mnemonic exists.
return parse_data, {
errors = { "There is no instruction with the name '" .. elements[1] .. "'" },
line = parse_data.parsed_lines
}
else
-- At least one instruction matched the name but rejected the operands.
return parse_data, {
errors = errors,
line = parse_data.parsed_lines
}
end
::continue::
end
until token == nil -- EOF
return parse_data, nil
end
--------------------------------------------------------------------
--- Interpreter
--------------------------------------------------------------------
--- Simple LIFO stack implementation used by the interpreter.
local Stack = {}
--- Puts the given value onto the stack.
--- @param value any The value to store.
function Stack:push(value)
self._content[self.size + 1] = value
self.size = self.size + 1
end
--- Removes the top value of the stack and returns it.
--- If there is no value on the stack, it returns `nil`.
--- @return any|nil The popped value, or `nil` if the stack is empty.
function Stack:pop()
if self.size == 0 then
return nil
end
self.size = self.size - 1
return self._content[self.size + 1]
end
--- Returns the top value on the stack, but does not remove it.
--- If there is no value on the stack, it returns `nil`.
--- @return any|nil The top value, or `nil` if the stack is empty.
function Stack:peek()
if self.size == 0 then
return nil
end
return self._content[self.size]
end
--- Gives the value on the stack with the index.
---
--- Given the scenario:
--- ```
--- stack:push("Hola")
--- stack:push("Hi")
--- stack:push("Hallo")
--- stack:push("Bonjour")
---
--- print(stack:get(1)) -- Prints "Hola"
--- print(stack:get(3)) -- Prints "Hallo"
--- print(stack:get(2)) -- Prints "Hi"
--- print(stack:get(4)) -- Prints "Bonjour"
--- ```
---
--- If the index is invalid, it will returns a nil:
--- ```
--- stack -- Currently has no elements
--- if stack:get(1) == nil then
--- print("Nothing here") -- Prints
--- end
---
--- stack:get(-10) -- Also returns nil
--- ```
---
--- The index is **1‑based** (bottom of the stack = 1).
--- @param index number The index to fetch.
--- @return any|nil The element at `index`, or `nil` if out of bounds.
function Stack:get(index)
if index < 0 or index > self.size then
return nil
end
return self._content[index]
end
--- Returns a stack datastructure.
--- A stack is a LIFO (Last‑In‑First‑Out) structure where the last element added
--- is the first one that can be removed.
--- @return table A fresh stack (initially empty).
function LuASM.stack()
local obj = {
_content = {},
size = 0,
}
setmetatable(obj, Stack)
Stack.__index = Stack
return obj
end
--------------------------------------------------------------------
--- Interpreter
--------------------------------------------------------------------
local interpreter = {}
--- Checks whether a label exists in the parsed data.
--- @param label string The label name to test.
--- @return boolean True if the label is defined.
function interpreter:label_exists(label)
return self.data.labels[label] ~= nil
end
--- Verifies that a program counter is within the bounds of the parsed program.
--- @param index number The line number (1‑based) to test.
--- @return boolean True if the index is valid.
function interpreter:in_bounds(index)
return not (index < 0 or index > self.data.parsed_lines)
end
--- Jumps to a specific line number or label.
--- @param index number|string The target line number or label name.
--- @return string|nil Error message on failure, `nil` on success.
function interpreter:jump(index)
if type(index) == "string" then -- Jump to label
index = self.data.labels[index]
if index == nil then
return "This label does not exist"
end
end
if type(index) ~= "number" then
error("The index must be a number or a string", 2)
end
if not self:in_bounds(index) then
error("This position does not exist.")
end
self.ic = index
return nil
end
--- Executes the next instruction in the program.
--- The function advances `self.ip` until it finds a non‑nil instruction,
--- then calls its `run` method (the executor supplied when the instruction
--- was defined).
--- @return boolean True if the instruction executed correctly
--- False if there is no instruction or the instruction errored out
function interpreter:next_instruction()
::start::
if self.data.parsed_lines < self.ip then
return false
end
local line = self.data.instructions[self.ip]
self.ip = self.ip + 1
if line == nil then
goto start
end
local result = line:run(self)
return result == nil or result
end
--- Creates a new interpreter instance.
--- @param data table Parsed program data (result of `LuASM:parse`).
--- @param memory table Optional memory layout (stack + heap). If omitted a default is created.
--- @return table Interpreter object ready to run.
function LuASM:interpreter(data, memory)
local obj = {}
setmetatable(obj, interpreter)
interpreter.__index = interpreter
obj.luasm = self
obj.ip = 1 -- instruction pointer (1‑based)
obj.data = data
obj.memory = memory or {
stack = LuASM.stack(),
heap = {}
}
return obj
end
return LuASM