Welcome to Saynaa – a programming language written in C++ from scratch.
To run the example code provided, use following command
make
./compiler -l stdlib/print_int.sa examples/main.sa -o examples/app
this should output output as expected
Now it supports import and export functions
you can exporta funcion like:
// sum.sa
export function sum(a, b) {
...
}
and import like
//main.sa
import sum from "sum"
function main() {
return sum(2,3);
}
and you build it like:
./compiler -l sum.sa main.sa -o app
you can add many -l flags
for example
./compiler -l sum.sa -l stdlib/print_int.sa main.sa -o app
WARNING: while iporting, the name of file
for example its "sum" here because we have -l sum.sa
import sum from "sum"
if we had -l example/main.sa
we would do
import sum from "example/main"
the import filename is always as defined to the compiler command, not relative file path
we can fix it later
I use clang-format inside VSCode for formatting.
Saynaa generates assembly code and manages variables using:
- A global array called
allVariable- Each entry is 9 bytes:
- 1 byte for type
- 8 bytes for value (or pointer to string in
.data)
- Each entry is 9 bytes:
- A temporary variable
tmpValueis used for intermediate storage like binary operations, etc.
Saynaa is designed to be fully modular, supporting loadable modules with priorities. Modules are placed inside the custom_modules directory.
Path: custom_modules/execute/module.cpp
#include "../../modules.h"
#include "stdio.h"
#include <cstdio>
#include <cstdlib>
static int execute_run(CompilerContext *ctx)
{
printf("[execute] running...\n");
if (std::system("nasm -g -f elf64 saynaa.asm && ld saynaa.o -o app"))
return 1;
printf("\n\033[1;32mGenerated executable: app\033[0m");
printf("\033[1;34m\nRun the executable with: ./app\033[0m\n");
printf("[execute] finished.\n");
return 0;
}
static CompilerModule execute_mod = {
.name = "Execuatable generator",
.priority = 3,
.run = execute_run,
};
__attribute__((constructor))
void init_execute_module() {
register_module(&execute_mod);
}make module NAME=custom_module_name- Rebuild the compiler:
make- Run the compiler and check output:
./saynaa example.sa.
├── app
├── count-line.sh
├── custom_modules
│ ├── compiler
│ │ ├── DEPENDENCY
│ │ ├── exports.h
│ │ └── module.cpp
│ ├── execute
│ │ ├── DEPENDENCY
│ │ └── module.cpp
│ ├── generator
│ │ ├── DEPENDENCY
│ │ ├── generator.h
│ │ ├── macros.h
│ │ └── module.cpp
│ └── tokenizer
│ ├── exports.h
│ └── module.cpp
├── example.sa
├── main.cpp
├── main.sa
├── Makefile
├── modules
│ └── register.c
├── modules.h
├── README.md
├── saynaa.asm
├── shared
│ ├── common.h
│ └── value.h
├── test.sa
└── utils
├── debug.cpp
├── debug.h
├── utils.cpp
└── utils.h
make
./saynaa example.sa && ./appSaynaa is available under the permissive MIT license.