Skip to content

AnastasMIPT/Language

Repository files navigation

My own programming language and compiler for it.

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.

How does it works?

There are several stages here:

  1. The program text is split into tokens
  2. 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.)
  3. 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.

The first stage: building a tree according to the program

Here is an example program on my language:

factorial prgrams

How does it turn into tree? First, the program is tokenized. This happens using the Tocens () function in main.cpp.

tocens 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:

factorial tree

After tranclation by backend_x86.cpp:

after transl

ELF file and compilation to binary code.

ELF

You can read about this here

About

My own language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published