The previous article. NET Framework source code interpretation of the Myc compiler said the MYC compiler architecture, the entire compiler is written in C # language, listing the MYC compiler compiled a C source file process, the main path is compiled as follows:
- First, the portal main function is used to parse the command-line arguments, read the source file, and begin the compilation process. The main function is in the MyC.cs file, while the IO.cs file mainly saves the related operation of reading the source file. The following table is the source code for the main function (annotations are displayed in comments), and the IO.cs file is described in a separate section:
public static void Main () { try{//Read the source comment, the code is written in 99, that is to say. NET year in development //Maybe that time the virtual machine is not ready to pass the command-line arguments to the main function //Use the following wonderful method to get the program's command-line arguments string[] args = Environment.getcommandlineargs (); Initializes the IO object that reads the source file, which is responsible for outputting the source file as a byte stream //output to the next object – lexical analysis io prog = new io (args); Lexical analysis object, the work of the object is to filter out the source of unnecessary characters, such as spaces //comments and so on, and the source code in the character collation –tokenize, so that the parser //More convenient parsing syntax Tok Tok = new Tok ( Prog); Parsing the object, the parse is completed is the code generation phase, but the general parsing process //will only generate a syntax tree, such a design can be connected to a variety of results file output means. For example, the Exe.cs of the generated executable and the asm.cs of generating IL source code in this example are through //traversal of the syntax tree, using different output strategies to generate the parse P = new Parse (prog, Tok) of the result file; c16/>//uses a top-down approach for parsing P.program (); The compilation work has completed, close the open source file handle and other resources prog. Finish (); } catch (Exception e) { //any error during compilation, i.e. interrupt processing, print error message and exit program Console.WriteLine ("Compiler aborting:" + e.tostring () ); }}
The syntax of the
- Myc is simple, so the compilation process is a very clean process of lexical analysis, parsing, code generation, and result output. Where the lexical analysis code in Tok.cs, parsing code in Parse.cs, Emit.cs processing code generation, and Asm.cs and Exe.cs respectively according to the command line parameter settings, to generate the final executable file and choose whether to output IL source. Io.cs-io processing to read the source files into memory, and the use of streaming code is placed in the IO class, IO constructor parsing command line parameters, and open source files, waiting for the Tok.cs inside the code to read the source file characters into memory and processing, the following is its constructor of the source code:
Public Io (string[] a) {int i; args = A; Parse the command line parameters, and open the internal control switch according to the parameters, see below the Parseargs//function of the Source code interpretation Parseargs (); Open the source file you want to compile ifile = new FileStream (Ifilename, FileMode.Open, FileAccess.Read, FileShare.Read, 8192); If the source file does not exist, an error exits if (ifile = = null) {Abort ("Could not open file '" "+ifilename+" \ n ");} Read source file Rfile = new StreamReader (ifile) with streaming mode; Open up a stream for reading//according to the name of the source file, set the file name of the result output files i = Ifilename. LastIndexOf ('. '); if (I < 0) Abort ("Bad filename" "+ifilename+" "); Int J = Ifilename. LastIndexOf (' \ \ '); if (J < 0) j = 0; else J + +; classname = Ifilename. Substring (J,I-J); Depending on the command line parameters, whether to generate an. exe,. dll, and so on, or output a. lst file containing//IL source code if (genexe) Ofilename = classname+ ". exe"; if (gendll) Ofilename = classname+ ". dll"; if (genlist) {//if is to output IL source code, because the original executable file also to output, need to create a new file Lst_ofilename = classname+ ". LST"; Lst_ofile = new FileStream (Lst_ofilename, FileMode.Create, FileAccess.Write, Fileshare.write, 8192);if (Lst_ofile = = null) Abort ("Could not open file '" +ofilename+ "\ n"); Lst_wfile = new StreamWriter (lst_ofile); } }
The compiler handles the command-line arguments in the IO class, and parameter parsing is actually a string-handling activity, and this article explains the key code:
void Parseargs () {int i = 1; The program requires a minimum of two parameters, otherwise it outputs the help text and exits if (args. Length < 2) {Abort ("Myc [/debug] [/nodebug] [/list] [/dll] [/exe] [/outdir:path] filename.myc\n"); }//traverse the command-line argument one by one while (true) {if (args[i][0]! = '/') break; Processing/? This parameter, which is the output help text if (Args[i]. Equals ("/?")) {Console.WriteLine ("Compiler options:\n Myc [/debug] [/nodebug] [/list] [/dll] [/exe] [/outdir:path] filename.myc\n "); Environment.exit (1); }//If there is a/debug parameter, open the internal gendebug switch, which is used in the code generation process//To use the IF (Args[i]. Equals ("/debug")) {Gendebug = true; i++; Continue }// ... ... Skips a similar code//If there is a/outdir parameter, gets the directory path specified on the command line if (Args[i]. Length > 8 && args[i]. Substring (0,8). Equals ("/outdir:")) {Genpath = Args[i]. Substring (8); i++; Continue }//in front of so many if equivalent to switch ... case processing path inside the default block//The code below is the default processing path – if the command line argument conforms to the previous if condition//will execute the inside of the continue clause to jump out of the loop , which can be performed here, indicates that the parameter//is an unrecognized parameter, so report the error and exit execution Abort ("Unmatched switch = ' +args[i]+ ' ' \narguments are:\nmyc [/debug] [/nodebug] [/list] [/dll] [/exe] [/outdir:path] Filename.myc\ n "); }//If the previous loop has finished executing, and the parameter list is not processed, the input is an unsupported parameter if (args. Length-i! = 1) {Abort ("Myc [/debug] [/nodebug] [/list] [/dll] [/exe] [/outdir:path] filename.myc\n");} The last parameter is the source file path to compile ifilename = Args[args. LENGTH-1]; FileName is the last}
Most functions in the IO class are services for Tok.cs, so other functions explain lexical analysis
MYC Compiler source Code Analysis