Last active
August 17, 2021 23:40
-
-
Save x4fx77x4f/43297770295cbcb2306ce12c7db37417 to your computer and use it in GitHub Desktop.
GLua to Lua transpilation test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- baseclass macro | |
baseclass = {} | |
function baseclass.Get(str) | |
return str | |
end | |
DEFINE_BASECLASS('DUMMY') | |
assert(BaseClass == 'DUMMY', "baseclass failed") | |
-- comparison operators | |
local a, b, c = 1, 2, false | |
if not ( a && b ) then error("and failed") end | |
if not ( a || c ) then error("or failed") end | |
if not ( !c ) then error("not failed") end | |
-- comments | |
foo = 1 // error("comment failed") | |
bar = 2 /* error("multiline comment failed") */ | |
/* | |
error("multiline comment failed 2") | |
error("multiline comment failed 2") | |
*/ | |
-- continue | |
-- TODO: while, repeat until | |
do | |
local i = 0 | |
for j=1, 10 do | |
if j == 5 then | |
continue | |
end | |
i = i+1 | |
end | |
assert(i == 9, "cont failed") | |
end | |
local i = 0 | |
for j=1, 10 do | |
if j == 5 then | |
continue | |
end | |
i = i+1 | |
end | |
assert(i == 9, "cont failed 2") | |
print("passed") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- baseclass macro | |
baseclass = {} | |
function baseclass.Get(str) | |
return str | |
end | |
local BaseClass = baseclass.Get('DUMMY') | |
assert(BaseClass == 'DUMMY', "baseclass failed") | |
-- comparison operators | |
local a, b, c = 1, 2, false | |
if not ( a and b ) then error("and failed") end | |
if not ( a or c ) then error("or failed") end | |
if not ( not c ) then error("not failed") end | |
-- comments | |
foo = 1 -- error("comment failed") | |
bar = 2 --[[ error("multiline comment failed") ]] | |
--[[ | |
error("multiline comment failed 2") | |
error("multiline comment failed 2") | |
]] | |
-- goto _GLUABOX_CONTINUE_1 | |
-- TODO: while, repeat until | |
do | |
local i = 0 | |
for j=1, 10 do | |
if j == 5 then | |
goto _GLUABOX_CONTINUE_2 | |
end | |
i = i+1 | |
::_GLUABOX_CONTINUE_2:: end | |
assert(i == 9, "cont failed") | |
end | |
local i = 0 | |
for j=1, 10 do | |
if j == 5 then | |
goto _GLUABOX_CONTINUE_3 | |
end | |
i = i+1 | |
::_GLUABOX_CONTINUE_3:: end | |
assert(i == 9, "cont failed 2") | |
print("passed") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function glua:transpile(src, loose) | |
src = string.gsub(src, '%z.+$', '') | |
src = string.gsub(src, '%z$', '') | |
src = string.gsub(src, 'D'..'EFINE_BASECLASS', 'local BaseClass = baseclass.Get') -- This is actually how it's implemented. GMod genuinely gives no fucks about strings here. | |
--src = string.gsub(src, '^#', ' #') -- GMod disabled shebang support -- NO IT DIDN'T I'M JUST DUMB | |
--src = string.gsub(src, '^#[^\n+]', '') | |
if loose then | |
-- I don't want to write a lexer, so this will have to do. | |
-- https://wiki.facepunch.com/gmod/Specific_Operators | |
src = string.gsub(src, ' && ', ' and ') | |
src = string.gsub(src, ' || ', ' or ') | |
src = string.gsub(src, ' !', ' not ') | |
src = string.gsub(src, ' != ', ' ~= ') | |
src = string.gsub(src, '/%*', '--[[') -- May add equal signs to reduce the chance of problems. | |
src = string.gsub(src, '%*/', ']]') | |
src = string.gsub(src, '//', '--') | |
-- Fucking "continue". God fucking dammit. | |
-- This is going to be the worst code I've ever written. | |
-- THIS WILL BREAK IF THE INDENTATION ISN'T JUST RIGHT. | |
-- THIS WILL BREAK ON LOOPS AT THE VERY BEGINNING OF THE STRING. | |
local replacements | |
local gotoCount = 0 | |
src, replacements = string.gsub(src, 'continue', function() | |
gotoCount = gotoCount+1 | |
return 'goto _GLUABOX_CONTINUE_'..gotoCount | |
end) | |
if replacements ~= 0 then | |
local i, indent = 1 | |
local patches = {} | |
while true do | |
j, k, indent = string.find(src, '(\n[ \t]+)for%s+', i) | |
if not j then | |
j, k, indent = string.find(src, '(\n)for%s+', i) | |
if not j then | |
break | |
end | |
end | |
--print(string.format("found for loop %q at %d to %d", string.sub(src, j, k), j, k)) | |
i = k | |
local l, m = string.find(src, indent..'end', k) | |
if l then | |
--print(string.format("found matching end %q at %d to %d", string.sub(src, l, m), l, m)) | |
table.insert(patches, {l, m, indent}) | |
end | |
end | |
for i=#patches, 1, -1 do | |
local l, m, indent = unpack(patches[i]) | |
src = string.sub(src, 1, l-1)..indent..'::_GLUABOX_CONTINUE_'..gotoCount..':: end'..string.sub(src, m+1) | |
gotoCount = gotoCount-1 | |
end | |
end | |
end | |
return src | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment