|
| 24 Dec 2012 02:00 PM |
Ok, so I have this script that is supposed to convert my syntax(it's kinda like JavaScript) to Lua. The problem is that I can't get it to convert right. The problem is that when converting the if statement, it gets the end bracket from the function and not the if statement. Could anybody help me improve my pattern?
local script = [[
if (A == B) { -- Start print('Hi') }
function Hi(df) { print(df) } -- Then it catches this.
]] local err = ""
local a = script:gsub("if (.+){(.+)}","if %1 then %2 end") --b = script:gsub("function(.+){(.+)}","function%1 %2 end")
print(a)
The output:
if (A == B) { print('Hi') } function Hi(df) then print(df) end
Could anybody help improve my pattern? |
|
|
| Report Abuse |
|
|
Rukiryo
|
  |
| Joined: 04 Sep 2009 |
| Total Posts: 1490 |
|
|
| 24 Dec 2012 02:04 PM |
| Some commenting would be nice. Though, one question. Why? |
|
|
| Report Abuse |
|
|
|
| 24 Dec 2012 02:04 PM |
local a = script:gsub("if (.-){(.-)}","if %1 then %2 end")
Use .- |
|
|
| Report Abuse |
|
|
|
| 24 Dec 2012 02:05 PM |
| Thanks, king. It worked perfectly! |
|
|
| Report Abuse |
|
|
|
| 24 Dec 2012 02:08 PM |
Wait. Now I am running into another problem. If you have more than one if statement or if you use tables inside of the first if statement, it doesn't stop at the right one. :/
Current Code:
local script = [[
if (A == B) { if (A2 == B2) { print('Hi') } }
function Hi(df) { print(df) }
]] local err = ""
local a = script:gsub("if (.-){(.-)}","if %1 then %2 end") --b = script:gsub("function(.+){(.+)}","function%1 %2 end")
print(a)
Output:
if (A == B) then if (A2 == B2) { print('Hi') end } function Hi(df) { print(df) } |
|
|
| Report Abuse |
|
|
|
| 24 Dec 2012 02:41 PM |
Untested, but should work:
local script = [[
if (A == B) { if (A2 == B2) { print('Hi') } }
function Hi(df) { print(df) }
]]
local function parseIf(a) if a:match("if%s?%(.-%)%s?{.-}") then return parseIf(a:gsub("if (.-){(.-)}","if %1 then %2 end")) end return a:gsub("if (.-){(.-)}", "if %1 then %2 end") end
script = parseIf(script)
print(script) |
|
|
| Report Abuse |
|
|
|
| 24 Dec 2012 02:45 PM |
King, there is a problem. The parseIf function parses the functions too.
Output:
if (A == B) then if (A2 == B2) then print('Hi') end end function Hi(df) then -- Lolwut? print(df) end |
|
|
| Report Abuse |
|
|
| |
|
|
| 26 Dec 2012 11:36 PM |
| This is interesting. I have no idea why it's catching that. I'll look into it. |
|
|
| Report Abuse |
|
|
| |
|
|
| 02 Jan 2013 10:20 AM |
local function parseIf(a) if a:match("if%s*%(.-%)%s*{.-}") then return parseIf(a:gsub("if%s*%((.-)%)%s*{(.-)}","if %1 then %2 end")) end return a:gsub("if%s*%((.-)%)%s*{(.-)}", "if %1 then %2 end") end
script = parseIf(script)
print(script) |
|
|
| Report Abuse |
|
|
|
| 02 Jan 2013 12:23 PM |
One problem: Tables and other things that use '}'.
Current code:
local script = [[ if (A == B) { if (A2 == B2) { print('Hi') local Tab = {"Hi","Derp"} } } function Hi(df) { function Derp(dfdf) { print(dfdf) } Derp(df) } Hi(A+B) ]] local function parseIf(a) if a:match("if%s*%(.-%)%s*{.-}") then return parseIf(a:gsub("if%s*%((.-)%)%s*{(.-)}","if %1 then %2 end")) end return a:gsub("if%s*%((.-)%)%s*{(.-)}", "if %1 then %2 end") end local function parseFunction(a) if a:match("function%s*.-%s*{.-}") then return parseFunction(a:gsub("function%s*(.-)%s*{(.-)}","function %1 %2 end")) end return a:gsub("function%s*(.-)%s*{(.-)}", "function %1 %2 end") end script = parseIf(script) script = parseFunction(script) print(script)
Output:
if A == B then if A2 == B2 then print('Hi') local Tab = {"Hi","Derp" -- Here end end } -- And here. function Hi(df) function Derp(dfdf) print(dfdf) end Derp(df) end Hi("Derp") |
|
|
| Report Abuse |
|
|
|
| 02 Jan 2013 06:10 PM |
| Is it possible to change your 'syntax' so that it's a little less annoying? |
|
|
| Report Abuse |
|
|
| |
|
|
| 02 Jan 2013 06:45 PM |
Requiring some character to signify the end of a statement would help you dramatically here.
Java, C, C++ and others all use ;. You have the freedom to choose whichever symbol you want. |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 10:36 AM |
I recall making a compiler once, and I made it by splitting the source into tokens.
local tokens = {} for tok in script:gmatch("%S+") do table.insert(tokens, tok) end
that should make everything exponentially easier, however you have to worry about things like the following: function hi() { vs. function hi (){
But perhaps if you made a strict rule about that, that too would become easier. |
|
|
| Report Abuse |
|
|
|
| 04 Jan 2013 11:02 AM |
I got it working. Here it is:
--[[ SYNTAX
function Name(Aguments) { --Code }
if (Condition) { --Code }
for (Variable = StartValue,EndValue,Increment) { --Code }
repeat { --Code } until (Condition)
while (Condition) { --Code }
When printing or anything else, put a semicolon(";") at the end of the line.
print("Hi"); Number = 5; Tab = {"Hi",3,"4"};
]]
local script = [[if (A == B) { if (A2 == B2) { local Tab = {"Hi","Derp"}; print(Tab[1]); } }
function Hi(df) { function Derp(dfdf) { print(dfdf); } Derp(df); }
for (i = 1,5) { print(i); }
Num = 0;
repeat { --wait(0.1); Num = Num+1; print(Num); } until (Num == 5)
Num = 0;
while (true) { --wait(0.1); Num = Num+1; print(Num); if (Num == 5) { break; } }
Hi("Hi2"); ]]
function ParseScript(script) local Lines = {} for Line in script:gmatch("[^\n]+") do Lines[#Lines+1] = Line end for i,v in pairs(Lines) do if v:match("if%s*%(.-%)%s*{") then v = v:sub(1,#v-1).."then" Lines[i] = v elseif v:match("function%s*.-%s*{") then v = v:sub(1,#v-1) Lines[i] = v elseif v:match("for%s*.-%s*{") then local num = v:find("%(") v = v:sub(1,num-1)..v:sub(num+1) local num2 = v:find("%)") v = v:sub(1,num2-1)..v:sub(num2+1) v = v:sub(1,#v-1).."do" Lines[i] = v elseif v:match("repeat%s*{") then v = v:sub(1,#v-1) Lines[i] = v elseif v:match("while%s*%(.-%)%s*{") then v = v:sub(1,#v-1).."do" Lines[i] = v elseif v:sub(1,7) == "} until" then v = v:sub(3) Lines[i] = v elseif v:sub(#v,#v) == "}" then v = v:sub(1,#v-1).."end" Lines[i] = v else if v:sub(#v,#v) ~= ";" and (v:match("%w") or v:match("%p")) then print("Missing semicolon at line "..i..". Your script could error.") end end end return table.concat(Lines,"\n") end
script = ParseScript(script)
print(script) |
|
|
| Report Abuse |
|
|