Page 1 of 1
string to code
Posted: Sat May 02, 2015 3:18 pm
by EliterScripts
Is there a way to run code that is in a string? for example:
Code: Select all
Example_String = "DOTHIS()"
Example2_String = "DOTHAT()"
function DOTHIS()
for i = 1, 100 do
print("LUA IS BETTER THAN PHP!")
end
end
function DOTHAT()
for i = 1, 100 do
print("this computer is wrong!")
end
end
if php<lua then
(RUN Example_String's code)
elseif php>lua
(RUN Example2_String's code)
end
what do I do?
Re: string to code
Posted: Sat May 02, 2015 3:25 pm
by ivan
Yep, Lua has "loadstring"
Code: Select all
code_func = loadstring("return 1 + 2")
result = code_func()
Although in most cases you don't need to compile Lua code at run time,
unless you got the string from a file or from user input.
Re: string to code
Posted: Sat May 02, 2015 4:16 pm
by EliterScripts
I want to make a custom interpreter (coded purely in lua, reading more lua (sounds weird, eh?)). I want an advanced interpreter that can read an outdated file containing old game stuff, and reading out outdated LOVE2D functions, so when people choose make really cool mods, and abandon them, people can run old stuff on the new stuff. Hopefully, I can get it to where you can run new stuff on old stuff too.
EDIT: to sum it up, it's basically clearing up what I think is "minecraft's bullcrappy version handler", where new stuff can't run, and old stuff can't run, and mod developers abandon cool stuff, and people have to suck it up, and find another mod to play.
Re: string to code
Posted: Sat May 02, 2015 4:38 pm
by s-ol
EliterScripts wrote:I want to make a custom interpreter (coded purely in lua, reading more lua (sounds weird, eh?)). I want an advanced interpreter that can read an outdated file containing old game stuff, and reading out outdated LOVE2D functions, so when people choose make really cool mods, and abandon them, people can run old stuff on the new stuff. Hopefully, I can get it to where you can run new stuff on old stuff too.
EDIT: to sum it up, it's basically clearing up what I think is "minecraft's bullcrappy version handler", where new stuff can't run, and old stuff can't run, and mod developers abandon cool stuff, and people have to suck it up, and find another mod to play.
Why don't you just
require it in
setfenv?
Re: string to code
Posted: Sat May 02, 2015 10:35 pm
by RBXcpmoderator12345
loadstring and write it to a .lua file
Re: string to code
Posted: Tue May 05, 2015 12:41 am
by EliterScripts
RBXcpmoderator12345 wrote:loadstring and write it to a .lua file
If I do that, it will have constant rewrite on the hard drive, and may cause some physical computer damage, so, trying to avoid that.
Re: string to code
Posted: Tue May 05, 2015 3:24 am
by I~=Spam
EliterScripts wrote:RBXcpmoderator12345 wrote:loadstring and write it to a .lua file
If I do that, it will have constant rewrite on the hard drive, and may cause some physical computer damage, so, trying to avoid that.
That seems a bit extreme to say. How often do you want to write to the hardive? Why not just store the contents of the .lua file in a string and update the .lua file every once in a while or after so many changes?
I really don't know what you are trying to do... you descriptions in your previous posts seem to contradict so I must not be understanding you right so I am not even sure why you would even want to write so often to a particular .lua file.