Compare commits
69
Commits
master
..
c0bd0d95b2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0bd0d95b2 | ||
|
|
26decf2626 | ||
|
|
358ede4c8c | ||
|
|
6a035a71eb | ||
|
|
8975d2fece | ||
|
|
3dd70ac293 | ||
|
|
4d9c4ddd21 | ||
|
|
a4aa220cfa | ||
|
|
5c96f96220 | ||
|
|
f769313169 | ||
|
|
0448be0d38 | ||
|
|
01dabd2bdd | ||
|
|
6f54286724 | ||
|
|
fe5fcd7b96 | ||
|
|
a885746fdd | ||
|
|
2cf96d06c6 | ||
|
|
bbdf91ee02 | ||
|
|
27374a96eb | ||
|
|
dcab2a8875 | ||
|
|
7b804b8809 | ||
|
|
eeca5e653d | ||
|
|
f4c97795fa | ||
|
|
e2de23ed57 | ||
|
|
8c90d54782 | ||
|
|
62b0030e4a | ||
|
|
bbdaa4a9b9 | ||
|
|
7c04d8b7ec | ||
|
|
567bf49d27 | ||
|
|
d93c315359 | ||
|
|
e11089c90f | ||
|
|
ea28c470d2 | ||
|
|
70bf63e2ea | ||
|
|
b24cefee96 | ||
|
|
6498c73960 | ||
|
|
dc80de66e4 | ||
|
|
c61322718d | ||
|
|
411d291a2c | ||
|
|
aa7a06bd22 | ||
|
|
a263b6206f | ||
|
|
25991c6f15 | ||
|
|
d7630dbb6a | ||
|
|
a70ad011f3 | ||
|
|
62ae05b4cd | ||
|
|
4904f40037 | ||
|
|
562b1b2e0a | ||
|
|
6d1f1ce66e | ||
|
|
c03fddf47f | ||
|
|
3bb60071bb | ||
|
|
04fdd58e4f | ||
|
|
e00e609589 | ||
|
|
a37b8cfe6e | ||
|
|
c4eb148d17 | ||
|
|
dbca3129ab | ||
|
|
e3878047d7 | ||
|
|
7cfb8efef4 | ||
|
|
b5ae959d14 | ||
|
|
0e319308a7 | ||
|
|
a20743a835 | ||
|
|
5dde75523d | ||
|
|
cc5bbd61f2 | ||
|
|
0188991f4d | ||
|
|
73a2a50a0b | ||
|
|
4a366cb0ac | ||
|
|
c7cce94119 | ||
|
|
673a28fbc7 | ||
|
|
160c6c6488 | ||
|
|
c8f3cd61f4 | ||
|
|
138d503bce | ||
|
|
e49ebba346 |
@@ -5,6 +5,7 @@ skip is
|
||||
skip -rfunction fzl_mk*
|
||||
skip -rfunction fzl_free*
|
||||
skip -rfunction fzl_symt*
|
||||
skip -rfunction fzl_linked*
|
||||
skip add_const
|
||||
skip push_op
|
||||
skip push_word
|
||||
|
||||
+3
-1
@@ -3,8 +3,11 @@
|
||||
# Dirs
|
||||
!/docs
|
||||
!/exl
|
||||
!/benchmarks
|
||||
!/include
|
||||
!/scripts
|
||||
!/src
|
||||
!/tests
|
||||
|
||||
# Files
|
||||
!/.clang-format
|
||||
@@ -14,4 +17,3 @@
|
||||
!/Makefile
|
||||
!/README.md
|
||||
!/README.ru.md
|
||||
|
||||
|
||||
+149
-115
@@ -8,135 +8,169 @@ 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)
|
||||
set(FZL_TESTS TRUE)
|
||||
endif()
|
||||
|
||||
include_directories(include)
|
||||
include(FetchContent)
|
||||
|
||||
# --- libffi ---
|
||||
|
||||
set(PREV_SUPPRESS_DEV ${CMAKE_SUPPRESS_DEVELOPER_WARNINGS})
|
||||
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS TRUE CACHE INTERNAL "")
|
||||
|
||||
FetchContent_Declare(ffi
|
||||
GIT_REPOSITORY "https://git.samsonium.dev/samsonium/libffi.git"
|
||||
GIT_TAG "main"
|
||||
)
|
||||
FetchContent_MakeAvailable(ffi)
|
||||
|
||||
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS ${PREV_SUPPRESS_DEV} CACHE INTERNAL "")
|
||||
|
||||
# --------------
|
||||
|
||||
# Compiler
|
||||
add_executable(fzc
|
||||
src/compiler/fzl_token.c
|
||||
src/compiler/fzl_reader.c
|
||||
src/compiler/fzl_parser.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)
|
||||
if (NOT FZL_SKIP_C)
|
||||
add_executable(fzc
|
||||
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/analyzer/utility.c
|
||||
src/compiler/analyzer/context.c
|
||||
src/compiler/analyzer/an_assign.c
|
||||
src/compiler/analyzer/an_binmath.c
|
||||
src/compiler/analyzer/an_block.c
|
||||
src/compiler/analyzer/an_break.c
|
||||
src/compiler/analyzer/an_call.c
|
||||
src/compiler/analyzer/an_class.c
|
||||
src/compiler/analyzer/an_common.c
|
||||
src/compiler/analyzer/an_condition.c
|
||||
src/compiler/analyzer/an_conditional.c
|
||||
src/compiler/analyzer/an_construct.c
|
||||
src/compiler/analyzer/an_declaration.c
|
||||
src/compiler/analyzer/an_extern.c
|
||||
src/compiler/analyzer/an_externfn.c
|
||||
src/compiler/analyzer/an_field.c
|
||||
src/compiler/analyzer/an_function.c
|
||||
src/compiler/analyzer/an_identifier.c
|
||||
src/compiler/analyzer/an_loop.c
|
||||
src/compiler/analyzer/an_ref.c
|
||||
src/compiler/analyzer/an_return.c
|
||||
src/compiler/analyzer/an_strfmt.c
|
||||
src/compiler/analyzer/an_struct.c
|
||||
src/compiler/analyzer/an_typedef.c
|
||||
src/compiler/analyzer/an_unarycond.c
|
||||
src/compiler/analyzer/an_unarymath.c
|
||||
src/compiler/parser/pa_and.c
|
||||
src/compiler/parser/pa_assignment.c
|
||||
src/compiler/parser/pa_block.c
|
||||
src/compiler/parser/pa_break.c
|
||||
src/compiler/parser/pa_class.c
|
||||
src/compiler/parser/pa_cond.c
|
||||
src/compiler/parser/pa_expression.c
|
||||
src/compiler/parser/pa_extern.c
|
||||
src/compiler/parser/pa_externfn.c
|
||||
src/compiler/parser/pa_factor.c
|
||||
src/compiler/parser/pa_for.c
|
||||
src/compiler/parser/pa_function.c
|
||||
src/compiler/parser/pa_if.c
|
||||
src/compiler/parser/pa_loop.c
|
||||
src/compiler/parser/pa_new.c
|
||||
src/compiler/parser/pa_or.c
|
||||
src/compiler/parser/pa_postfix.c
|
||||
src/compiler/parser/pa_power.c
|
||||
src/compiler/parser/pa_primary.c
|
||||
src/compiler/parser/pa_program.c
|
||||
src/compiler/parser/pa_ref.c
|
||||
src/compiler/parser/pa_return.c
|
||||
src/compiler/parser/pa_statement.c
|
||||
src/compiler/parser/pa_struct.c
|
||||
src/compiler/parser/pa_term.c
|
||||
src/compiler/parser/pa_typedef.c
|
||||
src/compiler/parser/pa_unary.c
|
||||
src/compiler/parser/pa_while.c
|
||||
src/compiler/parser/utility.c
|
||||
src/compiler/reader/rd_fmtstr.c
|
||||
src/compiler/reader/rd_name.c
|
||||
src/compiler/reader/rd_number.c
|
||||
src/compiler/reader/rd_string_literal.c
|
||||
src/compiler/reader/utility.c
|
||||
src/compiler/fzl_analyzer.c
|
||||
src/compiler/fzl_debug.c
|
||||
src/compiler/fzl_parser.c
|
||||
src/compiler/fzl_reader.c
|
||||
src/compiler/fzl_token.c
|
||||
src/compiler/fzl_walker.c
|
||||
src/compiler/fzl_writer.c
|
||||
src/compiler/main.c)
|
||||
endif ()
|
||||
|
||||
# Virtual machine
|
||||
add_executable(fz
|
||||
src/vm/fzl_vm.c
|
||||
src/vm/fzl_vm_env.c
|
||||
src/vm/fzl_vm_debug.c
|
||||
src/vm/main.c)
|
||||
if (NOT FZL_SKIP_VM)
|
||||
add_executable(fz
|
||||
src/vm/handlers/group_and_or.c
|
||||
src/vm/handlers/group_cmp.c
|
||||
src/vm/handlers/group_ctx.c
|
||||
src/vm/handlers/group_flow.c
|
||||
src/vm/handlers/group_fn.c
|
||||
src/vm/handlers/group_math.c
|
||||
src/vm/handlers/group_not.c
|
||||
src/vm/handlers/group_obj.c
|
||||
src/vm/handlers/group_objcmp.c
|
||||
src/vm/handlers/group_stack.c
|
||||
src/vm/handlers/group_str.c
|
||||
src/vm/handlers/group_strcmp.c
|
||||
src/vm/handlers/master.c
|
||||
src/vm/fzl_vm.c
|
||||
src/vm/fzl_vm_env.c
|
||||
src/vm/fzl_vm_debug.c
|
||||
src/vm/main.c)
|
||||
|
||||
target_link_libraries(fz PRIVATE ffi_static dl)
|
||||
endif ()
|
||||
|
||||
#
|
||||
# ========== Compilation tests ==========
|
||||
# ========== Installtion ===========
|
||||
#
|
||||
|
||||
enable_testing()
|
||||
if (NOT FZL_SKIP_C)
|
||||
install (TARGETS fzc RUNTIME DESTINATION bin)
|
||||
endif ()
|
||||
|
||||
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)
|
||||
if (NOT FZL_SKIP_VM)
|
||||
install (TARGETS fz RUNTIME DESTINATION bin)
|
||||
endif ()
|
||||
|
||||
#
|
||||
# ========== Runtime tests ==========
|
||||
# ========== 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"
|
||||
)
|
||||
if (FZL_TESTS)
|
||||
enable_testing()
|
||||
|
||||
file(GLOB TEST_PROGS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "tests/programs/*.fzs")
|
||||
foreach (TEST_FILE IN LISTS TEST_PROGS)
|
||||
get_filename_component(TEST_NAME "${TEST_FILE}" NAME_WE)
|
||||
|
||||
add_test(
|
||||
NAME "progs_compile_${TEST_NAME}"
|
||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/p_${TEST_NAME}.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_FILE}"
|
||||
)
|
||||
add_test(
|
||||
NAME "progs_runtime_${TEST_NAME}"
|
||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/p_${TEST_NAME}.fzb"
|
||||
)
|
||||
|
||||
if (TEST_NAME MATCHES "neg_")
|
||||
set_tests_properties("progs_compile_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
|
||||
set_tests_properties("progs_runtime_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
|
||||
endif()
|
||||
|
||||
if (TEST_NAME MATCHES "negr_")
|
||||
set_tests_properties("progs_runtime_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
|
||||
endif ()
|
||||
endforeach ()
|
||||
endif ()
|
||||
|
||||
|
||||
@@ -1,17 +1,58 @@
|
||||
.PHONY: configure build debug-c debug-vm test
|
||||
.PHONY: build debug-c debug-vm test bench
|
||||
|
||||
configure:
|
||||
BENCH_FILE ?= benchmarks/loop.fzs
|
||||
RUNS ?= 7
|
||||
WARMUP ?= 2
|
||||
MAX_ITER ?= 2000000
|
||||
|
||||
current_time = python3 -c "import time; print(time.time())"
|
||||
|
||||
build-debug:
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
|
||||
|
||||
build: configure
|
||||
cmake --build build
|
||||
|
||||
debug-c: build
|
||||
gdb --args ./build/fzc ${FILE}
|
||||
build:
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release $(COMPARING)
|
||||
cmake --build build $(COMPARING)
|
||||
|
||||
debug-vm: build
|
||||
install:
|
||||
cmake --install build
|
||||
|
||||
debug-c: build-debug
|
||||
gdb --args ./build/fzc -v ${FILE}
|
||||
|
||||
debug-vm: build-debug
|
||||
gdb --args ./build/fz ${FILE}
|
||||
|
||||
test-debug: build-debug
|
||||
ctest --output-on-failure --test-dir build
|
||||
|
||||
test: build
|
||||
ctest --output-on-failure --test-dir build
|
||||
|
||||
bench: COMPARING=&>/dev/null
|
||||
bench: build
|
||||
@./build/fzc -o build/benchmark.fzb $(BENCH_FILE)
|
||||
@python3 scripts/benchmark.py --runs $(RUNS) --warmup $(WARMUP) \
|
||||
--max-iter $(MAX_ITER) --vm ./build/fz --program build/benchmark.fzb
|
||||
|
||||
compare: COMPARING=&>/dev/null
|
||||
compare: build
|
||||
@echo "=== Million sums benchmark ==="
|
||||
|
||||
@./build/fzc -o build/bench.fzb $(BENCH_FILE) 1>/dev/null 2>/dev/null
|
||||
|
||||
@ST=$$($(current_time)); \
|
||||
./build/fz --allow-infinite build/bench.fzb 1>/dev/null 2>/dev/null; \
|
||||
ET=$$($(current_time)); \
|
||||
python3 -c "delta = ($$ET - $$ST) * 1000; print(f'FZLang : {delta:.5f} ms')"
|
||||
|
||||
@ST=$$($(current_time)); \
|
||||
./benchmarks/loop-c 1>/dev/null 2>/dev/null; \
|
||||
ET=$$($(current_time)); \
|
||||
python3 -c "delta = ($$ET - $$ST) * 1000; print(f'C : {delta:.5f} ms')"
|
||||
|
||||
@ST=$$($(current_time)); \
|
||||
python3 ./benchmarks/loop.py 1>/dev/null 2>/dev/null; \
|
||||
ET=$$($(current_time)); \
|
||||
python3 -c "delta = ($$ET - $$ST) * 1000; print(f'CPython: {delta:.5f} ms')"
|
||||
|
||||
@@ -27,7 +27,7 @@ Working examples of code can be found in `exl/` directory.
|
||||
## Building
|
||||
|
||||
1. Clone the repository: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||
2. Build: `make build`
|
||||
2. Build: `make build-release`
|
||||
|
||||
After this, a "build" directory will appear in the project root. It will contain two executable files:
|
||||
|
||||
|
||||
+18
-5
@@ -28,11 +28,7 @@
|
||||
|
||||
1. Склонировать проект: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||
2. Собрать проект: `make build`
|
||||
|
||||
После этого в корне появится директория **"build"**. В ней будут лежать два скомпилированных бинарных файла:
|
||||
|
||||
- `fzc` - компилятор
|
||||
- `fz` - виртуальная машина
|
||||
3. Установить проект: `sudo make install`
|
||||
|
||||
Оба бинарных файла предоставляют справку по использованию при запуске с флагом `--help` (или `-h`).
|
||||
|
||||
@@ -51,3 +47,20 @@ $ make debug-c FILE=exl/hw.fzs
|
||||
|
||||
Для проверки новых фич, нужно использовать make-таргет `test`, а тесты писать в `CMakeLists.txt`.
|
||||
|
||||
### Бенчмаркинг
|
||||
|
||||
`make bench` собирает Release-версию, компилирует `benchmarks/loop.fzs` один раз и
|
||||
измеряет время запуска программы в виртуальной машине. По умолчанию выполняются
|
||||
2 прогрева и 7 замеров; выводятся медиана, среднее, минимум и максимум. Компиляция
|
||||
исходника в измерения не входит.
|
||||
|
||||
Параметры можно переопределить:
|
||||
|
||||
```shell
|
||||
make bench RUNS=15 WARMUP=3
|
||||
make bench BENCH_FILE=path/to/program.fzs MAX_ITER=2000000
|
||||
```
|
||||
|
||||
Измеряется полное время запуска процесса `fz` (включая загрузку программы), а не
|
||||
только цикл интерпретации. Для сопоставимых результатов запускайте бенчмарк на
|
||||
одной машине и не меняйте нагрузку между прогонами.
|
||||
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,9 @@
|
||||
// Integer loop workload for measuring VM execution without I/O.
|
||||
uint i = 0;
|
||||
uint sum = 0;
|
||||
|
||||
for (i = 0; i < 1000000; i = i + 1)
|
||||
sum = sum + i;
|
||||
|
||||
printline(sum);
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
s = 0
|
||||
for i in range(1_000_000):
|
||||
s += i
|
||||
print(s)
|
||||
@@ -0,0 +1,32 @@
|
||||
// There are two ways to work with variables
|
||||
// 1. Declare variable:
|
||||
str someString = "world, hello!";
|
||||
|
||||
// 2. Change variable value:
|
||||
someString = "hello, world!";
|
||||
|
||||
// Variables help you store values to use it later
|
||||
int a = 2;
|
||||
int b = a + a * a;
|
||||
int c = a * b;
|
||||
|
||||
printline("Now we got 12:");
|
||||
printline(c);
|
||||
|
||||
// Also, there are many different variable types to help your program
|
||||
// work with many things:
|
||||
int signedInteger;
|
||||
uint unsignedInteger;
|
||||
str string;
|
||||
bool boolean;
|
||||
|
||||
i8 integer1; // 8-byte int
|
||||
i16 integer2; // 16-byte int
|
||||
i32 integer3; // 32-byte int
|
||||
i64 integer4; // 64-byte int
|
||||
|
||||
u8 integer5; // unsigned 8-byte integer
|
||||
u16 integer6; // unsigned 16-byte integer
|
||||
u32 integer7; // unsigned 32-byte integer
|
||||
u64 integer8; // unsigned 64-byte integer
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// When you need to check some value
|
||||
// a conditional statement might help you
|
||||
|
||||
int valueToCheck = 2 + 2;
|
||||
if (valueToCheck == 4)
|
||||
{
|
||||
printline("Yay! 2 + 2 = 4");
|
||||
}
|
||||
|
||||
// Conditional statement uses condition in parenthesis
|
||||
// after `if` keyword and executes command(s) after
|
||||
// if condition turns out to be true
|
||||
|
||||
// Otherwise, to execute block when condition turns
|
||||
// out to be false, you can use `else` keyword:
|
||||
|
||||
int age = 30;
|
||||
|
||||
if (age <= 20)
|
||||
printline("You're too young");
|
||||
else
|
||||
{
|
||||
age = 20;
|
||||
printline("You look too young!");
|
||||
}
|
||||
|
||||
// NOTE: If you need to execute only one statement
|
||||
// inside `if` or `else` block, then the curly
|
||||
// braces can be omitted.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// If you have some code section which is
|
||||
// duplicated in program several times
|
||||
// then it means your program needs some
|
||||
// functions!
|
||||
|
||||
fn doSomeWork()
|
||||
{
|
||||
printline("Some work");
|
||||
}
|
||||
|
||||
// If the function makes some kind of
|
||||
// calculations then you should specify
|
||||
// the result type
|
||||
|
||||
fn sum(int a, int b) int
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// The `return` keyword can be used to
|
||||
// actually return the value from the
|
||||
// function. Also, you can specify list
|
||||
// of arguments that you will use inside
|
||||
// the function
|
||||
|
||||
int first = 5;
|
||||
int second = 6;
|
||||
int result = sum(first, second);
|
||||
printline(result);
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
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);
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
// I'm a comment
|
||||
printline("Comment is skipped");;;;;
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
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
@@ -1,32 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
printline('hello, world');
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
extern "libglfw" {
|
||||
|
||||
// Opaque types
|
||||
type GLFWwindow handle;
|
||||
type GLFWmonitor handle;
|
||||
|
||||
// Struct definition
|
||||
type GLFWvidmode struct {
|
||||
int width;
|
||||
int height;
|
||||
int redBits;
|
||||
int greenBits;
|
||||
int blueBits;
|
||||
int refreshRate;
|
||||
};
|
||||
|
||||
/// GLFWvidmode*
|
||||
type GLFWvidmodeRef ref GLFWvidmode;
|
||||
|
||||
/// Create window with specified [width], [height] and [title]
|
||||
fn glfw_mkWindow(
|
||||
int width,
|
||||
int height,
|
||||
str title,
|
||||
GLFWmonitor mon,
|
||||
GLFWwindow shr
|
||||
) GLFWwindow = "glfwCreateWindow";
|
||||
|
||||
/// Get videomode from monitor [mon]
|
||||
fn glfw_getVideoMode(
|
||||
GLFWmonitor mon
|
||||
) GLFWvidmodeRef = "glfwGetVideoMode";
|
||||
|
||||
/// Get framebuffer [width] and [height]
|
||||
fn glfw_getFrameBufferSize(
|
||||
GLFWwindow win,
|
||||
out int width,
|
||||
out int height
|
||||
) = "glfwGetFramebufferSize";
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
printline("");
|
||||
@@ -1 +0,0 @@
|
||||
printline("hello);
|
||||
@@ -1,46 +0,0 @@
|
||||
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,86 @@
|
||||
#ifndef FZL_EXTERN_H
|
||||
#define FZL_EXTERN_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FZL_EXTERN_KIND_HANDLE,
|
||||
FZL_EXTERN_KIND_STRUCT,
|
||||
FZL_EXTERN_KIND_REF,
|
||||
FZL_EXTERN_KIND_ALIAS,
|
||||
} fzl_extern_kind_t;
|
||||
|
||||
// Type reference descriptor
|
||||
typedef struct
|
||||
{
|
||||
bool is_primitive;
|
||||
union
|
||||
{
|
||||
fzl_type_t primitive;
|
||||
size_t type_slot;
|
||||
};
|
||||
} fzl_extern_type_ref_t;
|
||||
|
||||
// Structure field descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
fzl_extern_type_ref_t type;
|
||||
} fzl_extern_struct_field_t;
|
||||
|
||||
// Type descriptor
|
||||
typedef struct
|
||||
{
|
||||
size_t lib_slot;
|
||||
char *name;
|
||||
fzl_extern_kind_t kind;
|
||||
|
||||
union
|
||||
{
|
||||
// Structure
|
||||
struct
|
||||
{
|
||||
fzl_extern_struct_field_t *fields;
|
||||
size_t fields_count;
|
||||
} structure;
|
||||
|
||||
// Pointer (ref)
|
||||
fzl_extern_type_ref_t ref_target;
|
||||
|
||||
// Alias
|
||||
fzl_extern_type_ref_t alias_target;
|
||||
};
|
||||
} fzl_extern_type_t;
|
||||
|
||||
// Function argument descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
fzl_extern_type_ref_t type;
|
||||
bool is_out;
|
||||
} fzl_extern_arg_t;
|
||||
|
||||
// Function descriptor
|
||||
typedef struct
|
||||
{
|
||||
size_t lib_slot;
|
||||
char *symbol_name;
|
||||
char *used_name;
|
||||
|
||||
bool has_return;
|
||||
fzl_extern_type_ref_t ret_type;
|
||||
|
||||
fzl_extern_arg_t *args;
|
||||
size_t args_count;
|
||||
} fzl_extern_fn_t;
|
||||
|
||||
// Library descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
} fzl_extern_lib_t;
|
||||
|
||||
#endif // FZL_EXTERN_H
|
||||
@@ -14,12 +14,15 @@ typedef enum
|
||||
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_LOAD_LOCAL = 0xF10006, // Load local variable
|
||||
FZL_OPCODE_STORE_LOCAL = 0xF10007, // Store local 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_CALL = 0xF30001, // Call function
|
||||
FZL_OPCODE_RET = 0xF30002, // Return from function
|
||||
FZL_OPCODE_CALL_EXTERN = 0xF30003, // Call external function
|
||||
|
||||
FZL_OPCODE_ALLOC_OBJECT = 0xFA0001, // Allocate N slots
|
||||
FZL_OPCODE_GET_FIELD = 0xFA0002, // Get field N of an object at dstack
|
||||
@@ -35,6 +38,8 @@ typedef enum
|
||||
FZL_OPCODE_MOD = 0xA00006, // Get module of two numbers in dstack
|
||||
FZL_OPCODE_NEG = 0xA00007, // Negotiate number in dstack
|
||||
|
||||
FZL_OPCODE_BUILD_STR = 0xA10001, // Build a string from N values in dstack
|
||||
|
||||
FZL_OPCODE_CMP_EQ = 0xB00001, // Equal
|
||||
FZL_OPCODE_CMP_NEQ = 0xB00002, // Not equal
|
||||
FZL_OPCODE_CMP_LT = 0xB00003, // Less than
|
||||
|
||||
@@ -6,6 +6,10 @@ typedef enum
|
||||
FZL_TYPE_NULL,
|
||||
FZL_TYPE_OBJECT,
|
||||
|
||||
FZL_TYPE_EXTERN_FN,
|
||||
FZL_TYPE_HANDLE,
|
||||
FZL_TYPE_STRUCT,
|
||||
|
||||
FZL_TYPE_STRING,
|
||||
FZL_TYPE_CHAR,
|
||||
FZL_TYPE_BOOL,
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
#ifndef FZL_ANALYZER_H
|
||||
#define FZL_ANALYZER_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Analyzing context type
|
||||
typedef enum
|
||||
{
|
||||
FZL_AN_CTX_TYPE_UNNECESSARY, // default type
|
||||
FZL_AN_CTX_TYPE_FUNCTION, // when analyzing function
|
||||
FZL_AN_CTX_TYPE_LOOP, // when analyzing loop
|
||||
FZL_AN_CTX_TYPE_CLASS, // when analyzing class
|
||||
} fzl_an_ctx_type_t;
|
||||
|
||||
// Inner node analyzing result descriptor
|
||||
typedef struct
|
||||
{
|
||||
bool error;
|
||||
|
||||
fzl_type_t type;
|
||||
char *classname;
|
||||
} fzl_an_res_t;
|
||||
|
||||
// Current node context descriptor
|
||||
typedef struct fzl_an_ctx
|
||||
{
|
||||
struct fzl_an_ctx *parent;
|
||||
|
||||
fzl_an_ctx_type_t type;
|
||||
fzl_symtable_t *symbols;
|
||||
size_t symbols_level;
|
||||
|
||||
union
|
||||
{
|
||||
fzl_node_t *fn;
|
||||
char *classname;
|
||||
};
|
||||
} fzl_an_ctx_t;
|
||||
|
||||
// Analyzer descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *warnings[256];
|
||||
size_t warnings_top;
|
||||
|
||||
char *errors[256];
|
||||
size_t errors_top;
|
||||
|
||||
fzl_symtable_t *symbols[256];
|
||||
size_t symbols_top;
|
||||
|
||||
char *alloc_names[256];
|
||||
size_t alloc_names_top;
|
||||
|
||||
fzl_node_t *root;
|
||||
fzl_an_ctx_t *ctx;
|
||||
} fzl_analyzer_t;
|
||||
|
||||
// Allocate analyzer
|
||||
fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node);
|
||||
|
||||
// Push error to errors list
|
||||
bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error);
|
||||
|
||||
// Push warning to warnings list
|
||||
bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning);
|
||||
|
||||
// Analyze AST
|
||||
void fzl_analyze(fzl_analyzer_t *analyzer);
|
||||
|
||||
// Free analyzer
|
||||
void fzl_freeanalyzer(fzl_analyzer_t *analyzer);
|
||||
|
||||
#endif // FZL_ANALYZER_H
|
||||
@@ -0,0 +1,77 @@
|
||||
#ifndef FZL_GENERATOR_H
|
||||
#define FZL_GENERATOR_H
|
||||
|
||||
#include <common/fzl_extern.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>
|
||||
|
||||
// Generator context type
|
||||
typedef enum
|
||||
{
|
||||
FZL_GEN_CTX_TYPE_UNECCESSARY,
|
||||
FZL_GEN_CTX_TYPE_FUNCTION,
|
||||
FZL_GEN_CTX_TYPE_CLASS,
|
||||
FZL_GEN_CTX_TYPE_LOOP
|
||||
} fzl_gen_ctx_type_t;
|
||||
|
||||
// Generator context descriptor
|
||||
typedef struct fzl_gen_ctx
|
||||
{
|
||||
fzl_gen_ctx_type_t type;
|
||||
struct fzl_gen_ctx *parent;
|
||||
|
||||
size_t level;
|
||||
|
||||
union
|
||||
{
|
||||
fzl_node_t *function;
|
||||
|
||||
struct
|
||||
{
|
||||
size_t breaks[256];
|
||||
size_t breaks_count;
|
||||
} loop;
|
||||
};
|
||||
} fzl_gen_ctx_t;
|
||||
|
||||
// Generator descriptor
|
||||
typedef struct
|
||||
{
|
||||
fzl_node_t *root;
|
||||
fzl_const_hash_pool_t *consts;
|
||||
fzl_gen_ctx_t *ctx;
|
||||
|
||||
size_t *ops;
|
||||
size_t ops_count;
|
||||
size_t ops_remainder;
|
||||
|
||||
fzl_symtable_t *syms[256];
|
||||
size_t syms_top;
|
||||
|
||||
fzl_ctable_t *classes[64];
|
||||
size_t classes_count;
|
||||
|
||||
fzl_extern_lib_t extern_libs[64];
|
||||
size_t extern_libs_count;
|
||||
|
||||
fzl_extern_type_t extern_types[256];
|
||||
size_t extern_types_count;
|
||||
|
||||
fzl_extern_fn_t extern_fns[256];
|
||||
size_t extern_fns_count;
|
||||
} fzl_gen_t;
|
||||
|
||||
// Allocate new generator
|
||||
fzl_gen_t *fzl_mkgen(fzl_node_t *root);
|
||||
|
||||
// Run program generation
|
||||
bool fzl_gen_run(fzl_gen_t *gen);
|
||||
|
||||
// Free generator
|
||||
void fzl_freegen(fzl_gen_t *gen);
|
||||
|
||||
#endif // FZL_GENERATOR_H
|
||||
@@ -3,22 +3,40 @@
|
||||
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// AST node type
|
||||
typedef enum
|
||||
{
|
||||
FZL_NODETYPE_TYPEDEF,
|
||||
FZL_NODETYPE_HANDLE,
|
||||
FZL_NODETYPE_ALIAS,
|
||||
FZL_NODETYPE_STRUCT,
|
||||
FZL_NODETYPE_REFERENCE,
|
||||
FZL_NODETYPE_EXTERN_FN,
|
||||
|
||||
FZL_NODETYPE_EXTERN,
|
||||
FZL_NODETYPE_BLOCK,
|
||||
FZL_NODETYPE_CALL,
|
||||
|
||||
FZL_NODETYPE_LITERAL,
|
||||
FZL_NODETYPE_FMT_STRING,
|
||||
|
||||
FZL_NODETYPE_IDENTIFIER,
|
||||
|
||||
FZL_NODETYPE_CONDITIONAL,
|
||||
|
||||
FZL_NODETYPE_FUNCTION,
|
||||
FZL_NODETYPE_RETURN,
|
||||
FZL_NODETYPE_CALL,
|
||||
|
||||
FZL_NODETYPE_CLASS,
|
||||
FZL_NODETYPE_FIELD,
|
||||
FZL_NODETYPE_CONSTRUCT,
|
||||
|
||||
FZL_NODETYPE_LOOP,
|
||||
FZL_NODETYPE_BREAK,
|
||||
|
||||
FZL_NODETYPE_ADD,
|
||||
FZL_NODETYPE_SUB,
|
||||
FZL_NODETYPE_MUL,
|
||||
@@ -89,6 +107,7 @@ typedef struct fzl_node
|
||||
{
|
||||
fzl_token_t *type;
|
||||
struct fzl_node *identifier;
|
||||
bool is_out;
|
||||
} var;
|
||||
|
||||
// Conditional blocks
|
||||
@@ -140,6 +159,56 @@ typedef struct fzl_node
|
||||
{
|
||||
struct fzl_node *name;
|
||||
} construct;
|
||||
|
||||
// Loop
|
||||
struct
|
||||
{
|
||||
struct fzl_node *init;
|
||||
struct fzl_node *cond;
|
||||
struct fzl_node *step;
|
||||
|
||||
struct fzl_node *body;
|
||||
} loop;
|
||||
|
||||
// Extern library
|
||||
struct
|
||||
{
|
||||
char *libname;
|
||||
|
||||
struct fzl_node **typedefs;
|
||||
size_t typedefs_count;
|
||||
|
||||
struct fzl_node **fns;
|
||||
size_t fns_count;
|
||||
} ext;
|
||||
|
||||
// Type definition
|
||||
struct
|
||||
{
|
||||
char *typename;
|
||||
struct fzl_node *type;
|
||||
} typedefinition;
|
||||
|
||||
// Structure
|
||||
struct
|
||||
{
|
||||
char *original_name;
|
||||
|
||||
struct fzl_node **entries;
|
||||
size_t entries_count;
|
||||
} structure;
|
||||
|
||||
// External function
|
||||
struct
|
||||
{
|
||||
char *name;
|
||||
char *original_name;
|
||||
|
||||
fzl_token_t *type;
|
||||
|
||||
struct fzl_node **args;
|
||||
size_t args_count;
|
||||
} ext_fn;
|
||||
};
|
||||
} fzl_node_t;
|
||||
|
||||
|
||||
@@ -8,53 +8,68 @@
|
||||
// Token type
|
||||
typedef enum
|
||||
{
|
||||
FZL_TOKTYPE_CLASS, // class
|
||||
FZL_TOKTYPE_NEW, // new
|
||||
FZL_TOKTYPE_IMPORT, // `import`
|
||||
FZL_TOKTYPE_EXTERN, // `extern`
|
||||
FZL_TOKTYPE_TYPEDEF, // `type`
|
||||
FZL_TOKTYPE_HANDLE, // `handle`
|
||||
FZL_TOKTYPE_STRUCT, // `struct`
|
||||
FZL_TOKTYPE_REFERENCE, // `ref`
|
||||
FZL_TOKTYPE_OUT, // `out`
|
||||
|
||||
FZL_TOKTYPE_FUNCTION, // fn
|
||||
FZL_TOKTYPE_RETURN, // return
|
||||
FZL_TOKTYPE_CLASS, // `class`
|
||||
FZL_TOKTYPE_NEW, // `new`
|
||||
|
||||
FZL_TOKTYPE_IF, // if
|
||||
FZL_TOKTYPE_ELSE, // else
|
||||
FZL_TOKTYPE_FUNCTION, // `fn`
|
||||
FZL_TOKTYPE_RETURN, // `return`
|
||||
|
||||
FZL_TOKTYPE_FOR, // `for`
|
||||
FZL_TOKTYPE_WHILE, // `while`
|
||||
FZL_TOKTYPE_BREAK, // `break`
|
||||
|
||||
FZL_TOKTYPE_IF, // `if`
|
||||
FZL_TOKTYPE_ELSE, // `else`
|
||||
|
||||
FZL_TOKTYPE_NAME, // abc
|
||||
FZL_TOKTYPE_TYPE, // i16
|
||||
|
||||
FZL_TOKTYPE_DDASH, // // :D
|
||||
FZL_TOKTYPE_DDASH, // `//` :D
|
||||
|
||||
FZL_TOKTYPE_LPAREN, // (
|
||||
FZL_TOKTYPE_RPAREN, // )
|
||||
FZL_TOKTYPE_LBRACE, // {
|
||||
FZL_TOKTYPE_RBRACE, // }
|
||||
FZL_TOKTYPE_LPAREN, // `(`
|
||||
FZL_TOKTYPE_RPAREN, // `)`
|
||||
FZL_TOKTYPE_LBRACE, // `{`
|
||||
FZL_TOKTYPE_RBRACE, // `}`
|
||||
|
||||
FZL_TOKTYPE_COMMA, // ,
|
||||
FZL_TOKTYPE_DOT, // .
|
||||
FZL_TOKTYPE_SEMICOLON, // ;
|
||||
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_STR_FORMAT_START, // `$` before `"`
|
||||
FZL_TOKTYPE_STR_FORMAT_END, // end of string formatting sequence
|
||||
|
||||
FZL_TOKTYPE_EQEQ, // ==
|
||||
FZL_TOKTYPE_REQ, // >=
|
||||
FZL_TOKTYPE_LEQ, // <=
|
||||
FZL_TOKTYPE_NEQ, // !=
|
||||
FZL_TOKTYPE_LANGLE, // <
|
||||
FZL_TOKTYPE_RANGLE, // >
|
||||
FZL_TOKTYPE_NOT, // !
|
||||
FZL_TOKTYPE_DAMPERSAND, // `&&`
|
||||
FZL_TOKTYPE_DVBAR, // `||`
|
||||
|
||||
FZL_TOKTYPE_STAR, // *
|
||||
FZL_TOKTYPE_SLASH, // /
|
||||
FZL_TOKTYPE_PLUS, // +
|
||||
FZL_TOKTYPE_MINUS, // -
|
||||
FZL_TOKTYPE_CARET, // ^
|
||||
FZL_TOKTYPE_PERCENT, // %
|
||||
FZL_TOKTYPE_EQEQ, // `==`
|
||||
FZL_TOKTYPE_REQ, // `>=`
|
||||
FZL_TOKTYPE_LEQ, // `<=`
|
||||
FZL_TOKTYPE_NEQ, // `!=`
|
||||
FZL_TOKTYPE_LANGLE, // `<`
|
||||
FZL_TOKTYPE_RANGLE, // `>`
|
||||
FZL_TOKTYPE_NOT, // `!`
|
||||
|
||||
FZL_TOKTYPE_EQ // =
|
||||
FZL_TOKTYPE_STAR, // `*`
|
||||
FZL_TOKTYPE_SLASH, // `/`
|
||||
FZL_TOKTYPE_PLUS, // `+`
|
||||
FZL_TOKTYPE_MINUS, // `-`
|
||||
FZL_TOKTYPE_CARET, // `^`
|
||||
FZL_TOKTYPE_PERCENT, // `%`
|
||||
|
||||
FZL_TOKTYPE_EQ // `=`
|
||||
} fzl_toktype_t;
|
||||
|
||||
// Token descriptor
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef FZL_WALKER_H
|
||||
#define FZL_WALKER_H
|
||||
|
||||
#include <common/fzl_extern.h>
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
@@ -16,7 +17,8 @@ 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_ASSIGN,
|
||||
FZL_WALK_CTX_TYPE_LOOP,
|
||||
} fzl_walk_ctx_type_t;
|
||||
|
||||
// Internal walk function result
|
||||
@@ -36,8 +38,23 @@ 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;
|
||||
union
|
||||
{
|
||||
// function info
|
||||
struct
|
||||
{
|
||||
fzl_node_t *fn;
|
||||
};
|
||||
|
||||
// loops `break`s writeback addrs
|
||||
struct
|
||||
{
|
||||
size_t *breaks;
|
||||
size_t breaks_count;
|
||||
size_t breaks_remainder;
|
||||
};
|
||||
};
|
||||
} fzl_walk_ctx_t;
|
||||
|
||||
// Walker descriptor
|
||||
@@ -57,6 +74,15 @@ typedef struct
|
||||
|
||||
fzl_ctable_t *classes[64];
|
||||
size_t classes_count;
|
||||
|
||||
fzl_extern_lib_t extern_libs[64];
|
||||
size_t extern_libs_count;
|
||||
|
||||
fzl_extern_type_t extern_types[256];
|
||||
size_t extern_types_count;
|
||||
|
||||
fzl_extern_fn_t extern_fns[256];
|
||||
size_t extern_fns_count;
|
||||
} fzl_walker_t;
|
||||
|
||||
// Allocate new walker
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef INTERNALS_ANALYZER_H
|
||||
#define INTERNALS_ANALYZER_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
extern char an_problem[256];
|
||||
|
||||
bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc);
|
||||
char *an_typetostr(fzl_type_t type, char *classname);
|
||||
fzl_type_t an_toktotype(fzl_analyzer_t *analyzer, fzl_token_t *tok,
|
||||
bool *sym_found);
|
||||
|
||||
fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node);
|
||||
fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node);
|
||||
fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id);
|
||||
fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond);
|
||||
fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node);
|
||||
fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node);
|
||||
fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node);
|
||||
fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node);
|
||||
fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field);
|
||||
fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl);
|
||||
fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct);
|
||||
fzl_an_res_t analyze_loop(fzl_analyzer_t *analyzer, fzl_node_t *loop_node);
|
||||
fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node);
|
||||
fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node);
|
||||
fzl_an_res_t analyze_unarymath(fzl_analyzer_t *analyzer, fzl_node_t *unary);
|
||||
fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node);
|
||||
fzl_an_res_t analyze_unarycond(fzl_analyzer_t *analyzer, fzl_node_t *unary);
|
||||
fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node);
|
||||
fzl_an_res_t analyze_strfmt(fzl_analyzer_t *analyzer, fzl_node_t *strfmt_node);
|
||||
fzl_an_res_t analyze_typedef(fzl_analyzer_t *analyzer, fzl_node_t *td_node);
|
||||
fzl_an_res_t analyze_struct(fzl_analyzer_t *analyzer, fzl_node_t *struct_node);
|
||||
fzl_an_res_t analyze_ref(fzl_analyzer_t *analyzer, fzl_node_t *ref_node);
|
||||
fzl_an_res_t analyze_externfn(fzl_analyzer_t *analyzer, fzl_node_t *externfn);
|
||||
fzl_an_res_t analyze_extern(fzl_analyzer_t *analyzer, fzl_node_t *extern_node);
|
||||
|
||||
fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type);
|
||||
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer);
|
||||
|
||||
#endif // INTERNALS_ANALYZER_H
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef INTERNALS_GEN_H
|
||||
#define INTERNALS_GEN_H
|
||||
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
extern char *null_name;
|
||||
|
||||
bool gen_resolve_name(fzl_gen_t *gen, char *name, fzl_sym_t *sym);
|
||||
bool gen_resolve_type(fzl_gen_t *gen, fzl_token_t *tok, fzl_type_t *t, char *c);
|
||||
bool gen_is_types_compatible(fzl_type_t t1, fzl_type_t t2, char *c1, char *c2);
|
||||
size_t gen_add_const(fzl_gen_t *gen, fzl_const_t c);
|
||||
void gen_op(fzl_gen_t *gen, fzl_opcode_t opcode);
|
||||
void gen_word(fzl_gen_t *gen, size_t word);
|
||||
|
||||
fzl_gen_ctx_t *gen_ctx(fzl_gen_t *gen, fzl_gen_ctx_type_t type,
|
||||
fzl_node_t *node);
|
||||
fzl_gen_ctx_t *gen_fin_ctx(fzl_gen_t *gen);
|
||||
|
||||
bool gen_common(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_extern(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_externfn(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_reference(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_struct(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_alias(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_handle(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_typedef(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_fmtstr(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_break(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_loop(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_class(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_construct(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_field(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_fn(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_return(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_not(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_compare(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_or_and(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_condition(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_unary_plus(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_unary_minus(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_identifier(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_block(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_call(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_literal(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_math(fzl_gen_t *gen, fzl_node_t *node);
|
||||
bool gen_assign(fzl_gen_t *gen, fzl_node_t *node);
|
||||
|
||||
#endif // FZL_INTERNALS_GEN_H
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef INTERNALS_PARSER_H
|
||||
#define INTERNALS_PARSER_H
|
||||
|
||||
#include "compiler/fzl_parser.h"
|
||||
#include "compiler/fzl_token.h"
|
||||
|
||||
extern char plast_err[256];
|
||||
extern fzl_token_t *last_known_tok;
|
||||
|
||||
extern fzl_token_t null_tok;
|
||||
extern fzl_token_t this_tok;
|
||||
|
||||
fzl_token_t *peek(fzl_parser_t *parser);
|
||||
fzl_token_t *consume(fzl_parser_t *parser);
|
||||
bool is(fzl_parser_t *parser, fzl_toktype_t type);
|
||||
fzl_nodetype_t math_op_to_type(fzl_toktype_t type);
|
||||
fzl_nodetype_t unary_op_to_type(fzl_toktype_t type);
|
||||
|
||||
fzl_node_t *pa_primary(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_postfix(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_unary(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_power(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_factor(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_term(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_cond(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_and(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_or(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_new(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_assignment(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_expression(fzl_parser_t *parser);
|
||||
|
||||
fzl_node_t *pa_struct(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_ref(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_typedef(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_externfn(fzl_parser_t *parser);
|
||||
|
||||
fzl_node_t *pa_class(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_function(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_return(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_if(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_block(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_for(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_while(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_loop(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_break(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_extern(fzl_parser_t *parser);
|
||||
|
||||
fzl_node_t *pa_statement(fzl_parser_t *parser);
|
||||
fzl_node_t *pa_program(fzl_parser_t *parser);
|
||||
|
||||
#endif // INTERNALS_PARSER_H
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef INTERNALS_READER_H
|
||||
#define INTERNALS_READER_H
|
||||
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <stddef.h>
|
||||
|
||||
extern char last_err[256];
|
||||
|
||||
size_t move(fzl_reader_t *reader, size_t len);
|
||||
void rd_comment(fzl_reader_t *reader);
|
||||
|
||||
fzl_token_t *rd_name(fzl_reader_t *reader);
|
||||
fzl_token_t *rd_string_literal(fzl_reader_t *reader);
|
||||
fzl_token_t *rd_number(fzl_reader_t *reader);
|
||||
fzl_token_t *rd_single(fzl_reader_t *reader, fzl_toktype_t type);
|
||||
fzl_token_t *rd_double(fzl_reader_t *reader, fzl_toktype_t type);
|
||||
fzl_token_t *rd_fmtstr(fzl_reader_t *reader);
|
||||
|
||||
#endif // INTERNALS_READER_H
|
||||
@@ -2,6 +2,7 @@
|
||||
#define FZL_SYMTABLE_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -19,8 +20,12 @@ typedef struct
|
||||
{
|
||||
char *name;
|
||||
fzl_type_t type;
|
||||
fzl_node_t *node;
|
||||
size_t ip;
|
||||
|
||||
bool slot_set;
|
||||
size_t slot;
|
||||
|
||||
char *classname;
|
||||
} fzl_sym_t;
|
||||
|
||||
@@ -30,6 +35,8 @@ typedef struct
|
||||
size_t size;
|
||||
size_t remainder;
|
||||
|
||||
size_t free_slot;
|
||||
|
||||
fzl_sym_t *symbols;
|
||||
} fzl_symtable_t;
|
||||
|
||||
@@ -37,13 +44,16 @@ typedef struct
|
||||
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);
|
||||
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, fzl_node_t *node,
|
||||
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);
|
||||
|
||||
// Assign a local slot to a variable
|
||||
int fzl_symt_setslot(fzl_symtable_t *symt, char *name);
|
||||
|
||||
// Get entry from specified symtable
|
||||
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res);
|
||||
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
|
||||
#include <common/fzl_value.h>
|
||||
#include <compiler/utils/fzl_const_pool.h> // NOTE: move outside compiler scope to common
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <vm/fzl_vm_env.h>
|
||||
#include <vm/fzl_vm_extern.h>
|
||||
#include <vm/fzl_vm_object.h>
|
||||
|
||||
// VM descriptor
|
||||
@@ -14,11 +16,17 @@ typedef struct
|
||||
fzl_const_t *consts;
|
||||
size_t consts_count;
|
||||
|
||||
fzl_vm_extern_t *externs;
|
||||
size_t externs_count;
|
||||
|
||||
uint64_t *ops;
|
||||
size_t ops_count;
|
||||
|
||||
size_t ip;
|
||||
|
||||
bool allow_infinite;
|
||||
size_t max_iter;
|
||||
|
||||
// Data stack
|
||||
fzl_value_t dstack[256];
|
||||
size_t dtop;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef FZL_VM_EXTERN_H
|
||||
#define FZL_VM_EXTERN_H
|
||||
|
||||
#include <ffi.h>
|
||||
|
||||
// Loaded extern function definition
|
||||
typedef struct
|
||||
{
|
||||
void *func_ptr;
|
||||
ffi_cif cif;
|
||||
ffi_type **arg_types;
|
||||
} fzl_vm_extern_t;
|
||||
|
||||
#endif // FZL_VM_EXTERN_H
|
||||
@@ -0,0 +1,112 @@
|
||||
#ifndef INTERNALS_OPCODE_H
|
||||
#define INTERNALS_OPCODE_H
|
||||
|
||||
#include "vm/fzl_vm.h"
|
||||
#include <stddef.h>
|
||||
|
||||
// Opcode handler descriptor
|
||||
typedef int (*opcode_handler)(fzl_vm_t *);
|
||||
|
||||
extern char last_err[256];
|
||||
extern size_t last_jmp_ip;
|
||||
extern size_t total_jmp_on_same;
|
||||
|
||||
void print_object(fzl_vm_object_t *obj);
|
||||
void print_err_data(fzl_vm_t *vm);
|
||||
void escape_recursive(fzl_vm_env_t *current, fzl_vm_env_t *parent,
|
||||
fzl_vm_object_t *obj);
|
||||
|
||||
// --- stack ---
|
||||
|
||||
extern const opcode_handler group_stack[];
|
||||
|
||||
int op_loadconst(fzl_vm_t *vm);
|
||||
int op_call_builtin(fzl_vm_t *vm);
|
||||
int op_pushnum(fzl_vm_t *vm);
|
||||
int op_pushbool(fzl_vm_t *vm);
|
||||
|
||||
// --- context ---
|
||||
|
||||
extern const opcode_handler group_ctx[];
|
||||
|
||||
int op_mk_envtable(fzl_vm_t *vm);
|
||||
int op_rm_envtable(fzl_vm_t *vm);
|
||||
int op_store(fzl_vm_t *vm);
|
||||
int op_lookup_env(fzl_vm_t *vm);
|
||||
int op_write(fzl_vm_t *vm);
|
||||
int op_load_local(fzl_vm_t *vm);
|
||||
int op_store_local(fzl_vm_t *vm);
|
||||
|
||||
// --- flow ---
|
||||
|
||||
extern const opcode_handler group_flow[];
|
||||
|
||||
int op_jmp(fzl_vm_t *vm);
|
||||
int op_jz(fzl_vm_t *vm);
|
||||
|
||||
// --- functions ---
|
||||
|
||||
extern const opcode_handler group_fn[];
|
||||
|
||||
int op_call(fzl_vm_t *vm);
|
||||
int op_ret(fzl_vm_t *vm);
|
||||
int op_call_extern(fzl_vm_t *vm);
|
||||
|
||||
// --- objects ---
|
||||
|
||||
extern const opcode_handler group_obj[];
|
||||
|
||||
int op_alloc_object(fzl_vm_t *vm);
|
||||
int op_get_field(fzl_vm_t *vm);
|
||||
int op_set_field(fzl_vm_t *vm);
|
||||
int op_escape_object(fzl_vm_t *vm);
|
||||
int op_dereference(fzl_vm_t *vm);
|
||||
|
||||
// --- math ---
|
||||
|
||||
extern const opcode_handler group_math[];
|
||||
|
||||
int op_add(fzl_vm_t *vm);
|
||||
int op_sub(fzl_vm_t *vm);
|
||||
int op_mul(fzl_vm_t *vm);
|
||||
int op_div(fzl_vm_t *vm);
|
||||
int op_pow(fzl_vm_t *vm);
|
||||
int op_mod(fzl_vm_t *vm);
|
||||
int op_neg(fzl_vm_t *vm);
|
||||
|
||||
// --- strings ---
|
||||
|
||||
extern const opcode_handler group_str[];
|
||||
|
||||
int op_build_str(fzl_vm_t *vm);
|
||||
|
||||
// --- conditions ---
|
||||
|
||||
extern const opcode_handler group_cmp[];
|
||||
int op_cmp_eq(fzl_vm_t *vm);
|
||||
int op_cmp_neq(fzl_vm_t *vm);
|
||||
int op_cmp_lt(fzl_vm_t *vm);
|
||||
int op_cmp_lteq(fzl_vm_t *vm);
|
||||
int op_cmp_gt(fzl_vm_t *vm);
|
||||
int op_cmp_gteq(fzl_vm_t *vm);
|
||||
|
||||
extern const opcode_handler group_strcmp[];
|
||||
int op_cmp_eq_str(fzl_vm_t *vm);
|
||||
int op_cmp_neq_str(fzl_vm_t *vm);
|
||||
|
||||
extern const opcode_handler group_objcmp[];
|
||||
int op_cmp_eq_obj(fzl_vm_t *vm);
|
||||
int op_cmp_neq_obj(fzl_vm_t *vm);
|
||||
|
||||
extern const opcode_handler group_and_or[];
|
||||
int op_and(fzl_vm_t *vm);
|
||||
int op_or(fzl_vm_t *vm);
|
||||
|
||||
extern const opcode_handler group_not[];
|
||||
int op_not(fzl_vm_t *vm);
|
||||
|
||||
// ------------------------
|
||||
|
||||
extern const opcode_handler *const master_tbl[];
|
||||
|
||||
#endif // INTERNALS_OPCODE_H
|
||||
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run an FZL VM benchmark and report wall-clock timing statistics."""
|
||||
|
||||
import argparse
|
||||
import statistics
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--runs", type=int, default=7, help="measured runs")
|
||||
parser.add_argument("--warmup", type=int, default=2, help="warm-up runs")
|
||||
parser.add_argument("--max-iter", type=int, default=2_000_000)
|
||||
parser.add_argument("--vm", required=True, help="path to the fz executable")
|
||||
parser.add_argument("--program", required=True, help="compiled .fzb program")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.runs < 1 or args.warmup < 0 or args.max_iter < 1:
|
||||
parser.error("runs/max-iter must be positive and warmup must be non-negative")
|
||||
|
||||
command = [args.vm, "--max-iter", str(args.max_iter), args.program]
|
||||
|
||||
for _ in range(args.warmup):
|
||||
result = subprocess.run(command, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
sys.stderr.write(result.stdout)
|
||||
sys.stderr.write(result.stderr)
|
||||
raise SystemExit(result.returncode)
|
||||
|
||||
measurements = []
|
||||
for _ in range(args.runs):
|
||||
start = time.perf_counter()
|
||||
result = subprocess.run(
|
||||
command, capture_output=True, text=True
|
||||
)
|
||||
elapsed = time.perf_counter() - start
|
||||
if result.returncode != 0:
|
||||
sys.stderr.write(result.stdout)
|
||||
sys.stderr.write(result.stderr)
|
||||
raise SystemExit(result.returncode)
|
||||
measurements.append(elapsed)
|
||||
|
||||
print(f"Runs: {args.runs} (warm-up: {args.warmup})")
|
||||
print(f"Median: {statistics.median(measurements):.6f} s")
|
||||
print(f"Mean: {statistics.mean(measurements):.6f} s")
|
||||
print(f"Min: {min(measurements):.6f} s")
|
||||
print(f"Max: {max(measurements):.6f} s")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze assigns
|
||||
fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!assign_node || assign_node->type != FZL_NODETYPE_ASSIGN)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not assignment");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *left = assign_node->binary_op.left;
|
||||
fzl_an_res_t leftside = analyze_common(analyzer, left);
|
||||
if (leftside.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t rightside =
|
||||
analyze_common(analyzer, assign_node->binary_op.right);
|
||||
if (rightside.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (left->type != FZL_NODETYPE_DECLARATION &&
|
||||
left->type != FZL_NODETYPE_IDENTIFIER && left->type != FZL_NODETYPE_FIELD)
|
||||
{
|
||||
an_push_error(analyzer, left->token,
|
||||
"left side must be one of these: declaration, identifier or "
|
||||
"field access");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(leftside.type, rightside.type, leftside.classname,
|
||||
rightside.classname))
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"incompatible types - '%s' is expected, got '%s'",
|
||||
an_typetostr(leftside.type, leftside.classname),
|
||||
an_typetostr(rightside.type, rightside.classname));
|
||||
an_push_error(analyzer, assign_node->binary_op.right->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze binary arithmetics
|
||||
fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!bin_node || (bin_node->type != FZL_NODETYPE_ADD &&
|
||||
bin_node->type != FZL_NODETYPE_SUB &&
|
||||
bin_node->type != FZL_NODETYPE_MUL &&
|
||||
bin_node->type != FZL_NODETYPE_DIV &&
|
||||
bin_node->type != FZL_NODETYPE_POW &&
|
||||
bin_node->type != FZL_NODETYPE_MOD))
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not binary math operator");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t left = analyze_common(analyzer, bin_node->binary_op.left);
|
||||
if (left.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t right = analyze_common(analyzer, bin_node->binary_op.right);
|
||||
if (right.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (left.type < FZL_TYPE_INT8)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"integer type is expected, got '%s'",
|
||||
an_typetostr(left.type, left.classname));
|
||||
an_push_error(analyzer, bin_node->binary_op.left->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (right.type < FZL_TYPE_INT8)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"integer type is expected, got '%s'",
|
||||
an_typetostr(right.type, right.classname));
|
||||
an_push_error(analyzer, bin_node->binary_op.right->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = left.type, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze block
|
||||
fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!block_node || block_node->type != FZL_NODETYPE_BLOCK)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "node is not a block");
|
||||
an_push_error(analyzer, NULL, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY);
|
||||
|
||||
bool has_return = false;
|
||||
bool error = false;
|
||||
for (size_t i = 0; i < block_node->list.entry_count; i++)
|
||||
{
|
||||
if (has_return)
|
||||
an_push_warning(analyzer, block_node->list.entries[i]->token,
|
||||
"this statement will never execute");
|
||||
|
||||
fzl_an_res_t entry_res =
|
||||
analyze_common(analyzer, block_node->list.entries[i]);
|
||||
if (entry_res.error)
|
||||
error = true;
|
||||
|
||||
if (block_node->list.entries[i]->type == FZL_NODETYPE_RETURN)
|
||||
has_return = true;
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *fnctx = analyzer->ctx;
|
||||
while (fnctx != NULL)
|
||||
{
|
||||
if (fnctx && fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
break;
|
||||
|
||||
fnctx = fnctx->parent;
|
||||
}
|
||||
|
||||
if (fnctx && fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
{
|
||||
bool found = false;
|
||||
fzl_type_t rettype = an_toktotype(analyzer, fnctx->fn->fn.type, &found);
|
||||
char *classname = NULL;
|
||||
char *rettype_str = an_typetostr(rettype, classname);
|
||||
|
||||
if (!found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)fnctx->fn->fn.type->value);
|
||||
an_push_error(analyzer, fnctx->fn->fn.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (rettype == FZL_TYPE_OBJECT)
|
||||
classname = (char *)fnctx->fn->fn.type->value;
|
||||
|
||||
if (rettype != FZL_TYPE_NULL)
|
||||
{
|
||||
if (!has_return || block_node->list.entry_count < 1)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"'return <%s>' is expected in the function with non-null "
|
||||
"return type",
|
||||
rettype_str);
|
||||
an_push_error(analyzer, block_node->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *last_node =
|
||||
block_node->list.entries[block_node->list.entry_count - 1];
|
||||
if (last_node->type != FZL_NODETYPE_RETURN)
|
||||
{
|
||||
// NOTE: replace with ghost walker to check if all branch return an
|
||||
// expected value type
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"expected last statement in block to be 'return <%s>'",
|
||||
rettype_str);
|
||||
an_push_error(analyzer, last_node->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
|
||||
// Analyze break node
|
||||
fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!break_node || break_node->type != FZL_NODETYPE_BREAK)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a break statement");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *ctx = analyzer->ctx;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
if (ctx && ctx->type == FZL_AN_CTX_TYPE_LOOP)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP)
|
||||
{
|
||||
an_push_error(analyzer, break_node->token,
|
||||
"cannot use 'break' outside the loop");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *tmp_method_name = NULL;
|
||||
char *tmp_fnname = NULL;
|
||||
char *tmp_classname = NULL;
|
||||
|
||||
// Try to get symbol name from callee
|
||||
char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||
{
|
||||
*method = false;
|
||||
|
||||
if (callee->type == FZL_NODETYPE_IDENTIFIER)
|
||||
return (char *)callee->token->value;
|
||||
|
||||
if (callee->type != FZL_NODETYPE_FIELD)
|
||||
{
|
||||
an_push_error(analyzer, callee->token, "unknown call format");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*method = true;
|
||||
fzl_an_res_t parent_res = analyze_common(analyzer, callee->memb.parent);
|
||||
if (parent_res.type != FZL_TYPE_OBJECT || !parent_res.classname)
|
||||
return NULL;
|
||||
|
||||
char *fn_name = callee->memb.field->token->value;
|
||||
tmp_fnname = fn_name;
|
||||
tmp_classname = parent_res.classname;
|
||||
size_t classname_len = strlen(parent_res.classname);
|
||||
size_t methodname_len = strlen(fn_name);
|
||||
tmp_method_name = malloc(classname_len + methodname_len + 2);
|
||||
if (!tmp_method_name)
|
||||
{
|
||||
an_push_error(analyzer, callee->token, "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(tmp_method_name, parent_res.classname);
|
||||
tmp_method_name[classname_len] = '_';
|
||||
strcpy(tmp_method_name + classname_len + 1, fn_name);
|
||||
tmp_method_name[classname_len + methodname_len + 1] = 0;
|
||||
|
||||
return tmp_method_name;
|
||||
}
|
||||
|
||||
// Analyze call node
|
||||
fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
{
|
||||
if (!call_node || call_node->type != FZL_NODETYPE_CALL)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not call");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *callee = call_node->call.callee;
|
||||
|
||||
bool is_method = false;
|
||||
char *callee_name = resolve_callee(analyzer, callee, &is_method);
|
||||
if (!callee_name)
|
||||
{
|
||||
an_push_error(analyzer, callee->token, "expression is not callable");
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
// NOTE: move into `compiler/analyzer/builtins.c`
|
||||
if (strcmp(callee_name, "printline") == 0 || strcmp(callee_name, "exit") == 0)
|
||||
{
|
||||
if (call_node->call.args_count != 1)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"'%s' builtin should be called with 1 argument instead of %zu",
|
||||
callee_name, call_node->call.args_count);
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found = fzl_symt_get(analyzer->symbols[i], callee_name, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot resolve symbol '%s'",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (sym.node->type != FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "'%s' is not callable",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
size_t offset = is_method ? 1 : 0;
|
||||
if ((sym.node->fn.args_count - offset) != call_node->call.args_count)
|
||||
{
|
||||
if (is_method)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"method '%s.%s' is expecting %zu argument(s), got %zu",
|
||||
tmp_classname, tmp_fnname, sym.node->fn.args_count,
|
||||
call_node->call.args_count - offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"function '%s' is expecting %zu argument(s), got %zu",
|
||||
callee_name, sym.node->fn.args_count,
|
||||
call_node->call.args_count - offset);
|
||||
}
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
for (size_t i = offset; i < sym.node->fn.args_count; i++)
|
||||
{
|
||||
fzl_node_t *arg = sym.node->fn.args[i];
|
||||
bool found = false;
|
||||
fzl_type_t req_type = an_toktotype(analyzer, arg->var.type, &found);
|
||||
char *req_classname = NULL;
|
||||
|
||||
if (!found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)arg->var.type->value);
|
||||
an_push_error(analyzer, arg->var.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (req_type == FZL_TYPE_OBJECT)
|
||||
req_classname = arg->var.type->value;
|
||||
|
||||
fzl_an_res_t got =
|
||||
analyze_common(analyzer, call_node->call.args[i - offset]);
|
||||
if (got.error)
|
||||
{
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(req_type, got.type, req_classname, got.classname))
|
||||
{
|
||||
if (is_method)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"method '%s.%s' is expecting %zu argument to have type '%s', "
|
||||
"got '%s'",
|
||||
tmp_classname, tmp_fnname, i + 1 - offset,
|
||||
an_typetostr(req_type, req_classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"function '%s' is expecting %zu argument to have type '%s', "
|
||||
"got '%s'",
|
||||
callee_name, i + 1 - offset,
|
||||
an_typetostr(req_type, req_classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
}
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
bool type_found = false;
|
||||
fzl_type_t type = an_toktotype(analyzer, sym.node->fn.type, &type_found);
|
||||
char *classname = sym.classname;
|
||||
|
||||
if (!type_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)sym.node->fn.type->value);
|
||||
an_push_error(analyzer, sym.node->fn.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze class declaration
|
||||
fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!class_node || class_node->type != FZL_NODETYPE_CLASS)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not class");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *classname = (char *)class_node->class.name->value;
|
||||
fzl_an_ctx_t *ctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_CLASS);
|
||||
ctx->classname = classname;
|
||||
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found = fzl_symt_get(analyzer->symbols[i], classname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
fzl_symtable_res_t res =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], class_node,
|
||||
classname, FZL_TYPE_NULL, 0);
|
||||
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"name '%s' already has been declared", classname);
|
||||
an_push_error(analyzer, class_node->token, an_problem);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot store name '%s'",
|
||||
classname);
|
||||
an_push_error(analyzer, class_node->token, an_problem);
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
bool error = false;
|
||||
for (size_t i = 0; i < class_node->class.fields_count; i++)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, class_node->class.fields[i]);
|
||||
if (res.error)
|
||||
error = true;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < class_node->class.methods_count; i++)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, class_node->class.methods[i]);
|
||||
if (res.error)
|
||||
error = true;
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Analyzer main function
|
||||
fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node)
|
||||
{
|
||||
if (!analyzer || !node)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t res = {.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_EXTERN:
|
||||
res = analyze_extern(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_EXTERN_FN:
|
||||
res = analyze_externfn(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_REFERENCE:
|
||||
res = analyze_ref(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_STRUCT:
|
||||
res = analyze_struct(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_ALIAS:
|
||||
res.type = an_toktotype(analyzer, node->token, NULL);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_HANDLE:
|
||||
res.type = FZL_TYPE_HANDLE;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_TYPEDEF:
|
||||
res = analyze_typedef(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FMT_STRING:
|
||||
res = analyze_strfmt(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_ASSIGN:
|
||||
res = analyze_assign(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_NOT:
|
||||
res = analyze_unarycond(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_OR:
|
||||
case FZL_NODETYPE_AND:
|
||||
case FZL_NODETYPE_LESS:
|
||||
case FZL_NODETYPE_LESS_EQUAL:
|
||||
case FZL_NODETYPE_GREATER:
|
||||
case FZL_NODETYPE_GREATER_EQUAL:
|
||||
case FZL_NODETYPE_EQUAL:
|
||||
case FZL_NODETYPE_NEQUAL:
|
||||
res = analyze_condition(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_UNARY_MINUS:
|
||||
case FZL_NODETYPE_UNARY_PLUS:
|
||||
res = analyze_unarymath(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_ADD:
|
||||
case FZL_NODETYPE_SUB:
|
||||
case FZL_NODETYPE_MUL:
|
||||
case FZL_NODETYPE_DIV:
|
||||
case FZL_NODETYPE_POW:
|
||||
case FZL_NODETYPE_MOD:
|
||||
res = analyze_binmath(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BREAK:
|
||||
res = analyze_break(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LOOP:
|
||||
res = analyze_loop(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONSTRUCT:
|
||||
res = analyze_construct(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
res = analyze_declaration(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FIELD:
|
||||
res = analyze_field(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CLASS:
|
||||
res = analyze_class(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_RETURN:
|
||||
res = analyze_return(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FUNCTION:
|
||||
res = analyze_function(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONDITIONAL:
|
||||
res = analyze_conditional(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
res = analyze_identifier(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
res = analyze_block(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CALL:
|
||||
res = analyze_call(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
switch (node->token->type)
|
||||
{
|
||||
case FZL_TOKTYPE_STR_LITERAL:
|
||||
res.type = FZL_TYPE_STRING;
|
||||
break;
|
||||
|
||||
case FZL_TOKTYPE_NUM_LITERAL:
|
||||
res.type = FZL_TYPE_INT;
|
||||
break;
|
||||
|
||||
case FZL_TOKTYPE_BOOL_LITERAL:
|
||||
res.type = FZL_TYPE_BOOL;
|
||||
break;
|
||||
|
||||
case FZL_TOKTYPE_NULL_LITERAL:
|
||||
res.type = FZL_TYPE_OBJECT;
|
||||
res.classname = "null";
|
||||
break;
|
||||
|
||||
default:
|
||||
an_push_error(analyzer, node->token, "unknown literal type");
|
||||
res.error = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze binary conditional nodes
|
||||
fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!cond_node || (cond_node->type != FZL_NODETYPE_OR &&
|
||||
cond_node->type != FZL_NODETYPE_AND &&
|
||||
cond_node->type != FZL_NODETYPE_LESS &&
|
||||
cond_node->type != FZL_NODETYPE_LESS_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_GREATER &&
|
||||
cond_node->type != FZL_NODETYPE_GREATER_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_NEQUAL))
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a binary conditional operator");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t left = analyze_common(analyzer, cond_node->binary_op.left);
|
||||
if (left.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t right = analyze_common(analyzer, cond_node->binary_op.right);
|
||||
if (right.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (cond_node->type == FZL_NODETYPE_OR || cond_node->type == FZL_NODETYPE_AND)
|
||||
{
|
||||
if (left.type != FZL_TYPE_BOOL)
|
||||
an_push_warning(analyzer, cond_node->binary_op.left->token,
|
||||
"left operand is not resulting with boolean type");
|
||||
|
||||
if (right.type != FZL_TYPE_BOOL)
|
||||
an_push_warning(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand is not resulting with boolean type");
|
||||
}
|
||||
else if (cond_node->type == FZL_NODETYPE_LESS ||
|
||||
cond_node->type == FZL_NODETYPE_LESS_EQUAL ||
|
||||
cond_node->type == FZL_NODETYPE_GREATER ||
|
||||
cond_node->type == FZL_NODETYPE_GREATER_EQUAL)
|
||||
{
|
||||
if (left.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"left operand must have integer type");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (left.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"right operand must have integer type");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (left.type >= FZL_TYPE_INT8)
|
||||
{
|
||||
if (right.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand must have integer type");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
else if (left.type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
if (!left.classname || !right.classname)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"unhandled error :(");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(left.type, right.type, left.classname,
|
||||
right.classname))
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "'%s' and '%s' are incompatible",
|
||||
an_typetostr(left.type, left.classname),
|
||||
an_typetostr(right.type, right.classname));
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (left.type == FZL_TYPE_STRING)
|
||||
{
|
||||
if (right.type != FZL_TYPE_STRING)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand must be a string");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (!an_is_compatible(left.type, right.type, left.classname,
|
||||
right.classname))
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"equality not supported between %s and %s",
|
||||
an_typetostr(left.type, left.classname),
|
||||
an_typetostr(right.type, right.classname));
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze conditional statement
|
||||
fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!cond || cond->type != FZL_NODETYPE_CONDITIONAL)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "provided node is a condition");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t cond_res = analyze_common(analyzer, cond->cond.condition);
|
||||
fzl_an_res_t true_res = analyze_common(analyzer, cond->cond.true_block);
|
||||
|
||||
if (cond->cond.else_block)
|
||||
{
|
||||
fzl_an_res_t else_res = analyze_common(analyzer, cond->cond.else_block);
|
||||
if (else_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (cond_res.error || true_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (cond_res.type != FZL_TYPE_BOOL)
|
||||
an_push_warning(analyzer, cond->cond.condition->token,
|
||||
"resulting type is not bool");
|
||||
|
||||
if (cond->cond.condition->token->type == FZL_TOKTYPE_BOOL_LITERAL)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "condition is always %s",
|
||||
(char *)cond->cond.condition->token->value);
|
||||
an_push_warning(analyzer, cond->cond.condition->token, an_problem);
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze class construct
|
||||
fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!construct || construct->type != FZL_NODETYPE_CONSTRUCT)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a construct");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *classname = (char *)construct->construct.name->token->value;
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found = fzl_symt_get(analyzer->symbols[i], classname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "class '%s' not found", classname);
|
||||
an_push_error(analyzer, construct->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_OBJECT, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze variable/field/argument declaration
|
||||
fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!decl || decl->type != FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a declaration");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *name = (char *)decl->var.identifier->token->value;
|
||||
bool type_found = false;
|
||||
fzl_type_t type = an_toktotype(analyzer, decl->var.type, &type_found);
|
||||
|
||||
if (!type_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)decl->var.type->value);
|
||||
an_push_error(analyzer, decl->var.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *classname = NULL;
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
classname = decl->var.type->value;
|
||||
|
||||
fzl_symtable_res_t res = fzl_symt_put(
|
||||
analyzer->symbols[analyzer->symbols_top - 1], decl, name, type, 0);
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
snprintf(an_problem, sizeof(an_problem), "'%s' already has been declared",
|
||||
name);
|
||||
else
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot store '%s'", name);
|
||||
|
||||
an_push_error(analyzer, decl->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
fzl_sym_t class;
|
||||
bool class_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((class_found = fzl_symt_get(analyzer->symbols[i],
|
||||
decl->var.type->value, &class)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!class_found || class.node->type != FZL_NODETYPE_CLASS)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "class '%s' does not exists",
|
||||
(char *)decl->var.type->value);
|
||||
an_push_error(analyzer, decl->var.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_symt_setobjclass(analyzer->symbols[analyzer->symbols_top - 1], name,
|
||||
classname);
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze extern block definition
|
||||
fzl_an_res_t analyze_extern(fzl_analyzer_t *analyzer, fzl_node_t *extern_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!extern_node || extern_node->type != FZL_NODETYPE_EXTERN)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not an extern block");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (extern_node->ext.typedefs_count < 1 && extern_node->ext.fns_count < 1)
|
||||
an_push_warning(analyzer, extern_node->token, "extern is empty");
|
||||
|
||||
for (size_t i = 0; i < extern_node->ext.typedefs_count; i++)
|
||||
{
|
||||
fzl_node_t *node = extern_node->ext.typedefs[i];
|
||||
fzl_an_res_t res = analyze_common(analyzer, node);
|
||||
if (res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (node->type != FZL_NODETYPE_TYPEDEF &&
|
||||
node->type != FZL_NODETYPE_EXTERN_FN)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"unexpected statement '%s' inside extern block",
|
||||
(char *)node->token->value);
|
||||
an_push_error(analyzer, node->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < extern_node->ext.fns_count; i++)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, extern_node->ext.fns[i]);
|
||||
if (res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze external function definition
|
||||
fzl_an_res_t analyze_externfn(fzl_analyzer_t *analyzer, fzl_node_t *externfn)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!externfn || externfn->type != FZL_NODETYPE_EXTERN_FN)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not an external function");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY);
|
||||
|
||||
for (size_t i = 0; i < externfn->ext_fn.args_count; i++)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, externfn->ext_fn.args[i]);
|
||||
if (res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (externfn->ext_fn.type->type == FZL_TOKTYPE_NAME)
|
||||
{
|
||||
fzl_sym_t sym;
|
||||
bool found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((found = fzl_symt_get(analyzer->symbols[i],
|
||||
(char *)externfn->ext_fn.type->value, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"cannot find '%s' used as return type",
|
||||
(char *)externfn->ext_fn.type->value);
|
||||
an_push_error(analyzer, externfn->ext_fn.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
fzl_symtable_res_t wrres =
|
||||
fzl_symt_put(analyzer->symbols[0], externfn,
|
||||
(char *)externfn->ext_fn.name, FZL_TYPE_EXTERN_FN, 0);
|
||||
if (wrres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (wrres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"symbol '%s' already has been declared", externfn->ext_fn.name);
|
||||
an_push_error(analyzer, externfn->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot store symbol '%s'",
|
||||
externfn->ext_fn.name);
|
||||
}
|
||||
|
||||
an_push_error(analyzer, externfn->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_EXTERN_FN, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Analyze field access
|
||||
fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!field || field->type != FZL_NODETYPE_FIELD)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not field access");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t parent = analyze_common(analyzer, field->memb.parent);
|
||||
if (parent.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (parent.type != FZL_TYPE_OBJECT || parent.classname == NULL)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "access to non-object value (%s)",
|
||||
an_typetostr(parent.type, parent.classname));
|
||||
an_push_error(analyzer, field->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *field_name = field->memb.field->token->value;
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found =
|
||||
fzl_symt_get(analyzer->symbols[i], parent.classname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "class '%s' not found",
|
||||
parent.classname);
|
||||
an_push_error(analyzer, field->parent->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *field_node = NULL;
|
||||
for (size_t i = 0; i < sym.node->class.fields_count; i++)
|
||||
{
|
||||
if (strcmp(sym.node->class.fields[i]->var.identifier->token->value,
|
||||
field_name) == 0)
|
||||
{
|
||||
field_node = sym.node->class.fields[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (field_node == NULL)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "field '%s.%s' does not exists",
|
||||
parent.classname, field_name);
|
||||
an_push_error(analyzer, field->memb.field->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
bool type_found = false;
|
||||
fzl_type_t field_type =
|
||||
an_toktotype(analyzer, field_node->var.type, &type_found);
|
||||
|
||||
if (!type_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)field_node->var.type->value);
|
||||
an_push_error(analyzer, field_node->var.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *classname = NULL;
|
||||
if (field_type == FZL_TYPE_OBJECT)
|
||||
classname = field_node->var.type->value;
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = field_type, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!fn_node || fn_node->type != FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a function");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *fnctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_FUNCTION);
|
||||
fnctx->fn = fn_node;
|
||||
|
||||
for (size_t i = 0; i < fn_node->fn.args_count; i++)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, fn_node->fn.args[i]);
|
||||
if (res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *ctx = analyzer->ctx;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
if (ctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
bool type_found = false;
|
||||
fzl_type_t rettype = an_toktotype(analyzer, fn_node->fn.type, &type_found);
|
||||
char *classname = NULL;
|
||||
|
||||
if (!type_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)fn_node->fn.type->value);
|
||||
an_push_error(analyzer, fn_node->fn.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (rettype == FZL_TYPE_OBJECT)
|
||||
classname = (char *)fn_node->fn.type->value;
|
||||
|
||||
if (ctx != NULL && ctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||
{
|
||||
for (size_t i = 0; i < fn_node->fn.args_count; i++)
|
||||
{
|
||||
fzl_node_t *decl = fn_node->fn.args[i];
|
||||
if (decl->type != FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
an_push_error(analyzer, decl->token, "argument must be a declaration");
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *argname = (char *)decl->var.identifier->token->value;
|
||||
if (strcmp(argname, "this") == 0 && i > 0)
|
||||
{
|
||||
an_push_error(analyzer, decl->token,
|
||||
"manual declaration of 'this' is not allowed in methods");
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
size_t classname_len = strlen(ctx->classname);
|
||||
size_t methodname_len = strlen((char *)fn_node->fn.name->value);
|
||||
char *methodname = malloc(classname_len + methodname_len + 2);
|
||||
|
||||
strcpy(methodname, ctx->classname);
|
||||
methodname[classname_len] = '_';
|
||||
strcpy(methodname + classname_len + 1, fn_node->fn.name->value);
|
||||
methodname[classname_len + methodname_len + 1] = 0;
|
||||
|
||||
analyzer->alloc_names[analyzer->alloc_names_top++] = methodname;
|
||||
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(analyzer->symbols[ctx->symbols_level - 1], fn_node,
|
||||
methodname, rettype, 0);
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"method '%s.%s' already has been declared", classname,
|
||||
(char *)fn_node->fn.name->value);
|
||||
an_push_error(analyzer, fn_node->token, an_problem);
|
||||
}
|
||||
else
|
||||
an_push_error(analyzer, fn_node->token, "failed to store method");
|
||||
|
||||
free(analyzer->alloc_names[--analyzer->alloc_names_top]);
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
fzl_symt_setobjclass(analyzer->symbols[ctx->symbols_level - 1], methodname,
|
||||
classname);
|
||||
}
|
||||
else
|
||||
{
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], fn_node,
|
||||
(char *)fn_node->fn.name->value, rettype, 0);
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"function '%s' already has been declared",
|
||||
(char *)fn_node->fn.name->value);
|
||||
an_push_error(analyzer, fn_node->token, an_problem);
|
||||
}
|
||||
else
|
||||
an_push_error(analyzer, fn_node->token, "failed to store function");
|
||||
|
||||
free(analyzer->alloc_names[--analyzer->alloc_names_top]);
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
fzl_an_res_t body_res = analyze_common(analyzer, fn_node->fn.body);
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = body_res.error, .type = rettype, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze identifier
|
||||
fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!id || id->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not an identifier");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *symname = id->token->value;
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found = fzl_symt_get(analyzer->symbols[i], symname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot find symbol '%s'",
|
||||
symname);
|
||||
an_push_error(analyzer, id->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = sym.type, .classname = sym.classname};
|
||||
}
|
||||
@@ -0,0 +1,465 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Init block analysis result
|
||||
typedef struct
|
||||
{
|
||||
bool has_var;
|
||||
char *name;
|
||||
fzl_type_t type;
|
||||
char *classname;
|
||||
} init_analysis;
|
||||
|
||||
// Check init block
|
||||
init_analysis check_init(fzl_analyzer_t *analyzer, fzl_node_t *init)
|
||||
{
|
||||
if (!analyzer || !init)
|
||||
return (init_analysis){.has_var = false};
|
||||
|
||||
if (init->type == FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
char *name = init->token->value;
|
||||
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found = fzl_symt_get(analyzer->symbols[i], name, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
return (init_analysis){.has_var = false};
|
||||
|
||||
char *classname = NULL;
|
||||
if (sym.type == FZL_TYPE_OBJECT)
|
||||
classname = sym.classname;
|
||||
|
||||
return (init_analysis){.has_var = true,
|
||||
.name = name,
|
||||
.type = sym.type,
|
||||
.classname = classname};
|
||||
}
|
||||
else if (init->type == FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
char *name = init->var.identifier->token->value;
|
||||
|
||||
bool type_found = false;
|
||||
fzl_type_t type = an_toktotype(analyzer, init->var.type, &type_found);
|
||||
char *classname = NULL;
|
||||
|
||||
if (!type_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)init->var.type->value);
|
||||
}
|
||||
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
classname = init->var.type->value;
|
||||
|
||||
return (init_analysis){
|
||||
.has_var = true, .name = name, .type = type, .classname = classname};
|
||||
}
|
||||
else if (init->type == FZL_NODETYPE_ASSIGN)
|
||||
{
|
||||
return (init_analysis)check_init(analyzer, init->binary_op.left);
|
||||
}
|
||||
|
||||
return (init_analysis){.has_var = false};
|
||||
}
|
||||
|
||||
// Condition block analysis result type
|
||||
typedef enum
|
||||
{
|
||||
COND_TYPE_CONST_TRUE, // `true` is used as condition
|
||||
COND_TYPE_CONST_FALSE, // `false` is used as condition
|
||||
COND_TYPE_VAR_LESS, // until variable value is less
|
||||
COND_TYPE_VAR_GREATER, // until variable value is greater
|
||||
COND_TYPE_UNKNOWN, // cannot determine in compilation time
|
||||
} cond_type;
|
||||
|
||||
// Condition block analysis result
|
||||
typedef struct
|
||||
{
|
||||
cond_type type;
|
||||
bool result_bool;
|
||||
char *varname;
|
||||
fzl_type_t vartype;
|
||||
char *classname;
|
||||
} cond_analysis;
|
||||
|
||||
// Check condition block
|
||||
cond_analysis check_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond,
|
||||
bool report)
|
||||
{
|
||||
if (!cond)
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||
|
||||
switch (cond->type)
|
||||
{
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
if (cond->token->type == FZL_TOKTYPE_STR_LITERAL)
|
||||
{
|
||||
if (report)
|
||||
an_push_warning(analyzer, cond->token,
|
||||
"string literal is used as condition");
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||
}
|
||||
else if (cond->token->type == FZL_TOKTYPE_NULL_LITERAL)
|
||||
{
|
||||
if (report)
|
||||
an_push_warning(analyzer, cond->token,
|
||||
"null literal is used as condition");
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||
}
|
||||
else if (cond->token->type == FZL_TOKTYPE_BOOL_LITERAL)
|
||||
{
|
||||
if (strcmp(cond->token->value, "true") == 0)
|
||||
return (cond_analysis){.type = COND_TYPE_CONST_TRUE,
|
||||
.result_bool = true};
|
||||
else
|
||||
return (cond_analysis){.type = COND_TYPE_CONST_FALSE,
|
||||
.result_bool = true};
|
||||
}
|
||||
else if (cond->token->type == FZL_TOKTYPE_NUM_LITERAL)
|
||||
{
|
||||
if (strcmp(cond->token->value, "0") == 0)
|
||||
return (cond_analysis){.type = COND_TYPE_CONST_FALSE,
|
||||
.result_bool = false};
|
||||
else
|
||||
return (cond_analysis){.type = COND_TYPE_CONST_TRUE,
|
||||
.result_bool = false};
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LESS:
|
||||
case FZL_NODETYPE_LESS_EQUAL:
|
||||
{
|
||||
fzl_node_t *comp_left = cond->binary_op.left;
|
||||
cond_analysis left_check = check_condition(analyzer, comp_left, false);
|
||||
if (left_check.varname != NULL)
|
||||
return (cond_analysis){.type = COND_TYPE_VAR_LESS,
|
||||
.result_bool = true,
|
||||
.varname = left_check.varname,
|
||||
.vartype = left_check.vartype,
|
||||
.classname = left_check.classname};
|
||||
|
||||
fzl_node_t *comp_right = cond->binary_op.right;
|
||||
cond_analysis right_check = check_condition(analyzer, comp_right, false);
|
||||
if (right_check.varname != NULL)
|
||||
return (cond_analysis){.type = COND_TYPE_VAR_GREATER,
|
||||
.result_bool = true,
|
||||
.varname = right_check.varname,
|
||||
.vartype = right_check.vartype,
|
||||
.classname = right_check.classname};
|
||||
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN};
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_GREATER:
|
||||
case FZL_NODETYPE_GREATER_EQUAL:
|
||||
{
|
||||
fzl_node_t *comp_left = cond->binary_op.left;
|
||||
cond_analysis left_check = check_condition(analyzer, comp_left, false);
|
||||
if (left_check.varname != NULL)
|
||||
return (cond_analysis){.type = COND_TYPE_VAR_GREATER,
|
||||
.result_bool = true,
|
||||
.varname = left_check.varname,
|
||||
.vartype = left_check.vartype,
|
||||
.classname = left_check.classname};
|
||||
|
||||
fzl_node_t *comp_right = cond->binary_op.right;
|
||||
cond_analysis right_check = check_condition(analyzer, comp_right, false);
|
||||
if (right_check.varname != NULL)
|
||||
return (cond_analysis){.type = COND_TYPE_VAR_LESS,
|
||||
.result_bool = true,
|
||||
.varname = right_check.varname,
|
||||
.vartype = right_check.vartype,
|
||||
.classname = right_check.classname};
|
||||
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN};
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
{
|
||||
char *name = cond->token->value;
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found = fzl_symt_get(analyzer->symbols[i], name, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .varname = NULL};
|
||||
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN,
|
||||
.varname = name,
|
||||
.vartype = sym.type,
|
||||
.classname = sym.classname};
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
fzl_an_res_t custom_res = analyze_common(analyzer, cond);
|
||||
if (custom_res.type == FZL_TYPE_BOOL)
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = true};
|
||||
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||
}
|
||||
|
||||
// Step block change direction
|
||||
typedef enum
|
||||
{
|
||||
STEP_DIR_TYPE_INCREASE,
|
||||
STEP_DIR_TYPE_DECREASE,
|
||||
STEP_DIR_TYPE_STALL,
|
||||
STEP_DIR_UNKNOWN,
|
||||
} step_dir_type;
|
||||
|
||||
// Step block analysis
|
||||
typedef struct
|
||||
{
|
||||
bool changing;
|
||||
char *changing_var;
|
||||
step_dir_type dir;
|
||||
} step_analysis;
|
||||
|
||||
// Check step block
|
||||
step_analysis check_step(fzl_node_t *step)
|
||||
{
|
||||
if (!step)
|
||||
return (step_analysis){.changing = false};
|
||||
|
||||
if (step->type == FZL_NODETYPE_ASSIGN)
|
||||
{
|
||||
if (step->binary_op.left->type != FZL_NODETYPE_IDENTIFIER)
|
||||
return (step_analysis){
|
||||
.changing = true, .changing_var = "-", .dir = STEP_DIR_UNKNOWN};
|
||||
|
||||
int dir = 0;
|
||||
fzl_node_t *right = step->binary_op.right;
|
||||
fzl_node_t *num = NULL;
|
||||
char *varname = NULL;
|
||||
if (right->binary_op.left->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
num = right->binary_op.left;
|
||||
if (right->binary_op.right->type == FZL_NODETYPE_IDENTIFIER)
|
||||
varname = right->binary_op.right->token->value;
|
||||
}
|
||||
else
|
||||
{
|
||||
num = right->binary_op.right;
|
||||
if (right->binary_op.left->type == FZL_NODETYPE_IDENTIFIER)
|
||||
varname = right->binary_op.left->token->value;
|
||||
}
|
||||
|
||||
if (right->type == FZL_NODETYPE_ADD || right->type == FZL_NODETYPE_MUL ||
|
||||
right->type == FZL_NODETYPE_POW)
|
||||
dir = 1;
|
||||
else if (right->type == FZL_NODETYPE_SUB ||
|
||||
right->type == FZL_NODETYPE_DIV || right->type == FZL_NODETYPE_MOD)
|
||||
dir = -1;
|
||||
|
||||
if (num && num->type == FZL_NODETYPE_UNARY_MINUS)
|
||||
dir *= -1;
|
||||
|
||||
step_dir_type dirtype = varname == NULL ? STEP_DIR_UNKNOWN
|
||||
: dir > 0 ? STEP_DIR_TYPE_INCREASE
|
||||
: dir < 0 ? STEP_DIR_TYPE_DECREASE
|
||||
: STEP_DIR_TYPE_STALL;
|
||||
|
||||
return (step_analysis){.changing = varname != NULL && dir != 0,
|
||||
.changing_var = varname,
|
||||
.dir = dirtype};
|
||||
}
|
||||
|
||||
return (step_analysis){.changing = false, .dir = STEP_DIR_UNKNOWN};
|
||||
}
|
||||
|
||||
// Body analysis result
|
||||
typedef struct
|
||||
{
|
||||
bool has_exit;
|
||||
} body_analysis;
|
||||
|
||||
body_analysis check_body(fzl_node_t *body, bool nested_loop)
|
||||
{
|
||||
body_analysis result = {.has_exit = false};
|
||||
switch (body->type)
|
||||
{
|
||||
case FZL_NODETYPE_RETURN:
|
||||
result.has_exit = true;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BREAK:
|
||||
if (!nested_loop)
|
||||
result.has_exit = true;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LOOP:
|
||||
if (body->loop.body)
|
||||
{
|
||||
body_analysis res = check_body(body->loop.body, true);
|
||||
if (res.has_exit)
|
||||
result.has_exit = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONDITIONAL:
|
||||
if (check_body(body->cond.true_block, nested_loop).has_exit)
|
||||
{
|
||||
result.has_exit = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
for (size_t i = 0; i < body->list.entry_count; i++)
|
||||
{
|
||||
body_analysis res = check_body(body->list.entries[i], nested_loop);
|
||||
if (res.has_exit)
|
||||
{
|
||||
result.has_exit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Analyze loop
|
||||
fzl_an_res_t analyze_loop(fzl_analyzer_t *analyzer, fzl_node_t *loop_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!loop_node || loop_node->type != FZL_NODETYPE_LOOP)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof LOOP");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_LOOP);
|
||||
|
||||
init_analysis init_data = {
|
||||
.has_var = false, .type = FZL_TYPE_NULL, .name = NULL, .classname = NULL};
|
||||
if (loop_node->loop.init)
|
||||
{
|
||||
fzl_an_res_t init_res = analyze_common(analyzer, loop_node->loop.init);
|
||||
if (init_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
init_data = check_init(analyzer, loop_node->loop.init);
|
||||
}
|
||||
|
||||
cond_analysis cond_data = {.type = COND_TYPE_CONST_TRUE,
|
||||
.varname = NULL,
|
||||
.vartype = FZL_TYPE_NULL,
|
||||
.classname = NULL};
|
||||
if (loop_node->loop.cond)
|
||||
{
|
||||
fzl_an_res_t cond_res = analyze_common(analyzer, loop_node->loop.cond);
|
||||
if (cond_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
cond_data = check_condition(analyzer, loop_node->loop.cond, true);
|
||||
|
||||
if (!cond_data.result_bool && cond_data.type != COND_TYPE_UNKNOWN)
|
||||
an_push_warning(analyzer, loop_node->loop.cond->token,
|
||||
"resulting type is not boolean");
|
||||
|
||||
if (cond_data.type == COND_TYPE_CONST_FALSE)
|
||||
an_push_warning(analyzer, loop_node->loop.cond->token,
|
||||
"loop will never execute");
|
||||
|
||||
if (init_data.name && cond_data.varname &&
|
||||
strcmp(init_data.name, cond_data.varname) != 0)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"'%s' is used in condition but '%s' is declared. Maybe there is "
|
||||
"mistake?",
|
||||
cond_data.varname, init_data.name);
|
||||
an_push_warning(analyzer, loop_node->loop.cond->token, an_problem);
|
||||
}
|
||||
}
|
||||
|
||||
step_analysis step_data = {
|
||||
.changing = false, .changing_var = NULL, .dir = STEP_DIR_UNKNOWN};
|
||||
if (loop_node->loop.step)
|
||||
{
|
||||
fzl_an_res_t step_res = analyze_common(analyzer, loop_node->loop.step);
|
||||
if (step_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
step_data = check_step(loop_node->loop.step);
|
||||
|
||||
if (step_data.dir != STEP_DIR_UNKNOWN)
|
||||
{
|
||||
if (step_data.changing_var && cond_data.varname &&
|
||||
strcmp(step_data.changing_var, cond_data.varname) != 0)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"changing '%s' but condition checking '%s'",
|
||||
step_data.changing_var, cond_data.varname);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
|
||||
}
|
||||
|
||||
if (cond_data.type == COND_TYPE_VAR_GREATER &&
|
||||
step_data.dir == STEP_DIR_TYPE_INCREASE)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"'%s' is increasing while condition is 'greater than'",
|
||||
step_data.changing_var);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
|
||||
}
|
||||
else if (cond_data.type == COND_TYPE_VAR_LESS &&
|
||||
step_data.dir == STEP_DIR_TYPE_DECREASE)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"'%s' is decreasing while condition is 'less than'",
|
||||
step_data.changing_var);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
|
||||
}
|
||||
else if ((cond_data.type == COND_TYPE_VAR_LESS ||
|
||||
cond_data.type == COND_TYPE_VAR_GREATER) &&
|
||||
step_data.dir == STEP_DIR_TYPE_STALL)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "'%s' is stalled",
|
||||
cond_data.varname);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
body_analysis body_data = {.has_exit = false};
|
||||
if (cond_data.type != COND_TYPE_CONST_FALSE)
|
||||
{
|
||||
fzl_an_res_t body_res = analyze_common(analyzer, loop_node->loop.body);
|
||||
if (body_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
body_data = check_body(loop_node->loop.body, false);
|
||||
if (cond_data.type == COND_TYPE_CONST_TRUE && !body_data.has_exit)
|
||||
an_push_warning(analyzer, loop_node->token, "possibly infinite loop");
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze reference
|
||||
fzl_an_res_t analyze_ref(fzl_analyzer_t *analyzer, fzl_node_t *ref_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!ref_node || ref_node->type != FZL_NODETYPE_REFERENCE)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a reference");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *refto = (char *)ref_node->token->value;
|
||||
fzl_sym_t sym;
|
||||
bool found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((found = fzl_symt_get(analyzer->symbols[i], refto, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot find name '%s'", refto);
|
||||
an_push_error(analyzer, ref_node->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_HANDLE, .classname = refto};
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze return statement
|
||||
fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!ret_node || ret_node->type != FZL_NODETYPE_RETURN)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a return statement");
|
||||
return (fzl_an_res_t){.error = false};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *fnctx = analyzer->ctx;
|
||||
while (fnctx != NULL)
|
||||
{
|
||||
if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
break;
|
||||
|
||||
fnctx = fnctx->parent;
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *classctx = analyzer->ctx;
|
||||
while (classctx != NULL)
|
||||
{
|
||||
if (classctx && classctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||
break;
|
||||
|
||||
classctx = classctx->parent;
|
||||
}
|
||||
|
||||
bool is_in_method =
|
||||
classctx != NULL && classctx->type == FZL_AN_CTX_TYPE_CLASS;
|
||||
|
||||
if (!fnctx || fnctx->type != FZL_AN_CTX_TYPE_FUNCTION)
|
||||
{
|
||||
an_push_error(analyzer, ret_node->token,
|
||||
"cannot use 'return' outside the function");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
bool type_found = false;
|
||||
fzl_type_t rettype = an_toktotype(analyzer, fnctx->fn->fn.type, &type_found);
|
||||
char *classname = NULL;
|
||||
|
||||
if (!type_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)fnctx->fn->fn.type->value);
|
||||
an_push_error(analyzer, fnctx->fn->fn.type, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (rettype == FZL_TYPE_OBJECT)
|
||||
classname = (char *)fnctx->fn->fn.type->value;
|
||||
|
||||
char *rettype_str = an_typetostr(rettype, classname);
|
||||
if (rettype != FZL_TYPE_NULL)
|
||||
{
|
||||
if (ret_node->ret.value == NULL)
|
||||
{
|
||||
if (is_in_method)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"cannot return null as method '%s.%s' have return type '%s'",
|
||||
classctx->classname, (char *)fnctx->fn->fn.name->value,
|
||||
rettype_str);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"cannot return null as function '%s' have return type '%s'",
|
||||
(char *)fnctx->fn->fn.name->value, rettype_str);
|
||||
}
|
||||
an_push_error(analyzer, ret_node->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t ret_res = analyze_common(analyzer, ret_node->ret.value);
|
||||
if (ret_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname))
|
||||
{
|
||||
if (is_in_method)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"method '%s.%s' is expecting return type '%s', got '%s'",
|
||||
classctx->classname, (char *)fnctx->fn->fn.name->value,
|
||||
rettype_str, an_typetostr(ret_res.type, ret_res.classname));
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"function '%s' is expecting return type '%s', got '%s'",
|
||||
(char *)fnctx->fn->fn.name->value, rettype_str,
|
||||
an_typetostr(ret_res.type, ret_res.classname));
|
||||
}
|
||||
an_push_error(analyzer, ret_node->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = rettype, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Analyze format string
|
||||
fzl_an_res_t analyze_strfmt(fzl_analyzer_t *analyzer, fzl_node_t *strfmt_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!strfmt_node || strfmt_node->type != FZL_NODETYPE_FMT_STRING)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not format string");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < strfmt_node->list.entry_count; i++)
|
||||
{
|
||||
fzl_node_t *node = strfmt_node->list.entries[i];
|
||||
|
||||
if (node->type != FZL_NODETYPE_LITERAL &&
|
||||
node->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
an_push_error(
|
||||
analyzer, node->token,
|
||||
"format string can only contain string literals and identifiers");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t node_res = analyze_common(analyzer, node);
|
||||
if (node_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_STRING, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Analyze structure definition
|
||||
fzl_an_res_t analyze_struct(fzl_analyzer_t *analyzer, fzl_node_t *struct_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!struct_node || struct_node->type != FZL_NODETYPE_STRUCT)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a structure");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY);
|
||||
|
||||
if (struct_node->structure.entries_count < 1)
|
||||
{
|
||||
an_push_warning(analyzer, struct_node->token, "empty structure");
|
||||
return (fzl_an_res_t){.error = false};
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < struct_node->structure.entries_count; i++)
|
||||
{
|
||||
fzl_node_t *entry = struct_node->structure.entries[i];
|
||||
if (entry->type != FZL_NODETYPE_DECLARATION &&
|
||||
entry->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
an_push_error(
|
||||
analyzer, entry->token,
|
||||
"structure can contain only declarations and another structures");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t res = analyze_common(analyzer, entry);
|
||||
if (res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (res.type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
an_push_error(analyzer, entry->token,
|
||||
"cannot store object inside struct");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_STRUCT, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze type definition
|
||||
fzl_an_res_t analyze_typedef(fzl_analyzer_t *analyzer, fzl_node_t *td_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!td_node || td_node->type != FZL_NODETYPE_TYPEDEF)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a type definition");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t res = analyze_common(analyzer, td_node->typedefinition.type);
|
||||
if (res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_nodetype_t type = td_node->typedefinition.type->type;
|
||||
if (type != FZL_NODETYPE_ALIAS && type != FZL_NODETYPE_STRUCT &&
|
||||
type != FZL_NODETYPE_HANDLE && type != FZL_NODETYPE_REFERENCE)
|
||||
{
|
||||
an_push_error(
|
||||
analyzer, td_node->token,
|
||||
"only alias, structure, reference and handle are allowed in type "
|
||||
"definition value");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(analyzer->symbols[0], td_node,
|
||||
td_node->typedefinition.typename, res.type, 0);
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"symbol '%s' already has been declared",
|
||||
td_node->typedefinition.typename);
|
||||
else
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot store symbol '%s'",
|
||||
td_node->typedefinition.typename);
|
||||
|
||||
an_push_error(analyzer, td_node->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (res.type == FZL_TYPE_STRUCT || res.type == FZL_TYPE_HANDLE)
|
||||
fzl_symt_setobjclass(analyzer->symbols[0], td_node->typedefinition.typename,
|
||||
td_node->typedefinition.typename);
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = res.type, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
|
||||
// Analyze unary condition
|
||||
fzl_an_res_t analyze_unarycond(fzl_analyzer_t *analyzer, fzl_node_t *unary)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!unary || unary->type != FZL_NODETYPE_NOT)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not an unary condition operator");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
|
||||
if (operand_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (operand_res.type != FZL_TYPE_BOOL && operand_res.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, unary->unary_op.operand->token,
|
||||
"unary condition only support bool and integer types");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// Analyze unary arithmetic
|
||||
fzl_an_res_t analyze_unarymath(fzl_analyzer_t *analyzer, fzl_node_t *unary)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!unary || (unary->type != FZL_NODETYPE_UNARY_MINUS &&
|
||||
unary->type != FZL_NODETYPE_UNARY_PLUS))
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not an unary math operator");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
|
||||
if (operand_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (operand_res.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, unary->unary_op.operand->token,
|
||||
"unary operator only support integer types");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = operand_res.type, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Nest context
|
||||
fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type)
|
||||
{
|
||||
if (!analyzer)
|
||||
return NULL;
|
||||
|
||||
fzl_an_ctx_t *ctx = malloc(sizeof(fzl_an_ctx_t));
|
||||
if (!ctx)
|
||||
return NULL;
|
||||
|
||||
memset(ctx, 0, sizeof(fzl_an_ctx_t));
|
||||
|
||||
ctx->type = type;
|
||||
if (analyzer->ctx != NULL)
|
||||
ctx->parent = analyzer->ctx;
|
||||
else
|
||||
ctx->parent = NULL;
|
||||
|
||||
ctx->symbols = fzl_mksymt();
|
||||
ctx->symbols_level = analyzer->symbols_top;
|
||||
analyzer->ctx = ctx;
|
||||
analyzer->symbols[analyzer->symbols_top++] = ctx->symbols;
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
// Close context and return parent
|
||||
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer)
|
||||
{
|
||||
if (!analyzer || analyzer->ctx == NULL)
|
||||
return NULL;
|
||||
|
||||
fzl_an_ctx_t *current = analyzer->ctx;
|
||||
fzl_an_ctx_t *parent = current->parent;
|
||||
analyzer->ctx = parent;
|
||||
|
||||
free(current);
|
||||
fzl_freesymt(analyzer->symbols[--analyzer->symbols_top]);
|
||||
|
||||
return parent;
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Check compatibility between A and B types (and classnames)
|
||||
bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc)
|
||||
{
|
||||
if (a == b)
|
||||
return true;
|
||||
|
||||
if (a == FZL_TYPE_OBJECT && b == FZL_TYPE_OBJECT)
|
||||
{
|
||||
if (strcmp(ac, "null") == 0 || strcmp(bc, "null") == 0)
|
||||
return true;
|
||||
|
||||
return strcmp(ac, bc) == 0;
|
||||
}
|
||||
|
||||
if (a >= FZL_TYPE_INT8 && b >= FZL_TYPE_INT8)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convert type to string
|
||||
char *an_typetostr(fzl_type_t type, char *classname)
|
||||
{
|
||||
if (classname)
|
||||
return classname;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case FZL_TYPE_NULL:
|
||||
return "null";
|
||||
|
||||
case FZL_TYPE_BOOL:
|
||||
return "boolean";
|
||||
|
||||
case FZL_TYPE_STRING:
|
||||
return "string";
|
||||
|
||||
case FZL_TYPE_CHAR:
|
||||
return "char";
|
||||
|
||||
case FZL_TYPE_INT8:
|
||||
return "i8";
|
||||
|
||||
case FZL_TYPE_INT16:
|
||||
return "i16";
|
||||
|
||||
case FZL_TYPE_INT32:
|
||||
return "i32";
|
||||
|
||||
case FZL_TYPE_INT64:
|
||||
return "i64";
|
||||
|
||||
case FZL_TYPE_UINT8:
|
||||
return "u8";
|
||||
|
||||
case FZL_TYPE_UINT16:
|
||||
return "u16";
|
||||
|
||||
case FZL_TYPE_UINT32:
|
||||
return "u32";
|
||||
|
||||
case FZL_TYPE_UINT64:
|
||||
return "u64";
|
||||
|
||||
case FZL_TYPE_INT:
|
||||
return "int";
|
||||
|
||||
case FZL_TYPE_UINT:
|
||||
return "uint";
|
||||
|
||||
default:
|
||||
return "<unknown>";
|
||||
}
|
||||
}
|
||||
|
||||
// Convert type token to type
|
||||
fzl_type_t an_toktotype(fzl_analyzer_t *analyzer, fzl_token_t *tok,
|
||||
bool *sym_found)
|
||||
{
|
||||
if (!tok)
|
||||
return FZL_TYPE_NULL;
|
||||
|
||||
if (tok->type == FZL_TOKTYPE_NAME)
|
||||
{
|
||||
fzl_sym_t sym;
|
||||
bool found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((found = fzl_symt_get(analyzer->symbols[i], tok->value, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (sym_found)
|
||||
*sym_found = found;
|
||||
|
||||
if (found && sym.node->type == FZL_NODETYPE_CLASS)
|
||||
return FZL_TYPE_OBJECT;
|
||||
|
||||
if (found && sym.node->type != FZL_NODETYPE_CLASS &&
|
||||
sym.node->type != FZL_NODETYPE_TYPEDEF)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot use '%s' as type",
|
||||
sym.name);
|
||||
an_push_error(analyzer, sym.node->token, an_problem);
|
||||
return FZL_TYPE_NULL;
|
||||
}
|
||||
|
||||
return (found || (tok->value && strcmp(tok->value, "null") == 0))
|
||||
? sym.type
|
||||
: FZL_TYPE_NULL;
|
||||
}
|
||||
|
||||
if (sym_found)
|
||||
*sym_found = true;
|
||||
|
||||
if (strcmp(tok->value, "null") == 0)
|
||||
return FZL_TYPE_NULL;
|
||||
|
||||
if (strcmp((char *)tok->value, "i8") == 0)
|
||||
return FZL_TYPE_INT8;
|
||||
if (strcmp((char *)tok->value, "i16") == 0)
|
||||
return FZL_TYPE_INT16;
|
||||
if (strcmp((char *)tok->value, "i32") == 0)
|
||||
return FZL_TYPE_INT32;
|
||||
if (strcmp((char *)tok->value, "i64") == 0)
|
||||
return FZL_TYPE_INT64;
|
||||
|
||||
if (strcmp((char *)tok->value, "u8") == 0)
|
||||
return FZL_TYPE_UINT8;
|
||||
if (strcmp((char *)tok->value, "u16") == 0)
|
||||
return FZL_TYPE_UINT16;
|
||||
if (strcmp((char *)tok->value, "u32") == 0)
|
||||
return FZL_TYPE_UINT32;
|
||||
if (strcmp((char *)tok->value, "u64") == 0)
|
||||
return FZL_TYPE_UINT64;
|
||||
|
||||
if (strcmp((char *)tok->value, "int") == 0)
|
||||
return FZL_TYPE_INT;
|
||||
if (strcmp((char *)tok->value, "uint") == 0)
|
||||
return FZL_TYPE_UINT;
|
||||
|
||||
if (strcmp((char *)tok->value, "bool") == 0)
|
||||
return FZL_TYPE_BOOL;
|
||||
if (strcmp((char *)tok->value, "str") == 0)
|
||||
return FZL_TYPE_STRING;
|
||||
if (strcmp((char *)tok->value, "char") == 0)
|
||||
return FZL_TYPE_CHAR;
|
||||
|
||||
return FZL_TYPE_NULL;
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char aerrors[256][256];
|
||||
size_t aerrors_top;
|
||||
|
||||
char awarnings[256][256];
|
||||
size_t awarnings_top;
|
||||
|
||||
char an_problem[256];
|
||||
|
||||
fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node)
|
||||
{
|
||||
if (!node)
|
||||
return NULL;
|
||||
|
||||
fzl_analyzer_t *analyzer = malloc(sizeof(fzl_analyzer_t));
|
||||
if (!analyzer)
|
||||
return NULL;
|
||||
|
||||
analyzer->root = node;
|
||||
analyzer->warnings_top = 0;
|
||||
analyzer->errors_top = 0;
|
||||
analyzer->symbols_top = 0;
|
||||
analyzer->alloc_names_top = 0;
|
||||
|
||||
memset(analyzer->errors, 0, sizeof(analyzer->errors));
|
||||
memset(analyzer->warnings, 0, sizeof(analyzer->warnings));
|
||||
memset(analyzer->symbols, 0, sizeof(analyzer->symbols));
|
||||
memset(analyzer->alloc_names, 0, sizeof(analyzer->alloc_names));
|
||||
|
||||
memset(aerrors, 0, sizeof(aerrors));
|
||||
memset(awarnings, 0, sizeof(awarnings));
|
||||
|
||||
return analyzer;
|
||||
}
|
||||
|
||||
bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error)
|
||||
{
|
||||
if (analyzer->errors_top >= 256 || !analyzer || !error)
|
||||
return false;
|
||||
|
||||
size_t ei = aerrors_top++;
|
||||
if (tok != NULL)
|
||||
snprintf(aerrors[ei], sizeof(aerrors[ei]), "'%s':%zu:%zu => %s",
|
||||
tok->filename, tok->ln, tok->cn, error);
|
||||
else
|
||||
snprintf(aerrors[ei], sizeof(aerrors[ei]), "<unknown location> => %s",
|
||||
error);
|
||||
|
||||
analyzer->errors[analyzer->errors_top++] = aerrors[ei];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning)
|
||||
{
|
||||
if (analyzer->warnings_top >= 256 || !analyzer || !warning)
|
||||
return false;
|
||||
|
||||
size_t wi = awarnings_top++;
|
||||
if (tok != NULL)
|
||||
snprintf(awarnings[wi], sizeof(awarnings[wi]), "'%s':%zu:%zu => %s",
|
||||
tok->filename, tok->ln, tok->cn, warning);
|
||||
else
|
||||
snprintf(awarnings[wi], sizeof(awarnings[wi]), "<unknown location> => %s",
|
||||
warning);
|
||||
|
||||
analyzer->warnings[analyzer->warnings_top++] = awarnings[wi];
|
||||
return true;
|
||||
}
|
||||
|
||||
void fzl_analyze(fzl_analyzer_t *analyzer)
|
||||
{
|
||||
if (!analyzer)
|
||||
return;
|
||||
|
||||
if (an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY) == NULL)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "failed to make global context");
|
||||
return;
|
||||
}
|
||||
|
||||
fzl_node_t *current = analyzer->root;
|
||||
while (current != NULL)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, current);
|
||||
if (res.error)
|
||||
break;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
for (ssize_t i = analyzer->symbols_top; i > 0; i--)
|
||||
an_fin_ctx(analyzer);
|
||||
}
|
||||
|
||||
void fzl_freeanalyzer(fzl_analyzer_t *analyzer)
|
||||
{
|
||||
if (!analyzer)
|
||||
return;
|
||||
|
||||
if (analyzer->alloc_names_top > 0)
|
||||
for (ssize_t i = analyzer->alloc_names_top - 1; i >= 0; i--)
|
||||
free(analyzer->alloc_names[i]);
|
||||
|
||||
memset(analyzer->errors, 0, sizeof(analyzer->errors));
|
||||
memset(analyzer->warnings, 0, sizeof(analyzer->warnings));
|
||||
memset(analyzer->symbols, 0, sizeof(analyzer->symbols));
|
||||
memset(analyzer->alloc_names, 0, sizeof(analyzer->alloc_names));
|
||||
free(analyzer);
|
||||
}
|
||||
@@ -27,10 +27,93 @@ void print_node(fzl_node_t *node, int level)
|
||||
if (!node)
|
||||
return;
|
||||
|
||||
print_level(levelbuf, level);
|
||||
strcpy(printbuf, levelbuf);
|
||||
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_EXTERN:
|
||||
printf("%s Extern bindings to library '%s':\n", levelbuf,
|
||||
node->ext.libname);
|
||||
printf("-%s Types:\n", levelbuf);
|
||||
for (size_t i = 0; i < node->ext.typedefs_count; i++)
|
||||
print_node(node->ext.typedefs[i], level + 2);
|
||||
printf("-%s Functions:\n", levelbuf);
|
||||
for (size_t i = 0; i < node->ext.fns_count; i++)
|
||||
print_node(node->ext.fns[i], level + 2);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_EXTERN_FN:
|
||||
printf("%s External function '%s' (original name %s), rettype '%s':\n",
|
||||
levelbuf, node->ext_fn.name, node->ext_fn.original_name,
|
||||
(char *)node->ext_fn.type->value);
|
||||
printf("-%s Arguments:\n", levelbuf);
|
||||
for (size_t i = 0; i < node->ext_fn.args_count; i++)
|
||||
print_node(node->ext_fn.args[i], level + 2);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_REFERENCE:
|
||||
printf("%s Reference (pointer type) to '%s'\n", levelbuf,
|
||||
(char *)node->token->value);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_STRUCT:
|
||||
printf("%s Structure '%s' (original name %s):\n", levelbuf,
|
||||
(char *)node->token->value, node->structure.original_name);
|
||||
for (size_t i = 0; i < node->structure.entries_count; i++)
|
||||
print_node(node->structure.entries[i], level + 1);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_ALIAS:
|
||||
printf("%s Type alias to '%s'\n", levelbuf, (char *)node->token->value);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_HANDLE:
|
||||
printf("%s Opaque pointer (handle)\n", levelbuf);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_TYPEDEF:
|
||||
printf("%s Type definition '%s':\n", levelbuf,
|
||||
node->typedefinition.typename);
|
||||
print_node(node->typedefinition.type, level + 1);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_UNARY_MINUS:
|
||||
printf("%s Unary '-':\n", levelbuf);
|
||||
print_node(node->unary_op.operand, level + 1);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_UNARY_PLUS:
|
||||
printf("%s Unary '+':\n", levelbuf);
|
||||
print_node(node->unary_op.operand, level + 1);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BREAK:
|
||||
printf("%s Break\n", levelbuf);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FMT_STRING:
|
||||
printf("%s Format string:\n", levelbuf);
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
print_node(node->list.entries[i], level + 1);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LOOP:
|
||||
printf("%s Loop:\n", levelbuf);
|
||||
printf("-%s Init:\n", levelbuf);
|
||||
if (node->loop.init)
|
||||
print_node(node->loop.init, level + 2);
|
||||
printf("-%s Condition:\n", levelbuf);
|
||||
if (node->loop.cond)
|
||||
print_node(node->loop.cond, level + 2);
|
||||
printf("-%s Step:\n", levelbuf);
|
||||
if (node->loop.step)
|
||||
print_node(node->loop.step, level + 2);
|
||||
printf("-%s Body:\n", levelbuf);
|
||||
if (node->loop.body)
|
||||
print_node(node->loop.body, level + 2);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONSTRUCT:
|
||||
printf("%s Construct class '%s':\n", levelbuf,
|
||||
(char *)node->construct.name->token->value);
|
||||
@@ -156,9 +239,6 @@ void print_node(fzl_node_t *node, int level)
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
printf("%s Identifier ('%s')\n", levelbuf, (char *)node->token->value);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("%s Secure node\n", levelbuf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
#include "compiler/fzl_parser.h"
|
||||
#include "compiler/utils/fzl_ctable.h"
|
||||
#include "compiler/utils/fzl_symtable.h"
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
fzl_gen_t *fzl_mkgen(fzl_node_t *root)
|
||||
{
|
||||
if (!root)
|
||||
return NULL;
|
||||
|
||||
fzl_gen_t *gen = malloc(sizeof(fzl_gen_t));
|
||||
if (!gen)
|
||||
return NULL;
|
||||
|
||||
memset(gen, 0, sizeof(fzl_gen_t));
|
||||
|
||||
gen->root = root;
|
||||
gen->consts = fzl_mkconstpool();
|
||||
if (!gen->consts)
|
||||
{
|
||||
free(gen);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gen->ops_remainder = 2;
|
||||
gen->ops = malloc(gen->ops_remainder * sizeof(size_t));
|
||||
|
||||
gen->ctx = gen_ctx(gen, FZL_GEN_CTX_TYPE_UNECCESSARY, NULL);
|
||||
if (!gen->ctx)
|
||||
{
|
||||
fzl_consts_pool_free(gen->consts);
|
||||
free(gen);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return gen;
|
||||
}
|
||||
|
||||
bool fzl_gen_run(fzl_gen_t *gen)
|
||||
{
|
||||
if (!gen)
|
||||
return false;
|
||||
|
||||
fzl_node_t *current = gen->root;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (!gen_common(gen, current))
|
||||
return false;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void fzl_freegen(fzl_gen_t *gen)
|
||||
{
|
||||
if (!gen)
|
||||
return;
|
||||
|
||||
if (gen->consts)
|
||||
fzl_consts_pool_free(gen->consts);
|
||||
|
||||
if (gen->ops)
|
||||
free(gen->ops);
|
||||
|
||||
if (gen->syms_top > 0)
|
||||
for (ssize_t i = gen->syms_top - 1; i >= 0; i--)
|
||||
fzl_freesymt(gen->syms[i]);
|
||||
|
||||
if (gen->classes_count > 0)
|
||||
for (ssize_t i = gen->classes_count - 1; i >= 0; i--)
|
||||
fzl_freectable(gen->classes[i]);
|
||||
|
||||
free(gen);
|
||||
}
|
||||
+51
-1011
File diff suppressed because it is too large
Load Diff
+49
-251
@@ -1,6 +1,7 @@
|
||||
#include "compiler/fzl_token.h"
|
||||
#include "compiler/utils/fzl_linked.h"
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/reader.h>
|
||||
#include <compiler/utils/fzl_linked.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
@@ -8,7 +9,7 @@
|
||||
#include <string.h>
|
||||
|
||||
// Last error from reader
|
||||
static char last_err[256] = {0};
|
||||
char last_err[256];
|
||||
|
||||
fzl_reader_t *fzl_mkreader(fzl_linked_t *linked)
|
||||
{
|
||||
@@ -24,300 +25,99 @@ fzl_reader_t *fzl_mkreader(fzl_linked_t *linked)
|
||||
reader->size = linked->total_sz;
|
||||
reader->position = 0;
|
||||
|
||||
memset(last_err, 0, sizeof(last_err));
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
// --- 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
|
||||
fzl_token_t *parse_name(fzl_reader_t *reader)
|
||||
{
|
||||
size_t lpos = reader->position;
|
||||
size_t size = 0;
|
||||
|
||||
do
|
||||
{
|
||||
lpos++;
|
||||
size++;
|
||||
} while ((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 + 1);
|
||||
memcpy(value, reader->source + reader->position, size);
|
||||
value[size] = '\0';
|
||||
|
||||
size_t start = reader->position;
|
||||
move(reader, size);
|
||||
|
||||
fzl_token_t *tok;
|
||||
fzl_file_loc l;
|
||||
bool ok = fzl_linked_getpos(reader->linked, start, &l);
|
||||
if (!ok)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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
|
||||
fzl_token_t *parse_string_literal(fzl_reader_t *reader)
|
||||
{
|
||||
size_t lpos = reader->position;
|
||||
size_t size = 0;
|
||||
|
||||
if (reader->source[lpos] != '\'' && reader->source[lpos] != '"')
|
||||
return NULL;
|
||||
|
||||
char open_symbol = reader->source[lpos];
|
||||
char close_symbol = '\0';
|
||||
|
||||
do
|
||||
{
|
||||
lpos++;
|
||||
size++;
|
||||
|
||||
if (reader->source[lpos] == open_symbol)
|
||||
close_symbol = open_symbol;
|
||||
|
||||
} while (reader->source[lpos] != open_symbol && lpos < reader->size);
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (reader->position >= reader->size)
|
||||
return 0;
|
||||
|
||||
bool is_format_str = false;
|
||||
|
||||
do
|
||||
{
|
||||
size_t p = reader->position;
|
||||
fzl_token_t *token;
|
||||
char curr = reader->source[p];
|
||||
|
||||
if (curr == '\n' || curr == ' ' || curr == '\r')
|
||||
if (is_format_str)
|
||||
{
|
||||
token = rd_fmtstr(reader);
|
||||
if (token && token->type == FZL_TOKTYPE_STR_FORMAT_END)
|
||||
is_format_str = false;
|
||||
}
|
||||
else if (curr == '\n' || curr == ' ' || curr == '\r')
|
||||
{
|
||||
move(reader, 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (curr == '\'' || curr == '"')
|
||||
token = parse_string_literal(reader);
|
||||
else if (curr == '\'' || curr == '"')
|
||||
token = rd_string_literal(reader);
|
||||
else if (curr == '/' && reader->source[p + 1] == '/')
|
||||
{
|
||||
skip_comment(reader);
|
||||
rd_comment(reader);
|
||||
continue;
|
||||
}
|
||||
else if (curr == '=' && reader->source[p + 1] == '=')
|
||||
token = add_double(reader, FZL_TOKTYPE_EQEQ);
|
||||
token = rd_double(reader, FZL_TOKTYPE_EQEQ);
|
||||
else if (curr == '>' && reader->source[p + 1] == '=')
|
||||
token = add_double(reader, FZL_TOKTYPE_REQ);
|
||||
token = rd_double(reader, FZL_TOKTYPE_REQ);
|
||||
else if (curr == '<' && reader->source[p + 1] == '=')
|
||||
token = add_double(reader, FZL_TOKTYPE_LEQ);
|
||||
token = rd_double(reader, FZL_TOKTYPE_LEQ);
|
||||
else if (curr == '!' && reader->source[p + 1] == '=')
|
||||
token = add_double(reader, FZL_TOKTYPE_NEQ);
|
||||
token = rd_double(reader, FZL_TOKTYPE_NEQ);
|
||||
else if (curr == '|' && reader->source[p + 1] == '|')
|
||||
token = add_double(reader, FZL_TOKTYPE_DVBAR);
|
||||
token = rd_double(reader, FZL_TOKTYPE_DVBAR);
|
||||
else if (curr == '&' && reader->source[p + 1] == '&')
|
||||
token = add_double(reader, FZL_TOKTYPE_DAMPERSAND);
|
||||
token = rd_double(reader, FZL_TOKTYPE_DAMPERSAND);
|
||||
else if (curr == '>')
|
||||
token = add_single(reader, FZL_TOKTYPE_RANGLE);
|
||||
token = rd_single(reader, FZL_TOKTYPE_RANGLE);
|
||||
else if (curr == '<')
|
||||
token = add_single(reader, FZL_TOKTYPE_LANGLE);
|
||||
token = rd_single(reader, FZL_TOKTYPE_LANGLE);
|
||||
else if (curr == '!')
|
||||
token = add_single(reader, FZL_TOKTYPE_NOT);
|
||||
token = rd_single(reader, FZL_TOKTYPE_NOT);
|
||||
else if (curr == ';')
|
||||
token = add_single(reader, FZL_TOKTYPE_SEMICOLON);
|
||||
token = rd_single(reader, FZL_TOKTYPE_SEMICOLON);
|
||||
else if (curr == '{')
|
||||
token = add_single(reader, FZL_TOKTYPE_LBRACE);
|
||||
token = rd_single(reader, FZL_TOKTYPE_LBRACE);
|
||||
else if (curr == '}')
|
||||
token = add_single(reader, FZL_TOKTYPE_RBRACE);
|
||||
token = rd_single(reader, FZL_TOKTYPE_RBRACE);
|
||||
else if (curr == '(')
|
||||
token = add_single(reader, FZL_TOKTYPE_LPAREN);
|
||||
token = rd_single(reader, FZL_TOKTYPE_LPAREN);
|
||||
else if (curr == ')')
|
||||
token = add_single(reader, FZL_TOKTYPE_RPAREN);
|
||||
token = rd_single(reader, FZL_TOKTYPE_RPAREN);
|
||||
else if (curr == ',')
|
||||
token = add_single(reader, FZL_TOKTYPE_COMMA);
|
||||
token = rd_single(reader, FZL_TOKTYPE_COMMA);
|
||||
else if (curr == '.')
|
||||
token = add_single(reader, FZL_TOKTYPE_DOT);
|
||||
token = rd_single(reader, FZL_TOKTYPE_DOT);
|
||||
else if (curr == '*')
|
||||
token = add_single(reader, FZL_TOKTYPE_STAR);
|
||||
token = rd_single(reader, FZL_TOKTYPE_STAR);
|
||||
else if (curr == '/')
|
||||
token = add_single(reader, FZL_TOKTYPE_SLASH);
|
||||
token = rd_single(reader, FZL_TOKTYPE_SLASH);
|
||||
else if (curr == '+')
|
||||
token = add_single(reader, FZL_TOKTYPE_PLUS);
|
||||
token = rd_single(reader, FZL_TOKTYPE_PLUS);
|
||||
else if (curr == '-')
|
||||
token = add_single(reader, FZL_TOKTYPE_MINUS);
|
||||
token = rd_single(reader, FZL_TOKTYPE_MINUS);
|
||||
else if (curr == '^')
|
||||
token = add_single(reader, FZL_TOKTYPE_CARET);
|
||||
token = rd_single(reader, FZL_TOKTYPE_CARET);
|
||||
else if (curr == '%')
|
||||
token = add_single(reader, FZL_TOKTYPE_PERCENT);
|
||||
token = rd_single(reader, FZL_TOKTYPE_PERCENT);
|
||||
else if (curr == '=')
|
||||
token = add_single(reader, FZL_TOKTYPE_EQ);
|
||||
token = rd_single(reader, FZL_TOKTYPE_EQ);
|
||||
else if (curr == '$' && reader->source[p + 1] == '"')
|
||||
{
|
||||
move(reader, 2);
|
||||
is_format_str = true;
|
||||
token = fzl_mktoken(FZL_TOKTYPE_STR_FORMAT_START, 0, 0, NULL, 0, "");
|
||||
}
|
||||
else if ((curr >= 'A' && curr <= 'Z') || (curr >= 'a' && curr <= 'z') ||
|
||||
curr == '_')
|
||||
token = parse_name(reader);
|
||||
token = rd_name(reader);
|
||||
else if (curr >= '0' && curr <= '9')
|
||||
token = parse_number(reader);
|
||||
token = rd_number(reader);
|
||||
else
|
||||
{
|
||||
fzl_file_loc l;
|
||||
@@ -363,8 +163,6 @@ int fzl_read(fzl_reader_t *reader)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- Read zone end ---
|
||||
|
||||
char *fzl_reader_geterror() { return last_err; }
|
||||
|
||||
void free_node_and_next(fzl_reader_ll_t *node)
|
||||
|
||||
@@ -30,7 +30,11 @@ fzl_token_t *fzl_mktoken(const fzl_toktype_t type, size_t line, size_t col,
|
||||
// Free allocated token
|
||||
void fzl_freetoken(fzl_token_t *tok)
|
||||
{
|
||||
if (!tok)
|
||||
return;
|
||||
|
||||
if (tok->store_value)
|
||||
free(tok->value);
|
||||
|
||||
free(tok);
|
||||
}
|
||||
|
||||
+261
-23
@@ -1,3 +1,4 @@
|
||||
#include "common/fzl_extern.h"
|
||||
#include <common/fzl_builtins.h>
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
@@ -14,6 +15,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
extern bool is_infinite_loop(fzl_walker_t *walker, fzl_node_t *node, size_t ls,
|
||||
size_t le);
|
||||
|
||||
char *null_name = "<null>";
|
||||
char wlast_err[256];
|
||||
|
||||
@@ -58,6 +62,14 @@ fzl_walker_t *fzl_mkwalker(fzl_node_t *root)
|
||||
walker->classes_count = 0;
|
||||
memset(walker->classes, 0, sizeof(walker->classes));
|
||||
|
||||
walker->extern_libs_count = 0;
|
||||
walker->extern_types_count = 0;
|
||||
walker->extern_fns_count = 0;
|
||||
|
||||
memset(walker->extern_libs, 0, sizeof(walker->extern_libs));
|
||||
memset(walker->extern_types, 0, sizeof(walker->extern_types));
|
||||
memset(walker->extern_fns, 0, sizeof(walker->extern_fns));
|
||||
|
||||
return walker;
|
||||
}
|
||||
|
||||
@@ -200,6 +212,190 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
{
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_EXTERN:
|
||||
walker->extern_libs[walker->extern_libs_count++] =
|
||||
(fzl_extern_lib_t){.name = node->ext.libname};
|
||||
|
||||
for (size_t i = 0; i < node->ext.typedefs_count; i++)
|
||||
{
|
||||
fzl_walk_res_t res = walk(walker, node->ext.typedefs[i]);
|
||||
if (res.error)
|
||||
return res;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < node->ext.fns_count; i++)
|
||||
{
|
||||
fzl_walk_res_t res = walk(walker, node->ext.fns[i]);
|
||||
if (res.error)
|
||||
return res;
|
||||
}
|
||||
|
||||
current_res = (fzl_walk_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_EXTERN_FN: // TODO: write to meta
|
||||
case FZL_NODETYPE_REFERENCE: // TODO: write to meta
|
||||
case FZL_NODETYPE_STRUCT: // TODO: write to meta
|
||||
case FZL_NODETYPE_ALIAS: // TODO: write to meta
|
||||
case FZL_NODETYPE_HANDLE: // TODO: write to meta
|
||||
case FZL_NODETYPE_TYPEDEF: // TODO: write to meta
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FMT_STRING:
|
||||
{
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
{
|
||||
fzl_walk_res_t res = walk(walker, node->list.entries[i]);
|
||||
if (res.error)
|
||||
return res;
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_BUILD_STR);
|
||||
push_word(walker, node->list.entry_count);
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_BREAK:
|
||||
{
|
||||
fzl_walk_ctx_t *ctx = walker->ctx;
|
||||
while (ctx)
|
||||
{
|
||||
if (ctx->type == FZL_WALK_CTX_TYPE_LOOP)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_WALK_CTX_TYPE_LOOP)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"'%s': usage of 'break' outsude the loop at %ld:%ld",
|
||||
node->token->filename, node->token->ln, node->token->cn);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
for (size_t i = walker->sym_top - 1; i > ctx->initial_env_lvl + 1; i--)
|
||||
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||
|
||||
if (ctx->breaks_remainder <= ctx->breaks_count)
|
||||
{
|
||||
ctx->breaks_remainder *= 2;
|
||||
ctx->breaks =
|
||||
realloc(ctx->breaks, ctx->breaks_remainder * sizeof(size_t));
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_JMP);
|
||||
ctx->breaks[ctx->breaks_count++] = walker->op_count;
|
||||
push_word(walker, 0);
|
||||
|
||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
// NOTE: add new levels for infinite loop check:
|
||||
// 1. Step direction (f.e. `for (; i > 5; i = i - 1)`)
|
||||
case FZL_NODETYPE_LOOP:
|
||||
{
|
||||
fzl_walk_ctx_t *newctx = nest_ctx(walker, FZL_WALK_CTX_TYPE_LOOP);
|
||||
if (!newctx)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err), "failed to nest context");
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
newctx->initial_env_lvl = walker->sym_top - 1;
|
||||
newctx->breaks_count = 0;
|
||||
newctx->breaks_remainder = 2;
|
||||
newctx->breaks = malloc(newctx->breaks_remainder * sizeof(size_t));
|
||||
|
||||
push_op(walker, FZL_OPCODE_MK_ENVTABLE);
|
||||
walker->syms[walker->sym_top++] = fzl_mksymt();
|
||||
// size_t loop_begin_ip = walker->op_count;
|
||||
|
||||
if (node->loop.init)
|
||||
{
|
||||
fzl_walk_res_t res = walk(walker, node->loop.init);
|
||||
if (res.error)
|
||||
{
|
||||
free(newctx->breaks);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
size_t start_ip = walker->op_count;
|
||||
if (node->loop.cond)
|
||||
{
|
||||
fzl_walk_res_t res = walk(walker, node->loop.cond);
|
||||
if (res.error)
|
||||
{
|
||||
free(newctx->breaks);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
push_op(walker, FZL_OPCODE_PUSHNUM);
|
||||
push_word(walker, 1);
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_JZ);
|
||||
newctx->breaks[newctx->breaks_count++] = walker->op_count;
|
||||
push_word(walker, 0);
|
||||
|
||||
fzl_walk_res_t loop_body_res = walk(walker, node->loop.body);
|
||||
if (loop_body_res.error)
|
||||
{
|
||||
free(newctx->breaks);
|
||||
return loop_body_res;
|
||||
}
|
||||
|
||||
if (node->loop.step)
|
||||
{
|
||||
fzl_walk_res_t res = walk(walker, node->loop.step);
|
||||
if (res.error)
|
||||
{
|
||||
free(newctx->breaks);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_JMP);
|
||||
push_word(walker, start_ip);
|
||||
|
||||
// backpatch `break`s
|
||||
for (size_t i = 0; i < newctx->breaks_count; i++)
|
||||
walker->ops[newctx->breaks[i]] = walker->op_count;
|
||||
|
||||
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||
|
||||
// bool is_infinite =
|
||||
// is_infinite_loop(walker, node, loop_begin_ip, walker->op_count -
|
||||
// 2);
|
||||
// if (is_infinite)
|
||||
// {
|
||||
// free(newctx->breaks);
|
||||
// snprintf(wlast_err, sizeof(wlast_err),
|
||||
// "'%s': infinite loop detected at %ld:%ld",
|
||||
// node->token->filename, node->token->ln, node->token->cn);
|
||||
// return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
// }
|
||||
|
||||
for (size_t i = walker->sym_top - 1; i > newctx->initial_env_lvl; i--)
|
||||
fzl_freesymt(walker->syms[--walker->sym_top]);
|
||||
|
||||
free(newctx->breaks);
|
||||
if (!fin_ctx(walker))
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"failed to finish loop context");
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_CLASS:
|
||||
{
|
||||
fzl_walk_ctx_t *newctx = nest_ctx(walker, FZL_WALK_CTX_TYPE_CLASS);
|
||||
@@ -370,7 +566,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
|
||||
fzl_type_t fntype = tok_to_type(node->fn.type);
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(walker->syms[walker->sym_top - 1],
|
||||
fzl_symt_put(walker->syms[walker->sym_top - 1], node,
|
||||
node->fn.name->value, fntype, walker->op_count);
|
||||
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
@@ -461,7 +657,16 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
|
||||
case FZL_NODETYPE_RETURN:
|
||||
{
|
||||
if (!walker->ctx || walker->ctx->type != FZL_WALK_CTX_TYPE_FN)
|
||||
fzl_walk_ctx_t *ctx = walker->ctx;
|
||||
while (ctx)
|
||||
{
|
||||
if (ctx && ctx->type == FZL_WALK_CTX_TYPE_FN)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_WALK_CTX_TYPE_FN)
|
||||
{
|
||||
snprintf(
|
||||
wlast_err, sizeof(wlast_err),
|
||||
@@ -689,14 +894,10 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
{
|
||||
size_t n = add_const(
|
||||
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = node->token->value,
|
||||
.as.string.length = node->token->value_sz});
|
||||
|
||||
fzl_sym_t sym;
|
||||
bool exists = false;
|
||||
for (ssize_t i = walker->sym_top - 1; i >= 0; i--)
|
||||
ssize_t i = walker->sym_top - 1;
|
||||
for (; i >= 0; i--)
|
||||
if ((exists =
|
||||
fzl_symt_get(walker->syms[i], node->token->value, &sym)))
|
||||
break;
|
||||
@@ -710,9 +911,17 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, n);
|
||||
push_op(walker, FZL_OPCODE_LOOKUP_ENV);
|
||||
if (!sym.slot_set)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"internal error: local '%s' has no slot", sym.name);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
size_t depth = walker->sym_top - 1 - i;
|
||||
push_op(walker, FZL_OPCODE_LOAD_LOCAL);
|
||||
push_word(walker, depth);
|
||||
push_word(walker, sym.slot);
|
||||
|
||||
current_res = (fzl_walk_res_t){
|
||||
.type = sym.type, .error = false, .classname = sym.classname};
|
||||
@@ -731,6 +940,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
}
|
||||
fzl_freesymt(walker->syms[--walker->sym_top]);
|
||||
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CALL:
|
||||
@@ -1045,7 +1255,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
}
|
||||
|
||||
fzl_symtable_res_t res = fzl_symt_put(
|
||||
walker->syms[walker->sym_top - 1],
|
||||
walker->syms[walker->sym_top - 1], node,
|
||||
node->var.identifier->token->value,
|
||||
ct != NULL ? FZL_TYPE_OBJECT : tok_to_type(node->var.type), 0);
|
||||
|
||||
@@ -1056,9 +1266,28 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
fzl_token_t *tok = node->var.identifier->token;
|
||||
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"'%s': cannot redefine '%s' at %ld:%ld", tok->filename,
|
||||
(char *)tok->value, tok->ln, tok->cn);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"'%s': cannot store '%s' at %ld:%ld", tok->filename,
|
||||
(char *)tok->value, tok->ln, tok->cn);
|
||||
}
|
||||
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
if (fzl_symt_setslot(walker->syms[walker->sym_top - 1],
|
||||
node->var.identifier->token->value) < 0)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"'%s': cannot store '%s' at %ld:%ld", tok->filename,
|
||||
(char *)tok->value, tok->ln, tok->cn);
|
||||
"failed to assign a local slot to '%s'",
|
||||
(char *)node->var.identifier->token->value);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
@@ -1106,7 +1335,8 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
{
|
||||
fzl_sym_t sym;
|
||||
bool found = false;
|
||||
for (ssize_t i = walker->sym_top - 1; i >= 0; i--)
|
||||
ssize_t i = walker->sym_top - 1;
|
||||
for (; i >= 0; i--)
|
||||
if ((found =
|
||||
fzl_symt_get(walker->syms[i],
|
||||
node->binary_op.left->token->value, &sym)))
|
||||
@@ -1131,14 +1361,17 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
size_t const_n = add_const(
|
||||
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = sym.name,
|
||||
.as.string.length =
|
||||
node->binary_op.left->token->value_sz});
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, const_n);
|
||||
push_op(walker, FZL_OPCODE_WRITE);
|
||||
if (!sym.slot_set)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"internal error: local '%s' has no slot", sym.name);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
size_t depth = walker->sym_top - 1 - i;
|
||||
push_op(walker, FZL_OPCODE_STORE_LOCAL);
|
||||
push_word(walker, depth);
|
||||
push_word(walker, sym.slot);
|
||||
}
|
||||
else if (node->binary_op.left->type == FZL_NODETYPE_FIELD)
|
||||
{
|
||||
@@ -1225,6 +1458,11 @@ int fzl_walk(fzl_walker_t *walker)
|
||||
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||
|
||||
fin_ctx(walker);
|
||||
if (walker->ctx)
|
||||
{
|
||||
while (walker->ctx)
|
||||
fin_ctx(walker);
|
||||
}
|
||||
|
||||
if (result.error && !wlast_err[0])
|
||||
snprintf(wlast_err, sizeof(wlast_err), "%s", result.error_msg);
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "compiler/fzl_parser.h"
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Nest context
|
||||
fzl_gen_ctx_t *gen_ctx(fzl_gen_t *gen, fzl_gen_ctx_type_t type,
|
||||
fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !type)
|
||||
return NULL;
|
||||
|
||||
fzl_gen_ctx_t *ctx = malloc(sizeof(fzl_gen_ctx_t));
|
||||
if (!ctx)
|
||||
return NULL;
|
||||
|
||||
memset(ctx, 0, sizeof(fzl_gen_ctx_t));
|
||||
|
||||
fzl_symtable_t *symt = fzl_mksymt();
|
||||
if (!symt)
|
||||
{
|
||||
free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gen->syms[gen->syms_top++] = symt;
|
||||
|
||||
size_t level = 0;
|
||||
fzl_gen_ctx_t *parent = gen->ctx;
|
||||
while (parent != NULL)
|
||||
{
|
||||
level++;
|
||||
parent = parent->parent;
|
||||
}
|
||||
|
||||
ctx->type = type;
|
||||
ctx->level = level;
|
||||
ctx->parent = gen->ctx;
|
||||
|
||||
if (type == FZL_GEN_CTX_TYPE_FUNCTION)
|
||||
{
|
||||
if (node->type == FZL_NODETYPE_FUNCTION)
|
||||
ctx->function = node;
|
||||
else
|
||||
{
|
||||
fzl_freesymt(symt);
|
||||
free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
gen->ctx = ctx;
|
||||
|
||||
return gen->ctx;
|
||||
}
|
||||
|
||||
fzl_gen_ctx_t *gen_fin_ctx(fzl_gen_t *gen)
|
||||
{
|
||||
if (!gen)
|
||||
return NULL;
|
||||
|
||||
fzl_gen_ctx_t *ctx = gen->ctx;
|
||||
if (!ctx)
|
||||
return NULL;
|
||||
|
||||
fzl_symtable_t *sym = gen->syms[--gen->syms_top];
|
||||
if (sym)
|
||||
fzl_freesymt(sym);
|
||||
|
||||
gen->ctx = ctx->parent;
|
||||
free(ctx);
|
||||
|
||||
return gen->ctx;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Check ops length and resize if needed
|
||||
bool ops_check_size(fzl_gen_t *gen)
|
||||
{
|
||||
if (!gen)
|
||||
return false;
|
||||
|
||||
if (gen->ops_count >= gen->ops_remainder)
|
||||
{
|
||||
gen->ops_remainder *= 2;
|
||||
size_t *newops = realloc(gen->ops, gen->ops_remainder * sizeof(size_t));
|
||||
if (!newops)
|
||||
return false;
|
||||
|
||||
gen->ops = newops;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Push opcode
|
||||
void gen_op(fzl_gen_t *gen, fzl_opcode_t opcode)
|
||||
{
|
||||
if (!gen)
|
||||
return;
|
||||
|
||||
if (!ops_check_size(gen))
|
||||
return;
|
||||
|
||||
gen->ops[gen->ops_count++] = opcode;
|
||||
}
|
||||
|
||||
// Push word
|
||||
void gen_word(fzl_gen_t *gen, size_t word)
|
||||
{
|
||||
if (!gen)
|
||||
return;
|
||||
|
||||
if (!ops_check_size(gen))
|
||||
return;
|
||||
|
||||
gen->ops[gen->ops_count++] = word;
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Resolve name in symbol tables
|
||||
bool gen_resolve_name(fzl_gen_t *gen, char *name, fzl_sym_t *sym)
|
||||
{
|
||||
if (!gen || !name || !sym)
|
||||
return NULL;
|
||||
|
||||
bool found = false;
|
||||
for (ssize_t i = gen->syms_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((found = fzl_symt_get(gen->syms[i], name, sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
// Resolve type and return if was resolved or not
|
||||
bool gen_resolve_type(fzl_gen_t *gen, fzl_token_t *tok, fzl_type_t *t, char *c)
|
||||
{
|
||||
if (!gen || !tok || !t)
|
||||
return false;
|
||||
|
||||
if (tok->type == FZL_TOKTYPE_NAME)
|
||||
{
|
||||
char *name = (char *)tok->value;
|
||||
if (!name)
|
||||
return false;
|
||||
|
||||
fzl_sym_t sym;
|
||||
if (!gen_resolve_name(gen, name, &sym))
|
||||
return false;
|
||||
|
||||
if (sym.node && sym.node->type == FZL_NODETYPE_CLASS)
|
||||
{
|
||||
*t = FZL_TYPE_OBJECT;
|
||||
if (c)
|
||||
c = sym.classname;
|
||||
}
|
||||
else if (sym.node && sym.node->type == FZL_NODETYPE_TYPEDEF)
|
||||
{
|
||||
*t = sym.type;
|
||||
if (c)
|
||||
c = sym.classname;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(tok->value, "null") == 0)
|
||||
*t = FZL_TYPE_NULL;
|
||||
else if (strcmp(tok->value, "str") == 0)
|
||||
*t = FZL_TYPE_STRING;
|
||||
else if (strcmp(tok->value, "bool") == 0)
|
||||
*t = FZL_TYPE_BOOL;
|
||||
else if (strcmp(tok->value, "char") == 0)
|
||||
*t = FZL_TYPE_CHAR;
|
||||
|
||||
else if (strcmp(tok->value, "uint") == 0)
|
||||
*t = FZL_TYPE_UINT;
|
||||
else if (strcmp(tok->value, "u8") == 0)
|
||||
*t = FZL_TYPE_UINT8;
|
||||
else if (strcmp(tok->value, "u16") == 0)
|
||||
*t = FZL_TYPE_UINT16;
|
||||
else if (strcmp(tok->value, "u32") == 0)
|
||||
*t = FZL_TYPE_UINT32;
|
||||
else if (strcmp(tok->value, "u64") == 0)
|
||||
*t = FZL_TYPE_UINT64;
|
||||
|
||||
else if (strcmp(tok->value, "int") == 0)
|
||||
*t = FZL_TYPE_INT;
|
||||
else if (strcmp(tok->value, "i8") == 0)
|
||||
*t = FZL_TYPE_INT8;
|
||||
else if (strcmp(tok->value, "i16") == 0)
|
||||
*t = FZL_TYPE_INT16;
|
||||
else if (strcmp(tok->value, "i32") == 0)
|
||||
*t = FZL_TYPE_INT32;
|
||||
else if (strcmp(tok->value, "i64") == 0)
|
||||
*t = FZL_TYPE_INT64;
|
||||
else
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if left and right types are compatible
|
||||
bool gen_is_types_compatible(fzl_type_t t1, fzl_type_t t2, char *c1, char *c2)
|
||||
{
|
||||
if (t1 == FZL_TYPE_OBJECT && t2 == FZL_TYPE_OBJECT)
|
||||
{
|
||||
if (!c1 || !c2)
|
||||
return false;
|
||||
|
||||
if (strcmp(c1, null_name) == 0 || strcmp(c2, null_name) == 0)
|
||||
return true;
|
||||
|
||||
return strcmp(c1, c2) == 0;
|
||||
}
|
||||
|
||||
if ((t1 == FZL_TYPE_HANDLE && t2 == FZL_TYPE_HANDLE) ||
|
||||
(t1 == FZL_TYPE_STRUCT && t2 == FZL_TYPE_STRUCT))
|
||||
{
|
||||
if (!c1 || !c2)
|
||||
return false;
|
||||
|
||||
return strcmp(c1, c2) == 0;
|
||||
}
|
||||
|
||||
if (t1 == t2)
|
||||
return true;
|
||||
|
||||
if (t1 >= FZL_TYPE_INT8 && t2 >= FZL_TYPE_INT8)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add new constant to constants table
|
||||
size_t gen_add_const(fzl_gen_t *gen, fzl_const_t c)
|
||||
{
|
||||
if (!gen)
|
||||
return 0;
|
||||
|
||||
return fzl_consts_pool_add(gen->consts, c);
|
||||
}
|
||||
+79
-5
@@ -1,4 +1,5 @@
|
||||
#include <common/fzl_colors.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_debug.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_reader.h>
|
||||
@@ -96,11 +97,21 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
fzl_parser_t *parser = fzl_mkparser(rd->first);
|
||||
result = fzl_parse(parser);
|
||||
if (!parser)
|
||||
{
|
||||
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freereader(rd);
|
||||
fzl_freelinked(linked);
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = fzl_parse(parser);
|
||||
if (result != 0)
|
||||
{
|
||||
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_parser_geterror());
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freereader(rd);
|
||||
fzl_freelinked(linked);
|
||||
@@ -112,16 +123,63 @@ int main(int argc, char **argv)
|
||||
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);
|
||||
printf("%s-> analyzing AST...%s", FZL_CDEBUG, FZL_CRST);
|
||||
}
|
||||
|
||||
fzl_analyzer_t *analyzer = fzl_mkanalyzer(parser->root);
|
||||
if (!analyzer)
|
||||
{
|
||||
printf("%sError%s: failed to allocate analyzer\n", FZL_CERROR, FZL_CRST);
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freereader(rd);
|
||||
fzl_freelinked(linked);
|
||||
}
|
||||
|
||||
fzl_analyze(analyzer);
|
||||
if (analyzer->errors_top > 0)
|
||||
{
|
||||
for (ssize_t i = analyzer->errors_top - 1; i >= 0; i--)
|
||||
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, analyzer->errors[i]);
|
||||
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freereader(rd);
|
||||
fzl_freelinked(linked);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (analyzer->warnings_top > 0)
|
||||
{
|
||||
for (ssize_t i = analyzer->warnings_top - 1; i >= 0; i--)
|
||||
printf("%sWarning%s: %s\n", FZL_CWARNING, FZL_CRST,
|
||||
analyzer->warnings[i]);
|
||||
}
|
||||
|
||||
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
||||
result = fzl_walk(walker);
|
||||
if (!walker)
|
||||
{
|
||||
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freereader(rd);
|
||||
fzl_freelinked(linked);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freereader(rd);
|
||||
fzl_freelinked(linked);
|
||||
@@ -142,7 +200,7 @@ int main(int argc, char **argv)
|
||||
{
|
||||
if (i > 0 && i < walker->op_count)
|
||||
printf("->");
|
||||
printf("%lu", walker->ops[i]);
|
||||
printf("%lX", walker->ops[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
@@ -162,13 +220,28 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
fzl_writer_t *writer = fzl_mkwriter(outpath);
|
||||
result = fzl_write(writer, walker);
|
||||
if (!writer)
|
||||
{
|
||||
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freewalker(walker);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freereader(rd);
|
||||
fzl_freelinked(linked);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freereader(rd);
|
||||
fzl_freelinked(linked);
|
||||
@@ -182,6 +255,7 @@ int main(int argc, char **argv)
|
||||
fzl_freelinked(linked);
|
||||
fzl_freereader(rd);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freewalker(walker);
|
||||
fzl_freewriter(writer);
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
|
||||
// Parse conditional &&
|
||||
fzl_node_t *pa_and(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = pa_cond(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
return NULL;
|
||||
|
||||
while (is(parser, FZL_TOKTYPE_DAMPERSAND))
|
||||
{
|
||||
fzl_token_t * or = consume(parser);
|
||||
right = pa_cond(parser);
|
||||
if (!right)
|
||||
{
|
||||
fzl_freeast(left);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_AND, or);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Assignments parsing
|
||||
fzl_node_t *pa_assignment(fzl_parser_t *parser)
|
||||
{
|
||||
fzl_node_t *left = pa_new(parser);
|
||||
if (!left)
|
||||
return NULL;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_EQ))
|
||||
{
|
||||
fzl_token_t *asn = consume(parser);
|
||||
if (!left || (left->type != FZL_NODETYPE_IDENTIFIER &&
|
||||
left->type != FZL_NODETYPE_DECLARATION &&
|
||||
left->type != FZL_NODETYPE_FIELD))
|
||||
{
|
||||
fzl_freeast(left);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': cannot assign to '%s' at %ld:%ld",
|
||||
last_known_tok->filename, (char *)left->token->value,
|
||||
last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *right = pa_assignment(parser);
|
||||
if (!right)
|
||||
{
|
||||
fzl_freeast(left);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_ASSIGN, asn);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Block statement parsing
|
||||
fzl_node_t *pa_block(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *block_tok = consume(parser);
|
||||
|
||||
size_t remainder = 2;
|
||||
size_t count = 0;
|
||||
fzl_node_t **stmts = malloc(remainder * sizeof(fzl_node_t *));
|
||||
|
||||
while (peek(parser) && !is(parser, FZL_TOKTYPE_RBRACE))
|
||||
{
|
||||
fzl_node_t *node = pa_statement(parser);
|
||||
if (!node)
|
||||
{
|
||||
free(stmts);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (remainder <= count)
|
||||
{
|
||||
remainder *= 2;
|
||||
stmts = realloc(stmts, remainder * sizeof(fzl_node_t *));
|
||||
}
|
||||
|
||||
stmts[count++] = node;
|
||||
}
|
||||
fzl_token_t *rbrace = consume(parser);
|
||||
if (!rbrace || rbrace->type != FZL_TOKTYPE_RBRACE)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(stmts[i]);
|
||||
free(stmts);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': missing '}' after block at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_BLOCK, block_tok);
|
||||
node->list.entries = stmts;
|
||||
node->list.entry_count = count;
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// `break` keyword parsing
|
||||
fzl_node_t *pa_break(fzl_parser_t *parser)
|
||||
{
|
||||
fzl_token_t *tok = consume(parser);
|
||||
if (!tok)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "'%s': EOF",
|
||||
last_known_tok->filename);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fzl_mknode(FZL_NODETYPE_BREAK, tok);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Parse class definition
|
||||
fzl_node_t *pa_class(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *class = consume(parser);
|
||||
fzl_token_t *name = consume(parser);
|
||||
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected class name at %ld:%ld", last_known_tok->filename,
|
||||
last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *lbrace = consume(parser);
|
||||
if (!lbrace || lbrace->type != FZL_TOKTYPE_LBRACE)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected '{' after class name at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t fremainder = 2;
|
||||
size_t fcount = 0;
|
||||
fzl_node_t **fields = malloc(fremainder * sizeof(fzl_node_t *));
|
||||
if (!fields)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"failed to allocate space for class fields");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t mremainder = 2;
|
||||
size_t mcount = 0;
|
||||
fzl_node_t **methods = malloc(mremainder * sizeof(fzl_node_t *));
|
||||
if (!methods)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"failed to allocate space for class fields");
|
||||
free(fields);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (!is(parser, FZL_TOKTYPE_RBRACE))
|
||||
{
|
||||
fzl_node_t *node = pa_statement(parser);
|
||||
if (!node)
|
||||
return NULL;
|
||||
|
||||
if (node->type == FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
if (fremainder <= fcount)
|
||||
{
|
||||
fremainder *= 2;
|
||||
fields = realloc(fields, fremainder * sizeof(fzl_node_t *));
|
||||
if (!fields)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"failed to resize fields array");
|
||||
fzl_freeast(node);
|
||||
free(methods);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fields[fcount++] = node;
|
||||
}
|
||||
else if (node->type == FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
if (mremainder <= mcount)
|
||||
{
|
||||
mremainder *= 2;
|
||||
methods = realloc(methods, mremainder * sizeof(fzl_node_t *));
|
||||
if (!methods)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"failed to resize methods array");
|
||||
fzl_freeast(node);
|
||||
free(fields);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fzl_node_t **new_args = realloc(node->fn.args, (++node->fn.args_count) *
|
||||
sizeof(fzl_node_t *));
|
||||
if (!new_args)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"failed to reallocate method args list");
|
||||
free(fields);
|
||||
fzl_freeast(node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (node->fn.args_count > 1)
|
||||
memmove(&new_args[1], new_args,
|
||||
(node->fn.args_count - 1) * sizeof(fzl_node_t *));
|
||||
|
||||
fzl_node_t *this_id = fzl_mknode(FZL_NODETYPE_IDENTIFIER, &this_tok);
|
||||
fzl_node_t *this_dec = fzl_mknode(FZL_NODETYPE_DECLARATION, name);
|
||||
this_dec->var.type = name;
|
||||
this_dec->var.identifier = this_id;
|
||||
|
||||
new_args[0] = this_dec;
|
||||
node->fn.args = new_args;
|
||||
methods[mcount++] = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
fzl_freeast(node);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': unknown type of class member at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
free(fields);
|
||||
free(methods);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
fzl_token_t *rbra = consume(parser);
|
||||
if (rbra->type != FZL_TOKTYPE_RBRACE)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected '}' after class definition at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
free(fields);
|
||||
free(methods);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_CLASS, class);
|
||||
node->class.name = name;
|
||||
node->class.fields = fields;
|
||||
node->class.fields_count = fcount;
|
||||
node->class.methods = methods;
|
||||
node->class.methods_count = mcount;
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
|
||||
// Parse conditional operations
|
||||
fzl_node_t *pa_cond(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = pa_term(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
return NULL;
|
||||
|
||||
while (is(parser, FZL_TOKTYPE_EQEQ) || is(parser, FZL_TOKTYPE_NEQ) ||
|
||||
is(parser, FZL_TOKTYPE_LANGLE) || is(parser, FZL_TOKTYPE_LEQ) ||
|
||||
is(parser, FZL_TOKTYPE_RANGLE) || is(parser, FZL_TOKTYPE_REQ))
|
||||
{
|
||||
fzl_token_t *op = consume(parser);
|
||||
right = pa_term(parser);
|
||||
if (!right)
|
||||
{
|
||||
fzl_freeast(left);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *result;
|
||||
switch (op->type)
|
||||
{
|
||||
case FZL_TOKTYPE_EQEQ:
|
||||
result = fzl_mknode(FZL_NODETYPE_EQUAL, op);
|
||||
break;
|
||||
case FZL_TOKTYPE_NEQ:
|
||||
result = fzl_mknode(FZL_NODETYPE_NEQUAL, op);
|
||||
break;
|
||||
case FZL_TOKTYPE_LANGLE:
|
||||
result = fzl_mknode(FZL_NODETYPE_LESS, op);
|
||||
break;
|
||||
case FZL_TOKTYPE_LEQ:
|
||||
result = fzl_mknode(FZL_NODETYPE_LESS_EQUAL, op);
|
||||
break;
|
||||
case FZL_TOKTYPE_RANGLE:
|
||||
result = fzl_mknode(FZL_NODETYPE_GREATER, op);
|
||||
break;
|
||||
case FZL_TOKTYPE_REQ:
|
||||
result = fzl_mknode(FZL_NODETYPE_GREATER_EQUAL, op);
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
result->binary_op.left = left;
|
||||
result->binary_op.right = right;
|
||||
left = result;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
|
||||
// Expression parsing
|
||||
fzl_node_t *pa_expression(fzl_parser_t *parser)
|
||||
{
|
||||
return pa_assignment(parser);
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Parse extern definition
|
||||
fzl_node_t *pa_extern(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser || !peek(parser))
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *kw = consume(parser);
|
||||
if (!kw || kw->type != FZL_TOKTYPE_EXTERN)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected 'extern' keyword",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *libname = consume(parser);
|
||||
if (!libname || libname->type != FZL_TOKTYPE_STR_LITERAL)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected library name after 'extern'",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *lbra = consume(parser);
|
||||
if (!lbra || lbra->type != FZL_TOKTYPE_LBRACE)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected '{' after library name",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Typedefs array
|
||||
size_t td_remainder = 2;
|
||||
size_t td_count = 0;
|
||||
fzl_node_t **td = malloc(td_remainder * sizeof(fzl_node_t *));
|
||||
if (!td)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Functions array
|
||||
size_t fn_remainder = 2;
|
||||
size_t fn_count = 0;
|
||||
fzl_node_t **fn = malloc(fn_remainder * sizeof(fzl_node_t *));
|
||||
if (!fn)
|
||||
{
|
||||
free(td);
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool error = false;
|
||||
while (peek(parser))
|
||||
{
|
||||
if (is(parser, FZL_TOKTYPE_RBRACE))
|
||||
break;
|
||||
|
||||
if (td_remainder <= td_count)
|
||||
{
|
||||
td_remainder *= 2;
|
||||
fzl_node_t **newarr = realloc(td, td_remainder * sizeof(fzl_node_t *));
|
||||
if (!newarr)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
td = newarr;
|
||||
}
|
||||
|
||||
if (fn_remainder <= fn_count)
|
||||
{
|
||||
fn_remainder *= 2;
|
||||
fzl_node_t **newarr = realloc(fn, fn_remainder * sizeof(fzl_node_t *));
|
||||
if (!newarr)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
fn = newarr;
|
||||
}
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_TYPEDEF))
|
||||
{
|
||||
fzl_node_t *result = pa_typedef(parser);
|
||||
if (!result)
|
||||
{
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
td[td_count++] = result;
|
||||
}
|
||||
else if (is(parser, FZL_TOKTYPE_FUNCTION))
|
||||
{
|
||||
fzl_node_t *result = pa_externfn(parser);
|
||||
if (!result)
|
||||
{
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
fn[fn_count++] = result;
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => unexpected statement inside extern block",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
fzl_token_t *sc = consume(parser);
|
||||
if (!sc || sc->type != FZL_TOKTYPE_SEMICOLON)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "'%s':%zu:%zu => missing ';'",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (error)
|
||||
{
|
||||
for (size_t i = 0; i < td_count; i++)
|
||||
fzl_freeast(td[i]);
|
||||
free(td);
|
||||
|
||||
for (size_t i = 0; i < fn_count; i++)
|
||||
fzl_freeast(fn[i]);
|
||||
free(fn);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *rbra = consume(parser);
|
||||
if (!rbra || rbra->type != FZL_TOKTYPE_RBRACE)
|
||||
{
|
||||
for (size_t i = 0; i < td_count; i++)
|
||||
fzl_freeast(td[i]);
|
||||
free(td);
|
||||
|
||||
for (size_t i = 0; i < fn_count; i++)
|
||||
fzl_freeast(fn[i]);
|
||||
free(fn);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected '}' after extern block",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_EXTERN, kw);
|
||||
if (!node)
|
||||
{
|
||||
for (size_t i = 0; i < td_count; i++)
|
||||
fzl_freeast(td[i]);
|
||||
free(td);
|
||||
|
||||
for (size_t i = 0; i < fn_count; i++)
|
||||
fzl_freeast(fn[i]);
|
||||
free(fn);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->ext.libname = (char *)libname->value;
|
||||
node->ext.typedefs = td;
|
||||
node->ext.typedefs_count = td_count;
|
||||
node->ext.fns = fn;
|
||||
node->ext.fns_count = fn_count;
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Parse external function
|
||||
fzl_node_t *pa_externfn(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser || !peek(parser))
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *tok = consume(parser);
|
||||
if (!tok || tok->type != FZL_TOKTYPE_FUNCTION)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected 'function' keyword",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *name = consume(parser);
|
||||
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected function name after 'fn'",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *lpar = consume(parser);
|
||||
if (!lpar || lpar->type != FZL_TOKTYPE_LPAREN)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected '(' after function name",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t remainder = 2;
|
||||
size_t count = 0;
|
||||
fzl_node_t **args = malloc(remainder * sizeof(fzl_node_t *));
|
||||
if (!args)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (peek(parser) && !is(parser, FZL_TOKTYPE_RPAREN))
|
||||
{
|
||||
if (remainder <= count)
|
||||
{
|
||||
remainder *= 2;
|
||||
fzl_node_t **newarr = realloc(args, remainder * sizeof(fzl_node_t *));
|
||||
if (!newarr)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(args[i]);
|
||||
free(args);
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
args = newarr;
|
||||
}
|
||||
|
||||
bool is_out = false;
|
||||
if (is(parser, FZL_TOKTYPE_OUT))
|
||||
{
|
||||
consume(parser);
|
||||
is_out = true;
|
||||
}
|
||||
|
||||
fzl_node_t *node = pa_expression(parser);
|
||||
if (!node || node->type != FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(args[i]);
|
||||
free(args);
|
||||
|
||||
if (!plast_err[0])
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected declaration",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->var.is_out = is_out;
|
||||
args[count++] = node;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_RPAREN))
|
||||
break;
|
||||
|
||||
fzl_token_t *comma = consume(parser);
|
||||
if (!comma || comma->type != FZL_TOKTYPE_COMMA)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(args[i]);
|
||||
free(args);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => missing ',' between arguments",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fzl_token_t *rpar = consume(parser);
|
||||
if (!rpar || rpar->type != FZL_TOKTYPE_RPAREN)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(args[i]);
|
||||
free(args);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected ')' after arguments list",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *original_name = NULL;
|
||||
fzl_token_t *type = &null_tok;
|
||||
if (is(parser, FZL_TOKTYPE_NAME) || is(parser, FZL_TOKTYPE_TYPE))
|
||||
type = consume(parser);
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_EQ))
|
||||
{
|
||||
consume(parser);
|
||||
fzl_token_t *orig = consume(parser);
|
||||
|
||||
if (!orig || orig->type != FZL_TOKTYPE_STR_LITERAL)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(args[i]);
|
||||
free(args);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected original function name",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
original_name = (char *)orig->value;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_EXTERN_FN, tok);
|
||||
node->ext_fn.name = (char *)name->value;
|
||||
node->ext_fn.original_name = original_name;
|
||||
node->ext_fn.args_count = count;
|
||||
node->ext_fn.args = args;
|
||||
node->ext_fn.type = type;
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Parse FACTOR: multiplication and division
|
||||
fzl_node_t *pa_factor(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = pa_power(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
return NULL;
|
||||
|
||||
while (is(parser, FZL_TOKTYPE_STAR) || is(parser, FZL_TOKTYPE_SLASH) ||
|
||||
is(parser, FZL_TOKTYPE_PERCENT))
|
||||
{
|
||||
fzl_token_t *op = consume(parser);
|
||||
|
||||
right = pa_factor(parser);
|
||||
if (!right)
|
||||
{
|
||||
fzl_freeast(left);
|
||||
if (!plast_err[0])
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected expression at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(math_op_to_type(op->type), op);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// For-loop parsing
|
||||
fzl_node_t *pa_for(fzl_parser_t *parser)
|
||||
{
|
||||
fzl_token_t *tok = consume(parser);
|
||||
fzl_token_t *lpar = consume(parser);
|
||||
if (!lpar || lpar->type != FZL_TOKTYPE_LPAREN)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected '(' after for keyword at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *start = NULL;
|
||||
if (!is(parser, FZL_TOKTYPE_SEMICOLON))
|
||||
{
|
||||
start = pa_expression(parser);
|
||||
if (!start)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected loop init or ';' at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *sc = consume(parser);
|
||||
if (!sc || sc->type != FZL_TOKTYPE_SEMICOLON)
|
||||
{
|
||||
fzl_freeast(start);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': missing ';' after init section at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
consume(parser);
|
||||
|
||||
fzl_node_t *condition = NULL;
|
||||
if (!is(parser, FZL_TOKTYPE_SEMICOLON))
|
||||
{
|
||||
condition = pa_expression(parser);
|
||||
if (!condition)
|
||||
{
|
||||
fzl_freeast(start);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected loop condition or ';' at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *sc = consume(parser);
|
||||
if (!sc || sc->type != FZL_TOKTYPE_SEMICOLON)
|
||||
{
|
||||
fzl_freeast(start);
|
||||
fzl_freeast(condition);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': missing ';' after condition at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
consume(parser);
|
||||
|
||||
fzl_node_t *step = NULL;
|
||||
if (!is(parser, FZL_TOKTYPE_RPAREN))
|
||||
{
|
||||
step = pa_expression(parser);
|
||||
if (!step)
|
||||
{
|
||||
fzl_freeast(start);
|
||||
fzl_freeast(condition);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected loop step end or ')' at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fzl_token_t *rpar = consume(parser);
|
||||
if (!rpar || rpar->type != FZL_TOKTYPE_RPAREN)
|
||||
{
|
||||
fzl_freeast(start);
|
||||
fzl_freeast(condition);
|
||||
fzl_freeast(step);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': missing ')' after loop setup at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *body = pa_statement(parser);
|
||||
if (!body)
|
||||
{
|
||||
fzl_freeast(start);
|
||||
fzl_freeast(condition);
|
||||
fzl_freeast(step);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': missing body of for loop at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_LOOP, tok);
|
||||
node->loop.init = start;
|
||||
node->loop.cond = condition;
|
||||
node->loop.step = step;
|
||||
node->loop.body = body;
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Functions parsing
|
||||
fzl_node_t *pa_function(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *fn = consume(parser);
|
||||
if (!fn || fn->type != FZL_TOKTYPE_FUNCTION)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected keyword 'fn' at %ld:%ld", last_known_tok->filename,
|
||||
last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *name = consume(parser);
|
||||
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected function name at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *lpar = consume(parser);
|
||||
if (!lpar || lpar->type != FZL_TOKTYPE_LPAREN)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "'%s': expected '(' at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t remainder = 1;
|
||||
size_t count = 0;
|
||||
fzl_node_t **args = malloc(remainder * sizeof(fzl_node_t *));
|
||||
|
||||
while (peek(parser) && !is(parser, FZL_TOKTYPE_RPAREN))
|
||||
{
|
||||
fzl_node_t *arg = pa_expression(parser);
|
||||
if (!arg)
|
||||
return NULL;
|
||||
|
||||
if (remainder <= count)
|
||||
{
|
||||
remainder *= 2;
|
||||
args = realloc(args, remainder * sizeof(fzl_node_t *));
|
||||
}
|
||||
|
||||
args[count++] = arg;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_COMMA))
|
||||
consume(parser);
|
||||
}
|
||||
|
||||
fzl_token_t *rpar = consume(parser);
|
||||
if (!rpar || rpar->type != FZL_TOKTYPE_RPAREN)
|
||||
{
|
||||
free(args);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected ')' after argument list at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *type = NULL;
|
||||
if (is(parser, FZL_TOKTYPE_TYPE) || is(parser, FZL_TOKTYPE_NAME))
|
||||
type = consume(parser);
|
||||
|
||||
if (!type)
|
||||
type = &null_tok;
|
||||
|
||||
if (!is(parser, FZL_TOKTYPE_LBRACE))
|
||||
{
|
||||
free(args);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected '{' after function declaration at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *body = pa_statement(parser);
|
||||
if (!body)
|
||||
{
|
||||
free(args);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_FUNCTION, name);
|
||||
node->fn.args = args;
|
||||
node->fn.args_count = count;
|
||||
node->fn.body = body;
|
||||
node->fn.name = name;
|
||||
node->fn.type = type;
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Parse if statement
|
||||
fzl_node_t *pa_if(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *if_tok = consume(parser);
|
||||
fzl_token_t *lparen = consume(parser);
|
||||
if (lparen->type != FZL_TOKTYPE_LPAREN)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected '(' before condition at %ld:%ld", lparen->filename,
|
||||
lparen->ln, lparen->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *condition = pa_expression(parser);
|
||||
if (!condition)
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *rparen = consume(parser);
|
||||
if (rparen->type != FZL_TOKTYPE_RPAREN)
|
||||
{
|
||||
fzl_freeast(condition);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected ')' after condition at %ld:%ld", rparen->filename,
|
||||
rparen->ln, rparen->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *else_block = NULL;
|
||||
fzl_node_t *true_block = pa_statement(parser);
|
||||
if (!true_block)
|
||||
{
|
||||
fzl_freeast(condition);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_ELSE))
|
||||
{
|
||||
consume(parser); // else
|
||||
else_block = pa_statement(parser);
|
||||
if (!else_block)
|
||||
{
|
||||
fzl_freeast(condition);
|
||||
fzl_freeast(true_block);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fzl_node_t *stmt_node = fzl_mknode(FZL_NODETYPE_CONDITIONAL, if_tok);
|
||||
stmt_node->cond.condition = condition;
|
||||
stmt_node->cond.true_block = true_block;
|
||||
stmt_node->cond.else_block = else_block;
|
||||
|
||||
return stmt_node;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// `while` and `for` loops
|
||||
fzl_node_t *pa_loop(fzl_parser_t *parser)
|
||||
{
|
||||
if (!peek(parser))
|
||||
return NULL;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_FOR))
|
||||
return pa_for(parser);
|
||||
else if (is(parser, FZL_TOKTYPE_WHILE))
|
||||
return pa_while(parser);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': unknown type of loop at %ld:%ld", last_known_tok->filename,
|
||||
last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Object construct parsing
|
||||
fzl_node_t *pa_new(fzl_parser_t *parser)
|
||||
{
|
||||
if (!is(parser, FZL_TOKTYPE_NEW))
|
||||
return pa_or(parser);
|
||||
|
||||
fzl_token_t *new = consume(parser);
|
||||
fzl_node_t *objname = pa_or(parser);
|
||||
if (!objname || (objname->type != FZL_NODETYPE_IDENTIFIER &&
|
||||
objname->type != FZL_NODETYPE_FIELD))
|
||||
{
|
||||
fzl_freeast(objname);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected class name after 'new' keyword at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_CONSTRUCT, new);
|
||||
node->construct.name = objname;
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
|
||||
// Parse OR (||)
|
||||
fzl_node_t *pa_or(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = pa_and(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
return NULL;
|
||||
|
||||
while (is(parser, FZL_TOKTYPE_DVBAR))
|
||||
{
|
||||
fzl_token_t * or = consume(parser);
|
||||
right = pa_and(parser);
|
||||
if (!right)
|
||||
{
|
||||
fzl_freeast(left);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_OR, or);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Parse postfix expressions (parens, index access, dots)
|
||||
fzl_node_t *pa_postfix(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = pa_primary(parser);
|
||||
|
||||
if (!left)
|
||||
return NULL;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_LPAREN) || is(parser, FZL_TOKTYPE_DOT))
|
||||
{
|
||||
while (is(parser, FZL_TOKTYPE_LPAREN) || is(parser, FZL_TOKTYPE_DOT))
|
||||
{
|
||||
fzl_token_t *post = consume(parser);
|
||||
|
||||
if (post->type == FZL_TOKTYPE_LPAREN)
|
||||
{
|
||||
size_t args_capacity = 2;
|
||||
size_t args_count = 0;
|
||||
fzl_node_t **args = malloc(args_capacity * sizeof(fzl_node_t *));
|
||||
|
||||
while (!is(parser, FZL_TOKTYPE_RPAREN))
|
||||
{
|
||||
fzl_node_t *arg = pa_expression(parser);
|
||||
if (!arg)
|
||||
{
|
||||
for (size_t i = 0; i < args_count; i++)
|
||||
fzl_freeast(args[i]);
|
||||
free(args);
|
||||
fzl_freeast(left);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (args_capacity <= args_count)
|
||||
{
|
||||
args_capacity *= 2;
|
||||
args = realloc(args, args_capacity * sizeof(fzl_node_t *));
|
||||
}
|
||||
|
||||
args[args_count] = arg;
|
||||
args_count++;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_COMMA))
|
||||
consume(parser);
|
||||
}
|
||||
|
||||
fzl_token_t *rparen = consume(parser);
|
||||
if (!rparen || rparen->type != FZL_TOKTYPE_RPAREN)
|
||||
{
|
||||
for (size_t i = 0; i < args_count; i++)
|
||||
fzl_freeast(args[i]);
|
||||
free(args);
|
||||
fzl_freeast(left);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected ')' after argument list at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_CALL, left->token);
|
||||
node->call.callee = left;
|
||||
node->call.args = args;
|
||||
node->call.args_count = args_count;
|
||||
|
||||
left = node;
|
||||
}
|
||||
else if (post->type == FZL_TOKTYPE_DOT)
|
||||
{
|
||||
if (!is(parser, FZL_TOKTYPE_NAME))
|
||||
{
|
||||
fzl_freeast(left);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected name after '.' at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *name = consume(parser);
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_FIELD, name);
|
||||
node->memb.parent = left;
|
||||
node->memb.field = fzl_mknode(FZL_NODETYPE_IDENTIFIER, name);
|
||||
|
||||
left = node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Parse power expression
|
||||
fzl_node_t *pa_power(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = pa_unary(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
return NULL;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_CARET))
|
||||
{
|
||||
fzl_token_t *caret = consume(parser);
|
||||
|
||||
right = pa_unary(parser);
|
||||
if (!right)
|
||||
{
|
||||
fzl_freeast(left);
|
||||
if (!plast_err[0])
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected expression at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_POW, caret);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Parse primary
|
||||
fzl_node_t *pa_primary(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser || !peek(parser))
|
||||
return NULL;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_STR_LITERAL) ||
|
||||
is(parser, FZL_TOKTYPE_NUM_LITERAL) ||
|
||||
is(parser, FZL_TOKTYPE_BOOL_LITERAL) ||
|
||||
is(parser, FZL_TOKTYPE_NULL_LITERAL))
|
||||
{
|
||||
fzl_token_t *tok = consume(parser);
|
||||
return fzl_mknode(FZL_NODETYPE_LITERAL, tok);
|
||||
}
|
||||
else if (is(parser, FZL_TOKTYPE_STR_FORMAT_START))
|
||||
{
|
||||
consume(parser);
|
||||
|
||||
size_t remainder = 2;
|
||||
size_t count = 0;
|
||||
fzl_node_t **entries = malloc(remainder * sizeof(fzl_node_t *));
|
||||
if (!entries)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (peek(parser) && peek(parser)->type != FZL_TOKTYPE_STR_FORMAT_END)
|
||||
{
|
||||
if (is(parser, FZL_TOKTYPE_STR_FORMAT_END))
|
||||
break;
|
||||
|
||||
if (remainder <= count)
|
||||
{
|
||||
remainder *= 2;
|
||||
fzl_node_t **newentries =
|
||||
realloc(entries, remainder * sizeof(fzl_node_t *));
|
||||
if (!newentries)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
|
||||
free(entries);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
entries = newentries;
|
||||
}
|
||||
|
||||
fzl_node_t *expr = pa_expression(parser);
|
||||
if (expr == NULL)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
|
||||
free(entries);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
entries[count++] = expr;
|
||||
}
|
||||
|
||||
fzl_token_t *end = consume(parser);
|
||||
if (end->type != FZL_TOKTYPE_STR_FORMAT_END)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => unterminated format string",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
|
||||
free(entries);
|
||||
}
|
||||
|
||||
if (count < 1)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => do you really need empty format string?",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
|
||||
free(entries);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_FMT_STRING, entries[0]->token);
|
||||
if (!node)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
|
||||
free(entries);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->list.entries = entries;
|
||||
node->list.entry_count = count;
|
||||
|
||||
return node;
|
||||
}
|
||||
else if (is(parser, FZL_TOKTYPE_LPAREN))
|
||||
{
|
||||
consume(parser);
|
||||
fzl_node_t *node = pa_expression(parser);
|
||||
if (!is(parser, FZL_TOKTYPE_RPAREN))
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "'%s': expected ')' at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
fzl_freeast(node);
|
||||
return NULL;
|
||||
}
|
||||
consume(parser);
|
||||
return node;
|
||||
}
|
||||
else if (is(parser, FZL_TOKTYPE_NAME))
|
||||
{
|
||||
fzl_token_t *name = consume(parser);
|
||||
if (!is(parser, FZL_TOKTYPE_NAME))
|
||||
return fzl_mknode(FZL_NODETYPE_IDENTIFIER, name);
|
||||
|
||||
fzl_token_t *realname = consume(parser); // :D
|
||||
fzl_node_t *decl = fzl_mknode(FZL_NODETYPE_DECLARATION, name);
|
||||
decl->var.type = name;
|
||||
decl->var.identifier = fzl_mknode(FZL_NODETYPE_IDENTIFIER, realname);
|
||||
return decl;
|
||||
}
|
||||
else if (is(parser, FZL_TOKTYPE_TYPE))
|
||||
{
|
||||
fzl_token_t *type = consume(parser);
|
||||
fzl_token_t *name = consume(parser);
|
||||
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected name after type at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *decl = fzl_mknode(FZL_NODETYPE_DECLARATION, type);
|
||||
decl->var.type = type;
|
||||
decl->var.identifier = fzl_mknode(FZL_NODETYPE_IDENTIFIER, name);
|
||||
return decl;
|
||||
}
|
||||
|
||||
if (!plast_err[0])
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s': expected expression at %ld:%ld", last_known_tok->filename,
|
||||
last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Internal parsing entry point
|
||||
fzl_node_t *pa_program(fzl_parser_t *parser)
|
||||
{
|
||||
if (!peek(parser))
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *first = NULL;
|
||||
fzl_node_t *last = NULL;
|
||||
fzl_node_t *node = NULL;
|
||||
while (peek(parser) != NULL)
|
||||
{
|
||||
node = pa_statement(parser);
|
||||
if (!node)
|
||||
{
|
||||
fzl_freeast(first);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!last)
|
||||
{
|
||||
first = node;
|
||||
last = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
last->next = node;
|
||||
last = node;
|
||||
}
|
||||
}
|
||||
|
||||
if (!first)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "nothing to parse >:(");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Parse reference definition
|
||||
fzl_node_t *pa_ref(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser || !peek(parser))
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *tok = consume(parser);
|
||||
if (!tok || tok->type != FZL_TOKTYPE_REFERENCE)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected 'ref' keyword", last_known_tok->filename,
|
||||
last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_token_t *name = consume(parser);
|
||||
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected name after 'ref'",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_REFERENCE, name);
|
||||
if (!node)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
|
||||
// Return parsing
|
||||
fzl_node_t *pa_return(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *ret = consume(parser);
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_RETURN, ret);
|
||||
|
||||
if (!is(parser, FZL_TOKTYPE_SEMICOLON))
|
||||
{
|
||||
fzl_node_t *retval = pa_expression(parser);
|
||||
if (!retval)
|
||||
{
|
||||
fzl_freeast(node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->ret.value = retval;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Statement parsing
|
||||
fzl_node_t *pa_statement(fzl_parser_t *parser)
|
||||
{
|
||||
if (!peek(parser))
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *res = NULL;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_CLASS))
|
||||
return pa_class(parser);
|
||||
else if (is(parser, FZL_TOKTYPE_FUNCTION))
|
||||
return pa_function(parser);
|
||||
else if (is(parser, FZL_TOKTYPE_RETURN))
|
||||
res = pa_return(parser);
|
||||
else if (is(parser, FZL_TOKTYPE_IF))
|
||||
return pa_if(parser);
|
||||
else if (is(parser, FZL_TOKTYPE_LBRACE))
|
||||
return pa_block(parser);
|
||||
else if (is(parser, FZL_TOKTYPE_FOR) || is(parser, FZL_TOKTYPE_WHILE))
|
||||
return pa_loop(parser);
|
||||
else if (is(parser, FZL_TOKTYPE_BREAK))
|
||||
res = pa_break(parser);
|
||||
else if (is(parser, FZL_TOKTYPE_EXTERN))
|
||||
return pa_extern(parser);
|
||||
|
||||
if (!res)
|
||||
res = pa_expression(parser);
|
||||
|
||||
if (!res)
|
||||
{
|
||||
fzl_freeast(res);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!is(parser, FZL_TOKTYPE_SEMICOLON))
|
||||
{
|
||||
fzl_freeast(res);
|
||||
snprintf(plast_err, sizeof(plast_err), "'%s': missing ';' at %ld:%ld",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
while (is(parser, FZL_TOKTYPE_SEMICOLON))
|
||||
consume(parser);
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/parser.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Parse structure
|
||||
fzl_node_t *pa_struct(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser || !peek(parser))
|
||||
return NULL;
|
||||
|
||||
fzl_token_t *tok = consume(parser);
|
||||
if (!tok || tok->type != FZL_TOKTYPE_STRUCT)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected 'struct' keyword",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *original = NULL;
|
||||
if (is(parser, FZL_TOKTYPE_NAME))
|
||||
{
|
||||
fzl_token_t *name = consume(parser);
|
||||
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected struct name after 'struct'",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
original = (char *)name->value;
|
||||
}
|
||||
|
||||
fzl_token_t *lbra = consume(parser);
|
||||
if (!lbra || lbra->type != FZL_TOKTYPE_LBRACE)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected '{' after structure name",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t remainder = 2;
|
||||
size_t count = 0;
|
||||
fzl_node_t **entries = malloc(remainder * sizeof(fzl_node_t *));
|
||||
if (!entries)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (peek(parser))
|
||||
{
|
||||
if (is(parser, FZL_TOKTYPE_RBRACE))
|
||||
break;
|
||||
|
||||
if (remainder <= count)
|
||||
{
|
||||
remainder *= 2;
|
||||
fzl_node_t **newarr = realloc(entries, remainder * sizeof(fzl_node_t *));
|
||||
if (!newarr)
|
||||
{
|
||||
free(entries);
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
entries = newarr;
|
||||
}
|
||||
|
||||
fzl_node_t *entry = pa_statement(parser);
|
||||
if (!entry)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
free(entries);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (entry->type != FZL_NODETYPE_DECLARATION &&
|
||||
entry->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
fzl_freeast(entry);
|
||||
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
free(entries);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => structure entry must be a declaration or "
|
||||
"another structure name",
|
||||
last_known_tok->filename, last_known_tok->ln,
|
||||
last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
entries[count++] = entry;
|
||||
}
|
||||
|
||||
fzl_token_t *rbra = consume(parser);
|
||||
if (!rbra || rbra->type != FZL_TOKTYPE_RBRACE)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
free(entries);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"'%s':%zu:%zu => expected '}' after structure fields",
|
||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_STRUCT, tok);
|
||||
if (!node)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
fzl_freeast(entries[i]);
|
||||
free(entries);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err), "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->structure.original_name = original;
|
||||
node->structure.entries = entries;
|
||||
node->structure.entries_count = count;
|
||||
|
||||
return node;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user