Start the interpreter.
I'm not sure whether the interpreter can write several sections, because a lot of related content is covered in previous versions.
Similarly, we should start with the main function of the interpreter entry.
int main (int argc, char *argv[]){ int i; int result = 0; iolib_open (); strlib_open (); mathlib_open (); lua_register("argv", lua_getargv); if (argc < 2) manual_input(); else { for (i=1; i<argc; i++) if (strcmp(argv[i], "--") == 0) { lua_argc = argc-i-1; lua_argv = argv+i; break; } for (i=1; i<argc; i++) { if (strcmp(argv[i], "--") == 0) break; else if (strcmp(argv[i], "-") == 0) manual_input(); else if (strcmp(argv[i], "-v") == 0) printf("%s %s\n(written by %s)\n\n", LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS); else { result = lua_dofile (argv[i]); if (result) fprintf(stderr, "lua: error trying to run file %s\n", argv[i]); } } } return result;}
The program first defines the return value of a program. According to the C language convention, if the program returns 0, it indicates that the operation is successful. If the value is not 0, it indicates that the operation fails.
Open several libraries, Io libraries, string libraries, and math libraries. The process of opening the database is to register the database function to the global symbol table.
Register an argv function to obtain the parameters for the Lua script from the command line in the Lua script. These parameters are stored in the C execution environment. The register function uses the lua_register macro, which is also used for the registration of the built-in library of the above program.
If no parameter exists, the program is executed in interactive mode. The content manually entered by the user in the command line is executed as a Lua script program.
static void manual_input (void){ if (isatty(0)) { char buffer[250]; while (fgets(buffer, sizeof(buffer), stdin) != 0) lua_dostring(buffer); } else lua_dofile(NULL); /* executes stdin as a file */}
Isatty checks whether a device is of a certain terminal type, which is defined in POSIX.
This is analyzed in windows, so replacing it with a macro is to treat the standard input as a TTY.
Therefore, in windows, isatty (0) is always true, that is, manually input manual_input is executed by the input script code in line and executed through lua_dostring.
Return to the main function.
Next, explain the parameters of the command line.
The first for loop is used to parse the location and number of input parameters of the Lua script. The parameter starting from the two minus signs "--" is the parameter of the Lua script.
for (i=1; i<argc; i++) if (strcmp(argv[i], "--") == 0) { lua_argc = argc-i-1; lua_argv = argv+i; break; }
The second for loop parses the command line input.
If there are two minus signs "--", it indicates that the Lua script starts and stops.
If it is a minus sign "-", it indicates the interactive mode. This is similar to the case where no parameter is available. manual_input is called to execute the user's manual input content.
If it is "-V", print the program version, copyright, author information.
Otherwise, the script file is interpreted and executed. Call lua_dofile to explain the script file.
If an error occurs while executing the script file (that is, if the result is not 0), print the error message.
In this version, the external compiler is added. Lua can directly explain the compiled luac. Out binary files. How does Lua distinguish between script files and compiled binary files?
----------------------------------------
So far:
> How does Lua distinguish between script files and compiled binary files?
----------------------------------------
Lua2.4 interpreter entry Lua. c