string to code

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
EliterScripts
Citizen
Posts: 85
Joined: Sat Oct 25, 2014 7:07 pm

string to code

Post 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?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: string to code

Post 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.
EliterScripts
Citizen
Posts: 85
Joined: Sat Oct 25, 2014 7:07 pm

Re: string to code

Post 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. :D
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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: string to code

Post 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. :D
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?

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
RBXcpmoderator12345
Citizen
Posts: 59
Joined: Sat Mar 07, 2015 11:53 pm

Re: string to code

Post by RBXcpmoderator12345 »

loadstring and write it to a .lua file
EliterScripts
Citizen
Posts: 85
Joined: Sat Oct 25, 2014 7:07 pm

Re: string to code

Post 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.
User avatar
I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

Re: string to code

Post 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. :huh:
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests