This repository contains a compiler for my own c-like programming language. My language:
- Turing-complete
- Supports loops and conditions
- Supports functions and recursion
- Supports a special operator that differentiates an expression.
The master branch contains the first version of my language interpreter, implemented using my stack processor and assembler I wrote. The current branch contains the latest version of my compiler which compiles a program written in my language into an executable elf file on the x86 architecture.
There are several stages here:
- The program text is split into tokens
- An array of tokens is converted into an abstract syntax tree (AST) by the recursive descent algorithm. At this stage most of the compilation errors are found (incorrect type of expression, etc.)
- The AST is compiled into executable code. Some more compilation errors are found here (e.g. double declaration of a variable or function). The algorithm is two-passed to support conditional constructs, loops and recursion.
Here is an example program on my language:
How does it turn into tree? First, the program is tokenized. This happens using the Tocens () function in main.cpp.
This function analyzes the text of the program breaking it into nodes with a certain type. It also fills in the arrays Ids, IdsFunc, IdsParam, that are responsible for local variables, function parameters, and the functions themselves. This is a tree of this program:
After tranclation by backend_x86.cpp:
You can read about this here