Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroflag committed Dec 27, 2024
1 parent 1ea100e commit d0b9ea5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 82 deletions.
4 changes: 4 additions & 0 deletions aux.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local Stack = require("stack_def")
local aux = Stack.new(128)

return aux
5 changes: 2 additions & 3 deletions compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
-- fix Lua's accidental global
-- table
--

local stack = require("stack")
local macros = require("macros")
local ops = require("ops")
Expand Down Expand Up @@ -62,13 +61,13 @@ function compiler.emit_lua_call(self, name, arity, vararg)
end
local params = ""
for i = 1, arity do
params = params .. "stack.pop()"
params = params .. "stack:pop()"
if i < arity then
params = params .. ", "
end
end
-- TODO reverse the order of params?
self:emit_line("stack.push(" .. name .. "(" .. params .. "))")
self:emit_line("stack:push(" .. name .. "(" .. params .. "))")
end

function compiler.compile_token(self, token, kind)
Expand Down
2 changes: 2 additions & 0 deletions dict.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dict.defword("not", "ops._not", false)
dict.defword("and", "ops._and", false)
dict.defword("or", "ops._or", false)
dict.defword("..", "ops.concat", false)
dict.defword(">a", "ops.to_aux", false)
dict.defword("a>", "ops.from_aux", false)
dict.defword("if", "macros._if", true)
dict.defword("then", "macros._then", true)
dict.defword("else", "macros._else", true)
Expand Down
18 changes: 9 additions & 9 deletions macros.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,35 @@ end
function macros.assignment(compiler)
local alias = compiler:word()
local name = "v_" .. alias
compiler:emit_line(name .. " = stack.pop()")
compiler:emit_line(name .. " = stack:pop()")
end

function macros._if(compiler)
local label = gen_label()
compiler:emit_line("if not stack.pop() then goto " .. label .. " end")
stack.push(label)
compiler:emit_line("if not stack:pop() then goto " .. label .. " end")
stack:push(label)
end

function macros._else(compiler)
local label = gen_label()
compiler:emit_line("goto " .. label)
compiler:emit_line("::" .. stack.pop() .. "::")
stack.push(label)
compiler:emit_line("::" .. stack:pop() .. "::")
stack:push(label)
end

function macros._then(compiler)
compiler:emit_line("::" .. stack.pop() .. "::")
compiler:emit_line("::" .. stack:pop() .. "::")
end

function macros._begin(compiler)
local label = gen_label()
compiler:emit_line("::" .. label .. "::")
stack.push(label)
stack:push(label)
end

function macros._until(compiler)
local label = stack.pop()
compiler:emit_line("if not stack.pop() then goto " .. label .. " end")
local label = stack:pop()
compiler:emit_line("if not stack:pop() then goto " .. label .. " end")
end

function macros._end(compiler)
Expand Down
89 changes: 49 additions & 40 deletions ops.lua
Original file line number Diff line number Diff line change
@@ -1,110 +1,119 @@
local stack = require("stack")
local aux = require("aux")
local ops = {}

function ops.dup()
stack.push(stack.tos())
stack:push(stack:tos())
end

function ops.add()
stack.push(stack.pop() + stack.pop())
stack:push(stack:pop() + stack:pop())
end

function ops.mul()
stack.push(stack.pop() * stack.pop())
stack:push(stack:pop() * stack:pop())
end

function ops.div()
local a = stack.pop()
local b = stack.pop()
stack.push(b / a)
local a = stack:pop()
local b = stack:pop()
stack:push(b / a)
end

function ops.sub()
local a = stack.pop()
local b = stack.pop()
stack.push(b - a)
local a = stack:pop()
local b = stack:pop()
stack:push(b - a)
end

function ops.depth()
stack.push(stack.depth())
stack:push(stack:depth())
end

function ops.swap()
local a = stack.pop()
local b = stack.pop()
stack.push(a)
stack.push(b)
local a = stack:pop()
local b = stack:pop()
stack:push(a)
stack:push(b)
end

function ops.over()
stack.push(stack.tos2())
stack:push(stack:tos2())
end

