Lua is a program---just like any other program---you run it, it reads input, it produces output. When you run lua MyProgram.lua
, the lua
program reads from the file MyProgram.lua
, and it writes output to the console. As with many other programs, what it spits out depends on what it read in.
The lua program is written in C.
If your MyProgram.lua
file contains print("x")
at top level, then when the lua
program reads that line it will print x
.
Note: It was lua
that printed x
. It wasn't really MyProgram.lua
. MyProgram.lua
is just a data file. The lua
program reads it in, and it uses the data to decide what it's supposed to do.
When the lua
program reads that line, it doesn't "translate" the line into C or into any other language. It just does what the line says to do.
It prints x
.
There's a name for that: We say that the lua
program interprets MyProgram.lua
.
Note: I lied. The lua
program doesn't really do anything. The lua program is just a data file. When you type lua MyProgram.lua
, the computer reads the data into memory, and then it uses the data to decide what it is supposed to do.
When we talk about a computer system, we speak at different levels of abstraction. When we say, "the computer hardware did X," we are speaking about a low level of abstraction. When we say, "MyProgram.lua
did Z", we are speaking about a higher level of abstraction. And, when we say that the lua
interpreter did something, we are talking about a level somewhere in-between.
In between the hardware and the end user's experience, you can find many levels of abstraction if you look deep enough.
But, back to Lua...
If your MyProgram.lua
contains function p() print("y") end
at top level, then the Lua program doesn't do anything with that right away. It just remembers what you wanted p()
to mean. Then later, if it sees p()
at top-level, then it prints y
.
You could write the program that does those things (i.e., you could write Lua
) in almost any language. Your choice of what language you used to implement lua might affect the internal architecture of your Lua interpreter, but it need not limit the language that your interpreter understands (i.e., the Lua language) in any way.