This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
The_Awe35's profile picture

How do programming languages work?

Started by The_Awe35, 08 March 2013 - 03:02 PM
The_Awe35 #1
Posted 08 March 2013 - 04:02 PM
This is something that has been bugging me for a while. When I make a program using lua (I'm using lua as an example as it's the only programming language I know), where does it get structure from? I mean, I could type anything I wanted, but how does typing a specific command, say


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?
Sammich Lord #2
Posted 08 March 2013 - 04:05 PM
Well there is the compiler for your chosen language that converts the text "print('hello')" into binary "10100111110000111" which the machine then executes and prints out the string. That is the simple version of it.
immibis #3
Posted 08 March 2013 - 06:40 PM
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).
PhilHibbs #4
Posted 08 March 2013 - 10:25 PM
This reminds me of a saying that I invented.

People often say "Why doesn't this computer work?" A more appropriate question would be, "Why does a computer work?" The answer is, "A billion tiny miracles every second, painstakingly crafted by engineers and programmers, and if any of those tiny miracles fails, then it stops working". It's my job to make some of those miracles, and boy does it feel good.

A more useful answer is, there's another program running in the computer that reads the code and looks for specific words, like PRINT or WHILE, and when it finds one of these words, it consults a list of rules as to what to do with whatever comes after it. There are various languages that are used to write those sets of rules as well, so it's all languages within languages until you get down to the most simple operations like adding and subtracting numbers and checking if the result is zero or not.