print("Hello World")
Why does that cause the program to spit out the string? Or an even better question, why does it even show up what text I enter? What exactly is done to make the leap from just random files on hardware, to working software?
Posted 08 March 2013 - 04:02 PM
print("Hello World")
Posted 08 March 2013 - 04:05 PM
Posted 08 March 2013 - 06:40 PM
PRINT any text goes here (prints text, and goes on to the next line) WRITE any text goes here (prints text and doesn't go to the next line) SLEEP any number goes here (sleeps for that many seconds) REPEAT (restarts the program from the beginning)A Silly program is a file containing these commands, one per line, and ending with REPEAT.
local lines = {}
-- Read the lines from the file into the "lines" table.
local f = fs.open("input", "r")
for line in f.readLine do
table.insert(lines, line)
end
f.close()
-- Stores the number of the line we're currently executing.
local currentLineNumber = 1
-- This program (the interpreter) just executes lines, forever.
while true do
-- Get the current line from the "lines" table.
local currentLine = lines[currentLineNumber]
if currentLine:sub(1,6) == "PRINT " then
-- Display everything after the space
print(currentLine:sub(7))
elseif currentLine:sub(1,6) == "WRITE " then
-- Display everything after the space
write(currentLine:sub(7))
elseif currentLine:sub(1,6) == "SLEEP " then
local seconds = tonumber(currentLine:sub(7))
-- Check that there actually was a number after sleep - if not, display an error
if seconds == nil then
error("Number expected after SLEEP on line "..currentLineNumber, 0)
end
-- Otherwise, sleep that many seconds
sleep(seconds)
elseif currentLine == "REPEAT" then
-- Handled below, when we set currentLineNumber
else
-- We don't know how to execute this line, so display an error message.
error("Invalid line "..currentLineNumber, 0)
end
-- The new current line is the one after the one we just executed, unless it was a REPEAT line.
if currentLine ~= "REPEAT" then
currentLineNumber = currentLineNumber + 1
else
-- If it was REPEAT, then the new current line is the first line.
currentLineNumber = 1
end
end
WRITE Hello SLEEP 0.2 PRINT world! SLEEP 1 REPEATNote that the first line has a space after "Hello". If you save this on a CC computer as "input", then run the interpreter, it will run the program.
Posted 08 March 2013 - 10:25 PM
0 members, 2 guests, 0 anonymous users