Write a C language compiler InnerC

InnerC for ILBC, I put it out independently of one version of the project Address:

 

https://github.com/kelin-xycs/InnerC              ,

 

InnerC is a C language compiler, originally intended as ILBC intermediate language compiler intermediate language for the compiled C.

About ILBC, see "ILBC norms"    https://www.cnblogs.com/KSongKing/p/10354824.html         .

 

At present, some InnerC realized only contains parsing and grammar checkers, does not contain the generated object code and links.

 

Currently InnerC support pointers array of function pointers global variable structure function, int float char, four operations, not less than greater than equal to the comparison, and NOR logic operations,

if statement, while statement, for statement is not supported, mostly too lazy to write, tired. It can be added later.

Support the return break continue statement.

Support scope, such a body is a function of the scope, a scope parameter is a function, while clauses and clause IF (loop) is a scope.

It does not support ++ - + = - =, is no time to write. It can be added later.

 

Only a rough part of the realization of the syntax check to check whether the variable has been declared, whether in the higher scope declared variables of the same name, just write code, not tested.

It also implements a named check function name and the name of the structure is to be underlined by the alphanumeric letters and begin with an underscore, and can not be the same as the keyword.

 

Named check and grammar check are separate, because the check function name and the structure name exists in the syntax check in, so the first named checks.

 

Most of the grammar checker in I_C_Member. Type and grammar check () method to achieve.

Just go to achieve the type and syntax checking I_C_Member interface () method on the line.

 

All members grammar inherited I_C_Member interfaces, including variable declarations structure function scope of various statements of various expressions.

 

So the architecture is very clear, complete the remaining part of the problem is just the workload.

 

Here the type of work to be done and grammar checker column about it:


Check that the higher scope has been defined variables with the same name

Variable parameter returns the correct type value field, such as whether it is int float type or the like base structure

Are you using an undefined variable parameter field

Variables can not be used before being declared

Type on both sides of the expression matches operator

Cast legality

The function returns the type and value of the return type declaration is consistent

Whether the functions and structures undefined

Dimensions length array declaration can only be a constant or a constant expression, if the constant array initialization, you can not declare a length dimension, but this seems to apply only to one-dimensional arrays

Global variables are initialized only by a constant or a constant expression

Because most of the grammar and the type of checks are so grouped together referred to as "type and syntax checking."

 

These contents are written in the comments in the code.

 

In addition to the above, there are two independent syntax check is performed after checking the type and syntax, respectively:

 

Check all the paths have the function return values

Check the cyclic structure can not contain

 

This process can clearly see in the code.

 

Can be seen in the solution of InnerC_Demo project Demo, this is a WinForm project, run InnerC_Demo.exe, designated to compile C source file, click on the "test" button, if no syntax errors, the C source files will be compiled into member tree grammar and syntax tree members to reverse the reduction in C source code, after the reduction of C source code is stored in another file, the file name of the file is the original file name plus ".reverse.c", such as the source file name is "ac", the file name after the reduction is "acreverse.c".

 

In the Bin \ Debug directory InnerC_Demo, there is a Test.c, run InnerC_Demo.exe can compile Test.c observe the demonstration effect.

 

This has little to modify the C syntax is C language with braces, such as {1, 2, 3, 4} represents an array constant, but it makes InnerC compiler becomes complicated.

Because braces are used to represent a block of code, such as body functions, structures, or if clause, or while clauses (loop),

An array of constants will resolve the first layer and the braces division function block structure becomes troublesome braces.

In order to maintain clear and simple compiler, I decided to make a reform,

In parentheses for use array constants, such as [1, 2, 3, 4], very interesting results. a ha ha ha .

I think the older generation to the invention may have a C language preference braces habit, or is likely to see when other languages ​​represented in parentheses array feel uncomfortable.

 

The future will adopt this approach D #, D # compiler can be extended on the basis of InnerC come on.

 

There Lambda expressions in D #, so that is not to increase still judge Lambda expressions?

Yes, but, Lambda expressions is an obvious major demand, but also whether there is () => The front operator braces expressly determines whether the braces Lambda expressions,

Whether to join in the braces block partition method is to determine the Lambda expressions braces will not let concerns compiler architecture dispersion,

And the array is a constant demand for a very weak, when added to the structure braces division function block determines whether the focus causes the compiler architecture array constants braces dispersion.

 

Huh? You might ask, in structure and function in vitro where they come from an array constant? Global variables ah, the initialization of global variables could use this braces array constants. 

If the internal structure and function, in fact, no problem.

 

Guess you like

Origin www.cnblogs.com/KSongKing/p/11013210.html