A compiler for a subset of Lua, written in Swift. It generates NASM-style 32-bit x86 assembly for Linux.
- Supports variable declaration and assignment
- Handles arithmetic operations
- Implements conditional statements (if-else)
- Supports loops (while)
- Basic I/O operations (print, read)
BLOCK = { STATEMENT };
STATEMENT = ( IDENTIFIER, "=", 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" ), "\n" ;
BOOL_EXP = BOOL_TERM, { ("or"), BOOL_TERM } ;
BOOL_TERM = REL_EXP, { ("and"), REL_EXP } ;
REL_EXP = EXPRESSION, { ("==" | ">" | "<"), EXPRESSION } ;
EXPRESSION = TERM, { ("+" | "-" | ".."), TERM } ;
TERM = FACTOR, { ("*" | "/"), FACTOR } ;
FACTOR = NUMBER | STRING | IDENTIFIER | (("+" | "-" | "not"), FACTOR ) | "(", BOOL_EXP, ")" | "read", "(", ")" ;
IDENTIFIER = LETTER, { LETTER | DIGIT | "_" } ;
NUMBER = DIGIT, { DIGIT } ;
LETTER = ( "a" | "..." | "z" | "A" | "..." | "Z" ) ;
DIGIT = ( "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "0" ) ;
STRING = '"', ({ LETTER | DIGIT | "_" }), '"' ;Generated by: DrawGrammar
Build the compiler with Swift:
git clone https://github.com/G3mha/lua-compiler.git
cd lua-compiler
swiftc main.swift -o lua-compiler
./lua-compiler example.luaReplace example.lua with your source file. The compiler writes example.asm.
Use a simple filename without extra dots: the output name is taken from the
part before the first dot in the input path.
Requires NASM, GCC with 32-bit development libraries, and an x86 Linux system
with 32-bit execution support. On Debian/Ubuntu, the packages are nasm,
gcc-multilib, and libc6-dev-i386.
nasm -f elf32 example.asm -o example.o
gcc -m32 -no-pie example.o -o example
./exampleGCC links the C library used by printf, scanf, and fflush. The output uses
Linux int 0x80 system calls and cannot run natively on macOS or Windows.
On Apple Silicon, use an x86 Linux VM or emulated environment with 32-bit support.
Handles the generation of assembly code:
- header: Contains assembly code constants and setup.
- footer: Contains assembly code for program termination.
- addInstruction: Adds a line of assembly instruction.
- generate: Generates the final assembly file.
Preprocesses the Lua code:
- filter: Removes comments and unnecessary spaces from the Lua code.
Manages variables and their values:
- initVar: Initializes a new variable.
- setValue: Sets the value of a variable.
- getValue: Retrieves the value of a variable.
- getOffset: Gets the memory offset for a variable.
Represents parts of the abstract syntax tree (AST):
- Block: Represents a block of statements.
- BinOp: Represents a binary operation.
- UnOp: Represents a unary operation.
- IntVal: Represents an integer value.
- StringVal: Represents a string value.
- NoOp: Represents a no-operation.
- VarDec: Represents a variable declaration.
- VarAssign: Represents a variable assignment.
- VarAccess: Represents variable access.
- Statements: Represents a list of statements.
- WhileOp: Represents a while loop.
- IfOp: Represents an if-else statement.
- ReadOp: Represents a read operation.
- PrintOp: Represents a print operation.
Tokenizes the input Lua code:
- selectNext: Selects the next token in the source code.
Parses the tokenized Lua code and generates the AST:
- parseFactor
- parseTerm
- parseExpression
- parseRelationalExpression
- parseBooleanTerm
- parseBoolExpression
- parseStatement
- parseBlock
- run: Parses and evaluates the Lua code.
local a = 10
local b = 20
if a < b then
print(a)
else
print(b)
end; constantes
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
True equ 1
False equ 0
segment .data
formatin: db "%d", 0
formatout: db "%d", 10, 0 ; newline, nul terminator
scanint: times 4 db 0 ; 32-bits integer = 4 bytes
segment .bss ; variaveis
res RESB 1
section .text
global main ; linux
;global _main ; windows
extern scanf ; linux
extern printf ; linux
;extern _scanf ; windows
;extern _printf; windows
extern fflush ; linux
;extern _fflush ; windows
extern stdout ; linux
;extern _stdout ; windows
; subrotinas if/while
binop_je:
JE binop_true
JMP binop_false
binop_jg:
JG binop_true
JMP binop_false
binop_jl:
JL binop_true
JMP binop_false
binop_false:
MOV EAX, False
JMP binop_exit
binop_true:
MOV EAX, True
binop_exit:
RET
main:
PUSH EBP ; guarda o base pointer
MOV EBP, ESP ; estabelece um novo base pointer
PUSH DWORD 0
MOV EAX, 10
MOV [EBP-4], EAX
PUSH DWORD 0
MOV EAX, 20
MOV [EBP-8], EAX
if_DJdIyHOAkk:
MOV EAX, [EBP-8]
PUSH EAX
MOV EAX, [EBP-4]
POP EBX
CMP EAX, EBX
CALL binop_jl
CMP EAX, False
JE if_else_DJdIyHOAkk
MOV EAX, [EBP-4]
PUSH EAX
PUSH formatout
CALL printf
ADD ESP, 8
JMP if_end_DJdIyHOAkk
if_else_DJdIyHOAkk:
MOV EAX, [EBP-8]
PUSH EAX
PUSH formatout
CALL printf
ADD ESP, 8
if_end_DJdIyHOAkk:
; interrupcao de saida (default)
PUSH DWORD [stdout]
CALL fflush
ADD ESP, 4
MOV ESP, EBP
POP EBP
MOV EAX, 1
XOR EBX, EBX
INT 0x80- DrawGrammar for generating the railroad diagrams.
- Lua for the inspiration and language reference.
This project is licensed under the AGPL License.
Feel free to contribute, open issues, or submit pull requests to improve this project. Happy coding!
