Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mekhyw/logicomp-compiler
Custom simplified Lua compiler that generates assembly code for Linux and Windows, developed from scratch in C++
https://github.com/mekhyw/logicomp-compiler
assembly compiler cpp lua
Last synced: 3 days ago
JSON representation
Custom simplified Lua compiler that generates assembly code for Linux and Windows, developed from scratch in C++
- Host: GitHub
- URL: https://github.com/mekhyw/logicomp-compiler
- Owner: MekhyW
- License: gpl-3.0
- Created: 2024-02-05T00:57:03.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-06-11T22:37:35.000Z (5 months ago)
- Last Synced: 2024-06-12T07:28:03.161Z (5 months ago)
- Topics: assembly, compiler, cpp, lua
- Language: C++
- Homepage:
- Size: 4.95 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LogiComp-Compiler
Custom, simplified Lua compiler that generates assembly code for Linux and Windows x86, developed from scratch in C++.
WARNING: This was made for educational purposes only. It is not intended to be used in production as it is not optimized and has many limitations compared to the original Lua compiler.
## Compiler structure
The compiler is divided into the following modules:
- **Main.cpp**: Main module that reads the input file and calls the other modules.
- **Preprocessor.h**: Preprocessor module that handles preprocessor directives and macros.
- **Tokenizer.h**: Tokenizer module that reads the input file and generates tokens.
- **Parser.h**: Parser module that generates the abstract syntax tree (AST) from the tokens.
- **Node.h**: Node module that defines the nodes of the AST.
- **SymbolTable.h**: Symbol table module that stores the variables and functions of the program (and their types).
- **Assembly.h**: Assembly module that generates the assembly code from the AST and the symbol table.![Compiler structure](docs/Diagram2.png)
## Syntactic diagram
This diagram shows how the Lua language is seen by the compiler´s parser, after the tokens are generated by the tokenizer.![Syntactic diagram](docs/Diagram.jpg)
## Extended Backus-Naur Form (EBNF)
IDENTIFIER, ( "=", BOOL_EXP | "(" , ( | BOOL_EXP, { ( "," ) , BOOL_EXP } ), ")" )
| "local", IDENTIFIER, ["=", BOOL_EXP]
| "print", "(", BOOL_EXP, ")"
| "while", BOOL_EXP, "do", "\n", { ( STATEMENT )}, "end"
| "if", BOOL_EXP, "then", "\n", { ( STATEMENT ) }, [ "else", "\n", { ( STATEMENT )}], "end"
| "function", IDENTIFIER, "(", ( | IDENTIFIER, { ( "," ), IDENTIFIER } ), ")", "\n", { ( STATEMENT ) }, "end"
| "return", BOOL_EXP
), "\n" ;
| STRING
| IDENTIFIER, ( | "(" , ( | BOOL_EXP, { ( "," ) , BOOL_EXP } ), ")" )
| ("+" | "-" | "not"), FACTOR
| "(", BOOL_EXP, ")"
| "read", "(", ")" ;
## Usage
Compile the compiler using g++:
```bash
g++ -Wall -O3 -w main.cpp -o main -std=c++17
```
Run the compiler:
```bash
./main program.lua
nasm -f elf -o program.o program.asm # For Linux
nasm -f win32 -o program.o program.asm # For Windows
gcc -m32 -o program program.o
./program
```