Jump to content




Making programs inside a program


  • You cannot reply to this topic
3 replies to this topic

#1 nikolay04

  • Members
  • 9 posts

Posted 31 January 2017 - 07:07 PM

I want to know how to make a program thats writes another program. How do i do this

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 31 January 2017 - 07:44 PM

Look into the fs API and possibly string manipulation. Why do you want to do this anyway?

#3 Exerro

  • Members
  • 801 posts

Posted 31 January 2017 - 11:21 PM

It depends on the complexity on the program you're creating. I'm assuming you'll be giving it some form of input, but what input will it have? What kinds of things is it expected to create?

#4 HaddockDev

  • Members
  • 61 posts
  • LocationSomewhere, deep down under a rainbow.

Posted 02 February 2017 - 08:02 PM

A good idea if your making a program is to use [[ and ]] for multi line strings(?).
If you need to supply some custom variables (maybe iteration of program) a good idea is to concatenate your variable to the start.
Here's a sample program you could hack on:
local code = [[
print("This is program number " .. n .. "!") --Notice the absence of n being defined.
]]
write("How many programs do you want to make? ")
inp = read()
amount = tonumber(inp)
if inp == nil then --Check if its actually a number
  error("Not a number")
end
for i=1,amount do --basically for(int i = 0; i < amount; i++) for some languages
  print("Program " .. i)
  local h = fs.open("program" .. i, "w") --open file in write mode
  h.write("local n = " .. i .. "\n") --add the n variable
  h.write(code) --write our code
  h.close() --close the file
end

If you run it, and give it say 5 programs to make, run one of them and it should output "This is program number <number>!"





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users