function ops.rot()
local c = stack.pop()
local b = stack.pop()
local a = stack.pop()
stack.push(b)
stack.push(c)
stack.push(a)
local c = stack:pop()
local b = stack:pop()
local a = stack:pop()
stack:push(b)
stack:push(c)
stack:push(a)
end

function ops.drop()
stack.pop()
stack:pop()
end

function ops.eq()
stack.push(stack.pop() == stack.pop())
stack:push(stack:pop() == stack:pop())
end

function ops.neq()
stack.push(stack.pop() ~= stack.pop())
stack:push(stack:pop() ~= stack:pop())
end

function ops.lt()
stack.push(stack.pop() > stack.pop())
stack:push(stack:pop() > stack:pop())
end

function ops.lte()
stack.push(stack.pop() >= stack.pop())
stack:push(stack:pop() >= stack:pop())
end

function ops.gt()
stack.push(stack.pop() < stack.pop())
stack:push(stack:pop() < stack:pop())
end

function ops.gte()
stack.push(stack.pop() <= stack.pop())
stack:push(stack:pop() <= stack:pop())
end

function ops._not()
stack.push(not stack.pop())
stack:push(not stack:pop())
end

function ops._and()
local a = stack.pop()
local b = stack.pop()
stack.push(a and b)
local a = stack:pop()
local b = stack:pop()
stack:push(a and b)
end

function ops._or()
local a = stack.pop()
local b = stack.pop()
stack.push(a or b)
local a = stack:pop()
local b = stack:pop()
stack:push(a or b)
end

function ops.concat()
local a = stack.pop()
local b = stack.pop()
stack.push(b .. a)
local a = stack:pop()
local b = stack:pop()
stack:push(b .. a)
end

function ops.dot()
print(stack.pop())
print(stack:pop())
end

function ops.lit(literal)
stack.push(literal)
stack:push(literal)
end

function ops.to_aux()
aux:push(stack:pop())
end

function ops.from_aux()
stack:push(aux:pop())
end

return ops
27 changes: 2 additions & 25 deletions stack.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
local sp = 1
local stack = {}
for i = 1, 128 do stack[i] = nil end

function stack.push(e)
stack[sp] = e
sp = sp + 1
end

function stack.pop()
sp = sp - 1
return stack[sp]
end

function stack.tos()
return stack[sp-1]
end

function stack.tos2()
return stack[sp-2]
end

function stack.depth()
return sp - 1
end
local Stack = require("stack_def")
local stack = Stack.new(128)

return stack
32 changes: 32 additions & 0 deletions stack_def.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local Stack = {}

function Stack.new(size)
local obj = {stack = {}, sp = 1}
-- TODO check size, overflow / underflow
setmetatable(obj, {__index = Stack})
return obj
end

function Stack.push(self, e)
self.stack[self.sp] = e
self.sp = self.sp + 1
end

function Stack.pop(self)
self.sp = self.sp - 1
return self.stack[self.sp]
end

function Stack.tos(self)
return self.stack[self.sp-1]
end

function Stack.tos2(self)
return self.stack[self.sp-2]
end

function Stack.depth(self)
return self.sp - 1
end

return Stack
14 changes: 9 additions & 5 deletions test_compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ compiler:eval_file("lib.eqx")

function assert_tos(result, code)
local stack = compiler:eval(code)
assert(stack.depth() == 1,
"'" .. code .. "' depth: " .. stack.depth())
assert(stack.tos() == result,
"'" .. code .. "' " .. tostring(stack.tos())
assert(stack:depth() == 1,
"'" .. code .. "' depth: " .. stack:depth())
assert(stack:tos() == result,
"'" .. code .. "' " .. tostring(stack:tos())
.. " <> " .. tostring(result))
stack.pop()
stack:pop()
end

-- arithmetics - add
Expand Down Expand Up @@ -211,6 +211,10 @@ assert_tos(502, [[
502 1002 math.min/2
]])

assert_tos(0, [[
1 2 >a 3 a> - -
]])

local status, result = pcall(
function()
return compiler:eval("1 2 math.min")
Expand Down

0 comments on commit d0b9ea5

Please sign in to comment.