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