Interpretation is the easiest-to-understand way of creating a programming language. (actually, of creating an
implementation of that language - the language was created when you thought of it)
As an example let's create a very basic and simple language with four commands, called Silly:
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.
We've just created a language. That's all you have to do to create a language - define what programs written in that language look like, and what they do. Of course, creating a
useful language is harder and requires a lot more planning.
We probably want a way to run programs in this language, though, so we can write an interpreter in a different language.
Let's make an interpreter that runs in CC and runs a Silly program from the file "input".
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
This is the Silly program we'll be using as a test:
WRITE Hello
SLEEP 0.2
PRINT world!
SLEEP 1
REPEAT
Note 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.
So to answer your questions:
- Why does ["PRINT world!"] cause the program to spit out the string?
Because that's what our interpreter makes it do.
- Or an even better question, why does it even show up what text I enter?
Not sure what you mean here. Are you asking why it shows "a" when you press "a", etc?
That's because the ComputerCraft edit program is written to, when it receives a char event, display the character and add it to the file.
There are several layers between your keyboard and the CC edit program, but I don't think you were asking about how the char event gets there in the first place.
- What exactly is done to make the leap from just random files on hardware, to working software?
The CPU runs machine code, because it's designed to do that using the laws of physics.
Everything that runs is either machine code, or is interpreted by an interpreter which is machine code, or possibly more than one (eg: CC programs run in LuaJ which runs in Java which is machine code).
Note: Just because a program is machine code doesn't mean it was written that way. No sane person writes anything directly in machine code if they can help it, they either use assembly language (which is a human-readable direct translation of machine code) or use a compiled language such as C or C++ (which is translated into machine code by a compiler).