.
This commit is contained in:
@@ -3,10 +3,11 @@ Progress of the Lua port:
|
||||
- [x] Assembler
|
||||
- [x] Basic VM execution
|
||||
- [x] Logic/arithmetic expressions
|
||||
- [ ] Functions
|
||||
- [ ] Variables
|
||||
- [ ] Local variables
|
||||
- [ ] Functions
|
||||
- [ ] Calling functions
|
||||
- [ ] Globals
|
||||
- [ ] Calling functions with parameters
|
||||
- [ ] Control flow
|
||||
- [ ] Lablels in Assembly
|
||||
- [ ] Recursion
|
||||
@@ -19,6 +20,7 @@ Progress of the Lua port:
|
||||
- [ ] Garbage collection
|
||||
- [ ] Metatables
|
||||
- [ ] Real
|
||||
- [ ] Globals
|
||||
- [ ] Error handling
|
||||
- [ ] Stack traces in case of errors
|
||||
- [ ] Closures/upvalues
|
||||
|
||||
@@ -181,4 +181,20 @@ do TEST("VM: logic/arithmetic")
|
||||
assert_eq(arith(20, 3, 'mod'):to_integer(-1), 2)
|
||||
end
|
||||
|
||||
do TEST("VM: local variables")
|
||||
local vm = VM:new():load(assemble([[
|
||||
.func 0
|
||||
pushv 2 ; local a, b
|
||||
pushi 3 ; a = 3
|
||||
set 0
|
||||
pushi 4 ; b = 4
|
||||
set 1
|
||||
dupv 0 ; return a
|
||||
ret
|
||||
]])):call(0)
|
||||
|
||||
assert_eq(vm:stack_sz(), 1)
|
||||
assert_eq(vm:to_integer(-1), 3)
|
||||
end
|
||||
|
||||
print('End.')
|
||||
@@ -21,6 +21,8 @@ function format_value(v)
|
||||
return '"' .. v.value .. '"'
|
||||
elseif v.type == 'function' then
|
||||
return '@' .. tostring(v.value)
|
||||
elseif v.type == 'nil' then
|
||||
return 'nil'
|
||||
else
|
||||
return pprint.format(v)
|
||||
end
|
||||
@@ -49,7 +51,8 @@ function Stack:top_fps()
|
||||
end
|
||||
|
||||
function Stack:push(value)
|
||||
assert(type(value) == 'table' and TYPE_MAP[value.type])
|
||||
assert(type(value) == 'table', "invalid value format (expected { type='...', value=... })")
|
||||
assert(TYPE_MAP[value.type], "missing field 'type' in value")
|
||||
table.insert(self.stack, value)
|
||||
end
|
||||
|
||||
@@ -208,6 +211,11 @@ function VM.new()
|
||||
}, VM)
|
||||
end
|
||||
|
||||
function VM:set_debug(b)
|
||||
self.debug = b
|
||||
return self
|
||||
end
|
||||
|
||||
--
|
||||
-- code management
|
||||
--
|
||||
@@ -226,6 +234,10 @@ function VM:push_integer(n)
|
||||
self.stack:push({ type = 'integer', value = n })
|
||||
end
|
||||
|
||||
function VM:push_nil()
|
||||
self.stack:push({ type = 'nil' })
|
||||
end
|
||||
|
||||
--
|
||||
-- information
|
||||
--
|
||||
@@ -281,6 +293,23 @@ function VM:_step()
|
||||
if op.operator == 'pushi' then
|
||||
self:push_integer(op.operand)
|
||||
|
||||
--
|
||||
-- local variables
|
||||
--
|
||||
|
||||
elseif op.operator == 'pushv' then
|
||||
for i=1,op.operand do
|
||||
self:push_nil()
|
||||
end
|
||||
|
||||
elseif op.operator == 'set' then
|
||||
local a = self.stack:pop()
|
||||
self.stack[op.operand + 1] = a
|
||||
|
||||
elseif op.operator == 'dupv' then
|
||||
local a = self.stack[op.operand + 1]
|
||||
self.stack:push(a)
|
||||
|
||||
--
|
||||
-- logic/arithmetic operations
|
||||
--
|
||||
|
||||
Reference in New Issue
Block a user