Compare commits
60
Commits
45206e3d07
..
v1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e518c4c82 | ||
|
|
3f2f03f931 | ||
|
|
1289f6c150 | ||
|
|
04b367e7a0 | ||
|
|
d09468cc6b | ||
|
|
41e8600ce4 | ||
|
|
801b28e27d | ||
|
|
c0df5fc9d4 | ||
|
|
f0e641928d | ||
|
|
dce64e8b4d | ||
|
|
0c8c206677 | ||
|
|
d33aa57f8e | ||
|
|
82b6cc3346 | ||
|
|
244dd33372 | ||
|
|
c0c90f4f1a | ||
|
|
8c33464656 | ||
|
|
ce13d24985 | ||
|
|
b48c196509 | ||
|
|
f1e97b94a4 | ||
|
|
645120eda6 | ||
|
|
2d307d9d42 | ||
|
|
c643f200a1 | ||
|
|
4e7d09cfa9 | ||
|
|
bc5d887ec4 | ||
|
|
683dd08181 | ||
|
|
69b2253c34 | ||
|
|
1035b9193c | ||
|
|
36ed86b07c | ||
|
|
2c206822ca | ||
|
|
cc3087296f | ||
|
|
83ff0b5774 | ||
|
|
4a908f9fd8 | ||
|
|
c0dc14a0c7 | ||
|
|
7e52c671aa | ||
|
|
8043c30d9d | ||
|
|
b8769ac81c | ||
|
|
d86d95f4fa | ||
|
|
e7573743aa | ||
|
|
2c76277a68 | ||
|
|
01afe2e972 | ||
|
|
83e20d78f3 | ||
|
|
e3b8ec6b7d | ||
|
|
028c8270f7 | ||
|
|
e01bf6e4ab | ||
|
|
96cd855c5d | ||
|
|
8696d5bfe7 | ||
|
|
4577b541c3 | ||
|
|
b9c43c6ad2 | ||
|
|
981603105b | ||
|
|
a815cd1549 | ||
|
|
1db985ecb5 | ||
|
|
f49a54c878 | ||
|
|
3f76cdcefb | ||
|
|
92ec929e9f | ||
|
|
cdf929dfc4 | ||
|
|
96115b347e | ||
|
|
e979a22739 | ||
|
|
2fabd0ec2d | ||
|
|
7744a28a12 | ||
|
|
1d7cc88c22 |
@@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
BreakBeforeBraces: Allman
|
||||||
|
IndentWidth: 2
|
||||||
|
TabWidth: 2
|
||||||
|
UseTab: Never
|
||||||
|
AlignConsecutiveAssignments: Consecutive
|
||||||
|
IndentCaseLabels: True
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
skip peek
|
||||||
|
skip consume
|
||||||
|
skip next
|
||||||
|
skip is
|
||||||
|
skip -rfunction fzl_mk*
|
||||||
|
skip -rfunction fzl_free*
|
||||||
|
skip -rfunction fzl_symt*
|
||||||
|
skip add_const
|
||||||
|
skip push_op
|
||||||
|
skip push_word
|
||||||
|
skip -rfunction fzl_env*
|
||||||
|
skip tok_to_type
|
||||||
|
skip nest_ctx
|
||||||
|
skip fin_ctx
|
||||||
|
|
||||||
@@ -1,13 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
# Dirs
|
# Dirs
|
||||||
|
!/docs
|
||||||
!/exl
|
!/exl
|
||||||
!/include
|
!/include
|
||||||
!/src
|
!/src
|
||||||
|
|
||||||
# Files
|
# Files
|
||||||
|
!/.clang-format
|
||||||
|
!/.gdbinit
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
!/CMakeLists.txt
|
!/CMakeLists.txt
|
||||||
!/Makefile
|
!/Makefile
|
||||||
!/README.md
|
!/README.md
|
||||||
|
!/README.ru.md
|
||||||
|
|
||||||
|
|||||||
+128
-4
@@ -1,7 +1,15 @@
|
|||||||
cmake_minimum_required(VERSION 4.3)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
project(fz-lang
|
project(fz-lang
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
add_compile_options(-Wall -Wextra -Werror=implicit-function-declaration)
|
||||||
|
|
||||||
|
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
add_compile_options(-fsanitize=address,undefined -g)
|
||||||
|
add_link_options(-fsanitize=address,undefined)
|
||||||
|
endif()
|
||||||
|
|
||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|
||||||
# Compiler
|
# Compiler
|
||||||
@@ -10,9 +18,125 @@ add_executable(fzc
|
|||||||
src/compiler/fzl_reader.c
|
src/compiler/fzl_reader.c
|
||||||
src/compiler/fzl_parser.c
|
src/compiler/fzl_parser.c
|
||||||
src/compiler/fzl_debug.c
|
src/compiler/fzl_debug.c
|
||||||
|
src/compiler/fzl_walker.c
|
||||||
|
src/compiler/fzl_writer.c
|
||||||
|
src/compiler/utils/fzl_const_pool.c
|
||||||
|
src/compiler/utils/fzl_symtable.c
|
||||||
|
src/compiler/utils/fzl_linked.c
|
||||||
|
src/compiler/utils/fzl_options.c
|
||||||
|
src/compiler/utils/fzl_ctable.c
|
||||||
src/compiler/main.c)
|
src/compiler/main.c)
|
||||||
|
|
||||||
# VM
|
# Virtual machine
|
||||||
# add_executable(fzvm
|
add_executable(fz
|
||||||
# src/vm/main.c)
|
src/vm/fzl_vm.c
|
||||||
|
src/vm/fzl_vm_env.c
|
||||||
|
src/vm/fzl_vm_debug.c
|
||||||
|
src/vm/main.c)
|
||||||
|
|
||||||
|
#
|
||||||
|
# ========== Compilation tests ==========
|
||||||
|
#
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_test(
|
||||||
|
NAME compile_hw
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/hw.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/hw.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_print_empty
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/print_empty.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/print_empty.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_problematic_hw
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/problematic_hw.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/problematic_hw.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_base_math
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/base_math.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/base_math.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_variables
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/variables.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/variables.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_conditions
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/conditions.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/conditions.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_fn
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/fn.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/fn.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_comment
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/comment.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/comment.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_class
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/class.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/class.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_raii
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/raii.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/raii.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_borrow
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/borrow.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/borrow.fzs"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME compile_hard_1
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/hard-1.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/hard-1.fzs"
|
||||||
|
)
|
||||||
|
|
||||||
|
set_tests_properties(compile_problematic_hw PROPERTIES WILL_FAIL TRUE)
|
||||||
|
|
||||||
|
#
|
||||||
|
# ========== Runtime tests ==========
|
||||||
|
#
|
||||||
|
|
||||||
|
add_test(
|
||||||
|
NAME runtime_hw
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/hw.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_print_empty
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/print_empty.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_base_math
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/base_math.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_variables
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/variables.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_conditions
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/conditions.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_fn
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/fn.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_comment
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/comment.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_class
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/class.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_raii
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/raii.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_borrow
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/borrow.fzb"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME runtime_hard_1
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/hard-1.fzb"
|
||||||
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
.PHONY: configure build
|
.PHONY: configure build debug-c debug-vm test
|
||||||
|
|
||||||
configure:
|
configure:
|
||||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
|
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
|
||||||
@@ -6,3 +6,12 @@ configure:
|
|||||||
build: configure
|
build: configure
|
||||||
cmake --build build
|
cmake --build build
|
||||||
|
|
||||||
|
debug-c: build
|
||||||
|
gdb --args ./build/fzc ${FILE}
|
||||||
|
|
||||||
|
debug-vm: build
|
||||||
|
gdb --args ./build/fz ${FILE}
|
||||||
|
|
||||||
|
test: build
|
||||||
|
ctest --output-on-failure --test-dir build
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
# FromZero / Language
|
||||||
|
|
||||||
|
[README in Russian](./README.ru.md)
|
||||||
|
|
||||||
|
**FromZero/Language** is a package consisting of a JIT compiler and a virtual machine for the FZL language.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
At the moment, the language has the following capabilities:
|
||||||
|
|
||||||
|
- Builtin `printline` function for printing argument to stdout
|
||||||
|
- String and numeric literals
|
||||||
|
- Arithmetic operations: addition, subtraction, multiplication, division, modulo, power
|
||||||
|
- Variables declaration with following types:
|
||||||
|
- `uint`, `int` - for the machine-word size integers
|
||||||
|
- `i8`..`i64` - integers with specified size
|
||||||
|
- `u8`..`u64` - unsigned integers with specified size
|
||||||
|
- `str` - strings
|
||||||
|
- `bool` - boolean
|
||||||
|
- Assignments
|
||||||
|
- Functions
|
||||||
|
- Classes and objects
|
||||||
|
|
||||||
|
Working examples of code can be found in `exl/` directory.
|
||||||
|
|
||||||
|
|
||||||
|
## File extensions
|
||||||
|
|
||||||
|
Standard file extensions:
|
||||||
|
|
||||||
|
- `.fzs` - source code
|
||||||
|
- `.fzb` - compiled binary file
|
||||||
|
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
1. Clone the repository: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||||
|
2. Build: `make build`
|
||||||
|
|
||||||
|
After this, a "build" directory will appear in the project root. It will contain two executable files:
|
||||||
|
|
||||||
|
- `fzc` - a compiler
|
||||||
|
- `fz` - virtual machine
|
||||||
|
|
||||||
|
Both executables have usage help that can be displayed using `--help` flag (or `-h`).
|
||||||
|
|
||||||
|
|
||||||
|
## Development guideline
|
||||||
|
|
||||||
|
You need a code editor with Clang LSP support because of `.clang-format` file in the project root.
|
||||||
|
This file provides set up codestyle rules.
|
||||||
|
|
||||||
|
There is two targets in Makefile for debugging purposes: `build-c` for the compiler and `build-vm` for the VM.
|
||||||
|
Each target can be called with `FILE` variable. For example:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ make debug-c FILE=exl/hw.fzs
|
||||||
|
```
|
||||||
|
|
||||||
|
To test new features, use `test` make target and the tests, defined in `CMakeLists.txt`.
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
# FromZero / Language
|
||||||
|
|
||||||
|
[README in English](./README.md)
|
||||||
|
|
||||||
|
**FromZero/Language** - пакет из JIT-компилятора и виртуальной машины для языка FZL.
|
||||||
|
|
||||||
|
## Текущие возможности
|
||||||
|
|
||||||
|
На данный момент язык имеет:
|
||||||
|
|
||||||
|
- Встроенную функцию `printline` для вывода аргумента в stdout
|
||||||
|
- Строковый, булев и числовой литералы
|
||||||
|
- Арифметические операции: сложение, вычитание, умножение, деление, модуло, возведение в степень
|
||||||
|
- Декларация переменных со следующими типами:
|
||||||
|
- `uint`, `int` - целые числа размером с машинное слово
|
||||||
|
- `i8`..`i64` - целые числа со знаком
|
||||||
|
- `u8`..`u64` - целые числа без знака
|
||||||
|
- `str` - строки
|
||||||
|
- `bool` - булевы
|
||||||
|
- Присваивание
|
||||||
|
- Функции
|
||||||
|
- Классы и объекты
|
||||||
|
|
||||||
|
Примеры кода лежат в директории `exl/`.
|
||||||
|
|
||||||
|
|
||||||
|
## Расширения файлов
|
||||||
|
|
||||||
|
Стандартными для языка являются два расширения файлов:
|
||||||
|
|
||||||
|
- `.fzs` - исходный код
|
||||||
|
- `.fzb` - скомпилированный бинарный файл
|
||||||
|
|
||||||
|
|
||||||
|
## Инструкции по сборке
|
||||||
|
|
||||||
|
1. Склонировать проект: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||||
|
2. Собрать проект: `make build`
|
||||||
|
|
||||||
|
После этого в корне появится директория **"build"**. В ней будут лежать два скомпилированных бинарных файла:
|
||||||
|
|
||||||
|
- `fzc` - компилятор
|
||||||
|
- `fz` - виртуальная машина
|
||||||
|
|
||||||
|
Оба бинарных файла предоставляют справку по использованию при запуске с флагом `--help` (или `-h`).
|
||||||
|
|
||||||
|
|
||||||
|
## Гайдлайн по разработке
|
||||||
|
|
||||||
|
Желательно иметь редактор кода, поддерживающий Clang LSP, так как в проекте есть готовые настройки для
|
||||||
|
форматтера в виде файла `.clang-format`.
|
||||||
|
|
||||||
|
Для дебаггинга есть два таргета в CMake: `build-c` для компилятора и `build-vm` для виртуальной машины.
|
||||||
|
Оба таргета позволяют указать переменную с путём файла. Например, так:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ make debug-c FILE=exl/hw.fzs
|
||||||
|
```
|
||||||
|
|
||||||
|
Для проверки новых фич, нужно использовать make-таргет `test`, а тесты писать в `CMakeLists.txt`.
|
||||||
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
program := statement*
|
||||||
|
statement := expression
|
||||||
|
expression := call | string_literal
|
||||||
|
call := name "(" argument-list? ")"
|
||||||
|
argument-list := string_literal ("," string_literal)*
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
printline(2 + 2 * 2);
|
||||||
|
printline(-1 + 1);
|
||||||
|
printline(2 ^ 2);
|
||||||
|
printline(1 + 1 * 1 / 1);
|
||||||
|
printline(4 % 2);
|
||||||
|
printline(10 / 2);
|
||||||
|
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
class A
|
||||||
|
{
|
||||||
|
uint some;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn doSome(A a) A
|
||||||
|
{
|
||||||
|
// NOTE: section below won't change `a.some` value
|
||||||
|
|
||||||
|
if (a.some < 1)
|
||||||
|
{
|
||||||
|
a.some = 1;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn doSomeNew() A
|
||||||
|
{
|
||||||
|
if (1 == 1)
|
||||||
|
{
|
||||||
|
return new A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
A a = new A;
|
||||||
|
a.some = 0;
|
||||||
|
if (a.some != 0)
|
||||||
|
{
|
||||||
|
printline("Test failed (first a.some != 0)");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else printline(a);
|
||||||
|
|
||||||
|
a = doSome(a);
|
||||||
|
if (a.some != 1)
|
||||||
|
{
|
||||||
|
printline("Test failed (2nd a.some != 1)");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else printline(a);
|
||||||
|
|
||||||
|
A b = doSomeNew();
|
||||||
|
printline(b);
|
||||||
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
class Point
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
// Move point by [dx,dy]
|
||||||
|
fn move(int dx, int dy) {
|
||||||
|
this.x = this.x + dx;
|
||||||
|
this.y = this.y + dy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Point obj = new Point;
|
||||||
|
obj.x = 1;
|
||||||
|
obj.y = 2;
|
||||||
|
|
||||||
|
printline("Current X,Y:");
|
||||||
|
printline(obj.x);
|
||||||
|
printline(obj.y);
|
||||||
|
|
||||||
|
// NOTE: WHY THE FUCK THIS FUCKING COMPILER REQUIRES ';' AFTER '='?!
|
||||||
|
// |
|
||||||
|
// v
|
||||||
|
obj.move(9, 8);
|
||||||
|
printline("X,Y after move:");
|
||||||
|
printline(obj.x);
|
||||||
|
printline(obj.y);
|
||||||
|
|
||||||
|
if (obj.x == 10 && obj.y == 10)
|
||||||
|
printline("Test passed");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("Test failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
// I'm a comment
|
||||||
|
printline("Comment is skipped");;;;;
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
bool flag = true;
|
||||||
|
|
||||||
|
if (!flag)
|
||||||
|
{
|
||||||
|
printline("buhhh, flag is not true");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else if (2 + 2 >= 4)
|
||||||
|
{
|
||||||
|
printline ("two plus two is four");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printline("gotcha");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
printline("block after");
|
||||||
|
}
|
||||||
|
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
fn fib(int n) int
|
||||||
|
{
|
||||||
|
if (n <= 1)
|
||||||
|
return n;
|
||||||
|
|
||||||
|
return fib(n - 1) + fib(n - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum(uint a, uint b) uint
|
||||||
|
{
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
printline("Test #1: fibonacci");
|
||||||
|
int result = fib(7);
|
||||||
|
if (result == 13)
|
||||||
|
printline("Recurse test passed");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("Recurse test failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printline("Test #2: sum");
|
||||||
|
if (sum(2, 2) == 4)
|
||||||
|
printline("Sum test passed");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("Sum test failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
class Node
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
Node next;
|
||||||
|
|
||||||
|
fn append(int val) Node
|
||||||
|
{
|
||||||
|
if (this.next == null)
|
||||||
|
{
|
||||||
|
Node tail = new Node;
|
||||||
|
tail.value = val;
|
||||||
|
tail.next = null;
|
||||||
|
this.next = tail;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.next = this.next.append(val);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum() int
|
||||||
|
{
|
||||||
|
if (this.next == null)
|
||||||
|
return this.value;
|
||||||
|
|
||||||
|
return this.value + this.next.sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn printList()
|
||||||
|
{
|
||||||
|
printline(this.value);
|
||||||
|
if (this.next != null)
|
||||||
|
this.next.printList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn createList(int count) Node
|
||||||
|
{
|
||||||
|
if (count <= 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
Node head = new Node;
|
||||||
|
head.value = count;
|
||||||
|
head.next = createList(count - 1);
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
printline("===== Test 1: factory & recursive escaping =====");
|
||||||
|
Node list = createList(3);
|
||||||
|
printline("Elements:");
|
||||||
|
list.printList();
|
||||||
|
|
||||||
|
printline("===== Test 2: mutation & methods =====");
|
||||||
|
list = list.append(10);
|
||||||
|
int total = list.sum();
|
||||||
|
printline("Sum of elements:");
|
||||||
|
printline(total);
|
||||||
|
|
||||||
|
if (total == 16)
|
||||||
|
printline("Tests passed!");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("Tests failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
printline('hello, world')
|
|
||||||
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
printline('hello, world');
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
printline("");
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
printline("hello);
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
class Animal
|
||||||
|
{
|
||||||
|
str type;
|
||||||
|
str name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
fn copy() Animal
|
||||||
|
{
|
||||||
|
Animal cp = new Animal;
|
||||||
|
cp.type = this.type;
|
||||||
|
cp.name = this.name;
|
||||||
|
cp.age = this.age;
|
||||||
|
|
||||||
|
return cp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Animal a = new Animal;
|
||||||
|
a.type = "Cow";
|
||||||
|
a.name = "Buryatka";
|
||||||
|
a.age = 5;
|
||||||
|
|
||||||
|
Animal b = a.copy();
|
||||||
|
b.name = "Gavryusha";
|
||||||
|
b.age = 10;
|
||||||
|
|
||||||
|
printline(a);
|
||||||
|
printline(b);
|
||||||
|
|
||||||
|
printline("Types are same:");
|
||||||
|
printline(a.type == b.type);
|
||||||
|
|
||||||
|
printline("Names are NOT the same");
|
||||||
|
printline(a.name != b.name);
|
||||||
|
|
||||||
|
printline("1st age less than 2nd");
|
||||||
|
printline(a.age < b.age);
|
||||||
|
|
||||||
|
if (a.type == b.type && a.name != b.name && a.age < b.age)
|
||||||
|
printline("Test passed");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("Test failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
uint work_it = 21 * 2 ^ 2;
|
||||||
|
int make_it = 10;
|
||||||
|
|
||||||
|
u64 do_it;
|
||||||
|
do_it = 15;
|
||||||
|
|
||||||
|
printline(work_it);
|
||||||
|
printline(make_it + do_it);
|
||||||
|
|
||||||
|
char make_us;
|
||||||
|
str floyd = "hello, Pink Floyd!";
|
||||||
|
printline(floyd);
|
||||||
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef FZL_BUILTINS_H
|
||||||
|
#define FZL_BUILTINS_H
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_BUILTIN_PRINTLINE, // Print message in arg with newline at end
|
||||||
|
FZL_BUILTIN_EXIT, // Exit program with code specified in arg
|
||||||
|
} fzl_builtin_t;
|
||||||
|
|
||||||
|
#endif // FZL_BUILTINS_H
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef FZL_COLORS_H
|
||||||
|
#define FZL_COLORS_H
|
||||||
|
|
||||||
|
#define FZL_CERROR "\033[0;31m"
|
||||||
|
#define FZL_CWARNING "\033[0;33m"
|
||||||
|
#define FZL_COK "\033[0;32m"
|
||||||
|
#define FZL_CDEBUG "\033[0;34m"
|
||||||
|
#define FZL_CRST "\033[0m"
|
||||||
|
|
||||||
|
#endif // FZL_COLORS_H
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#ifndef FZL_OPCODES_H
|
||||||
|
#define FZL_OPCODES_H
|
||||||
|
|
||||||
|
// Available opcodes
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_OPCODE_LOADCONST = 0xF00001, // Load const into T (temp) reg
|
||||||
|
FZL_OPCODE_CALL_BUILTIN = 0xF00002, // Call builtin function
|
||||||
|
FZL_OPCODE_PUSHNUM = 0xF00003, // Push number into stack
|
||||||
|
FZL_OPCODE_PUSHBOOL = 0xF00004, // Push boolean into stack
|
||||||
|
|
||||||
|
FZL_OPCODE_MK_ENVTABLE = 0xF10001, // Allocate new environment table
|
||||||
|
FZL_OPCODE_RM_ENVTABLE = 0xF10002, // Free environment table
|
||||||
|
FZL_OPCODE_STORE = 0xF10003, // Push variable to environment table
|
||||||
|
FZL_OPCODE_LOOKUP_ENV = 0xF10004, // Lookup name in environment table
|
||||||
|
FZL_OPCODE_WRITE = 0xF10005, // Wirte into variable
|
||||||
|
|
||||||
|
FZL_OPCODE_JMP = 0xF20001, // Jump to specified IP address
|
||||||
|
FZL_OPCODE_JZ = 0xF20002, // Jump if top value in dstack is zero
|
||||||
|
|
||||||
|
FZL_OPCODE_CALL = 0xF30001, // Call function
|
||||||
|
FZL_OPCODE_RET = 0xF30002, // Return from function
|
||||||
|
|
||||||
|
FZL_OPCODE_ALLOC_OBJECT = 0xFA0001, // Allocate N slots
|
||||||
|
FZL_OPCODE_GET_FIELD = 0xFA0002, // Get field N of an object at dstack
|
||||||
|
FZL_OPCODE_SET_FIELD = 0xFA0003, // Set field N of an object at dstack
|
||||||
|
FZL_OPCODE_ESCAPE_OBJECT = 0xFA0004, // Make parent envtable as owner
|
||||||
|
FZL_OPCODE_DEREFERENCE = 0xFA0005, // Dereference address
|
||||||
|
|
||||||
|
FZL_OPCODE_ADD = 0xA00001, // Sum two numbers in dstack
|
||||||
|
FZL_OPCODE_SUB = 0xA00002, // Subract two numbers in dstack
|
||||||
|
FZL_OPCODE_MUL = 0xA00003, // Multiply two numbers in dstack
|
||||||
|
FZL_OPCODE_DIV = 0xA00004, // Divide two numbers in dstack
|
||||||
|
FZL_OPCODE_POW = 0xA00005, // Power first number in dstack by second
|
||||||
|
FZL_OPCODE_MOD = 0xA00006, // Get module of two numbers in dstack
|
||||||
|
FZL_OPCODE_NEG = 0xA00007, // Negotiate number in dstack
|
||||||
|
|
||||||
|
FZL_OPCODE_CMP_EQ = 0xB00001, // Equal
|
||||||
|
FZL_OPCODE_CMP_NEQ = 0xB00002, // Not equal
|
||||||
|
FZL_OPCODE_CMP_LT = 0xB00003, // Less than
|
||||||
|
FZL_OPCODE_CMP_LTEQ = 0xB00004, // Less than or equal
|
||||||
|
FZL_OPCODE_CMP_GT = 0xB00005, // Greater than
|
||||||
|
FZL_OPCODE_CMP_GTEQ = 0xB00006, // Greater than or equal
|
||||||
|
|
||||||
|
FZL_OPCODE_CMP_EQ_STR = 0xB10001, // Strings equality
|
||||||
|
FZL_OPCODE_CMP_NEQ_STR = 0xB10002, // Strings non-equality
|
||||||
|
|
||||||
|
FZL_OPCODE_CMP_EQ_OBJ = 0xB20001, // Objects equality (by pointer)
|
||||||
|
FZL_OPCODE_CMP_NEQ_OBJ = 0xB20002, // Objects non-equality (by pointer)
|
||||||
|
|
||||||
|
FZL_OPCODE_AND = 0xC00001, // A && B
|
||||||
|
FZL_OPCODE_OR = 0xC00002, // A || B
|
||||||
|
|
||||||
|
FZL_OPCODE_NOT = 0xD00001, // !A
|
||||||
|
} fzl_opcode_t;
|
||||||
|
|
||||||
|
#endif // FZL_OPCODES_H
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef FZL_TYPES_H
|
||||||
|
#define FZL_TYPES_H
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_TYPE_NULL,
|
||||||
|
FZL_TYPE_OBJECT,
|
||||||
|
|
||||||
|
FZL_TYPE_STRING,
|
||||||
|
FZL_TYPE_CHAR,
|
||||||
|
FZL_TYPE_BOOL,
|
||||||
|
|
||||||
|
FZL_TYPE_INT8,
|
||||||
|
FZL_TYPE_UINT8,
|
||||||
|
FZL_TYPE_INT16,
|
||||||
|
FZL_TYPE_UINT16,
|
||||||
|
FZL_TYPE_INT32,
|
||||||
|
FZL_TYPE_UINT32,
|
||||||
|
FZL_TYPE_INT64,
|
||||||
|
FZL_TYPE_UINT64,
|
||||||
|
FZL_TYPE_INT, // machine word int
|
||||||
|
FZL_TYPE_UINT // machine word unsigned int
|
||||||
|
} fzl_type_t;
|
||||||
|
|
||||||
|
#endif // FZL_TYPES_H
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef FZL_VALUE_H
|
||||||
|
#define FZL_VALUE_H
|
||||||
|
|
||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// Value container descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
fzl_type_t type;
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
void *obj;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
const char *value;
|
||||||
|
size_t length;
|
||||||
|
} s;
|
||||||
|
|
||||||
|
char c;
|
||||||
|
bool b;
|
||||||
|
|
||||||
|
int8_t i8;
|
||||||
|
uint8_t u8;
|
||||||
|
|
||||||
|
int16_t i16;
|
||||||
|
uint16_t u16;
|
||||||
|
|
||||||
|
int32_t i32;
|
||||||
|
uint32_t u32;
|
||||||
|
|
||||||
|
int64_t i64;
|
||||||
|
uint64_t u64;
|
||||||
|
|
||||||
|
intptr_t i;
|
||||||
|
uintptr_t u;
|
||||||
|
} as;
|
||||||
|
} fzl_value_t;
|
||||||
|
|
||||||
|
#endif // FZL_VALUE_H
|
||||||
@@ -6,5 +6,4 @@
|
|||||||
// Print AST
|
// Print AST
|
||||||
void fzl_debug_print_ast(fzl_node_t *root);
|
void fzl_debug_print_ast(fzl_node_t *root);
|
||||||
|
|
||||||
#endif//FZL_DEBUG_H
|
#endif // FZL_DEBUG_H
|
||||||
|
|
||||||
|
|||||||
+144
-25
@@ -1,39 +1,156 @@
|
|||||||
#ifndef FZL_PARSER_H
|
#ifndef FZL_PARSER_H
|
||||||
#define FZL_PARSER_H
|
#define FZL_PARSER_H
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <compiler/fzl_token.h>
|
|
||||||
#include <compiler/fzl_reader.h>
|
#include <compiler/fzl_reader.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
// AST node type
|
// AST node type
|
||||||
typedef enum {
|
typedef enum
|
||||||
FZL_NODETYPE_BLOCK,
|
{
|
||||||
FZL_NODETYPE_CALL,
|
FZL_NODETYPE_BLOCK,
|
||||||
FZL_NODETYPE_LITERAL,
|
FZL_NODETYPE_CALL,
|
||||||
|
FZL_NODETYPE_LITERAL,
|
||||||
|
FZL_NODETYPE_IDENTIFIER,
|
||||||
|
FZL_NODETYPE_CONDITIONAL,
|
||||||
|
FZL_NODETYPE_FUNCTION,
|
||||||
|
FZL_NODETYPE_RETURN,
|
||||||
|
FZL_NODETYPE_CLASS,
|
||||||
|
FZL_NODETYPE_FIELD,
|
||||||
|
FZL_NODETYPE_CONSTRUCT,
|
||||||
|
|
||||||
|
FZL_NODETYPE_ADD,
|
||||||
|
FZL_NODETYPE_SUB,
|
||||||
|
FZL_NODETYPE_MUL,
|
||||||
|
FZL_NODETYPE_DIV,
|
||||||
|
FZL_NODETYPE_POW,
|
||||||
|
FZL_NODETYPE_MOD,
|
||||||
|
|
||||||
|
FZL_NODETYPE_UNARY_PLUS,
|
||||||
|
FZL_NODETYPE_UNARY_MINUS,
|
||||||
|
|
||||||
|
FZL_NODETYPE_OR,
|
||||||
|
FZL_NODETYPE_AND,
|
||||||
|
|
||||||
|
FZL_NODETYPE_EQUAL,
|
||||||
|
FZL_NODETYPE_NEQUAL,
|
||||||
|
FZL_NODETYPE_LESS,
|
||||||
|
FZL_NODETYPE_LESS_EQUAL,
|
||||||
|
FZL_NODETYPE_GREATER,
|
||||||
|
FZL_NODETYPE_GREATER_EQUAL,
|
||||||
|
|
||||||
|
FZL_NODETYPE_NOT,
|
||||||
|
|
||||||
|
FZL_NODETYPE_DECLARATION,
|
||||||
|
FZL_NODETYPE_ASSIGN
|
||||||
} fzl_nodetype_t;
|
} fzl_nodetype_t;
|
||||||
|
|
||||||
// AST node descriptor
|
// AST node descriptor
|
||||||
typedef struct fzl_node {
|
typedef struct fzl_node
|
||||||
fzl_nodetype_t type;
|
{
|
||||||
fzl_token_t *token;
|
fzl_nodetype_t type;
|
||||||
struct fzl_node *parent;
|
fzl_token_t *token;
|
||||||
struct fzl_node *next;
|
struct fzl_node *parent;
|
||||||
|
struct fzl_node *next;
|
||||||
|
|
||||||
union {
|
union
|
||||||
struct {
|
{
|
||||||
struct fzl_node **entries;
|
// Array of nested nodes (for blocks, etc)
|
||||||
size_t entry_count;
|
struct
|
||||||
} list;
|
{
|
||||||
};
|
struct fzl_node **entries;
|
||||||
|
size_t entry_count;
|
||||||
|
} list;
|
||||||
|
|
||||||
|
// Call data
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node *callee;
|
||||||
|
|
||||||
|
struct fzl_node **args;
|
||||||
|
size_t args_count;
|
||||||
|
} call;
|
||||||
|
|
||||||
|
// Binary operations
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node *left;
|
||||||
|
struct fzl_node *right;
|
||||||
|
} binary_op;
|
||||||
|
|
||||||
|
// Unary operations
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node *operand;
|
||||||
|
} unary_op;
|
||||||
|
|
||||||
|
// Variables declaration and initializing
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
fzl_token_t *type;
|
||||||
|
struct fzl_node *identifier;
|
||||||
|
} var;
|
||||||
|
|
||||||
|
// Conditional blocks
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node *condition;
|
||||||
|
struct fzl_node *true_block;
|
||||||
|
struct fzl_node *else_block;
|
||||||
|
} cond;
|
||||||
|
|
||||||
|
// Function declaration
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node **args;
|
||||||
|
size_t args_count;
|
||||||
|
|
||||||
|
struct fzl_node *body;
|
||||||
|
fzl_token_t *name;
|
||||||
|
fzl_token_t *type;
|
||||||
|
} fn;
|
||||||
|
|
||||||
|
// Return value
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node *value;
|
||||||
|
} ret;
|
||||||
|
|
||||||
|
// Class declaration
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
fzl_token_t *name;
|
||||||
|
|
||||||
|
struct fzl_node **fields;
|
||||||
|
size_t fields_count;
|
||||||
|
|
||||||
|
struct fzl_node **methods;
|
||||||
|
size_t methods_count;
|
||||||
|
} class;
|
||||||
|
|
||||||
|
// Member access
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node *parent;
|
||||||
|
struct fzl_node *field;
|
||||||
|
} memb;
|
||||||
|
|
||||||
|
// Object construct
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node *name;
|
||||||
|
} construct;
|
||||||
|
};
|
||||||
} fzl_node_t;
|
} fzl_node_t;
|
||||||
|
|
||||||
// Token sequence parser descriptor
|
// Token sequence parser descriptor
|
||||||
typedef struct {
|
typedef struct
|
||||||
fzl_reader_ll_t *first;
|
{
|
||||||
size_t token_count;
|
fzl_reader_ll_t *first;
|
||||||
|
size_t token_count;
|
||||||
|
|
||||||
fzl_reader_ll_t *cursor;
|
fzl_reader_ll_t *cursor;
|
||||||
fzl_node_t *root;
|
fzl_node_t *root;
|
||||||
} fzl_parser_t;
|
} fzl_parser_t;
|
||||||
|
|
||||||
// Allocate parser
|
// Allocate parser
|
||||||
@@ -49,10 +166,12 @@ int fzl_parse(fzl_parser_t *parser);
|
|||||||
char *fzl_parser_geterror();
|
char *fzl_parser_geterror();
|
||||||
|
|
||||||
// Free node
|
// Free node
|
||||||
void fzl_freenode(fzl_node_t* node);
|
void fzl_freenode(fzl_node_t *node);
|
||||||
|
|
||||||
|
// Free entire AST going from root node
|
||||||
|
void fzl_freeast(fzl_node_t *root);
|
||||||
|
|
||||||
// Free parser
|
// Free parser
|
||||||
void fzl_freeparser(fzl_parser_t *parser);
|
void fzl_freeparser(fzl_parser_t *parser);
|
||||||
|
|
||||||
#endif//FZL_PARSER_H
|
#endif // FZL_PARSER_H
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,35 @@
|
|||||||
#ifndef FZL_READER_H
|
#ifndef FZL_READER_H
|
||||||
#define FZL_READER_H
|
#define FZL_READER_H
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <compiler/fzl_token.h>
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <compiler/utils/fzl_linked.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
// Tokens linked list descriptor
|
// Tokens linked list descriptor
|
||||||
typedef struct fzl_reader_ll {
|
typedef struct fzl_reader_ll
|
||||||
fzl_token_t *token;
|
{
|
||||||
struct fzl_reader_ll *next;
|
fzl_token_t *token;
|
||||||
|
struct fzl_reader_ll *next;
|
||||||
} fzl_reader_ll_t;
|
} fzl_reader_ll_t;
|
||||||
|
|
||||||
// Source reader description
|
// Source reader description
|
||||||
typedef struct {
|
typedef struct
|
||||||
fzl_reader_ll_t *first;
|
{
|
||||||
fzl_reader_ll_t *last;
|
fzl_reader_ll_t *first;
|
||||||
|
fzl_reader_ll_t *last;
|
||||||
|
|
||||||
char *source;
|
fzl_linked_t *linked;
|
||||||
size_t position;
|
|
||||||
bool at_end;
|
char *source;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
size_t position;
|
||||||
} fzl_reader_t;
|
} fzl_reader_t;
|
||||||
|
|
||||||
// Allocate new reader
|
// Allocate new reader
|
||||||
fzl_reader_t *fzl_mkreader(char *source);
|
fzl_reader_t *fzl_mkreader(fzl_linked_t *linked);
|
||||||
|
|
||||||
// Read source file
|
// Read source file
|
||||||
int fzl_read(fzl_reader_t *reader);
|
int fzl_read(fzl_reader_t *reader);
|
||||||
@@ -34,5 +40,4 @@ char *fzl_reader_geterror();
|
|||||||
// Free reader
|
// Free reader
|
||||||
void fzl_freereader(fzl_reader_t *reader);
|
void fzl_freereader(fzl_reader_t *reader);
|
||||||
|
|
||||||
#endif//FZL_READER_H
|
#endif // FZL_READER_H
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +1,79 @@
|
|||||||
#ifndef FZL_TOKEN_H
|
#ifndef FZL_TOKEN_H
|
||||||
#define FZL_TOKEN_H
|
#define FZL_TOKEN_H
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
// Token type
|
// Token type
|
||||||
typedef enum {
|
typedef enum
|
||||||
FZL_TOKTYPE_NAME,
|
{
|
||||||
FZL_TOKTYPE_LPAIR,
|
FZL_TOKTYPE_CLASS, // class
|
||||||
FZL_TOKTYPE_RPAIR,
|
FZL_TOKTYPE_NEW, // new
|
||||||
FZL_TOKTYPE_COMMA,
|
|
||||||
FZL_TOKTYPE_STR_LITERAL
|
FZL_TOKTYPE_FUNCTION, // fn
|
||||||
|
FZL_TOKTYPE_RETURN, // return
|
||||||
|
|
||||||
|
FZL_TOKTYPE_IF, // if
|
||||||
|
FZL_TOKTYPE_ELSE, // else
|
||||||
|
|
||||||
|
FZL_TOKTYPE_NAME, // abc
|
||||||
|
FZL_TOKTYPE_TYPE, // i16
|
||||||
|
|
||||||
|
FZL_TOKTYPE_DDASH, // // :D
|
||||||
|
|
||||||
|
FZL_TOKTYPE_LPAREN, // (
|
||||||
|
FZL_TOKTYPE_RPAREN, // )
|
||||||
|
FZL_TOKTYPE_LBRACE, // {
|
||||||
|
FZL_TOKTYPE_RBRACE, // }
|
||||||
|
|
||||||
|
FZL_TOKTYPE_COMMA, // ,
|
||||||
|
FZL_TOKTYPE_DOT, // .
|
||||||
|
FZL_TOKTYPE_SEMICOLON, // ;
|
||||||
|
|
||||||
|
FZL_TOKTYPE_STR_LITERAL, // ""
|
||||||
|
FZL_TOKTYPE_NUM_LITERAL, // 123
|
||||||
|
FZL_TOKTYPE_BOOL_LITERAL, // true
|
||||||
|
FZL_TOKTYPE_NULL_LITERAL, // null
|
||||||
|
|
||||||
|
FZL_TOKTYPE_DAMPERSAND, // &&
|
||||||
|
FZL_TOKTYPE_DVBAR, // ||
|
||||||
|
|
||||||
|
FZL_TOKTYPE_EQEQ, // ==
|
||||||
|
FZL_TOKTYPE_REQ, // >=
|
||||||
|
FZL_TOKTYPE_LEQ, // <=
|
||||||
|
FZL_TOKTYPE_NEQ, // !=
|
||||||
|
FZL_TOKTYPE_LANGLE, // <
|
||||||
|
FZL_TOKTYPE_RANGLE, // >
|
||||||
|
FZL_TOKTYPE_NOT, // !
|
||||||
|
|
||||||
|
FZL_TOKTYPE_STAR, // *
|
||||||
|
FZL_TOKTYPE_SLASH, // /
|
||||||
|
FZL_TOKTYPE_PLUS, // +
|
||||||
|
FZL_TOKTYPE_MINUS, // -
|
||||||
|
FZL_TOKTYPE_CARET, // ^
|
||||||
|
FZL_TOKTYPE_PERCENT, // %
|
||||||
|
|
||||||
|
FZL_TOKTYPE_EQ // =
|
||||||
} fzl_toktype_t;
|
} fzl_toktype_t;
|
||||||
|
|
||||||
// Token descriptor
|
// Token descriptor
|
||||||
typedef struct {
|
typedef struct
|
||||||
fzl_toktype_t type;
|
{
|
||||||
uint16_t i0;
|
fzl_toktype_t type;
|
||||||
uint16_t i1;
|
size_t ln;
|
||||||
bool store_value;
|
size_t cn;
|
||||||
void *value;
|
bool store_value;
|
||||||
uint16_t value_sz;
|
void *value;
|
||||||
|
uint16_t value_sz;
|
||||||
|
char *filename;
|
||||||
} fzl_token_t;
|
} fzl_token_t;
|
||||||
|
|
||||||
// Allocate new token
|
// Allocate new token
|
||||||
fzl_token_t *fzl_mktoken(
|
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, size_t line, size_t col,
|
||||||
const fzl_toktype_t type, uint16_t from, uint16_t to,
|
void *value, size_t value_sz, char *filename);
|
||||||
void *value, size_t value_sz);
|
|
||||||
|
|
||||||
// Free token and its contents
|
// Free token and its contents
|
||||||
void fzl_freetoken(fzl_token_t *token);
|
void fzl_freetoken(fzl_token_t *token);
|
||||||
|
|
||||||
#endif//FZL_TOKEN_H
|
#endif // FZL_TOKEN_H
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
#ifndef FZL_WALKER_H
|
||||||
|
#define FZL_WALKER_H
|
||||||
|
|
||||||
|
#include <common/fzl_opcodes.h>
|
||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/utils/fzl_const_pool.h>
|
||||||
|
#include <compiler/utils/fzl_ctable.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// Type of context
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_WALK_CTX_TYPE_GLOBAL,
|
||||||
|
FZL_WALK_CTX_TYPE_FN,
|
||||||
|
FZL_WALK_CTX_TYPE_CLASS,
|
||||||
|
FZL_WALK_CTX_TYPE_ASSIGN
|
||||||
|
} fzl_walk_ctx_type_t;
|
||||||
|
|
||||||
|
// Internal walk function result
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
fzl_type_t type;
|
||||||
|
|
||||||
|
char *error_msg;
|
||||||
|
bool error;
|
||||||
|
|
||||||
|
char *classname;
|
||||||
|
} fzl_walk_res_t;
|
||||||
|
|
||||||
|
// Function context descriptor
|
||||||
|
typedef struct fzl_walk_ctx
|
||||||
|
{
|
||||||
|
fzl_walk_ctx_type_t type;
|
||||||
|
struct fzl_walk_ctx *parent;
|
||||||
|
|
||||||
|
fzl_node_t *fn;
|
||||||
|
size_t initial_env_lvl;
|
||||||
|
} fzl_walk_ctx_t;
|
||||||
|
|
||||||
|
// Walker descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
fzl_node_t *root;
|
||||||
|
fzl_const_hash_pool_t *consts;
|
||||||
|
|
||||||
|
uint64_t *ops;
|
||||||
|
size_t op_count;
|
||||||
|
size_t op_remainder;
|
||||||
|
|
||||||
|
fzl_symtable_t *syms[64];
|
||||||
|
size_t sym_top;
|
||||||
|
|
||||||
|
fzl_walk_ctx_t *ctx;
|
||||||
|
|
||||||
|
fzl_ctable_t *classes[64];
|
||||||
|
size_t classes_count;
|
||||||
|
} fzl_walker_t;
|
||||||
|
|
||||||
|
// Allocate new walker
|
||||||
|
fzl_walker_t *fzl_mkwalker(fzl_node_t *root);
|
||||||
|
|
||||||
|
// Walk throught the entire AST and build:
|
||||||
|
// 1. Constants table
|
||||||
|
// 2. Operations
|
||||||
|
int fzl_walk(fzl_walker_t *walker);
|
||||||
|
|
||||||
|
// Get last error message
|
||||||
|
char *fzl_walker_geterror();
|
||||||
|
|
||||||
|
// Free walker
|
||||||
|
void fzl_freewalker(fzl_walker_t *walker);
|
||||||
|
|
||||||
|
#endif // FZL_WALKER_H
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef FZL_WRITER_H
|
||||||
|
#define FZL_WRITER_H
|
||||||
|
|
||||||
|
#include <compiler/fzl_walker.h>
|
||||||
|
#include <compiler/utils/fzl_const_pool.h>
|
||||||
|
|
||||||
|
// File writer descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
const char *filepath;
|
||||||
|
} fzl_writer_t;
|
||||||
|
|
||||||
|
// Allocate writer
|
||||||
|
fzl_writer_t *fzl_mkwriter(const char *filepath);
|
||||||
|
|
||||||
|
// Write out a file
|
||||||
|
int fzl_write(fzl_writer_t *writer, fzl_walker_t *walker);
|
||||||
|
|
||||||
|
// Get last writer error message
|
||||||
|
char *fzl_writer_geterror();
|
||||||
|
|
||||||
|
// Free writer
|
||||||
|
void fzl_freewriter(fzl_writer_t *writer);
|
||||||
|
|
||||||
|
#endif // FZL_WRITER_H
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
#ifndef FZL_CONST_SET_H
|
||||||
|
#define FZL_CONST_SET_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Constant types
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_CONSTTYPE_STRING,
|
||||||
|
} fzl_consttype_t;
|
||||||
|
|
||||||
|
// Constant descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
fzl_consttype_t type;
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
const char *chars;
|
||||||
|
size_t length;
|
||||||
|
} string;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
uint64_t value;
|
||||||
|
} num;
|
||||||
|
} as;
|
||||||
|
} fzl_const_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
ssize_t pool_index;
|
||||||
|
} fzl_const_hash_slot_t;
|
||||||
|
|
||||||
|
// Constant pool descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
fzl_const_t *array;
|
||||||
|
size_t size;
|
||||||
|
size_t capacity;
|
||||||
|
|
||||||
|
fzl_const_hash_slot_t *slots;
|
||||||
|
size_t slots_count;
|
||||||
|
size_t slots_capacity;
|
||||||
|
} fzl_const_hash_pool_t;
|
||||||
|
|
||||||
|
// Compare two constants for equality
|
||||||
|
bool fzl_consts_equal(fzl_const_t a, fzl_const_t b);
|
||||||
|
|
||||||
|
// Get hash of specified constant
|
||||||
|
uint32_t fzl_hash_const(fzl_const_t c);
|
||||||
|
|
||||||
|
// Allocate constants hash pool
|
||||||
|
fzl_const_hash_pool_t *fzl_mkconstpool();
|
||||||
|
|
||||||
|
// Resize the hash table
|
||||||
|
void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool);
|
||||||
|
|
||||||
|
// Push constant. Returns unique index
|
||||||
|
size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c);
|
||||||
|
|
||||||
|
// Free constants hash pool
|
||||||
|
void fzl_consts_pool_free(fzl_const_hash_pool_t *pool);
|
||||||
|
|
||||||
|
#endif // FZL_CONST_SET_H
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef FZL_CTABLE_H
|
||||||
|
#define FZL_CTABLE_H
|
||||||
|
|
||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// Class field descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
size_t slot_index;
|
||||||
|
fzl_type_t type;
|
||||||
|
} fzl_ctable_field_t;
|
||||||
|
|
||||||
|
// Class table descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
fzl_ctable_field_t *fields;
|
||||||
|
size_t fields_count;
|
||||||
|
size_t fields_remainder;
|
||||||
|
} fzl_ctable_t;
|
||||||
|
|
||||||
|
// Allocate class table
|
||||||
|
fzl_ctable_t *fzl_mkctable(char *name);
|
||||||
|
|
||||||
|
// Free class table
|
||||||
|
void fzl_freectable(fzl_ctable_t *ct);
|
||||||
|
|
||||||
|
#endif // FZL_CTABLE_H
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#ifndef FZL_LINKED_H
|
||||||
|
#define FZL_LINKED_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// Linked file descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
size_t start;
|
||||||
|
size_t end;
|
||||||
|
} fzl_linked_entry_t;
|
||||||
|
|
||||||
|
// Linked descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
fzl_linked_entry_t *entries;
|
||||||
|
size_t entries_count;
|
||||||
|
|
||||||
|
char *source;
|
||||||
|
size_t total_sz;
|
||||||
|
} fzl_linked_t;
|
||||||
|
|
||||||
|
// Position in file
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
size_t line;
|
||||||
|
size_t col;
|
||||||
|
|
||||||
|
char *filename;
|
||||||
|
} fzl_file_loc;
|
||||||
|
|
||||||
|
// Allocated new linked
|
||||||
|
fzl_linked_t *fzl_mklinked();
|
||||||
|
|
||||||
|
// Read specified files
|
||||||
|
bool fzl_linked_add_files(fzl_linked_t *l, char **files, size_t files_count);
|
||||||
|
|
||||||
|
// Get last error from linked
|
||||||
|
char *fzl_linked_geterror();
|
||||||
|
|
||||||
|
// Get filename and position in it by reader cursor
|
||||||
|
bool fzl_linked_getpos(fzl_linked_t *l, size_t pos, fzl_file_loc *res);
|
||||||
|
|
||||||
|
// Free linked obj
|
||||||
|
void fzl_freelinked(fzl_linked_t *linked);
|
||||||
|
|
||||||
|
#endif // FZL_LINKED_H
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef FZL_OPTIONS_H
|
||||||
|
#define FZL_OPTIONS_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// Compiler options
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char **files;
|
||||||
|
size_t files_count;
|
||||||
|
|
||||||
|
char *outname;
|
||||||
|
|
||||||
|
bool should_continue;
|
||||||
|
bool verbose;
|
||||||
|
} fzl_options_t;
|
||||||
|
|
||||||
|
// Parse command line arguments
|
||||||
|
fzl_options_t *fzl_mkoptions(int argc, char **argv);
|
||||||
|
|
||||||
|
// Free options
|
||||||
|
void fzl_freeoptions(fzl_options_t *options);
|
||||||
|
|
||||||
|
#endif // FZL_OPTIONS_H
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
#ifndef FZL_SYMTABLE_H
|
||||||
|
#define FZL_SYMTABLE_H
|
||||||
|
|
||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_SYMTABLE_RESULT_OK,
|
||||||
|
FZL_SYMTABLE_RESULT_ERR,
|
||||||
|
FZL_SYMTABLE_RESULT_EXISTS,
|
||||||
|
FZL_SYMTABLE_RESULT_NOT_OBJECT
|
||||||
|
} fzl_symtable_res_t;
|
||||||
|
|
||||||
|
// Symbols table entry descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
fzl_type_t type;
|
||||||
|
size_t ip;
|
||||||
|
|
||||||
|
char *classname;
|
||||||
|
} fzl_sym_t;
|
||||||
|
|
||||||
|
// Symbols table descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
size_t size;
|
||||||
|
size_t remainder;
|
||||||
|
|
||||||
|
fzl_sym_t *symbols;
|
||||||
|
} fzl_symtable_t;
|
||||||
|
|
||||||
|
// Allocate new symbtable
|
||||||
|
fzl_symtable_t *fzl_mksymt();
|
||||||
|
|
||||||
|
// Put new entry into symtable
|
||||||
|
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
||||||
|
fzl_type_t type, uint64_t ip);
|
||||||
|
|
||||||
|
// Assign class name to object type
|
||||||
|
fzl_symtable_res_t fzl_symt_setobjclass(fzl_symtable_t *symt, char *name,
|
||||||
|
char *classname);
|
||||||
|
|
||||||
|
// Get entry from specified symtable
|
||||||
|
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res);
|
||||||
|
|
||||||
|
// Free symbtable
|
||||||
|
void fzl_freesymt(fzl_symtable_t *symt);
|
||||||
|
|
||||||
|
#endif // FZL_SYMTABLE_H
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#ifndef FZL_VM_H
|
||||||
|
#define FZL_VM_H
|
||||||
|
|
||||||
|
#include <common/fzl_value.h>
|
||||||
|
#include <compiler/utils/fzl_const_pool.h> // NOTE: move outside compiler scope to common
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <vm/fzl_vm_env.h>
|
||||||
|
#include <vm/fzl_vm_object.h>
|
||||||
|
|
||||||
|
// VM descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
fzl_const_t *consts;
|
||||||
|
size_t consts_count;
|
||||||
|
|
||||||
|
uint64_t *ops;
|
||||||
|
size_t ops_count;
|
||||||
|
|
||||||
|
size_t ip;
|
||||||
|
|
||||||
|
// Data stack
|
||||||
|
fzl_value_t dstack[256];
|
||||||
|
size_t dtop;
|
||||||
|
|
||||||
|
// Call stack
|
||||||
|
size_t cstack[256];
|
||||||
|
size_t ctop;
|
||||||
|
|
||||||
|
// Environment stack
|
||||||
|
fzl_vm_env_t *estack[256];
|
||||||
|
size_t etop;
|
||||||
|
} fzl_vm_t;
|
||||||
|
|
||||||
|
// Allocate FZL VM
|
||||||
|
fzl_vm_t *fzl_mkvm(const char *filepath);
|
||||||
|
|
||||||
|
// Load and run code
|
||||||
|
int fzl_vm_run(fzl_vm_t *vm);
|
||||||
|
|
||||||
|
// Get last VM error message
|
||||||
|
char *fzl_vm_geterror();
|
||||||
|
|
||||||
|
// Print opcodes sequence
|
||||||
|
void fzl_vm_print_ops(fzl_vm_t *vm);
|
||||||
|
|
||||||
|
// Free VM
|
||||||
|
void fzl_freevm(fzl_vm_t *vm);
|
||||||
|
|
||||||
|
#endif // FZL_VM_H
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#ifndef FZL_VM_ENV_H
|
||||||
|
#define FZL_VM_ENV_H
|
||||||
|
|
||||||
|
#include <common/fzl_value.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <vm/fzl_vm_object.h>
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_VM_ENV_WRESULT_OK,
|
||||||
|
FZL_VM_ENV_WRESULT_ERR,
|
||||||
|
FZL_VM_ENV_WRESULT_ERR_EXISTS,
|
||||||
|
FZL_VM_ENV_WRESULT_ERR_NOT_FOUND
|
||||||
|
} fzl_vm_env_wresult_t;
|
||||||
|
|
||||||
|
// Environment table entry descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
fzl_value_t value;
|
||||||
|
} fzl_vm_env_entry_t;
|
||||||
|
|
||||||
|
// Environment table descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
size_t size;
|
||||||
|
size_t remainder;
|
||||||
|
|
||||||
|
fzl_vm_env_entry_t *table;
|
||||||
|
|
||||||
|
fzl_vm_object_t **owned_objects;
|
||||||
|
size_t owned_objects_remainder;
|
||||||
|
size_t owned_objects_count;
|
||||||
|
} fzl_vm_env_t;
|
||||||
|
|
||||||
|
// Allocate env table
|
||||||
|
fzl_vm_env_t *fzl_mkenv();
|
||||||
|
|
||||||
|
// Try get value by name
|
||||||
|
bool fzl_env_lookup(fzl_vm_env_t *env, char *name, fzl_value_t *res);
|
||||||
|
|
||||||
|
// Check name in env table
|
||||||
|
bool fzl_env_contains(fzl_vm_env_t *env, char *name);
|
||||||
|
|
||||||
|
// Store new value
|
||||||
|
fzl_vm_env_wresult_t fzl_env_storevalue(fzl_vm_env_t *env, char *name,
|
||||||
|
fzl_value_t val);
|
||||||
|
|
||||||
|
// Overwrite existing value
|
||||||
|
fzl_vm_env_wresult_t fzl_env_writevalue(fzl_vm_env_t *env, char *name,
|
||||||
|
fzl_value_t newval);
|
||||||
|
|
||||||
|
// Free env table
|
||||||
|
void fzl_freeenv(fzl_vm_env_t *env);
|
||||||
|
|
||||||
|
#endif // FZL_VM_ENV_H
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef FZL_VM_OBJECT_H
|
||||||
|
#define FZL_VM_OBJECT_H
|
||||||
|
|
||||||
|
#include <common/fzl_value.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// Object descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
fzl_value_t *values;
|
||||||
|
size_t values_count;
|
||||||
|
} fzl_vm_object_t;
|
||||||
|
|
||||||
|
#endif // FZL_VM_OBJECT_H
|
||||||
+150
-55
@@ -1,82 +1,177 @@
|
|||||||
|
#include "compiler/fzl_parser.h"
|
||||||
#include <compiler/fzl_debug.h>
|
#include <compiler/fzl_debug.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
size_t print_level(char *buf, int level)
|
size_t print_level(char *buf, int level)
|
||||||
{
|
{
|
||||||
int lvl = 0;
|
int lvl = 0;
|
||||||
while (lvl < level)
|
while (lvl < level)
|
||||||
{
|
{
|
||||||
buf[lvl] = '-';
|
buf[lvl] = '-';
|
||||||
lvl++;
|
lvl++;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf[lvl] = '>';
|
buf[lvl] = '>';
|
||||||
return strlen(buf);
|
return strlen(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_node(fzl_node_t *node, int level)
|
void print_node(fzl_node_t *node, int level)
|
||||||
{
|
{
|
||||||
char levelbuf[32];
|
char levelbuf[32];
|
||||||
char printbuf[256];
|
char printbuf[256];
|
||||||
memset(levelbuf, 0, sizeof(levelbuf));
|
memset(levelbuf, 0, sizeof(levelbuf));
|
||||||
memset(printbuf, 0, sizeof(printbuf));
|
memset(printbuf, 0, sizeof(printbuf));
|
||||||
|
|
||||||
if (!node) return;
|
if (!node)
|
||||||
|
return;
|
||||||
|
|
||||||
size_t lvllen = print_level(levelbuf, level);
|
strcpy(printbuf, levelbuf);
|
||||||
strcpy(printbuf, levelbuf);
|
|
||||||
|
|
||||||
switch (node->type)
|
switch (node->type)
|
||||||
{
|
{
|
||||||
case FZL_NODETYPE_CALL:
|
case FZL_NODETYPE_CONSTRUCT:
|
||||||
if (node->list.entry_count > 0)
|
printf("%s Construct class '%s':\n", levelbuf,
|
||||||
{
|
(char *)node->construct.name->token->value);
|
||||||
printf("%s Call fn '%s' with args:\n", levelbuf, (char*)node->token->value);
|
break;
|
||||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
|
||||||
print_node(node->list.entries[i], level + 1);
|
|
||||||
}
|
|
||||||
else printf("%s Call fn '%s' w/o args\n", levelbuf, (char*)node->token->value);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FZL_NODETYPE_LITERAL:
|
case FZL_NODETYPE_CLASS:
|
||||||
printf("%s Literal '%s'\n",
|
printf("%s Class '%s':\n", levelbuf, (char *)node->token->value);
|
||||||
levelbuf,
|
printf("-%s Fields:\n", levelbuf);
|
||||||
(char*)node->token->value);
|
for (size_t i = 0; i < node->class.fields_count; i++)
|
||||||
break;
|
print_node(node->class.fields[i], level + 2);
|
||||||
|
printf("-%s Methods:\n", levelbuf);
|
||||||
|
for (size_t i = 0; i < node->class.methods_count; i++)
|
||||||
|
print_node(node->class.methods[i], level + 2);
|
||||||
|
break;
|
||||||
|
|
||||||
case FZL_NODETYPE_BLOCK:
|
case FZL_NODETYPE_FIELD:
|
||||||
printf("%s Block: ", levelbuf);
|
printf("%s Field '%s' access:\n", levelbuf,
|
||||||
|
(char *)node->memb.field->token->value);
|
||||||
|
print_node(node->memb.parent, level + 1);
|
||||||
|
break;
|
||||||
|
|
||||||
if (node->list.entry_count < 1)
|
case FZL_NODETYPE_DECLARATION:
|
||||||
{
|
printf("%s Declare variable with type %d:\n", levelbuf,
|
||||||
printf("<empty>\n");
|
node->var.type->type);
|
||||||
break;
|
print_node(node->var.identifier, level + 1);
|
||||||
}
|
break;
|
||||||
|
|
||||||
printf("\n");
|
case FZL_NODETYPE_ASSIGN:
|
||||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
printf("%s Assign:\n", levelbuf);
|
||||||
print_node(node->list.entries[i], level + 1);
|
printf("-%s Target:\n", levelbuf);
|
||||||
|
print_node(node->binary_op.left, level + 2);
|
||||||
|
printf("-%s Source:\n", levelbuf);
|
||||||
|
print_node(node->binary_op.right, level + 2);
|
||||||
|
break;
|
||||||
|
|
||||||
break;
|
case FZL_NODETYPE_CALL:
|
||||||
|
if (node->call.args_count > 0)
|
||||||
|
{
|
||||||
|
printf("%s Call fn '%s' with args:\n", levelbuf,
|
||||||
|
(char *)node->token->value);
|
||||||
|
for (size_t i = 0; i < node->call.args_count; i++)
|
||||||
|
print_node(node->call.args[i], level + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("%s Call fn '%s' w/o args\n", levelbuf,
|
||||||
|
(char *)node->token->value);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
case FZL_NODETYPE_LITERAL:
|
||||||
printf("%s Secure node\n", levelbuf);
|
printf("%s Literal '%s'\n", levelbuf, (char *)node->token->value);
|
||||||
}
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_CONDITIONAL:
|
||||||
|
printf("%s Conditional block:\n", levelbuf);
|
||||||
|
printf("-%s Condition:\n", levelbuf);
|
||||||
|
print_node(node->cond.condition, level + 2);
|
||||||
|
printf("-%s True block:\n", levelbuf);
|
||||||
|
print_node(node->cond.true_block, level + 2);
|
||||||
|
if (node->cond.else_block)
|
||||||
|
{
|
||||||
|
printf("-%s Else block:\n", levelbuf);
|
||||||
|
print_node(node->cond.else_block, level + 2);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_BLOCK:
|
||||||
|
printf("%s Block: ", levelbuf);
|
||||||
|
|
||||||
|
if (node->list.entry_count < 1)
|
||||||
|
{
|
||||||
|
printf("<empty>\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||||
|
print_node(node->list.entries[i], level + 1);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_NOT:
|
||||||
|
printf("%s Inverse:\n", levelbuf);
|
||||||
|
print_node(node->unary_op.operand, level + 1);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_FUNCTION:
|
||||||
|
printf("%s Function named '%s' with ret type '%s':\n", levelbuf,
|
||||||
|
(char *)node->fn.name->value, (char *)node->fn.type->value);
|
||||||
|
printf("-%s Args:\n", levelbuf);
|
||||||
|
for (size_t i = 0; i < node->fn.args_count; i++)
|
||||||
|
print_node(node->fn.args[i], level + 2);
|
||||||
|
printf("-%s Body:\n", levelbuf);
|
||||||
|
print_node(node->fn.body, level + 2);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_RETURN:
|
||||||
|
printf("%s Return:\n", levelbuf);
|
||||||
|
if (node->ret.value)
|
||||||
|
print_node(node->ret.value, level + 1);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_AND:
|
||||||
|
case FZL_NODETYPE_OR:
|
||||||
|
case FZL_NODETYPE_EQUAL:
|
||||||
|
case FZL_NODETYPE_NEQUAL:
|
||||||
|
case FZL_NODETYPE_LESS:
|
||||||
|
case FZL_NODETYPE_LESS_EQUAL:
|
||||||
|
case FZL_NODETYPE_GREATER:
|
||||||
|
case FZL_NODETYPE_GREATER_EQUAL:
|
||||||
|
case FZL_NODETYPE_ADD:
|
||||||
|
case FZL_NODETYPE_SUB:
|
||||||
|
case FZL_NODETYPE_MUL:
|
||||||
|
case FZL_NODETYPE_DIV:
|
||||||
|
case FZL_NODETYPE_POW:
|
||||||
|
case FZL_NODETYPE_MOD:
|
||||||
|
printf("%s Binary op (%d)\n", levelbuf, node->type);
|
||||||
|
printf("-%s LEFT:\n", levelbuf);
|
||||||
|
print_node(node->binary_op.left, level + 2);
|
||||||
|
printf("-%s RIGHT:\n", levelbuf);
|
||||||
|
print_node(node->binary_op.right, level + 2);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_IDENTIFIER:
|
||||||
|
printf("%s Identifier ('%s')\n", levelbuf, (char *)node->token->value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("%s Secure node\n", levelbuf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void fzl_debug_print_ast(fzl_node_t *root)
|
void fzl_debug_print_ast(fzl_node_t *root)
|
||||||
{
|
{
|
||||||
printf("> AST tree:\n");
|
printf("> AST tree:\n");
|
||||||
|
|
||||||
fzl_node_t *curr = root;
|
fzl_node_t *curr = root;
|
||||||
while (curr != NULL)
|
while (curr != NULL)
|
||||||
{
|
{
|
||||||
print_node(curr, 2);
|
print_node(curr, 2);
|
||||||
curr = curr->next;
|
curr = curr->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("-> FINISH\n");
|
printf("-> FINISH\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1119
-132
File diff suppressed because it is too large
Load Diff
+329
-140
@@ -1,4 +1,8 @@
|
|||||||
|
#include "compiler/fzl_token.h"
|
||||||
|
#include "compiler/utils/fzl_linked.h"
|
||||||
#include <compiler/fzl_reader.h>
|
#include <compiler/fzl_reader.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -6,197 +10,382 @@
|
|||||||
// Last error from reader
|
// Last error from reader
|
||||||
static char last_err[256] = {0};
|
static char last_err[256] = {0};
|
||||||
|
|
||||||
fzl_reader_t *fzl_mkreader(char *source)
|
fzl_reader_t *fzl_mkreader(fzl_linked_t *linked)
|
||||||
{
|
{
|
||||||
fzl_reader_ll_t *first_node = malloc(sizeof(fzl_reader_ll_t));
|
fzl_reader_ll_t *first_node = malloc(sizeof(fzl_reader_ll_t));
|
||||||
first_node->token = NULL;
|
first_node->token = NULL;
|
||||||
first_node->next = NULL;
|
first_node->next = NULL;
|
||||||
|
|
||||||
fzl_reader_t *reader = malloc(sizeof(fzl_reader_t));
|
fzl_reader_t *reader = malloc(sizeof(fzl_reader_t));
|
||||||
reader->first = first_node;
|
reader->first = first_node;
|
||||||
reader->last = first_node;
|
reader->last = first_node;
|
||||||
reader->source = source;
|
reader->linked = linked;
|
||||||
reader->position = 0;
|
reader->source = linked->source;
|
||||||
reader->at_end = true;
|
reader->size = linked->total_sz;
|
||||||
|
reader->position = 0;
|
||||||
|
|
||||||
return reader;
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Read zone ---
|
// --- Read zone ---
|
||||||
|
|
||||||
|
// Move cursor forward
|
||||||
|
// @deprecated
|
||||||
|
static size_t move(fzl_reader_t *reader, size_t len)
|
||||||
|
{
|
||||||
|
if (!reader)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return reader->position += len;
|
||||||
|
}
|
||||||
|
|
||||||
// Try parse name
|
// Try parse name
|
||||||
fzl_token_t *parse_name(fzl_reader_t *reader)
|
fzl_token_t *parse_name(fzl_reader_t *reader)
|
||||||
{
|
{
|
||||||
size_t lpos = reader->position;
|
size_t lpos = reader->position;
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
lpos++;
|
lpos++;
|
||||||
size++;
|
size++;
|
||||||
}
|
} while ((reader->source[lpos] >= 'A' && reader->source[lpos] <= 'Z') ||
|
||||||
while ((reader->source[lpos] >= 'A' && reader->source[lpos] <= 'Z')
|
(reader->source[lpos] >= 'a' && reader->source[lpos] <= 'z') ||
|
||||||
|| (reader->source[lpos] >= 'a' && reader->source[lpos] <= 'z'));
|
(reader->source[lpos] >= '0' && reader->source[lpos] <= '9') ||
|
||||||
|
(reader->source[lpos] == '_'));
|
||||||
|
|
||||||
char *value = malloc(size);
|
char *value = malloc(size + 1);
|
||||||
memcpy(value, reader->source + reader->position, size);
|
memcpy(value, reader->source + reader->position, size);
|
||||||
|
value[size] = '\0';
|
||||||
|
|
||||||
size_t start = reader->position;
|
size_t start = reader->position;
|
||||||
reader->position += size;
|
move(reader, size);
|
||||||
|
|
||||||
fzl_token_t *tok = fzl_mktoken(
|
fzl_token_t *tok;
|
||||||
FZL_TOKTYPE_NAME,
|
fzl_file_loc l;
|
||||||
start,
|
bool ok = fzl_linked_getpos(reader->linked, start, &l);
|
||||||
reader->position - 1,
|
if (!ok)
|
||||||
value,
|
{
|
||||||
size
|
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||||
);
|
return NULL;
|
||||||
free(value);
|
}
|
||||||
|
|
||||||
return tok;
|
if (strcmp(value, "class") == 0)
|
||||||
|
tok =
|
||||||
|
fzl_mktoken(FZL_TOKTYPE_CLASS, l.line, l.col, value, size, l.filename);
|
||||||
|
else if (strcmp(value, "new") == 0)
|
||||||
|
tok = fzl_mktoken(FZL_TOKTYPE_NEW, l.line, l.col, value, size, l.filename);
|
||||||
|
else if (strcmp(value, "fn") == 0)
|
||||||
|
tok = fzl_mktoken(FZL_TOKTYPE_FUNCTION, l.line, l.col, value, size,
|
||||||
|
l.filename);
|
||||||
|
else if (strcmp(value, "return") == 0)
|
||||||
|
tok =
|
||||||
|
fzl_mktoken(FZL_TOKTYPE_RETURN, l.line, l.col, value, size, l.filename);
|
||||||
|
else if (strcmp(value, "if") == 0)
|
||||||
|
tok = fzl_mktoken(FZL_TOKTYPE_IF, l.line, l.col, value, size, l.filename);
|
||||||
|
else if (strcmp(value, "else") == 0)
|
||||||
|
tok = fzl_mktoken(FZL_TOKTYPE_ELSE, l.line, l.col, value, size, l.filename);
|
||||||
|
else if (strcmp(value, "true") == 0 || strcmp(value, "false") == 0)
|
||||||
|
tok = fzl_mktoken(FZL_TOKTYPE_BOOL_LITERAL, l.line, l.col, value, size,
|
||||||
|
l.filename);
|
||||||
|
else if (strcmp(value, "null") == 0)
|
||||||
|
tok = fzl_mktoken(FZL_TOKTYPE_NULL_LITERAL, l.line, l.col, value, size,
|
||||||
|
l.filename);
|
||||||
|
else if (strcmp(value, "str") == 0 || strcmp(value, "char") == 0 ||
|
||||||
|
strcmp(value, "bool") == 0 || strcmp(value, "uint") == 0 ||
|
||||||
|
strcmp(value, "int") == 0 || strcmp(value, "i8") == 0 ||
|
||||||
|
strcmp(value, "i16") == 0 || strcmp(value, "i32") == 0 ||
|
||||||
|
strcmp(value, "i64") == 0 || strcmp(value, "u8") == 0 ||
|
||||||
|
strcmp(value, "u16") == 0 || strcmp(value, "u32") == 0 ||
|
||||||
|
strcmp(value, "u64") == 0)
|
||||||
|
tok = fzl_mktoken(FZL_TOKTYPE_TYPE, l.line, l.col, value, size, l.filename);
|
||||||
|
else
|
||||||
|
tok = fzl_mktoken(FZL_TOKTYPE_NAME, l.line, l.col, value, size, l.filename);
|
||||||
|
free(value);
|
||||||
|
|
||||||
|
return tok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try parse string literal
|
// Try parse string literal
|
||||||
fzl_token_t *parse_string_literal(fzl_reader_t *reader)
|
fzl_token_t *parse_string_literal(fzl_reader_t *reader)
|
||||||
{
|
{
|
||||||
size_t lpos = reader->position;
|
size_t lpos = reader->position;
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|
||||||
if (reader->source[lpos] != '\'' && reader->source[lpos] != '"')
|
if (reader->source[lpos] != '\'' && reader->source[lpos] != '"')
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
char open_symbol = reader->source[lpos];
|
char open_symbol = reader->source[lpos];
|
||||||
|
char close_symbol = '\0';
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
lpos++;
|
lpos++;
|
||||||
size++;
|
size++;
|
||||||
}
|
|
||||||
while (reader->source[lpos] != open_symbol);
|
|
||||||
size++;
|
|
||||||
|
|
||||||
if (size == 2)
|
if (reader->source[lpos] == open_symbol)
|
||||||
{
|
close_symbol = open_symbol;
|
||||||
fzl_token_t *tok = fzl_mktoken(
|
|
||||||
FZL_TOKTYPE_STR_LITERAL,
|
|
||||||
reader->position,
|
|
||||||
reader->position + 1,
|
|
||||||
"",
|
|
||||||
0
|
|
||||||
);
|
|
||||||
return tok;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *value = malloc(size - 2);
|
} while (reader->source[lpos] != open_symbol && lpos < reader->size);
|
||||||
memcpy(value, reader->source + reader->position + 1, size - 2);
|
size++;
|
||||||
fzl_token_t *tok = fzl_mktoken(
|
|
||||||
FZL_TOKTYPE_STR_LITERAL,
|
|
||||||
reader->position,
|
|
||||||
lpos,
|
|
||||||
value,
|
|
||||||
size - 2
|
|
||||||
);
|
|
||||||
free(value);
|
|
||||||
|
|
||||||
reader->position += size;
|
fzl_file_loc l;
|
||||||
|
bool ok = fzl_linked_getpos(reader->linked, reader->position, &l);
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return tok;
|
if (close_symbol != open_symbol)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err), "'%s': unterminated string at %ld:%ld",
|
||||||
|
l.filename, l.line, l.col);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size == 2)
|
||||||
|
{
|
||||||
|
fzl_token_t *tok =
|
||||||
|
fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, l.line, l.col, "", 0, l.filename);
|
||||||
|
move(reader, 2);
|
||||||
|
return tok;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *value = malloc(size - 2);
|
||||||
|
memcpy(value, reader->source + reader->position + 1, size - 2);
|
||||||
|
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, l.line, l.col, value,
|
||||||
|
size - 2, l.filename);
|
||||||
|
free(value);
|
||||||
|
move(reader, size);
|
||||||
|
|
||||||
|
return tok;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse numeric literal
|
||||||
|
fzl_token_t *parse_number(fzl_reader_t *reader)
|
||||||
|
{
|
||||||
|
if (!reader)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t size = 0;
|
||||||
|
size_t lpos = reader->position;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
size++;
|
||||||
|
lpos++;
|
||||||
|
} while ((reader->source[lpos] >= '0' && reader->source[lpos] <= '9') &&
|
||||||
|
lpos < reader->size);
|
||||||
|
|
||||||
|
char *value = malloc(size * sizeof(char) + 1);
|
||||||
|
memcpy(value, reader->source + reader->position, size);
|
||||||
|
value[size] = '\0';
|
||||||
|
|
||||||
|
fzl_file_loc l;
|
||||||
|
bool ok = fzl_linked_getpos(reader->linked, reader->position, &l);
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_NUM_LITERAL, l.line, l.col, value,
|
||||||
|
size, l.filename);
|
||||||
|
free(value);
|
||||||
|
|
||||||
|
if (!tok)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
move(reader, size);
|
||||||
|
return tok;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add single-char token
|
||||||
|
fzl_token_t *add_single(fzl_reader_t *reader, fzl_toktype_t type)
|
||||||
|
{
|
||||||
|
fzl_file_loc l;
|
||||||
|
bool ok = fzl_linked_getpos(reader->linked, reader->position, &l);
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
move(reader, 1);
|
||||||
|
return fzl_mktoken(type, l.line, l.col, "", 0, l.filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add double-char token
|
||||||
|
fzl_token_t *add_double(fzl_reader_t *reader, fzl_toktype_t type)
|
||||||
|
{
|
||||||
|
fzl_file_loc l;
|
||||||
|
bool ok = fzl_linked_getpos(reader->linked, reader->position, &l);
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
move(reader, 2);
|
||||||
|
return fzl_mktoken(type, l.line, l.col, "", 0, l.filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip symbols until '\n' or EOF
|
||||||
|
void skip_comment(fzl_reader_t *reader)
|
||||||
|
{
|
||||||
|
move(reader, 2);
|
||||||
|
|
||||||
|
while (reader->position < reader->size)
|
||||||
|
{
|
||||||
|
if (reader->source[reader->position++] == '\n')
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int fzl_read(fzl_reader_t *reader)
|
int fzl_read(fzl_reader_t *reader)
|
||||||
{
|
{
|
||||||
const size_t len = strlen(reader->source);
|
if (reader->position >= reader->size)
|
||||||
if (reader->position >= len) return 0;
|
return 0;
|
||||||
|
|
||||||
do {
|
do
|
||||||
size_t p = reader->position;
|
{
|
||||||
fzl_token_t *token;
|
size_t p = reader->position;
|
||||||
char curr = reader->source[p];
|
fzl_token_t *token;
|
||||||
|
char curr = reader->source[p];
|
||||||
|
|
||||||
if (curr == '\n' || curr == ' ' || curr == '\r')
|
if (curr == '\n' || curr == ' ' || curr == '\r')
|
||||||
{
|
{
|
||||||
reader->position++;
|
move(reader, 1);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (curr == '\'' || curr == '"')
|
if (curr == '\'' || curr == '"')
|
||||||
token = parse_string_literal(reader);
|
token = parse_string_literal(reader);
|
||||||
else if (curr == '(')
|
else if (curr == '/' && reader->source[p + 1] == '/')
|
||||||
{
|
{
|
||||||
token = fzl_mktoken(
|
skip_comment(reader);
|
||||||
FZL_TOKTYPE_LPAIR,
|
continue;
|
||||||
p, p, NULL, 0);
|
}
|
||||||
reader->position++;
|
else if (curr == '=' && reader->source[p + 1] == '=')
|
||||||
}
|
token = add_double(reader, FZL_TOKTYPE_EQEQ);
|
||||||
else if (curr == ')')
|
else if (curr == '>' && reader->source[p + 1] == '=')
|
||||||
{
|
token = add_double(reader, FZL_TOKTYPE_REQ);
|
||||||
token = fzl_mktoken(
|
else if (curr == '<' && reader->source[p + 1] == '=')
|
||||||
FZL_TOKTYPE_RPAIR,
|
token = add_double(reader, FZL_TOKTYPE_LEQ);
|
||||||
p, p, NULL, 0);
|
else if (curr == '!' && reader->source[p + 1] == '=')
|
||||||
reader->position++;
|
token = add_double(reader, FZL_TOKTYPE_NEQ);
|
||||||
}
|
else if (curr == '|' && reader->source[p + 1] == '|')
|
||||||
else if (curr >= 'A' && curr <= 'Z' ||
|
token = add_double(reader, FZL_TOKTYPE_DVBAR);
|
||||||
curr >= 'a' && curr <= 'z')
|
else if (curr == '&' && reader->source[p + 1] == '&')
|
||||||
token = parse_name(reader);
|
token = add_double(reader, FZL_TOKTYPE_DAMPERSAND);
|
||||||
else
|
else if (curr == '>')
|
||||||
{
|
token = add_single(reader, FZL_TOKTYPE_RANGLE);
|
||||||
sprintf(last_err, "undefined token '%c'", curr);
|
else if (curr == '<')
|
||||||
return -1;
|
token = add_single(reader, FZL_TOKTYPE_LANGLE);
|
||||||
}
|
else if (curr == '!')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_NOT);
|
||||||
|
else if (curr == ';')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_SEMICOLON);
|
||||||
|
else if (curr == '{')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_LBRACE);
|
||||||
|
else if (curr == '}')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_RBRACE);
|
||||||
|
else if (curr == '(')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_LPAREN);
|
||||||
|
else if (curr == ')')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_RPAREN);
|
||||||
|
else if (curr == ',')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_COMMA);
|
||||||
|
else if (curr == '.')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_DOT);
|
||||||
|
else if (curr == '*')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_STAR);
|
||||||
|
else if (curr == '/')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_SLASH);
|
||||||
|
else if (curr == '+')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_PLUS);
|
||||||
|
else if (curr == '-')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_MINUS);
|
||||||
|
else if (curr == '^')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_CARET);
|
||||||
|
else if (curr == '%')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_PERCENT);
|
||||||
|
else if (curr == '=')
|
||||||
|
token = add_single(reader, FZL_TOKTYPE_EQ);
|
||||||
|
else if ((curr >= 'A' && curr <= 'Z') || (curr >= 'a' && curr <= 'z') ||
|
||||||
|
curr == '_')
|
||||||
|
token = parse_name(reader);
|
||||||
|
else if (curr >= '0' && curr <= '9')
|
||||||
|
token = parse_number(reader);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fzl_file_loc l;
|
||||||
|
bool ok = fzl_linked_getpos(reader->linked, p, &l);
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!token)
|
sprintf(last_err, "'%s': undefined token '%c' at %ld:%ld", l.filename,
|
||||||
{
|
curr, l.line, l.col);
|
||||||
size_t line = 1, linepos = 1;
|
return -1;
|
||||||
for (size_t i = 0; i < p; i++)
|
}
|
||||||
{
|
|
||||||
char sym = reader->source[i];
|
|
||||||
if (sym == '\n')
|
|
||||||
{
|
|
||||||
linepos = 1;
|
|
||||||
line++;
|
|
||||||
} else linepos++;
|
|
||||||
}
|
|
||||||
|
|
||||||
sprintf(last_err, "error parsing token (position %ld of line %ld)", line, linepos);
|
if (!token)
|
||||||
return -1;
|
{
|
||||||
}
|
if (!last_err[0])
|
||||||
|
{
|
||||||
|
fzl_file_loc l;
|
||||||
|
bool ok = fzl_linked_getpos(reader->linked, p, &l);
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
fzl_reader_ll_t *tok_node = malloc(sizeof(fzl_reader_ll_t));
|
sprintf(last_err, "'%s': cannot parse token at %ld:%ld", l.filename,
|
||||||
tok_node->token = token;
|
l.line, l.col);
|
||||||
|
}
|
||||||
|
|
||||||
reader->last->next = tok_node;
|
return -1;
|
||||||
reader->last = tok_node;
|
}
|
||||||
} while (reader->position < len);
|
|
||||||
|
|
||||||
reader->at_end = true;
|
fzl_reader_ll_t *tok_node = malloc(sizeof(fzl_reader_ll_t));
|
||||||
return 0;
|
tok_node->next = NULL;
|
||||||
|
tok_node->token = token;
|
||||||
|
|
||||||
|
reader->last->next = tok_node;
|
||||||
|
reader->last = tok_node;
|
||||||
|
} while (reader->position < reader->size);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Read zone end ---
|
// --- Read zone end ---
|
||||||
|
|
||||||
char *fzl_reader_geterror()
|
char *fzl_reader_geterror() { return last_err; }
|
||||||
{
|
|
||||||
return last_err;
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_node_and_next(fzl_reader_ll_t *node)
|
void free_node_and_next(fzl_reader_ll_t *node)
|
||||||
{
|
{
|
||||||
if (!node) return;
|
if (!node)
|
||||||
if (node->next)
|
return;
|
||||||
free_node_and_next(node->next);
|
if (node->next)
|
||||||
|
free_node_and_next(node->next);
|
||||||
|
|
||||||
if (node->token)
|
if (node->token)
|
||||||
fzl_freetoken(node->token);
|
fzl_freetoken(node->token);
|
||||||
|
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fzl_freereader(fzl_reader_t *reader)
|
void fzl_freereader(fzl_reader_t *reader)
|
||||||
{
|
{
|
||||||
if (!reader) return;
|
if (!reader)
|
||||||
if (reader->first)
|
return;
|
||||||
free_node_and_next(reader->first);
|
if (reader->first)
|
||||||
|
free_node_and_next(reader->first);
|
||||||
|
|
||||||
free(reader);
|
free(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+23
-16
@@ -1,29 +1,36 @@
|
|||||||
#include <compiler/fzl_token.h>
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
fzl_token_t *fzl_mktoken(
|
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, size_t line, size_t col,
|
||||||
const fzl_toktype_t type, uint16_t from, uint16_t to,
|
void *value, size_t value_len, char *filename)
|
||||||
void *value, size_t value_len)
|
|
||||||
{
|
{
|
||||||
void *valholder = malloc(value_len);
|
char *valholder = NULL;
|
||||||
memcpy(valholder, value, value_len);
|
|
||||||
|
|
||||||
fzl_token_t *token = malloc(sizeof(fzl_token_t));
|
if (value && value_len > 0)
|
||||||
token->type = type;
|
{
|
||||||
token->i0 = from;
|
valholder = malloc(value_len + 1);
|
||||||
token->i1 = to;
|
memcpy(valholder, value, value_len);
|
||||||
token->store_value = value_len > 0;
|
valholder[value_len] = '\0';
|
||||||
token->value = valholder;
|
}
|
||||||
token->value_sz = value_len;
|
|
||||||
|
|
||||||
return token;
|
fzl_token_t *token = malloc(sizeof(fzl_token_t));
|
||||||
|
token->type = type;
|
||||||
|
token->ln = line;
|
||||||
|
token->cn = col;
|
||||||
|
token->store_value = (valholder != NULL);
|
||||||
|
token->value = valholder;
|
||||||
|
token->value_sz = value_len;
|
||||||
|
token->filename = filename;
|
||||||
|
|
||||||
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free allocated token
|
// Free allocated token
|
||||||
void fzl_freetoken(fzl_token_t *tok)
|
void fzl_freetoken(fzl_token_t *tok)
|
||||||
{
|
{
|
||||||
if (tok->store_value) free(tok->value);
|
if (tok->store_value)
|
||||||
free(tok);
|
free(tok->value);
|
||||||
|
free(tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,72 @@
|
|||||||
|
#include <compiler/fzl_writer.h>
|
||||||
|
#include <compiler/utils/fzl_const_pool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char wrlast_err[256];
|
||||||
|
|
||||||
|
fzl_writer_t *fzl_mkwriter(const char *filepath)
|
||||||
|
{
|
||||||
|
if (strlen(filepath) < 1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
fzl_writer_t *writer = malloc(sizeof(fzl_writer_t));
|
||||||
|
if (!writer)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
writer->filepath = filepath;
|
||||||
|
|
||||||
|
return writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fzl_write(fzl_writer_t *writer, fzl_walker_t *walker)
|
||||||
|
{
|
||||||
|
if (!writer || !walker)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
FILE *fp = fopen(writer->filepath, "wb");
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
snprintf(wrlast_err, sizeof(wrlast_err), "failed to open '%s' to write",
|
||||||
|
writer->filepath);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char hdr[5] = {'F', 'Z', 'L', 0x0, 0x1};
|
||||||
|
fwrite(hdr, sizeof(char), 5, fp);
|
||||||
|
fwrite(&walker->consts->size, sizeof(size_t), 1, fp);
|
||||||
|
fwrite(&walker->op_count, sizeof(size_t), 1, fp);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < walker->consts->size; i++)
|
||||||
|
{
|
||||||
|
fzl_const_t c = walker->consts->array[i];
|
||||||
|
fwrite(&c.type, sizeof(fzl_consttype_t), 1, fp);
|
||||||
|
|
||||||
|
if (c.type == FZL_CONSTTYPE_STRING)
|
||||||
|
{
|
||||||
|
size_t strlen = c.as.string.length;
|
||||||
|
fwrite(&c.as.string.length, sizeof(size_t), 1, fp);
|
||||||
|
fwrite(c.as.string.chars, sizeof(char), strlen, fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (walker->op_count > 0)
|
||||||
|
fwrite(walker->ops, sizeof(uint64_t), walker->op_count, fp);
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *fzl_writer_geterror() { return wrlast_err; }
|
||||||
|
|
||||||
|
void fzl_freewriter(fzl_writer_t *writer)
|
||||||
|
{
|
||||||
|
if (!writer)
|
||||||
|
return;
|
||||||
|
|
||||||
|
free(writer);
|
||||||
|
}
|
||||||
+176
-65
@@ -1,80 +1,191 @@
|
|||||||
#include <stdio.h>
|
#include <common/fzl_colors.h>
|
||||||
#include <string.h>
|
|
||||||
#include <compiler/fzl_reader.h>
|
|
||||||
#include <compiler/fzl_parser.h>
|
|
||||||
#include <compiler/fzl_debug.h>
|
#include <compiler/fzl_debug.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_reader.h>
|
||||||
|
#include <compiler/fzl_walker.h>
|
||||||
|
#include <compiler/fzl_writer.h>
|
||||||
|
#include <compiler/utils/fzl_linked.h>
|
||||||
|
#include <compiler/utils/fzl_options.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char **argv)
|
||||||
if (argc < 2)
|
{
|
||||||
{
|
fzl_options_t *opts = fzl_mkoptions(argc, argv);
|
||||||
printf("No files provided >:(\n");
|
if (!opts)
|
||||||
return -1;
|
{
|
||||||
}
|
fzl_freeoptions(opts);
|
||||||
else if (argc > 2)
|
return -1;
|
||||||
{
|
}
|
||||||
printf("warn: flags are not supported yet\n");
|
else if (!opts->should_continue)
|
||||||
}
|
{
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
printf("> analyze\n-> reading file...");
|
if (opts->verbose)
|
||||||
|
printf("%s> analyze\n-> reading file...%s", FZL_CDEBUG, FZL_CRST);
|
||||||
|
|
||||||
char source[8192];
|
fzl_linked_t *linked = fzl_mklinked();
|
||||||
FILE *fd = fopen(argv[argc - 1], "r");
|
if (!linked)
|
||||||
if (!fd)
|
{
|
||||||
{
|
char *lasterr = fzl_linked_geterror();
|
||||||
printf("failed to open file '%s'\n", argv[argc-1]);
|
if (lasterr[0])
|
||||||
return -1;
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, lasterr);
|
||||||
}
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
fseek(fd, 0L, SEEK_END);
|
bool read_result =
|
||||||
size_t fsz = ftell(fd);
|
fzl_linked_add_files(linked, opts->files, opts->files_count);
|
||||||
rewind(fd);
|
if (!read_result)
|
||||||
|
{
|
||||||
|
char *lasterr = fzl_linked_geterror();
|
||||||
|
if (lasterr[0])
|
||||||
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, lasterr);
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
fread(source, fsz, 1, fd);
|
if (opts->verbose)
|
||||||
fclose(fd);
|
{
|
||||||
printf("ok!\n");
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||||
|
printf("%s--> Red %ld bytes%s\n", FZL_CDEBUG, linked->total_sz, FZL_CRST);
|
||||||
|
printf("%s-> tokenizing...%s", FZL_CDEBUG, FZL_CRST);
|
||||||
|
}
|
||||||
|
|
||||||
printf("-> tokenizing...");
|
fzl_reader_t *rd = fzl_mkreader(linked);
|
||||||
fzl_reader_t *rd = fzl_mkreader(source);
|
int result = fzl_read(rd);
|
||||||
int result = fzl_read(rd);
|
if (result != 0)
|
||||||
if (result != 0)
|
{
|
||||||
{
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_reader_geterror());
|
||||||
printf("error: %s\n", fzl_reader_geterror());
|
fzl_freeoptions(opts);
|
||||||
return -1;
|
fzl_freelinked(linked);
|
||||||
}
|
fzl_freereader(rd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
printf("ok!\n");
|
if (opts->verbose)
|
||||||
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||||
|
|
||||||
fzl_reader_ll_t *entry = rd->first;
|
fzl_reader_ll_t *entry = rd->first;
|
||||||
printf("--> structure: ");
|
|
||||||
while (entry)
|
|
||||||
{
|
|
||||||
if (entry->token)
|
|
||||||
{
|
|
||||||
if (entry->token->store_value)
|
|
||||||
printf("<TOK:%d:%s>", entry->token->type, (char*)entry->token->value);
|
|
||||||
else
|
|
||||||
printf("<TOK:%d>", entry->token->type);
|
|
||||||
}
|
|
||||||
|
|
||||||
entry = entry->next;
|
if (opts->verbose)
|
||||||
}
|
{
|
||||||
|
printf("%s--> structure: %s", FZL_CDEBUG, FZL_CRST);
|
||||||
|
while (entry)
|
||||||
|
{
|
||||||
|
if (entry->token)
|
||||||
|
{
|
||||||
|
if (entry->token->store_value)
|
||||||
|
printf("<TOK:%d:%s>", entry->token->type,
|
||||||
|
(char *)entry->token->value);
|
||||||
|
else
|
||||||
|
printf("<TOK:%d>", entry->token->type);
|
||||||
|
}
|
||||||
|
|
||||||
printf("\n");
|
entry = entry->next;
|
||||||
|
}
|
||||||
|
|
||||||
printf("-> parsing...");
|
printf("\n");
|
||||||
fzl_parser_t *parser = fzl_mkparser(rd->first);
|
printf("%s-> parsing...%s", FZL_CDEBUG, FZL_CRST);
|
||||||
result = fzl_parse(parser);
|
}
|
||||||
if (result != 0)
|
|
||||||
{
|
|
||||||
printf("error: %s\n", fzl_parser_geterror());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("ok!\n");
|
fzl_parser_t *parser = fzl_mkparser(rd->first);
|
||||||
fzl_debug_print_ast(parser->root);
|
result = fzl_parse(parser);
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_parser_geterror());
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
fzl_freereader(rd);
|
if (opts->verbose)
|
||||||
fzl_freeparser(parser);
|
{
|
||||||
return 0;
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||||
|
fzl_debug_print_ast(parser->root);
|
||||||
|
printf("%s> building%s\n", FZL_CDEBUG, FZL_CRST);
|
||||||
|
printf("%s-> walking AST...%s\n", FZL_CDEBUG, FZL_CRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
||||||
|
result = fzl_walk(walker);
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_walker_geterror());
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freewalker(walker);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts->verbose)
|
||||||
|
{
|
||||||
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||||
|
|
||||||
|
printf("%s--> Constants table%s:\n", FZL_CDEBUG, FZL_CRST);
|
||||||
|
for (size_t i = 0; i < walker->consts->size; i++)
|
||||||
|
printf("---> %s\n", walker->consts->array[i].as.string.chars);
|
||||||
|
|
||||||
|
printf("%s--> Ops%s:\n", FZL_CDEBUG, FZL_CRST);
|
||||||
|
printf("---> ");
|
||||||
|
for (size_t i = 0; i < walker->op_count; i++)
|
||||||
|
{
|
||||||
|
if (i > 0 && i < walker->op_count)
|
||||||
|
printf("->");
|
||||||
|
printf("%lu", walker->ops[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("%s-> preparing final write%s\n", FZL_CDEBUG, FZL_CRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
char outpath[256];
|
||||||
|
if (opts->outname)
|
||||||
|
strcpy(outpath, opts->outname);
|
||||||
|
else
|
||||||
|
strcpy(outpath, "a.fzb");
|
||||||
|
|
||||||
|
if (opts->verbose)
|
||||||
|
{
|
||||||
|
printf("%s-> output file path is%s: %s\n", FZL_CDEBUG, FZL_CRST, outpath);
|
||||||
|
printf("%s-> writing file...%s", FZL_CDEBUG, FZL_CRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_writer_t *writer = fzl_mkwriter(outpath);
|
||||||
|
result = fzl_write(writer, walker);
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_writer_geterror());
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freewriter(writer);
|
||||||
|
fzl_freewalker(walker);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts->verbose)
|
||||||
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||||
|
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freeast(parser->root);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
fzl_freewalker(walker);
|
||||||
|
fzl_freewriter(writer);
|
||||||
|
|
||||||
|
printf("%sCompiled successfully%s.\n", FZL_COK, FZL_CRST);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,140 @@
|
|||||||
|
#include <compiler/utils/fzl_const_pool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
bool fzl_consts_equal(fzl_const_t a, fzl_const_t b)
|
||||||
|
{
|
||||||
|
if (a.type != b.type)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
switch (a.type)
|
||||||
|
{
|
||||||
|
case FZL_CONSTTYPE_STRING:
|
||||||
|
{
|
||||||
|
if (a.as.string.length != b.as.string.length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return memcmp(a.as.string.chars, b.as.string.chars, a.as.string.length) ==
|
||||||
|
0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t fzl_hash_const(fzl_const_t c)
|
||||||
|
{
|
||||||
|
uint32_t hash = 2166136261u;
|
||||||
|
const uint8_t *data = NULL;
|
||||||
|
size_t size = 0;
|
||||||
|
|
||||||
|
switch (c.type)
|
||||||
|
{
|
||||||
|
case FZL_CONSTTYPE_STRING:
|
||||||
|
data = (const uint8_t *)c.as.string.chars;
|
||||||
|
size = c.as.string.length;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
hash ^= data[i];
|
||||||
|
hash *= 16777619u;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_const_hash_pool_t *fzl_mkconstpool()
|
||||||
|
{
|
||||||
|
fzl_const_hash_pool_t *pool = malloc(sizeof(fzl_const_hash_pool_t));
|
||||||
|
|
||||||
|
pool->size = 0;
|
||||||
|
pool->capacity = 8;
|
||||||
|
pool->array = malloc(pool->capacity * sizeof(fzl_const_t));
|
||||||
|
|
||||||
|
pool->slots_count = 0;
|
||||||
|
pool->slots_capacity = 16;
|
||||||
|
pool->slots = malloc(pool->slots_capacity * sizeof(fzl_const_hash_slot_t));
|
||||||
|
for (size_t i = 0; i < pool->slots_capacity; i++)
|
||||||
|
pool->slots[i].pool_index = -1;
|
||||||
|
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool)
|
||||||
|
{
|
||||||
|
int old_capacity = pool->slots_capacity;
|
||||||
|
fzl_const_hash_slot_t *old_slots = pool->slots;
|
||||||
|
|
||||||
|
pool->slots_capacity = old_capacity * 2;
|
||||||
|
pool->slots = malloc(pool->slots_capacity * sizeof(fzl_const_hash_slot_t));
|
||||||
|
for (size_t i = 0; i < pool->slots_capacity; i++)
|
||||||
|
pool->slots[i].pool_index = -1;
|
||||||
|
pool->slots_count = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < old_capacity; i++)
|
||||||
|
{
|
||||||
|
if (old_slots[i].pool_index == -1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int index_in_pool = old_slots[i].pool_index;
|
||||||
|
fzl_const_t c = pool->array[index_in_pool];
|
||||||
|
uint32_t hash = fzl_hash_const(c);
|
||||||
|
|
||||||
|
uint32_t bucket = hash & (pool->slots_capacity - 1);
|
||||||
|
while (pool->slots[bucket].pool_index != -1)
|
||||||
|
bucket = (bucket + 1) & (pool->slots_capacity - 1);
|
||||||
|
|
||||||
|
pool->slots[bucket].pool_index = index_in_pool;
|
||||||
|
pool->slots_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(old_slots);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c)
|
||||||
|
{
|
||||||
|
if (pool->slots_count * 4 >= pool->slots_capacity * 3)
|
||||||
|
fzl_consts_pool_resize_hash(pool);
|
||||||
|
|
||||||
|
uint32_t hash = fzl_hash_const(c);
|
||||||
|
uint32_t bucket = hash & (pool->slots_capacity - 1);
|
||||||
|
|
||||||
|
while (pool->slots[bucket].pool_index != -1)
|
||||||
|
{
|
||||||
|
size_t existing_index = pool->slots[bucket].pool_index;
|
||||||
|
if (fzl_consts_equal(pool->array[existing_index], c))
|
||||||
|
return existing_index;
|
||||||
|
|
||||||
|
bucket = (bucket + 1) & (pool->slots_capacity - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pool->size >= pool->capacity)
|
||||||
|
{
|
||||||
|
pool->capacity *= 2;
|
||||||
|
pool->array = (fzl_const_t *)realloc(pool->array,
|
||||||
|
pool->capacity * sizeof(fzl_const_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t new_index = pool->size;
|
||||||
|
pool->array[new_index] = c;
|
||||||
|
pool->size++;
|
||||||
|
|
||||||
|
pool->slots[bucket].pool_index = new_index;
|
||||||
|
pool->slots_count++;
|
||||||
|
|
||||||
|
return new_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_consts_pool_free(fzl_const_hash_pool_t *pool)
|
||||||
|
{
|
||||||
|
if (!pool)
|
||||||
|
return;
|
||||||
|
|
||||||
|
free(pool->array);
|
||||||
|
free(pool->slots);
|
||||||
|
free(pool);
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include <compiler/utils/fzl_ctable.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
fzl_ctable_t *fzl_mkctable(char *name)
|
||||||
|
{
|
||||||
|
fzl_ctable_t *ctable = malloc(sizeof(fzl_ctable_t));
|
||||||
|
if (!ctable)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memset(ctable, 0, sizeof(fzl_ctable_t));
|
||||||
|
ctable->name = name;
|
||||||
|
ctable->fields_remainder = 2;
|
||||||
|
ctable->fields =
|
||||||
|
malloc(ctable->fields_remainder * sizeof(fzl_ctable_field_t));
|
||||||
|
|
||||||
|
return ctable;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_freectable(fzl_ctable_t *ct)
|
||||||
|
{
|
||||||
|
if (!ct)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (ct->fields)
|
||||||
|
free(ct->fields);
|
||||||
|
|
||||||
|
free(ct);
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
#include <compiler/utils/fzl_linked.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char llast_err[256];
|
||||||
|
|
||||||
|
fzl_linked_t *fzl_mklinked()
|
||||||
|
{
|
||||||
|
fzl_linked_t *l = malloc(sizeof(fzl_linked_t));
|
||||||
|
if (!l)
|
||||||
|
{
|
||||||
|
snprintf(llast_err, sizeof(llast_err), "potato PC...");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
l->entries = NULL;
|
||||||
|
l->entries_count = 0;
|
||||||
|
l->source = NULL;
|
||||||
|
l->total_sz = 0;
|
||||||
|
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fzl_linked_add_files(fzl_linked_t *l, char **files, size_t files_count)
|
||||||
|
{
|
||||||
|
if (!l)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
size_t eremainder = 2;
|
||||||
|
size_t ecount = 0;
|
||||||
|
fzl_linked_entry_t *entries = malloc(eremainder * sizeof(fzl_linked_entry_t));
|
||||||
|
|
||||||
|
size_t current_start = 0;
|
||||||
|
for (size_t i = 0; i < files_count; i++)
|
||||||
|
{
|
||||||
|
char *source;
|
||||||
|
FILE *fd = fopen(files[i], "r");
|
||||||
|
if (!fd)
|
||||||
|
{
|
||||||
|
snprintf(llast_err, sizeof(llast_err), "failed to open file '%s'\n",
|
||||||
|
files[i]);
|
||||||
|
free(entries);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(fd, 0L, SEEK_END);
|
||||||
|
size_t file_sz = ftell(fd);
|
||||||
|
rewind(fd);
|
||||||
|
|
||||||
|
source = malloc(file_sz + 1);
|
||||||
|
fread(source, file_sz, 1, fd);
|
||||||
|
fclose(fd);
|
||||||
|
source[file_sz] = 0;
|
||||||
|
|
||||||
|
if (eremainder <= ecount)
|
||||||
|
{
|
||||||
|
eremainder *= 2;
|
||||||
|
entries = realloc(entries, eremainder * sizeof(fzl_linked_entry_t));
|
||||||
|
if (!entries)
|
||||||
|
{
|
||||||
|
snprintf(llast_err, sizeof(llast_err), "potato pc...");
|
||||||
|
free(source);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entries[ecount++] =
|
||||||
|
(fzl_linked_entry_t){.name = files[i],
|
||||||
|
.start = current_start,
|
||||||
|
.end = current_start + file_sz - 1};
|
||||||
|
|
||||||
|
if (!l->source)
|
||||||
|
l->source = malloc(file_sz + 1);
|
||||||
|
else
|
||||||
|
l->source = realloc(l->source, current_start + file_sz + 1);
|
||||||
|
|
||||||
|
if (!l->source)
|
||||||
|
{
|
||||||
|
snprintf(llast_err, sizeof(llast_err),
|
||||||
|
"failed to allocate space for source code holder");
|
||||||
|
free(entries);
|
||||||
|
free(source);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(l->source + current_start, source);
|
||||||
|
free(source);
|
||||||
|
current_start += file_sz;
|
||||||
|
}
|
||||||
|
|
||||||
|
l->source = realloc(l->source, current_start + 1);
|
||||||
|
if (!l->source)
|
||||||
|
{
|
||||||
|
snprintf(llast_err, sizeof(llast_err),
|
||||||
|
"not enough space for entire source\n");
|
||||||
|
free(entries);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
l->source[current_start] = 0;
|
||||||
|
|
||||||
|
l->entries = entries;
|
||||||
|
l->entries_count = ecount;
|
||||||
|
l->total_sz = current_start;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *fzl_linked_geterror() { return llast_err; }
|
||||||
|
|
||||||
|
bool fzl_linked_getpos(fzl_linked_t *l, size_t pos, fzl_file_loc *res)
|
||||||
|
{
|
||||||
|
if (!l)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
fzl_linked_entry_t e;
|
||||||
|
for (size_t i = 0; i < l->entries_count; i++)
|
||||||
|
{
|
||||||
|
e = l->entries[i];
|
||||||
|
if (pos >= e.start && pos <= e.end)
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t line = 1;
|
||||||
|
size_t line_start = e.start;
|
||||||
|
for (size_t p = e.start; p < pos; p++)
|
||||||
|
{
|
||||||
|
if (l->source[p] == '\n')
|
||||||
|
{
|
||||||
|
line++;
|
||||||
|
line_start = p + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*res =
|
||||||
|
(fzl_file_loc){.filename = e.name, .line = line, .col = pos - line_start};
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_freelinked(fzl_linked_t *linked)
|
||||||
|
{
|
||||||
|
if (!linked)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (linked->source)
|
||||||
|
free(linked->source);
|
||||||
|
|
||||||
|
if (linked->entries)
|
||||||
|
free(linked->entries);
|
||||||
|
|
||||||
|
free(linked);
|
||||||
|
}
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
#include <common/fzl_colors.h>
|
||||||
|
#include <compiler/utils/fzl_options.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
const char version[] = "FZL Compiler 1.0";
|
||||||
|
|
||||||
|
void print_help()
|
||||||
|
{
|
||||||
|
printf("Usage: fzc [flags] <sources...>\n");
|
||||||
|
printf("Available flags:\n");
|
||||||
|
printf("\t-h\t--help\t\tPrint help\n");
|
||||||
|
printf("\t-v\t--verbose\tDisplay compilation info (tokens, AST, consts "
|
||||||
|
"table, etc)\n");
|
||||||
|
printf("\t-V\t--version\tPrint compiler version\n");
|
||||||
|
printf("\t-o\t--out\t\tSet compiled file name\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_options_t *fzl_mkoptions(int argc, char **argv)
|
||||||
|
{
|
||||||
|
fzl_options_t *opts = malloc(sizeof(fzl_options_t));
|
||||||
|
if (!opts)
|
||||||
|
{
|
||||||
|
printf("%sBruh... Potato PC%s\n", FZL_CERROR, FZL_CRST);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(opts, 0, sizeof(fzl_options_t));
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
print_help();
|
||||||
|
opts->should_continue = false;
|
||||||
|
return opts;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for (i = 1; i < argc; i++)
|
||||||
|
{
|
||||||
|
if (argv[i][0] != '-')
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
|
||||||
|
{
|
||||||
|
print_help();
|
||||||
|
opts->should_continue = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0)
|
||||||
|
{
|
||||||
|
printf("%s\n", version);
|
||||||
|
opts->should_continue = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0)
|
||||||
|
{
|
||||||
|
opts->verbose = true;
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--out") == 0)
|
||||||
|
{
|
||||||
|
if (i == argc - 1)
|
||||||
|
{
|
||||||
|
printf("%sError%s: missing out file name\n", FZL_CERROR, FZL_CRST);
|
||||||
|
free(opts);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *filename = argv[++i];
|
||||||
|
opts->outname = filename;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("%sError%s: undefined flag '%s'\n", FZL_CERROR, FZL_CRST, argv[i]);
|
||||||
|
free(opts);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fremainder = 2;
|
||||||
|
size_t fcount = 0;
|
||||||
|
char **files = malloc(fremainder * sizeof(char *));
|
||||||
|
|
||||||
|
for (; i < argc; i++)
|
||||||
|
{
|
||||||
|
if (argv[i][0] == '-')
|
||||||
|
{
|
||||||
|
printf(
|
||||||
|
"%sWarning%s: unhandled flag '%s' (should be defined before sources "
|
||||||
|
"list)\n",
|
||||||
|
FZL_CWARNING, FZL_CRST, argv[i]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fremainder <= fcount)
|
||||||
|
{
|
||||||
|
fremainder *= 2;
|
||||||
|
files = realloc(files, fremainder * sizeof(char *));
|
||||||
|
}
|
||||||
|
|
||||||
|
files[fcount++] = argv[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
opts->files = files;
|
||||||
|
opts->files_count = fcount;
|
||||||
|
|
||||||
|
if (i < argc - 1)
|
||||||
|
{
|
||||||
|
printf("%sError%s: flags should go before source files list.\n", FZL_CERROR,
|
||||||
|
FZL_CRST);
|
||||||
|
printf("Remain %d arguments are not handled.\n", argc - 1 - i);
|
||||||
|
|
||||||
|
free(files);
|
||||||
|
free(opts);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
opts->should_continue = true;
|
||||||
|
return opts;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_freeoptions(fzl_options_t *options)
|
||||||
|
{
|
||||||
|
if (!options)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (options->files)
|
||||||
|
free(options->files);
|
||||||
|
|
||||||
|
free(options);
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
fzl_symtable_t *fzl_mksymt()
|
||||||
|
{
|
||||||
|
fzl_symtable_t *tbl = malloc(sizeof(fzl_symtable_t));
|
||||||
|
if (!tbl)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
tbl->remainder = 2;
|
||||||
|
tbl->size = 0;
|
||||||
|
tbl->symbols = malloc(tbl->remainder * sizeof(fzl_sym_t));
|
||||||
|
if (!tbl->symbols)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return tbl;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
||||||
|
fzl_type_t type, uint64_t ip)
|
||||||
|
{
|
||||||
|
if (!symt)
|
||||||
|
return FZL_SYMTABLE_RESULT_ERR;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < symt->size; i++)
|
||||||
|
if (strcmp(symt->symbols[i].name, name) == 0)
|
||||||
|
return FZL_SYMTABLE_RESULT_EXISTS;
|
||||||
|
|
||||||
|
if (symt->remainder <= symt->size)
|
||||||
|
{
|
||||||
|
symt->remainder *= 2;
|
||||||
|
symt->symbols = realloc(symt->symbols, symt->remainder * sizeof(fzl_sym_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
symt->symbols[symt->size++] =
|
||||||
|
(fzl_sym_t){.name = name, .type = type, .ip = ip, .classname = NULL};
|
||||||
|
return FZL_SYMTABLE_RESULT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_symtable_res_t fzl_symt_setobjclass(fzl_symtable_t *symt, char *name,
|
||||||
|
char *classname)
|
||||||
|
{
|
||||||
|
if (!symt)
|
||||||
|
return FZL_SYMTABLE_RESULT_ERR;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < symt->size; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(symt->symbols[i].name, name) == 0)
|
||||||
|
{
|
||||||
|
if (symt->symbols[i].type != FZL_TYPE_OBJECT)
|
||||||
|
return FZL_SYMTABLE_RESULT_NOT_OBJECT;
|
||||||
|
|
||||||
|
symt->symbols[i].classname = classname;
|
||||||
|
return FZL_SYMTABLE_RESULT_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return FZL_SYMTABLE_RESULT_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res)
|
||||||
|
{
|
||||||
|
if (!symt)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < symt->size; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(symt->symbols[i].name, name) == 0)
|
||||||
|
{
|
||||||
|
*res = symt->symbols[i];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_freesymt(fzl_symtable_t *symt)
|
||||||
|
{
|
||||||
|
if (!symt)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (symt->symbols)
|
||||||
|
free(symt->symbols);
|
||||||
|
|
||||||
|
free(symt);
|
||||||
|
}
|
||||||
+1026
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,171 @@
|
|||||||
|
#include "common/fzl_opcodes.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <vm/fzl_vm.h>
|
||||||
|
|
||||||
|
void fzl_vm_print_ops(fzl_vm_t *vm)
|
||||||
|
{
|
||||||
|
if (!vm)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (vm->ops_count < 1)
|
||||||
|
{
|
||||||
|
printf("no opcodes\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < vm->ops_count; i++)
|
||||||
|
{
|
||||||
|
printf("%04ld: ", i);
|
||||||
|
|
||||||
|
switch (vm->ops[i])
|
||||||
|
{
|
||||||
|
case FZL_OPCODE_DEREFERENCE:
|
||||||
|
printf("DEREFERENCE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_EQ_STR:
|
||||||
|
printf("CMP_EQ_STR\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_NEQ_STR:
|
||||||
|
printf("CMP_NEQ_STR\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_EQ_OBJ:
|
||||||
|
printf("CMP_EQ_OBJ\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_NEQ_OBJ:
|
||||||
|
printf("CMP_NEQ_OBJ\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_ESCAPE_OBJECT:
|
||||||
|
printf("ESCAPE_OBJ\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_ALLOC_OBJECT:
|
||||||
|
printf("ALLOC_OBJECT: %lu fields\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_GET_FIELD:
|
||||||
|
printf("GET_FIELD #%lu\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_SET_FIELD:
|
||||||
|
printf("SET_FIELD #%lu\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CALL:
|
||||||
|
{
|
||||||
|
size_t ip = vm->ops[++i];
|
||||||
|
printf("CALL IP: %lu\n", ip);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case FZL_OPCODE_RET:
|
||||||
|
printf("RET\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_LOADCONST:
|
||||||
|
{
|
||||||
|
size_t const_n = vm->ops[++i];
|
||||||
|
fzl_const_t c = vm->consts[const_n];
|
||||||
|
printf("LOADCONST #%lu ('%s')\n", const_n, c.as.string.chars);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case FZL_OPCODE_CALL_BUILTIN:
|
||||||
|
printf("CALL_BUILTIN #%lu\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_PUSHNUM:
|
||||||
|
printf("PUSHNUM %lu\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_PUSHBOOL:
|
||||||
|
printf("PUSHBOOL %s\n", vm->ops[++i] ? "true" : "false");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_MK_ENVTABLE:
|
||||||
|
printf("MK_ENVTABLE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_RM_ENVTABLE:
|
||||||
|
printf("RM_ENVTABLE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_STORE:
|
||||||
|
printf("STORE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_LOOKUP_ENV:
|
||||||
|
printf("LOOKUP_ENV\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_WRITE:
|
||||||
|
printf("WRITE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_ADD:
|
||||||
|
case FZL_OPCODE_SUB:
|
||||||
|
case FZL_OPCODE_MUL:
|
||||||
|
case FZL_OPCODE_DIV:
|
||||||
|
case FZL_OPCODE_POW:
|
||||||
|
case FZL_OPCODE_MOD:
|
||||||
|
printf("MATH\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_AND:
|
||||||
|
printf("AND\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_OR:
|
||||||
|
printf("OR\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_EQ:
|
||||||
|
printf("CMP_EQ\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_NEQ:
|
||||||
|
printf("CMP_NEQ\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_LT:
|
||||||
|
printf("CMP_LT\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_LTEQ:
|
||||||
|
printf("CMP_LTEQ\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_GT:
|
||||||
|
printf("CMP_GT\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CMP_GTEQ:
|
||||||
|
printf("CMP_GTEQ\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_NOT:
|
||||||
|
printf("CMP_NOT\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_NEG:
|
||||||
|
printf("NEG\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_JZ:
|
||||||
|
printf("JZ IP: %ld\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_JMP:
|
||||||
|
printf("JMP IP: %ld\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("<UNKNOWN>\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
#include "vm/fzl_vm_object.h"
|
||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <common/fzl_value.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <vm/fzl_vm_env.h>
|
||||||
|
|
||||||
|
fzl_vm_env_t *fzl_mkenv()
|
||||||
|
{
|
||||||
|
fzl_vm_env_t *env = malloc(sizeof(fzl_vm_env_t));
|
||||||
|
env->remainder = 2;
|
||||||
|
env->size = 0;
|
||||||
|
env->table = malloc(env->remainder * sizeof(fzl_vm_env_entry_t));
|
||||||
|
|
||||||
|
env->owned_objects_remainder = 2;
|
||||||
|
env->owned_objects_count = 0;
|
||||||
|
env->owned_objects =
|
||||||
|
malloc(env->owned_objects_remainder * sizeof(fzl_vm_object_t *));
|
||||||
|
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resize table if size eq to remainder
|
||||||
|
static void resize_env_if_needed(fzl_vm_env_t *env)
|
||||||
|
{
|
||||||
|
if (env->remainder > env->size)
|
||||||
|
return;
|
||||||
|
|
||||||
|
env->remainder *= 2;
|
||||||
|
env->table = realloc(env->table, env->remainder * sizeof(fzl_vm_env_entry_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fzl_env_lookup(fzl_vm_env_t *env, char *name, fzl_value_t *res)
|
||||||
|
{
|
||||||
|
if (!env)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < env->size; i++)
|
||||||
|
if (strcmp(env->table[i].name, name) == 0)
|
||||||
|
{
|
||||||
|
*res = env->table[i].value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fzl_env_contains(fzl_vm_env_t *env, char *name)
|
||||||
|
{
|
||||||
|
if (!env)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < env->size; i++)
|
||||||
|
if (strcmp(env->table[i].name, name) == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_vm_env_wresult_t fzl_env_storevalue(fzl_vm_env_t *env, char *name,
|
||||||
|
fzl_value_t val)
|
||||||
|
{
|
||||||
|
if (!env)
|
||||||
|
return FZL_VM_ENV_WRESULT_ERR;
|
||||||
|
|
||||||
|
resize_env_if_needed(env);
|
||||||
|
if (fzl_env_contains(env, name))
|
||||||
|
return FZL_VM_ENV_WRESULT_ERR_EXISTS;
|
||||||
|
|
||||||
|
env->table[env->size++] = (fzl_vm_env_entry_t){.name = name, .value = val};
|
||||||
|
|
||||||
|
return FZL_VM_ENV_WRESULT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_vm_env_wresult_t fzl_env_writevalue(fzl_vm_env_t *env, char *name,
|
||||||
|
fzl_value_t newval)
|
||||||
|
{
|
||||||
|
if (!env)
|
||||||
|
return FZL_VM_ENV_WRESULT_ERR;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < env->size; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(env->table[i].name, name) == 0)
|
||||||
|
{
|
||||||
|
env->table[i].value = newval;
|
||||||
|
return FZL_VM_ENV_WRESULT_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return FZL_VM_ENV_WRESULT_ERR_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_freeenv(fzl_vm_env_t *env)
|
||||||
|
{
|
||||||
|
if (!env)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (env->table)
|
||||||
|
free(env->table);
|
||||||
|
|
||||||
|
if (env->owned_objects)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < env->owned_objects_count; i++)
|
||||||
|
{
|
||||||
|
if (!env->owned_objects[i])
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (env->owned_objects[i]->values)
|
||||||
|
free(env->owned_objects[i]->values);
|
||||||
|
|
||||||
|
free(env->owned_objects[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(env->owned_objects);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(env);
|
||||||
|
}
|
||||||
+111
@@ -0,0 +1,111 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <vm/fzl_vm.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *filepath;
|
||||||
|
bool help;
|
||||||
|
bool debug;
|
||||||
|
} run_args_t;
|
||||||
|
|
||||||
|
void print_hint()
|
||||||
|
{
|
||||||
|
printf("Usage: fz [flags] <file path>\n");
|
||||||
|
printf("Flags:\n");
|
||||||
|
printf("\t-h\t--help\tprint usage information\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse passed arguments and build args setup structure
|
||||||
|
run_args_t *parse_args(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
print_hint();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
run_args_t *args = malloc(sizeof(run_args_t));
|
||||||
|
args->filepath = NULL;
|
||||||
|
args->help = false;
|
||||||
|
args->debug = false;
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; i++)
|
||||||
|
{
|
||||||
|
if (argv[i][0] != '-')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
|
||||||
|
{
|
||||||
|
args->help = true;
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0)
|
||||||
|
args->debug = true;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Error: unknown flag '%s'.\n", argv[i]);
|
||||||
|
printf("Use 'fz --help' to read the usage\n");
|
||||||
|
free(args);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argv[argc - 1][0] == '-')
|
||||||
|
{
|
||||||
|
printf("Error: last argument MUST be a file path\n");
|
||||||
|
printf("Use 'fz --help' for help\n");
|
||||||
|
free(args);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
args->filepath = argv[argc - 1];
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
run_args_t *args = parse_args(argc, argv);
|
||||||
|
if (!args)
|
||||||
|
return -1;
|
||||||
|
else if (args->help)
|
||||||
|
{
|
||||||
|
print_hint();
|
||||||
|
free(args);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_vm_t *vm = fzl_mkvm(args->filepath);
|
||||||
|
if (!vm)
|
||||||
|
{
|
||||||
|
printf("Error: %s\n", fzl_vm_geterror());
|
||||||
|
free(args);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args->debug)
|
||||||
|
fzl_vm_print_ops(vm);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int result = fzl_vm_run(vm);
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
char *err = fzl_vm_geterror();
|
||||||
|
if (err && err[0])
|
||||||
|
printf("Error: %s\n", err);
|
||||||
|
|
||||||
|
free(args);
|
||||||
|
fzl_freevm(vm);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(args);
|
||||||
|
fzl_freevm(vm);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user