mirror of
https://github.com/Reuh/candran.git
synced 2025-10-27 09:59:29 +00:00
Fixed using break and continue in the same loop, added vanilla Lua 5.1 target
somehow I never encountered this before... well now there's more tests
This commit is contained in:
parent
ea7720b7c3
commit
91948109ca
8 changed files with 1883 additions and 1093 deletions
26
compiler/lua51.can
Normal file
26
compiler/lua51.can
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
states.continue = {} -- when in a loop that use continue
|
||||
|
||||
CONTINUE_START = ()
|
||||
return "local " .. var("break") .. newline() .. "repeat" .. indent() .. push("continue", var("break"))
|
||||
end
|
||||
CONTINUE_STOP = ()
|
||||
return pop("continue") .. unindent() .. "until true" .. newline() .. "if " .. var("break") .. " then break end"
|
||||
end
|
||||
|
||||
tags.Continue = ()
|
||||
return "break"
|
||||
end
|
||||
tags.Break = ()
|
||||
local inContinue = peek("continue")
|
||||
if inContinue then
|
||||
return inContinue .. " = true" .. newline() .. "break"
|
||||
else
|
||||
return "break"
|
||||
end
|
||||
end
|
||||
|
||||
#local patch = output
|
||||
#output = ""
|
||||
#import("compiler.luajit", { patch = patch, loadPackage = false })
|
||||
|
||||
return luajit
|
||||
|
|
@ -122,6 +122,12 @@ return function(code, ast, options)
|
|||
local APPEND = (t, toAppend) -- append values "toAppend" (multiple values possible) to t
|
||||
return "do" .. indent() .. "local a = table.pack(" .. toAppend .. ")" .. newline() .. "table.move(a, 1, a.n, #" .. t .. "+1, " .. t .. ")" .. unindent() .. "end"
|
||||
end
|
||||
local CONTINUE_START = () -- at the start of loops using continue
|
||||
return ""
|
||||
end
|
||||
local CONTINUE_STOP = () -- at the start of loops using continue
|
||||
return newline() .. "::" .. var("continue") .. "::"
|
||||
end
|
||||
|
||||
--- Tag constructors
|
||||
tags = setmetatable({
|
||||
|
|
@ -187,25 +193,25 @@ return function(code, ast, options)
|
|||
local hasContinue = any(t[2], { "Continue" }, loop)
|
||||
local r = "while " .. lua(t[1]) .. " do" .. indent()
|
||||
if hasContinue then
|
||||
r ..= "repeat" .. indent()
|
||||
r ..= CONTINUE_START()
|
||||
end
|
||||
r .. = lua(t[2])
|
||||
if hasContinue then
|
||||
r ..= unindent() .. "until true"
|
||||
r ..= CONTINUE_STOP()
|
||||
end
|
||||
r ..= unindent() .. "end"
|
||||
return r
|
||||
end,
|
||||
-- Repeat{ block expr }
|
||||
Repeat = (t)
|
||||
local hasContinue = any(t[2], { "Continue" }, loop)
|
||||
local hasContinue = any(t[1], { "Continue" }, loop)
|
||||
local r = "repeat" .. indent()
|
||||
if hasContinue then
|
||||
r ..= "repeat" .. indent()
|
||||
r ..= CONTINUE_START()
|
||||
end
|
||||
r .. = lua(t[1])
|
||||
if hasContinue then
|
||||
r ..= unindent() .. "until true"
|
||||
r ..= CONTINUE_STOP()
|
||||
end
|
||||
r ..= unindent() .. "until " .. lua(t[2])
|
||||
return r
|
||||
|
|
@ -228,22 +234,22 @@ return function(code, ast, options)
|
|||
local hasContinue = any(t[5], { "Continue" }, loop)
|
||||
r ..= ", " .. lua(t[4]) .. " do" .. indent()
|
||||
if hasContinue then
|
||||
r ..= "repeat" .. indent()
|
||||
r ..= CONTINUE_START()
|
||||
end
|
||||
r ..= lua(t[5])
|
||||
if hasContinue then
|
||||
r ..= unindent() .. "until true"
|
||||
r ..= CONTINUE_STOP()
|
||||
end
|
||||
return r .. unindent() .. "end"
|
||||
else
|
||||
local hasContinue = any(t[4], { "Continue" }, loop)
|
||||
r ..= " do" .. indent()
|
||||
if hasContinue then
|
||||
r ..= "repeat" .. indent()
|
||||
r ..= CONTINUE_START()
|
||||
end
|
||||
r ..= lua(t[4])
|
||||
if hasContinue then
|
||||
r ..= unindent() .. "until true"
|
||||
r ..= CONTINUE_STOP()
|
||||
end
|
||||
return r .. unindent() .. "end"
|
||||
end
|
||||
|
|
@ -253,11 +259,11 @@ return function(code, ast, options)
|
|||
local hasContinue = any(t[3], { "Continue" }, loop)
|
||||
local r = "for " .. lua(t[1], "_lhs") .. " in " .. lua(t[2], "_lhs") .. " do" .. indent()
|
||||
if hasContinue then
|
||||
r ..= "repeat" .. indent()
|
||||
r ..= CONTINUE_START()
|
||||
end
|
||||
r ..= lua(t[3])
|
||||
if hasContinue then
|
||||
r ..= unindent() .. "until true"
|
||||
r ..= CONTINUE_STOP()
|
||||
end
|
||||
return r .. unindent() .. "end"
|
||||
end,
|
||||
|
|
@ -329,7 +335,7 @@ return function(code, ast, options)
|
|||
end,
|
||||
-- Continue
|
||||
Continue = ()
|
||||
return "break"
|
||||
return "goto " .. var("continue")
|
||||
end,
|
||||
-- apply (below)
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@ tags._opid.bnot = (right)
|
|||
return var("bnot") .. "(" .. lua(right) .. ")"
|
||||
end
|
||||
|
||||
#placeholder("patch")
|
||||
|
||||
#local patch = output
|
||||
#output = ""
|
||||
#import("compiler.lua53", { patch = patch, loadPackage = false })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue