Jump to content
I'm not 100% sure about this, but you may be able to override the equals operator (and others):
http://lua.2524044.n2.nabble.com/override-assignment-operator-td7195255.html (not the solution, but may help)
It seems for tables and globals this is feasible, but locals are a bit harder.
Hopefully someone with a little more Lua black magic skill will read this and answer.
I gave this a real go, but couldn't work it out. Sorry.
My best suggestion. Work up a better converter and convert the code with a script.
You may need to run it with a standalone version of Lua to do the conversion work since Corona Lua may not support all the features needed by the converter scripts.
Edited by roaminggamer, 08 December 2018 - 10:16 AM.
I'm going to assume that your parsing is rock-solid and you operate at the clause level, where in an expression like
if A and B or C and not D or (E and (F or G)) then
the capital letters stand for said clauses. Is this accurate? (I don't know off-hand if Lua and JS honor the same precedences?)
If you have nested parentheses like so then some of this gets hairier, of course, since you might want a 0 to bubble up. But if not, three clause forms come to mind:
VALUE Just a name, like Kronqueste, or a constant like 4 or our nemesis 0.
LHS comp RHS An expression with a comparison operator (==, ~=, <, <=, >, >=, or JS equivalents depending on when analyzed) between a left- and right-hand side.
EXPRESSION Some assortment of values and arithmetic operators that might culminate in 0, e.g. Kronqueste * 3 + 7.
The VALUE case you can detect by trimming off spaces at each end, string.match()'ing the result against patterns like "[_%w]+" or "%d+", and seeing whether you got the whole match. In the constant case, just replace the result by true or false. Otherwise, either do as roaminggamer suggests and say throw a proxy over storyVariables (rawget might come in handy as well, if you do so), or append the comparison against 0.
Failing that, do a search for any of the comparison operators and if so leave the comp case intact.
Otherwise, you have the EXPRESSION case and can just parenthesize that and compare against 0.
All untested, of course.
Thanks for your reply and trying, I also thought about using a proxy object and metatable but the problem then is situations with actual integer comparisons ...
@StarCrunch - well I did not do a lot of parsing, what I did was modify the scripting into valid Lua code like replace != with ~=, replaced all kinds of "variables" actually requiring function calls to the hero or other gameworld related data, the variables with the lookup into the storyVariables table and some tweaks here and there which worked quite well - until I noticed some strange behaviour given by 0 interpreted as true
I do have the code to manually handle the original expressions with all details and special cases so I can just port this to Lua and be done.
It's just that, I've come so far and I'd have loved to get it fully working as (in a simple way) transformed Lua code
if you're doing the string conversions (ie the parsing of one script language to another) then why not just "wrap" the output script using conversion functions of your own making?
old output: storyVariables["Kronqueste"]
new output: tobool(storyVariables["Kronqueste"])
local function tobool(v) if (type(v) == "number") then return (v~=0) end return (not not v) end
The problem is, the scripts use those variables very arbitrarily (they were originally done by various book authors, over a timeframe of almost 15 years and already ported form another platform and language once during this time, so it's just lot's of legacy data not always in an ideal format), i.e. it's very possible that the same variable will sometimes be used as a boolean but in another expression as an integer in a calculation and in another one as a boolen but by comparing to 0.
I just started to port the original expression handling code - it's not that bad, it's just not as beautiful as pure Lua would be
Community Forum Software by IP.Board