Compare commits
56
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0fc6406e97 | ||
|
|
d09b4f2761 | ||
|
|
2488e9fb07 | ||
|
|
899c22eece | ||
|
|
1c57525604 | ||
|
|
5de995a3c2 | ||
|
|
7759136aa8 | ||
|
|
d38365f10c | ||
|
|
317b846108 | ||
|
|
376e7bd1b4 | ||
|
|
d95ff88b66 | ||
|
|
1db42c1e8e | ||
|
|
84572edb0d | ||
|
|
d91ba3d63a | ||
|
|
de0e88f2e9 | ||
|
|
66f31ab865 | ||
|
|
bd8272abc7 | ||
|
|
181458ecda | ||
|
|
aae183c0c0 | ||
|
|
b278e8bb74 | ||
|
|
7fe3fe3ff3 | ||
|
|
731b662cfc | ||
|
|
f2b88e448e | ||
|
|
c5c522cde8 | ||
|
|
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 |
@@ -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,16 +3,18 @@
|
||||
# Dirs
|
||||
!/docs
|
||||
!/exl
|
||||
!/benchmarks
|
||||
!/include
|
||||
!/scripts
|
||||
!/src
|
||||
!/tests
|
||||
|
||||
# Files
|
||||
!/.clang-format
|
||||
!/.clangd
|
||||
!/.gdbinit
|
||||
!/.gitignore
|
||||
!/CMakeLists.txt
|
||||
!/Makefile
|
||||
!/README.md
|
||||
!/README.ru.md
|
||||
|
||||
|
||||
+202
-61
@@ -8,80 +8,221 @@ 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/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/checkers/fzl_loop_check.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_field.c
|
||||
src/compiler/analyzer/an_function.c
|
||||
src/compiler/analyzer/an_identifier.c
|
||||
src/compiler/analyzer/an_loop.c
|
||||
src/compiler/analyzer/an_return.c
|
||||
src/compiler/analyzer/an_unarycond.c
|
||||
src/compiler/analyzer/an_unarymath.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)
|
||||
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/generator/context.c
|
||||
src/compiler/generator/op.c
|
||||
src/compiler/generator/utility.c
|
||||
src/compiler/generator/gen_alias.c
|
||||
src/compiler/generator/gen_assign.c
|
||||
src/compiler/generator/gen_block.c
|
||||
src/compiler/generator/gen_break.c
|
||||
src/compiler/generator/gen_call.c
|
||||
src/compiler/generator/gen_class.c
|
||||
src/compiler/generator/gen_common.c
|
||||
src/compiler/generator/gen_compare.c
|
||||
src/compiler/generator/gen_condition.c
|
||||
src/compiler/generator/gen_construct.c
|
||||
src/compiler/generator/gen_declaration.c
|
||||
src/compiler/generator/gen_extern.c
|
||||
src/compiler/generator/gen_externfn.c
|
||||
src/compiler/generator/gen_field.c
|
||||
src/compiler/generator/gen_fmtstr.c
|
||||
src/compiler/generator/gen_fn.c
|
||||
src/compiler/generator/gen_handle.c
|
||||
src/compiler/generator/gen_identifier.c
|
||||
src/compiler/generator/gen_literal.c
|
||||
src/compiler/generator/gen_logical.c
|
||||
src/compiler/generator/gen_loop.c
|
||||
src/compiler/generator/gen_math.c
|
||||
src/compiler/generator/gen_reference.c
|
||||
src/compiler/generator/gen_return.c
|
||||
src/compiler/generator/gen_struct.c
|
||||
src/compiler/generator/gen_typedef.c
|
||||
src/compiler/generator/gen_unary_logical.c
|
||||
src/compiler/generator/gen_unary_math.c
|
||||
src/compiler/parser/utility.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/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/writer/wr_consts.c
|
||||
src/compiler/writer/wr_extern_fns.c
|
||||
src/compiler/writer/wr_extern_libs.c
|
||||
src/compiler/writer/wr_extern_types.c
|
||||
src/compiler/writer/wr_ops.c
|
||||
src/compiler/fzl_analyzer.c
|
||||
src/compiler/fzl_debug.c
|
||||
src/compiler/fzl_gen.c
|
||||
src/compiler/fzl_parser.c
|
||||
src/compiler/fzl_reader.c
|
||||
src/compiler/fzl_token.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/reader/utility.c
|
||||
src/vm/reader/vm_rd_consts.c
|
||||
src/vm/reader/vm_rd_extern_fns.c
|
||||
src/vm/reader/vm_rd_extern_libs.c
|
||||
src/vm/reader/vm_rd_extern_types.c
|
||||
src/vm/reader/vm_rd_header.c
|
||||
src/vm/reader/vm_rd_ops.c
|
||||
src/vm/reader/vm_rd_sections_sz.c
|
||||
src/vm/fzl_vm.c
|
||||
src/vm/fzl_vm_env.c
|
||||
src/vm/fzl_vm_extern.c
|
||||
src/vm/fzl_vm_reader.c
|
||||
src/vm/fzl_vm_debug.c
|
||||
src/vm/main.c)
|
||||
|
||||
target_link_libraries(fz PRIVATE ffi_static dl)
|
||||
endif ()
|
||||
|
||||
#
|
||||
# ========== Installtion ===========
|
||||
#
|
||||
|
||||
if (NOT FZL_SKIP_C)
|
||||
install (TARGETS fzc RUNTIME DESTINATION bin)
|
||||
endif ()
|
||||
|
||||
if (NOT FZL_SKIP_VM)
|
||||
install (TARGETS fz RUNTIME DESTINATION bin)
|
||||
endif ()
|
||||
|
||||
#
|
||||
# ========== Tests ==========
|
||||
#
|
||||
|
||||
enable_testing()
|
||||
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"
|
||||
)
|
||||
file(GLOB TEST_LIBS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "tests/libs/*.c")
|
||||
foreach (TEST_LIB IN LISTS TEST_LIBS)
|
||||
get_filename_component(LIB_NAME "${TEST_LIB}" NAME_WE)
|
||||
add_library("${LIB_NAME}" ${TEST_LIB})
|
||||
endforeach ()
|
||||
|
||||
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()
|
||||
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 "negr_")
|
||||
set_tests_properties("progs_runtime_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
|
||||
endif()
|
||||
endforeach()
|
||||
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,19 +1,58 @@
|
||||
.PHONY: build debug-c debug-vm test
|
||||
.PHONY: build debug-c debug-vm test bench
|
||||
|
||||
build:
|
||||
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
|
||||
cmake --build build
|
||||
|
||||
build-release:
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build
|
||||
build:
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release $(COMPARING)
|
||||
cmake --build build $(COMPARING)
|
||||
|
||||
debug-c: build
|
||||
gdb --args ./build/fzc ${FILE}
|
||||
install:
|
||||
cmake --install build
|
||||
|
||||
debug-vm: 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')"
|
||||
|
||||
+19
-6
@@ -27,12 +27,8 @@
|
||||
## Инструкции по сборке
|
||||
|
||||
1. Склонировать проект: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||
2. Собрать проект: `make build-release`
|
||||
|
||||
После этого в корне появится директория **"build"**. В ней будут лежать два скомпилированных бинарных файла:
|
||||
|
||||
- `fzc` - компилятор
|
||||
- `fz` - виртуальная машина
|
||||
2. Собрать проект: `make build`
|
||||
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,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";
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -76,32 +76,4 @@ void fzl_analyze(fzl_analyzer_t *analyzer);
|
||||
// Free analyzer
|
||||
void fzl_freeanalyzer(fzl_analyzer_t *analyzer);
|
||||
|
||||
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_token_t *tok);
|
||||
|
||||
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_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 // 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,21 +3,37 @@
|
||||
|
||||
#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,
|
||||
|
||||
@@ -91,6 +107,7 @@ typedef struct fzl_node
|
||||
{
|
||||
fzl_token_t *type;
|
||||
struct fzl_node *identifier;
|
||||
bool is_out;
|
||||
} var;
|
||||
|
||||
// Conditional blocks
|
||||
@@ -152,6 +169,46 @@ typedef struct fzl_node
|
||||
|
||||
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,57 +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_FOR, // for
|
||||
FZL_TOKTYPE_WHILE, // while
|
||||
FZL_TOKTYPE_BREAK, // break
|
||||
FZL_TOKTYPE_FUNCTION, // `fn`
|
||||
FZL_TOKTYPE_RETURN, // `return`
|
||||
|
||||
FZL_TOKTYPE_IF, // if
|
||||
FZL_TOKTYPE_ELSE, // else
|
||||
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,92 +0,0 @@
|
||||
#ifndef FZL_WALKER_H
|
||||
#define FZL_WALKER_H
|
||||
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <compiler/utils/fzl_ctable.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Type of context
|
||||
typedef enum
|
||||
{
|
||||
FZL_WALK_CTX_TYPE_GLOBAL,
|
||||
FZL_WALK_CTX_TYPE_FN,
|
||||
FZL_WALK_CTX_TYPE_CLASS,
|
||||
FZL_WALK_CTX_TYPE_ASSIGN,
|
||||
FZL_WALK_CTX_TYPE_LOOP,
|
||||
} fzl_walk_ctx_type_t;
|
||||
|
||||
// Internal walk function result
|
||||
typedef struct
|
||||
{
|
||||
fzl_type_t type;
|
||||
|
||||
char *error_msg;
|
||||
bool error;
|
||||
|
||||
char *classname;
|
||||
} fzl_walk_res_t;
|
||||
|
||||
// Function context descriptor
|
||||
typedef struct fzl_walk_ctx
|
||||
{
|
||||
fzl_walk_ctx_type_t type;
|
||||
struct fzl_walk_ctx *parent;
|
||||
|
||||
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
|
||||
typedef struct
|
||||
{
|
||||
fzl_node_t *root;
|
||||
fzl_const_hash_pool_t *consts;
|
||||
|
||||
uint64_t *ops;
|
||||
size_t op_count;
|
||||
size_t op_remainder;
|
||||
|
||||
fzl_symtable_t *syms[64];
|
||||
size_t sym_top;
|
||||
|
||||
fzl_walk_ctx_t *ctx;
|
||||
|
||||
fzl_ctable_t *classes[64];
|
||||
size_t classes_count;
|
||||
} fzl_walker_t;
|
||||
|
||||
// Allocate new walker
|
||||
fzl_walker_t *fzl_mkwalker(fzl_node_t *root);
|
||||
|
||||
// Walk throught the entire AST and build:
|
||||
// 1. Constants table
|
||||
// 2. Operations
|
||||
int fzl_walk(fzl_walker_t *walker);
|
||||
|
||||
// Get last error message
|
||||
char *fzl_walker_geterror();
|
||||
|
||||
// Free walker
|
||||
void fzl_freewalker(fzl_walker_t *walker);
|
||||
|
||||
#endif // FZL_WALKER_H
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef FZL_WRITER_H
|
||||
#define FZL_WRITER_H
|
||||
|
||||
#include <compiler/fzl_walker.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
|
||||
// File writer descriptor
|
||||
@@ -14,7 +14,7 @@ typedef struct
|
||||
fzl_writer_t *fzl_mkwriter(const char *filepath);
|
||||
|
||||
// Write out a file
|
||||
int fzl_write(fzl_writer_t *writer, fzl_walker_t *walker);
|
||||
int fzl_write(fzl_writer_t *writer, fzl_gen_t *gen);
|
||||
|
||||
// Get last writer error message
|
||||
char *fzl_writer_geterror();
|
||||
|
||||
@@ -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,69 @@
|
||||
#ifndef INTERNALS_GEN_H
|
||||
#define INTERNALS_GEN_H
|
||||
|
||||
#include <common/fzl_extern.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>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
bool error;
|
||||
fzl_type_t type;
|
||||
char *classname;
|
||||
} gen_res_t;
|
||||
|
||||
extern gen_res_t gen_error;
|
||||
extern gen_res_t gen_ok_null;
|
||||
extern char *null_name;
|
||||
extern fzl_extern_type_t type_buf;
|
||||
|
||||
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);
|
||||
ssize_t gen_resolve_extern_type(fzl_gen_t *gen, char *name);
|
||||
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);
|
||||
|
||||
gen_res_t gen_common(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_extern(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_externfn(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_reference(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_struct(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_alias(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_handle(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_typedef(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_fmtstr(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_break(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_loop(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_class(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_construct(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_field(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_fn(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_return(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_unary_logical(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_compare(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_logical(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_condition(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_unary_math(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_declaration(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_identifier(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_block(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_call(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_literal(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t gen_math(fzl_gen_t *gen, fzl_node_t *node);
|
||||
gen_res_t 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
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef INTERNALS_WRITER_H
|
||||
#define INTERNALS_WRITER_H
|
||||
|
||||
#include <common/fzl_extern.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
bool wr_consts(FILE *fd, fzl_const_hash_pool_t *consts);
|
||||
bool wr_extern_libs(FILE *fd, fzl_extern_lib_t *libs, size_t libs_count);
|
||||
bool wr_extern_types(FILE *fd, fzl_extern_type_t *types, size_t types_count);
|
||||
bool wr_extern_fns(FILE *fd, fzl_extern_fn_t *fns, size_t fns_count);
|
||||
bool wr_ops(FILE *fd, size_t *ops, size_t ops_count);
|
||||
|
||||
#endif // INTERNALS_WRITER_H
|
||||
@@ -23,6 +23,9 @@ typedef struct
|
||||
fzl_node_t *node;
|
||||
size_t ip;
|
||||
|
||||
bool slot_set;
|
||||
size_t slot;
|
||||
|
||||
char *classname;
|
||||
} fzl_sym_t;
|
||||
|
||||
@@ -32,6 +35,8 @@ typedef struct
|
||||
size_t size;
|
||||
size_t remainder;
|
||||
|
||||
size_t free_slot;
|
||||
|
||||
fzl_sym_t *symbols;
|
||||
} fzl_symtable_t;
|
||||
|
||||
@@ -46,6 +51,9 @@ fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, fzl_node_t *node,
|
||||
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);
|
||||
|
||||
|
||||
+10
-10
@@ -1,24 +1,24 @@
|
||||
#ifndef FZL_VM_H
|
||||
#define FZL_VM_H
|
||||
|
||||
#include <common/fzl_value.h>
|
||||
#include <compiler/utils/fzl_const_pool.h> // NOTE: move outside compiler scope to common
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <vm/fzl_vm_env.h>
|
||||
#include <vm/fzl_vm_object.h>
|
||||
#include <vm/fzl_vm_extern.h>
|
||||
#include <vm/fzl_vm_reader.h>
|
||||
|
||||
// VM descriptor
|
||||
typedef struct
|
||||
{
|
||||
fzl_const_t *consts;
|
||||
size_t consts_count;
|
||||
|
||||
uint64_t *ops;
|
||||
size_t ops_count;
|
||||
fzl_vm_reader_t *reader;
|
||||
fzl_vm_extern_t *prep_fns;
|
||||
|
||||
size_t ip;
|
||||
|
||||
bool allow_infinite;
|
||||
size_t max_iter;
|
||||
|
||||
// Data stack
|
||||
fzl_value_t dstack[256];
|
||||
size_t dtop;
|
||||
@@ -33,10 +33,10 @@ typedef struct
|
||||
} fzl_vm_t;
|
||||
|
||||
// Allocate FZL VM
|
||||
fzl_vm_t *fzl_mkvm(const char *filepath);
|
||||
fzl_vm_t *fzl_mkvm(fzl_vm_reader_t *reader);
|
||||
|
||||
// Load and run code
|
||||
int fzl_vm_run(fzl_vm_t *vm, bool allow_infinite, uint64_t max_iter);
|
||||
int fzl_vm_run(fzl_vm_t *vm);
|
||||
|
||||
// Get last VM error message
|
||||
char *fzl_vm_geterror();
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef FZL_VM_EXTERN_H
|
||||
#define FZL_VM_EXTERN_H
|
||||
|
||||
#include <ffi.h>
|
||||
#include <stddef.h>
|
||||
#include <vm/fzl_vm_reader.h>
|
||||
|
||||
// Loaded extern function definition
|
||||
typedef struct
|
||||
{
|
||||
void *func_ptr;
|
||||
ffi_cif cif;
|
||||
ffi_type **arg_types;
|
||||
} fzl_vm_sym_t;
|
||||
|
||||
// Extern object descriptor
|
||||
typedef struct
|
||||
{
|
||||
void **libs;
|
||||
size_t libs_loaded;
|
||||
|
||||
fzl_vm_sym_t *syms;
|
||||
size_t syms_loaded;
|
||||
} fzl_vm_extern_t;
|
||||
|
||||
// Load externs
|
||||
fzl_vm_extern_t *fzl_vm_init_externs(fzl_vm_reader_t *reader);
|
||||
|
||||
// Get last error from externs loader
|
||||
char *fzl_vm_externs_geterror();
|
||||
|
||||
// Close and free externs
|
||||
void fzl_vm_free_externs(fzl_vm_extern_t *ex);
|
||||
|
||||
#endif // FZL_VM_EXTERN_H
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef FZL_VM_READER_H
|
||||
#define FZL_VM_READER_H
|
||||
|
||||
#include <common/fzl_extern.h>
|
||||
#include <common/fzl_value.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Binary file reader descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *filepath;
|
||||
|
||||
fzl_value_t *consts;
|
||||
uint16_t consts_count;
|
||||
|
||||
size_t *ops;
|
||||
size_t ops_count;
|
||||
|
||||
char **extern_libs;
|
||||
uint16_t extern_libs_count;
|
||||
|
||||
fzl_extern_type_t *extern_types;
|
||||
uint16_t extern_types_count;
|
||||
|
||||
fzl_extern_fn_t *extern_fns;
|
||||
uint16_t extern_fns_count;
|
||||
} fzl_vm_reader_t;
|
||||
|
||||
// Allocate binary file reader
|
||||
fzl_vm_reader_t *fzl_vm_mkreader(char *filepath);
|
||||
|
||||
// Read the file
|
||||
bool fzl_vm_read(fzl_vm_reader_t *reader);
|
||||
|
||||
// Get last error message from the reader
|
||||
char *fzl_vm_getreadererror();
|
||||
|
||||
// Free file reader
|
||||
void fzl_vm_freereader(fzl_vm_reader_t *reader);
|
||||
|
||||
#endif // FZL_VM_READER_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[512];
|
||||
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,24 @@
|
||||
#ifndef INTERNALS_VM_READER_H
|
||||
#define INTERNALS_VM_READER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <vm/fzl_vm_reader.h>
|
||||
|
||||
extern char internal_error[256];
|
||||
|
||||
bool vm_rd_char(FILE *fd, size_t count, char *outbuf);
|
||||
bool vm_rd_u16(FILE *fd, size_t count, uint16_t *outbuf);
|
||||
bool vm_rd_word(FILE *fd, size_t count, size_t *outbuf);
|
||||
|
||||
bool vm_rd_header(fzl_vm_reader_t *reader, FILE *fd);
|
||||
bool vm_rd_sections_sz(fzl_vm_reader_t *reader, FILE *fd);
|
||||
bool vm_rd_consts(fzl_vm_reader_t *reader, FILE *fd);
|
||||
bool vm_rd_extern_libs(fzl_vm_reader_t *reader, FILE *fd);
|
||||
bool vm_rd_extern_types(fzl_vm_reader_t *reader, FILE *fd);
|
||||
bool vm_rd_extern_fns(fzl_vm_reader_t *reader, FILE *fd);
|
||||
bool vm_rd_ops(fzl_vm_reader_t *reader, FILE *fd);
|
||||
|
||||
#endif // INTERNALS_VM_READER_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()
|
||||
@@ -1,6 +1,7 @@
|
||||
#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
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#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
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#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>
|
||||
|
||||
@@ -48,10 +50,19 @@ fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
|
||||
|
||||
if (fnctx && fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
{
|
||||
fzl_type_t rettype = an_toktotype(fnctx->fn->fn.type);
|
||||
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;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#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)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#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>
|
||||
@@ -130,7 +131,8 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (sym.node->type != FZL_NODETYPE_FUNCTION)
|
||||
if (sym.node->type != FZL_NODETYPE_FUNCTION &&
|
||||
sym.node->type != FZL_NODETYPE_EXTERN_FN)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "'%s' is not callable",
|
||||
callee_name);
|
||||
@@ -145,22 +147,24 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
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)
|
||||
size_t offset = is_method ? 1 : 0;
|
||||
size_t args_count = sym.node->type == FZL_NODETYPE_EXTERN_FN
|
||||
? sym.node->ext_fn.args_count
|
||||
: sym.node->fn.args_count;
|
||||
if ((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,
|
||||
tmp_classname, tmp_fnname, 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);
|
||||
callee_name, args_count, call_node->call.args_count - offset);
|
||||
}
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
@@ -173,11 +177,24 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t **args = sym.node->type == FZL_NODETYPE_EXTERN_FN
|
||||
? sym.node->ext_fn.args
|
||||
: sym.node->fn.args;
|
||||
for (size_t i = offset; i < sym.node->fn.args_count; i++)
|
||||
{
|
||||
fzl_node_t *arg = sym.node->fn.args[i];
|
||||
fzl_type_t req_type = an_toktotype(arg->var.type);
|
||||
fzl_node_t *arg = 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;
|
||||
|
||||
@@ -225,9 +242,32 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
}
|
||||
}
|
||||
|
||||
fzl_type_t type = an_toktotype(sym.node->fn.type);
|
||||
bool type_found = false;
|
||||
fzl_token_t *ret_tok = NULL;
|
||||
void *ret_name = NULL;
|
||||
|
||||
if (sym.node->type == FZL_NODETYPE_EXTERN_FN)
|
||||
{
|
||||
ret_tok = sym.node->ext_fn.type;
|
||||
ret_name = sym.node->ext_fn.name;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret_tok = sym.node->fn.type;
|
||||
ret_name = sym.node->fn.name;
|
||||
}
|
||||
|
||||
fzl_type_t type = an_toktotype(analyzer, ret_tok, &type_found);
|
||||
char *classname = sym.classname;
|
||||
|
||||
if (!type_found)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "symbol '%s' not found",
|
||||
(char *)ret_name);
|
||||
an_push_error(analyzer, ret_tok, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#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>
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
#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
|
||||
@@ -14,6 +16,38 @@ fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node)
|
||||
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;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#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
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#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
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#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>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#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>
|
||||
@@ -19,14 +20,23 @@ fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
|
||||
}
|
||||
|
||||
char *name = (char *)decl->var.identifier->token->value;
|
||||
fzl_type_t type = an_toktotype(decl->var.type);
|
||||
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,
|
||||
an_toktotype(decl->var.type), 0);
|
||||
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)
|
||||
|
||||
@@ -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};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#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>
|
||||
@@ -68,8 +69,19 @@ fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_type_t field_type = an_toktotype(field_node->var.type);
|
||||
char *classname = NULL;
|
||||
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;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#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>
|
||||
@@ -37,8 +38,18 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
fzl_type_t rettype = an_toktotype(fn_node->fn.type);
|
||||
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;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#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>
|
||||
@@ -48,9 +49,18 @@ init_analysis check_init(fzl_analyzer_t *analyzer, fzl_node_t *init)
|
||||
}
|
||||
else if (init->type == FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
char *name = init->var.identifier->token->value;
|
||||
fzl_type_t type = an_toktotype(init->var.type);
|
||||
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;
|
||||
|
||||
|
||||
@@ -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};
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
#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
|
||||
@@ -44,8 +46,18 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_type_t rettype = an_toktotype(fnctx->fn->fn.type);
|
||||
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;
|
||||
|
||||
|
||||
@@ -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};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#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)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#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
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/internals/analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#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)
|
||||
@@ -79,13 +84,44 @@ char *an_typetostr(fzl_type_t type, char *classname)
|
||||
}
|
||||
|
||||
// Convert type token to type
|
||||
fzl_type_t an_toktotype(fzl_token_t *tok)
|
||||
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)
|
||||
return FZL_TYPE_OBJECT;
|
||||
{
|
||||
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;
|
||||
|
||||
@@ -1,229 +0,0 @@
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_walker.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
ssize_t check_top;
|
||||
fzl_nodetype_t check_tree[256];
|
||||
|
||||
// Ghost walk AST tree (w/o adding opcodes) to check if
|
||||
// variable used in condition is changed
|
||||
void ghost_walk_changed(fzl_node_t *node, bool *changed, fzl_sym_t *sym)
|
||||
{
|
||||
if (!node || !changed || *changed)
|
||||
return;
|
||||
|
||||
check_tree[check_top++] = node->type;
|
||||
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_BREAK:
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
case FZL_NODETYPE_CONSTRUCT:
|
||||
case FZL_NODETYPE_CLASS:
|
||||
case FZL_NODETYPE_FUNCTION:
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_UNARY_PLUS:
|
||||
case FZL_NODETYPE_UNARY_MINUS:
|
||||
case FZL_NODETYPE_NOT:
|
||||
ghost_walk_changed(node->unary_op.operand, changed, sym);
|
||||
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:
|
||||
case FZL_NODETYPE_OR:
|
||||
case FZL_NODETYPE_AND:
|
||||
case FZL_NODETYPE_EQUAL:
|
||||
case FZL_NODETYPE_NEQUAL:
|
||||
case FZL_NODETYPE_LESS:
|
||||
case FZL_NODETYPE_LESS_EQUAL:
|
||||
case FZL_NODETYPE_GREATER:
|
||||
case FZL_NODETYPE_GREATER_EQUAL:
|
||||
case FZL_NODETYPE_ASSIGN:
|
||||
ghost_walk_changed(node->binary_op.left, changed, sym);
|
||||
ghost_walk_changed(node->binary_op.right, changed, sym);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FIELD:
|
||||
ghost_walk_changed(node->memb.parent, changed, sym);
|
||||
ghost_walk_changed(node->memb.field, changed, sym);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_RETURN:
|
||||
ghost_walk_changed(node->ret.value, changed, sym);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONDITIONAL:
|
||||
ghost_walk_changed(node->cond.condition, changed, sym);
|
||||
ghost_walk_changed(node->cond.true_block, changed, sym);
|
||||
ghost_walk_changed(node->cond.else_block, changed, sym);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LOOP:
|
||||
ghost_walk_changed(node->loop.cond, changed, sym);
|
||||
ghost_walk_changed(node->loop.step, changed, sym);
|
||||
ghost_walk_changed(node->loop.body, changed, sym);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
ghost_walk_changed(node->list.entries[i], changed, sym);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CALL:
|
||||
ghost_walk_changed(node->call.callee, changed, sym);
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
ghost_walk_changed(node->call.args[i], changed, sym);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
if (strcmp(node->token->value, sym->name) == 0)
|
||||
{
|
||||
*changed = true;
|
||||
}
|
||||
|
||||
if (*changed)
|
||||
{
|
||||
*changed = false;
|
||||
for (ssize_t i = check_top - 1; i >= 0; i--)
|
||||
{
|
||||
fzl_nodetype_t cur_t = check_tree[i];
|
||||
if (cur_t == FZL_NODETYPE_ADD || cur_t == FZL_NODETYPE_SUB ||
|
||||
cur_t == FZL_NODETYPE_MUL || cur_t == FZL_NODETYPE_DIV ||
|
||||
cur_t == FZL_NODETYPE_POW || cur_t == FZL_NODETYPE_MOD ||
|
||||
cur_t == FZL_NODETYPE_UNARY_MINUS)
|
||||
{
|
||||
*changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
check_tree[--check_top] = 0;
|
||||
}
|
||||
|
||||
// Whether value changed in loop body
|
||||
bool value_is_changed(fzl_node_t *node, fzl_sym_t *sym)
|
||||
{
|
||||
if (!node || !sym)
|
||||
return false;
|
||||
|
||||
bool changed = false;
|
||||
check_top = 0;
|
||||
memset(check_tree, 0, sizeof(check_tree));
|
||||
ghost_walk_changed(node, &changed, sym);
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// Try to find symbol in loop condition node
|
||||
bool retrieve_cond_sym(fzl_walker_t *walker, fzl_node_t *node, fzl_sym_t *sym)
|
||||
{
|
||||
if (!walker || !node)
|
||||
return false;
|
||||
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_BREAK:
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
case FZL_NODETYPE_CALL:
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
case FZL_NODETYPE_CONDITIONAL:
|
||||
case FZL_NODETYPE_FUNCTION:
|
||||
case FZL_NODETYPE_RETURN:
|
||||
case FZL_NODETYPE_CLASS:
|
||||
case FZL_NODETYPE_FIELD:
|
||||
case FZL_NODETYPE_CONSTRUCT:
|
||||
case FZL_NODETYPE_LOOP:
|
||||
case FZL_NODETYPE_ASSIGN:
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
return false;
|
||||
|
||||
case FZL_NODETYPE_UNARY_MINUS:
|
||||
case FZL_NODETYPE_UNARY_PLUS:
|
||||
case FZL_NODETYPE_NOT:
|
||||
return retrieve_cond_sym(walker, node->unary_op.operand, sym);
|
||||
|
||||
case FZL_NODETYPE_ADD:
|
||||
case FZL_NODETYPE_SUB:
|
||||
case FZL_NODETYPE_MUL:
|
||||
case FZL_NODETYPE_DIV:
|
||||
case FZL_NODETYPE_POW:
|
||||
case FZL_NODETYPE_MOD:
|
||||
case FZL_NODETYPE_AND:
|
||||
case FZL_NODETYPE_OR:
|
||||
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:
|
||||
if (retrieve_cond_sym(walker, node->binary_op.left, sym))
|
||||
return true;
|
||||
if (retrieve_cond_sym(walker, node->binary_op.right, sym))
|
||||
return true;
|
||||
return false;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
{
|
||||
bool exists;
|
||||
for (ssize_t i = walker->sym_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((exists = fzl_symt_get(walker->syms[i], (char *)node->token->value,
|
||||
sym)))
|
||||
break;
|
||||
}
|
||||
return exists;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for infinite loop
|
||||
bool is_infinite_loop(fzl_walker_t *walker, fzl_node_t *node, size_t start_ip,
|
||||
size_t end_ip)
|
||||
{
|
||||
if (!walker)
|
||||
return false;
|
||||
|
||||
end_ip = (end_ip >= walker->op_count) ? (walker->op_count - 1) : end_ip;
|
||||
|
||||
fzl_sym_t sym;
|
||||
bool sym_exists = retrieve_cond_sym(walker, node->loop.cond, &sym);
|
||||
if (sym_exists)
|
||||
{
|
||||
if (sym.type >= FZL_TYPE_INT8)
|
||||
{
|
||||
bool value_changed = value_is_changed(node, &sym);
|
||||
if (value_changed)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// At least has jump outside
|
||||
for (size_t i = start_ip; i <= end_ip; i++)
|
||||
{
|
||||
if (walker->ops[i] != FZL_OPCODE_JMP)
|
||||
continue;
|
||||
|
||||
size_t jump_ip = walker->ops[++i];
|
||||
if (jump_ip >= end_ip)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#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>
|
||||
|
||||
@@ -27,10 +27,77 @@ 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);
|
||||
@@ -172,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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +249,7 @@ void fzl_debug_print_ast(fzl_node_t *root)
|
||||
fzl_node_t *curr = root;
|
||||
while (curr != NULL)
|
||||
{
|
||||
print_node(curr, 3);
|
||||
print_node(curr, 2);
|
||||
curr = curr->next;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.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>
|
||||
#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->ops);
|
||||
free(gen);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return gen;
|
||||
}
|
||||
|
||||
bool fzl_gen_run(fzl_gen_t *gen)
|
||||
{
|
||||
if (!gen)
|
||||
return false;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_MK_ENVTABLE);
|
||||
|
||||
fzl_node_t *current = gen->root;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (gen_common(gen, current).error)
|
||||
return false;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
gen_op(gen, FZL_OPCODE_RM_ENVTABLE);
|
||||
|
||||
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);
|
||||
|
||||
while (gen->ctx != NULL)
|
||||
gen_fin_ctx(gen);
|
||||
|
||||
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]);
|
||||
|
||||
for (size_t i = 0; i < gen->extern_types_count; i++)
|
||||
{
|
||||
fzl_extern_type_t *type = &gen->extern_types[i];
|
||||
if (type->kind == FZL_EXTERN_KIND_STRUCT)
|
||||
if (type->structure.fields)
|
||||
free(type->structure.fields);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < gen->extern_fns_count; i++)
|
||||
{
|
||||
fzl_extern_fn_t *fn = &gen->extern_fns[i];
|
||||
if (fn->args)
|
||||
free(fn->args);
|
||||
}
|
||||
|
||||
free(gen);
|
||||
}
|
||||
+48
-1247
File diff suppressed because it is too large
Load Diff
+49
-259
@@ -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,308 +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, "for") == 0)
|
||||
tok = fzl_mktoken(FZL_TOKTYPE_FOR, l.line, l.col, value, size, l.filename);
|
||||
else if (strcmp(value, "while") == 0)
|
||||
tok =
|
||||
fzl_mktoken(FZL_TOKTYPE_WHILE, l.line, l.col, value, size, l.filename);
|
||||
else if (strcmp(value, "break") == 0)
|
||||
tok =
|
||||
fzl_mktoken(FZL_TOKTYPE_BREAK, 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;
|
||||
@@ -371,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->store_value)
|
||||
if (!tok)
|
||||
return;
|
||||
|
||||
if (tok->store_value && tok->value)
|
||||
free(tok->value);
|
||||
|
||||
free(tok);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+55
-23
@@ -1,4 +1,6 @@
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_writer.h>
|
||||
#include <compiler/internals/writer.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -11,52 +13,82 @@ char wrlast_err[256];
|
||||
fzl_writer_t *fzl_mkwriter(const char *filepath)
|
||||
{
|
||||
if (strlen(filepath) < 1)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
|
||||
fzl_writer_t *writer = malloc(sizeof(fzl_writer_t));
|
||||
if (!writer)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
|
||||
writer->filepath = filepath;
|
||||
|
||||
return writer;
|
||||
}
|
||||
|
||||
int fzl_write(fzl_writer_t *writer, fzl_walker_t *walker)
|
||||
int fzl_write(fzl_writer_t *writer, fzl_gen_t *gen)
|
||||
{
|
||||
if (!writer || !walker)
|
||||
if (!writer || !gen)
|
||||
return -1;
|
||||
|
||||
FILE *fp = fopen(writer->filepath, "wb");
|
||||
if (!fp)
|
||||
FILE *fd = fopen(writer->filepath, "wb");
|
||||
if (!fd)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err), "failed to open '%s' to write",
|
||||
writer->filepath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char hdr[5] = {'F', 'Z', 'L', 0x0, 0x1};
|
||||
fwrite(hdr, sizeof(char), 5, fp);
|
||||
fwrite(&walker->consts->size, sizeof(size_t), 1, fp);
|
||||
fwrite(&walker->op_count, sizeof(size_t), 1, fp);
|
||||
constexpr char hdr[5] = {'F', 'Z', 'L', 0x0, 0x2};
|
||||
fwrite(hdr, sizeof(char), 5, fd);
|
||||
fwrite(&gen->consts->size, sizeof(uint16_t), 1, fd);
|
||||
fwrite(&gen->extern_libs_count, sizeof(uint16_t), 1, fd);
|
||||
fwrite(&gen->extern_types_count, sizeof(uint16_t), 1, fd);
|
||||
fwrite(&gen->extern_fns_count, sizeof(uint16_t), 1, fd);
|
||||
fwrite(&gen->ops_count, sizeof(size_t), 1, fd);
|
||||
|
||||
for (size_t i = 0; i < walker->consts->size; i++)
|
||||
bool result = wr_consts(fd, gen->consts);
|
||||
if (!result)
|
||||
{
|
||||
fzl_const_t c = walker->consts->array[i];
|
||||
fwrite(&c.type, sizeof(fzl_consttype_t), 1, fp);
|
||||
|
||||
if (c.type == FZL_CONSTTYPE_STRING)
|
||||
{
|
||||
size_t strlen = c.as.string.length;
|
||||
fwrite(&c.as.string.length, sizeof(size_t), 1, fp);
|
||||
fwrite(c.as.string.chars, sizeof(char), strlen, fp);
|
||||
}
|
||||
snprintf(wrlast_err, sizeof(wrlast_err), "failed to write constants table");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (walker->op_count > 0)
|
||||
fwrite(walker->ops, sizeof(uint64_t), walker->op_count, fp);
|
||||
result = wr_extern_libs(fd, gen->extern_libs, gen->extern_libs_count);
|
||||
if (!result)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err),
|
||||
"failed to write external lib names");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
result = wr_extern_types(fd, gen->extern_types, gen->extern_types_count);
|
||||
if (!result)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err),
|
||||
"failed to write extern lib types");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = wr_extern_fns(fd, gen->extern_fns, gen->extern_fns_count);
|
||||
if (!result)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err),
|
||||
"failed to write extern lib functions");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = wr_ops(fd, gen->ops, gen->ops_count);
|
||||
if (!result)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err), "failed to write opcodes");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#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)
|
||||
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));
|
||||
ctx->type = type;
|
||||
|
||||
if (ctx->type != FZL_GEN_CTX_TYPE_CLASS)
|
||||
{
|
||||
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->level = level;
|
||||
ctx->parent = gen->ctx;
|
||||
|
||||
if (type == FZL_GEN_CTX_TYPE_FUNCTION)
|
||||
{
|
||||
if (node->type == FZL_NODETYPE_FUNCTION ||
|
||||
node->type == FZL_NODETYPE_EXTERN_FN)
|
||||
ctx->function = node;
|
||||
else
|
||||
{
|
||||
fzl_freesymt(gen->syms[--gen->syms_top]);
|
||||
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;
|
||||
|
||||
if (ctx->type != FZL_GEN_CTX_TYPE_CLASS)
|
||||
{
|
||||
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,31 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char alias_typecn[256];
|
||||
|
||||
// Type aliasing
|
||||
gen_res_t gen_alias(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_type_t type;
|
||||
memset(alias_typecn, 0, sizeof(alias_typecn));
|
||||
|
||||
bool resolved = gen_resolve_type(gen, node->token, &type, alias_typecn);
|
||||
if (!resolved)
|
||||
return gen_error;
|
||||
|
||||
ssize_t slot = gen_resolve_extern_type(gen, alias_typecn);
|
||||
if (slot == -1)
|
||||
type_buf.alias_target =
|
||||
(fzl_extern_type_ref_t){.is_primitive = true, .primitive = type};
|
||||
else
|
||||
type_buf.alias_target =
|
||||
(fzl_extern_type_ref_t){.is_primitive = false, .type_slot = slot};
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <string.h>
|
||||
|
||||
// Assignment to declaration
|
||||
gen_res_t assign_declaration(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
gen_res_t rres = gen_common(gen, node->binary_op.right);
|
||||
if (rres.error)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t lres = gen_common(gen, node->binary_op.left);
|
||||
if (lres.error)
|
||||
return gen_error;
|
||||
|
||||
if (!gen_is_types_compatible(rres.type, lres.type, rres.classname,
|
||||
lres.classname))
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_WRITE);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = rres.type, .classname = rres.classname};
|
||||
}
|
||||
|
||||
// Assignment to identifier
|
||||
gen_res_t assign_id(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
gen_res_t rres = gen_common(gen, node->binary_op.right);
|
||||
if (rres.error)
|
||||
return gen_error;
|
||||
|
||||
char *name = (char *)node->binary_op.left->token->value;
|
||||
fzl_sym_t sym;
|
||||
bool found = false;
|
||||
ssize_t i = gen->syms_top - 1;
|
||||
for (; i >= 0; i--)
|
||||
if ((found = fzl_symt_get(gen->syms[i], name, &sym)))
|
||||
break;
|
||||
|
||||
if (!found)
|
||||
return gen_error;
|
||||
|
||||
if (!gen_is_types_compatible(sym.type, rres.type, sym.classname,
|
||||
rres.classname))
|
||||
return gen_error;
|
||||
|
||||
if (!sym.slot_set)
|
||||
return gen_error;
|
||||
|
||||
size_t depth = gen->syms_top - 1 - i;
|
||||
gen_op(gen, FZL_OPCODE_STORE_LOCAL);
|
||||
gen_word(gen, depth);
|
||||
gen_word(gen, sym.slot);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = rres.type, .classname = rres.classname};
|
||||
}
|
||||
|
||||
// Assignment to field
|
||||
gen_res_t assign_field(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
gen_res_t rres = gen_common(gen, node->binary_op.right);
|
||||
if (rres.error)
|
||||
return gen_error;
|
||||
|
||||
fzl_node_t *field_node = node->binary_op.left;
|
||||
gen_res_t obj_res = gen_common(gen, field_node->memb.parent);
|
||||
if (obj_res.error)
|
||||
return gen_error;
|
||||
|
||||
fzl_ctable_t *ct = NULL;
|
||||
for (size_t i = 0; i < gen->classes_count; i++)
|
||||
{
|
||||
if (strcmp(obj_res.classname, gen->classes[i]->name) == 0)
|
||||
{
|
||||
ct = gen->classes[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ct)
|
||||
return gen_error;
|
||||
|
||||
fzl_ctable_field_t *ctf = NULL;
|
||||
for (size_t i = 0; i < ct->fields_count; i++)
|
||||
{
|
||||
if (strcmp(field_node->memb.field->token->value, ct->fields[i].name) == 0)
|
||||
{
|
||||
ctf = &ct->fields[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ctf)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_SET_FIELD);
|
||||
gen_word(gen, ctf->slot_index);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = rres.type, .classname = rres.classname};
|
||||
}
|
||||
|
||||
// Assignment
|
||||
gen_res_t gen_assign(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
if (node->binary_op.left->type == FZL_NODETYPE_DECLARATION)
|
||||
return assign_declaration(gen, node);
|
||||
|
||||
if (node->binary_op.left->type == FZL_NODETYPE_IDENTIFIER)
|
||||
return assign_id(gen, node);
|
||||
|
||||
if (node->binary_op.left->type == FZL_NODETYPE_FIELD)
|
||||
return assign_field(gen, node);
|
||||
|
||||
return gen_error;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
|
||||
// Block definition
|
||||
gen_res_t gen_block(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_gen_ctx_t *ctx = gen_ctx(gen, FZL_GEN_CTX_TYPE_UNECCESSARY, node);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_MK_ENVTABLE);
|
||||
if (node->list.entry_count > 0)
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
if (gen_common(gen, node->list.entries[i]).error)
|
||||
return gen_error;
|
||||
|
||||
ctx = gen_fin_ctx(gen);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_RM_ENVTABLE);
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Break keyword
|
||||
gen_res_t gen_break(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_gen_ctx_t *ctx = gen->ctx;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
if (ctx && ctx->type == FZL_GEN_CTX_TYPE_LOOP)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_GEN_CTX_TYPE_LOOP)
|
||||
return gen_error;
|
||||
|
||||
for (size_t i = gen->syms_top - 1; i > ctx->level + 1; i--)
|
||||
gen_op(gen, FZL_OPCODE_RM_ENVTABLE);
|
||||
|
||||
gen_op(gen, FZL_OPCODE_JMP);
|
||||
ctx->loop.breaks[ctx->loop.breaks_count++] = gen->ops_count;
|
||||
gen_word(gen, 0);
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
#include <common/fzl_builtins.h>
|
||||
#include <common/fzl_extern.h>
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Call definition
|
||||
// NOTE: split by call type (builtin/declared/extern)
|
||||
gen_res_t gen_call(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
// `printline(x)` builtin function
|
||||
if (strcmp(node->token->value, "printline") == 0)
|
||||
{
|
||||
if (node->call.args_count > 0)
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
if (gen_common(gen, node->call.args[i]).error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_CALL_BUILTIN);
|
||||
gen_word(gen, FZL_BUILTIN_PRINTLINE);
|
||||
if (node->call.args_count < 1)
|
||||
return gen_error;
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
|
||||
// `exit(x?)` builtin function
|
||||
if (strcmp(node->token->value, "exit") == 0)
|
||||
{
|
||||
if (node->call.args_count > 0)
|
||||
{
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
if (gen_common(gen, node->call.args[i]).error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_CALL_BUILTIN);
|
||||
gen_word(gen, FZL_BUILTIN_EXIT);
|
||||
if (node->call.args_count < 1)
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
|
||||
if (node->call.callee->type == FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
if (node->call.args_count > 0)
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
if (gen_common(gen, node->call.args[i]).error)
|
||||
return gen_error;
|
||||
|
||||
char *name = (char *)node->token->value;
|
||||
fzl_sym_t sym;
|
||||
bool found = gen_resolve_name(gen, name, &sym);
|
||||
if (!found)
|
||||
return gen_error;
|
||||
|
||||
if (sym.node->type == FZL_NODETYPE_EXTERN_FN)
|
||||
{
|
||||
size_t fn_n = 0;
|
||||
fzl_extern_fn_t *fn = NULL;
|
||||
for (size_t i = 0; i < gen->extern_fns_count; i++)
|
||||
{
|
||||
if (strcmp(gen->extern_fns[i].used_name, name) == 0)
|
||||
{
|
||||
fn = &gen->extern_fns[i];
|
||||
fn_n = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!fn)
|
||||
return gen_error;
|
||||
|
||||
fzl_type_t rettype;
|
||||
char *rettypecn = NULL;
|
||||
|
||||
if (!fn->has_return)
|
||||
{
|
||||
rettype = FZL_TYPE_NULL;
|
||||
}
|
||||
else if (fn->ret_type.is_primitive)
|
||||
{
|
||||
rettype = fn->ret_type.primitive;
|
||||
}
|
||||
else
|
||||
{
|
||||
fzl_extern_type_t *type = &gen->extern_types[fn->ret_type.type_slot];
|
||||
switch (type->kind)
|
||||
{
|
||||
case FZL_EXTERN_KIND_REF:
|
||||
rettype = FZL_TYPE_HANDLE;
|
||||
rettypecn = type->name;
|
||||
break;
|
||||
|
||||
case FZL_EXTERN_KIND_STRUCT:
|
||||
rettype = FZL_TYPE_STRUCT;
|
||||
rettypecn = type->name;
|
||||
break;
|
||||
|
||||
case FZL_EXTERN_KIND_HANDLE:
|
||||
rettype = FZL_TYPE_HANDLE;
|
||||
rettypecn = type->name;
|
||||
break;
|
||||
|
||||
case FZL_EXTERN_KIND_ALIAS:
|
||||
if (type->alias_target.is_primitive)
|
||||
{
|
||||
rettype = type->alias_target.primitive;
|
||||
}
|
||||
else
|
||||
{
|
||||
fzl_extern_type_t *aliased =
|
||||
&gen->extern_types[type->alias_target.type_slot];
|
||||
switch (aliased->kind)
|
||||
{
|
||||
case FZL_EXTERN_KIND_ALIAS:
|
||||
return gen_error;
|
||||
|
||||
case FZL_EXTERN_KIND_HANDLE:
|
||||
rettype = FZL_TYPE_HANDLE;
|
||||
rettypecn = aliased->name;
|
||||
break;
|
||||
|
||||
case FZL_EXTERN_KIND_STRUCT:
|
||||
rettype = FZL_TYPE_STRUCT;
|
||||
rettypecn = aliased->name;
|
||||
break;
|
||||
|
||||
case FZL_EXTERN_KIND_REF:
|
||||
rettype = FZL_TYPE_HANDLE;
|
||||
rettypecn = aliased->name;
|
||||
break;
|
||||
|
||||
default:
|
||||
return gen_error;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return gen_error;
|
||||
}
|
||||
}
|
||||
|
||||
gen_op(gen, FZL_OPCODE_CALL_EXTERN);
|
||||
gen_word(gen, fn_n);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = rettype, .classname = rettypecn};
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!sym.ip)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_CALL);
|
||||
gen_word(gen, sym.ip);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = sym.type, .classname = sym.classname};
|
||||
}
|
||||
}
|
||||
|
||||
if (node->call.callee->type == FZL_NODETYPE_FIELD)
|
||||
{
|
||||
fzl_node_t *parent_node = node->call.callee->memb.parent;
|
||||
fzl_node_t *method_node = node->call.callee->memb.field;
|
||||
|
||||
gen_res_t parent_res = gen_common(gen, parent_node);
|
||||
if (parent_res.error)
|
||||
return gen_error;
|
||||
|
||||
if (parent_res.type != FZL_TYPE_OBJECT || !parent_res.classname)
|
||||
return gen_error;
|
||||
|
||||
char *classname = parent_res.classname;
|
||||
bool class_exists = false;
|
||||
for (size_t i = 0; i < gen->classes_count; i++)
|
||||
{
|
||||
if (strcmp(classname, gen->classes[i]->name) == 0)
|
||||
{
|
||||
class_exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!class_exists)
|
||||
return gen_error;
|
||||
|
||||
size_t classlen = strlen(classname);
|
||||
size_t fnlen = strlen(method_node->token->value);
|
||||
char *methodname = malloc(classlen + fnlen + 2);
|
||||
strcpy(methodname, classname);
|
||||
strcpy((methodname + classlen + 1), method_node->token->value);
|
||||
methodname[classlen] = '_';
|
||||
methodname[classlen + fnlen + 1] = 0;
|
||||
|
||||
fzl_sym_t fn_sym;
|
||||
bool found_method = false;
|
||||
for (ssize_t i = gen->syms_top - 1; i >= 0; i--)
|
||||
if ((found_method = fzl_symt_get(gen->syms[i], methodname, &fn_sym)))
|
||||
break;
|
||||
|
||||
free(methodname);
|
||||
if (!found_method)
|
||||
return gen_error;
|
||||
|
||||
if (node->call.args_count > 0)
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
if (gen_common(gen, node->call.args[i]).error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_CALL);
|
||||
gen_word(gen, fn_sym.ip);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = fn_sym.type, .classname = fn_sym.classname};
|
||||
}
|
||||
|
||||
return gen_error;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char class_typecn[256];
|
||||
|
||||
// Class definition
|
||||
gen_res_t gen_class(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_gen_ctx_t *ctx = gen_ctx(gen, FZL_GEN_CTX_TYPE_CLASS, node);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
gen->classes[gen->classes_count] = fzl_mkctable(node->class.name->value);
|
||||
fzl_ctable_t *class = gen->classes[gen->classes_count++];
|
||||
class->name = (char *)node->class.name->value;
|
||||
|
||||
for (size_t i = 0; i < node->class.fields_count; i++)
|
||||
{
|
||||
if (class->fields_remainder <= class->fields_count)
|
||||
{
|
||||
class->fields_remainder *= 2;
|
||||
class->fields = realloc(class->fields, class->fields_remainder *
|
||||
sizeof(fzl_ctable_field_t));
|
||||
}
|
||||
|
||||
fzl_type_t type;
|
||||
memset(class_typecn, 0, sizeof(class_typecn));
|
||||
|
||||
bool resolved = gen_resolve_type(gen, node->class.fields[i]->var.type,
|
||||
&type, class_typecn);
|
||||
if (!resolved)
|
||||
return gen_error;
|
||||
|
||||
class->fields[class->fields_count] = (fzl_ctable_field_t){
|
||||
.name = node->class.fields[i]->var.identifier->token->value,
|
||||
.type = type,
|
||||
.slot_index = class->fields_count++};
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < node->class.methods_count; i++)
|
||||
{
|
||||
fzl_node_t *fn = node->class.methods[i];
|
||||
|
||||
size_t classname_len = strlen(class->name);
|
||||
size_t fnname_len = strlen(fn->fn.name->value);
|
||||
char *method_name = malloc(classname_len + fnname_len + 2);
|
||||
|
||||
strcpy(method_name, class->name);
|
||||
method_name[classname_len] = '_';
|
||||
strcpy((method_name + classname_len + 1), fn->fn.name->value);
|
||||
method_name[classname_len + fnname_len + 1] = 0;
|
||||
|
||||
if (fn->fn.name->value)
|
||||
free(fn->fn.name->value);
|
||||
|
||||
fn->fn.name->value = method_name;
|
||||
if (gen_common(gen, fn).error)
|
||||
{
|
||||
free(method_name);
|
||||
fn->fn.name->value = NULL;
|
||||
return gen_error;
|
||||
}
|
||||
}
|
||||
|
||||
ctx = gen_fin_ctx(gen);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef gen_res_t (*gen_handle_fn)(fzl_gen_t *, fzl_node_t *);
|
||||
|
||||
char *null_name = "<null>";
|
||||
|
||||
gen_res_t gen_error = (gen_res_t){.error = true};
|
||||
gen_res_t gen_ok_null =
|
||||
(gen_res_t){.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
|
||||
// Common generator entry point
|
||||
gen_res_t gen_common(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_handle_fn res = NULL;
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_ASSIGN:
|
||||
res = gen_assign;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
res = gen_declaration;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_NOT:
|
||||
res = gen_unary_logical;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_EQUAL:
|
||||
case FZL_NODETYPE_NEQUAL:
|
||||
case FZL_NODETYPE_LESS:
|
||||
case FZL_NODETYPE_LESS_EQUAL:
|
||||
case FZL_NODETYPE_GREATER:
|
||||
case FZL_NODETYPE_GREATER_EQUAL:
|
||||
res = gen_compare;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_OR:
|
||||
case FZL_NODETYPE_AND:
|
||||
res = gen_logical;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_UNARY_PLUS:
|
||||
case FZL_NODETYPE_UNARY_MINUS:
|
||||
res = gen_unary_math;
|
||||
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 = gen_math;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BREAK:
|
||||
res = gen_break;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LOOP:
|
||||
res = gen_loop;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONSTRUCT:
|
||||
res = gen_construct;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FIELD:
|
||||
res = gen_field;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CLASS:
|
||||
res = gen_class;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CALL:
|
||||
res = gen_call;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_RETURN:
|
||||
res = gen_return;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FUNCTION:
|
||||
res = gen_fn;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONDITIONAL:
|
||||
res = gen_condition;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
res = gen_identifier;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FMT_STRING:
|
||||
res = gen_fmtstr;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
res = gen_literal;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
res = gen_block;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_EXTERN:
|
||||
res = gen_extern;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_TYPEDEF:
|
||||
res = gen_typedef;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_HANDLE:
|
||||
res = gen_handle;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_ALIAS:
|
||||
res = gen_alias;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_STRUCT:
|
||||
res = gen_struct;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_REFERENCE:
|
||||
res = gen_reference;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_EXTERN_FN:
|
||||
res = gen_externfn;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!res)
|
||||
{
|
||||
printf("Bruh, you, suka, go fix the generator blyat (%s:%d)\n", __FILE__,
|
||||
__LINE__);
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
return res(gen, node);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Comparison operations
|
||||
gen_res_t gen_compare(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t left = gen_common(gen, node->binary_op.left);
|
||||
if (left.error)
|
||||
return left;
|
||||
|
||||
gen_res_t right = gen_common(gen, node->binary_op.right);
|
||||
if (right.error)
|
||||
return right;
|
||||
|
||||
if (!gen_is_types_compatible(left.type, right.type, left.classname,
|
||||
right.classname))
|
||||
return gen_error;
|
||||
|
||||
if (left.type == FZL_TYPE_STRING && right.type == FZL_TYPE_STRING)
|
||||
{
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_EQUAL:
|
||||
gen_op(gen, FZL_OPCODE_CMP_EQ_STR);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_NEQUAL:
|
||||
gen_op(gen, FZL_OPCODE_CMP_NEQ_STR);
|
||||
break;
|
||||
|
||||
default:
|
||||
return gen_error;
|
||||
}
|
||||
}
|
||||
else if (left.type == FZL_TYPE_OBJECT && right.type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_EQUAL:
|
||||
gen_op(gen, FZL_OPCODE_CMP_EQ_OBJ);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_NEQUAL:
|
||||
gen_op(gen, FZL_OPCODE_CMP_NEQ_OBJ);
|
||||
break;
|
||||
|
||||
default:
|
||||
return gen_error;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_EQUAL:
|
||||
gen_op(gen, FZL_OPCODE_CMP_EQ);
|
||||
break;
|
||||
case FZL_NODETYPE_NEQUAL:
|
||||
gen_op(gen, FZL_OPCODE_CMP_NEQ);
|
||||
break;
|
||||
case FZL_NODETYPE_LESS:
|
||||
gen_op(gen, FZL_OPCODE_CMP_LT);
|
||||
break;
|
||||
case FZL_NODETYPE_LESS_EQUAL:
|
||||
gen_op(gen, FZL_OPCODE_CMP_LTEQ);
|
||||
break;
|
||||
case FZL_NODETYPE_GREATER:
|
||||
gen_op(gen, FZL_OPCODE_CMP_GT);
|
||||
break;
|
||||
case FZL_NODETYPE_GREATER_EQUAL:
|
||||
gen_op(gen, FZL_OPCODE_CMP_GTEQ);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (gen_res_t){.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <compiler/internals/gen.h>
|
||||
|
||||
// Conditional operation
|
||||
gen_res_t gen_condition(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t cres = gen_common(gen, node->cond.condition);
|
||||
if (cres.error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_JZ);
|
||||
|
||||
size_t jz_if = gen->ops_count;
|
||||
gen_word(gen, (uint64_t)0);
|
||||
|
||||
gen_res_t tblock_res = gen_common(gen, node->cond.true_block);
|
||||
if (tblock_res.error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_JMP);
|
||||
size_t jmp_else = gen->ops_count;
|
||||
gen_word(gen, (uint64_t)0);
|
||||
|
||||
gen->ops[jz_if] = gen->ops_count;
|
||||
|
||||
if (node->cond.else_block)
|
||||
{
|
||||
gen_res_t fblock_res = gen_common(gen, node->cond.else_block);
|
||||
if (fblock_res.error)
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
gen->ops[jmp_else] = gen->ops_count;
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// Object constructing
|
||||
gen_res_t gen_construct(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_node_t *classname = node->construct.name;
|
||||
fzl_ctable_t *ct = NULL;
|
||||
for (size_t i = 0; i < gen->classes_count; i++)
|
||||
{
|
||||
if (strcmp((char *)classname->token->value, gen->classes[i]->name) == 0)
|
||||
{
|
||||
ct = gen->classes[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ct)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_ALLOC_OBJECT);
|
||||
gen_word(gen, ct->fields_count);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = FZL_TYPE_OBJECT, .classname = ct->name};
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
char dec_typecn[256];
|
||||
|
||||
// Variable (or argument) declaration
|
||||
gen_res_t gen_declaration(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
char *name = (char *)node->var.identifier->token->value;
|
||||
fzl_type_t type;
|
||||
memset(dec_typecn, 0, sizeof(dec_typecn));
|
||||
|
||||
bool resolved = gen_resolve_type(gen, node->var.type, &type, dec_typecn);
|
||||
if (!resolved)
|
||||
return gen_error;
|
||||
|
||||
fzl_ctable_t *ct = NULL;
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
for (size_t i = 0; i < gen->classes_count; i++)
|
||||
{
|
||||
if (strcmp(node->var.type->value, gen->classes[i]->name) == 0)
|
||||
{
|
||||
ct = gen->classes[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fzl_symtable_res_t res =
|
||||
fzl_symt_put(gen->syms[gen->syms_top - 1], node, name, type, 0);
|
||||
|
||||
if (ct)
|
||||
fzl_symt_setobjclass(gen->syms[gen->syms_top - 1], name, ct->name);
|
||||
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
return gen_error;
|
||||
|
||||
if (fzl_symt_setslot(gen->syms[gen->syms_top - 1], name) < 0)
|
||||
return gen_error;
|
||||
|
||||
fzl_token_t *id_tok = node->var.identifier->token;
|
||||
size_t const_n =
|
||||
gen_add_const(gen, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = id_tok->value,
|
||||
.as.string.length = id_tok->value_sz});
|
||||
|
||||
gen_op(gen, FZL_OPCODE_LOADCONST);
|
||||
gen_word(gen, const_n);
|
||||
|
||||
gen_op(gen, FZL_OPCODE_STORE);
|
||||
gen_op(gen, FZL_OPCODE_LOADCONST);
|
||||
gen_word(gen, const_n);
|
||||
|
||||
return (gen_res_t){.error = false, .type = type, .classname = dec_typecn};
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <common/fzl_extern.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Extern block
|
||||
gen_res_t gen_extern(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen->extern_libs[gen->extern_libs_count++] =
|
||||
(fzl_extern_lib_t){.name = (char *)node->ext.libname};
|
||||
|
||||
for (size_t i = 0; i < node->ext.typedefs_count; i++)
|
||||
if (gen_common(gen, node->ext.typedefs[i]).error)
|
||||
return gen_error;
|
||||
|
||||
for (size_t i = 0; i < node->ext.fns_count; i++)
|
||||
if (gen_common(gen, node->ext.fns[i]).error)
|
||||
return gen_error;
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
#include <common/fzl_extern.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char ex_argcn[256];
|
||||
char ex_retcn[256];
|
||||
|
||||
// External function definition
|
||||
gen_res_t gen_externfn(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_gen_ctx_t *ctx = gen_ctx(gen, FZL_GEN_CTX_TYPE_FUNCTION, node);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
size_t args_remainder = 2;
|
||||
size_t args_count = 0;
|
||||
fzl_extern_arg_t *args = malloc(args_remainder * sizeof(fzl_extern_arg_t));
|
||||
if (!args)
|
||||
return gen_error;
|
||||
|
||||
for (size_t i = 0; i < node->ext_fn.args_count; i++)
|
||||
{
|
||||
fzl_node_t *argnode = node->ext_fn.args[i];
|
||||
if (args_count >= args_remainder)
|
||||
{
|
||||
args_remainder *= 2;
|
||||
fzl_extern_arg_t *newargs =
|
||||
realloc(args, args_remainder * sizeof(fzl_extern_arg_t));
|
||||
if (!newargs)
|
||||
{
|
||||
free(args);
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
args = newargs;
|
||||
}
|
||||
|
||||
if (argnode->type != FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
free(args);
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
fzl_extern_arg_t *arg = &args[args_count++];
|
||||
|
||||
fzl_type_t argtype;
|
||||
memset(ex_argcn, 0, sizeof(ex_argcn));
|
||||
|
||||
bool resolved =
|
||||
gen_resolve_type(gen, argnode->var.type, &argtype, ex_argcn);
|
||||
if (!resolved)
|
||||
{
|
||||
free(args);
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
ssize_t slot = gen_resolve_extern_type(gen, ex_argcn);
|
||||
if (slot == -1)
|
||||
arg->type =
|
||||
(fzl_extern_type_ref_t){.is_primitive = true, .primitive = argtype};
|
||||
else
|
||||
arg->type =
|
||||
(fzl_extern_type_ref_t){.is_primitive = false, .type_slot = slot};
|
||||
|
||||
arg->name = (char *)argnode->var.identifier->token->value;
|
||||
arg->is_out = argnode->var.is_out;
|
||||
}
|
||||
|
||||
ctx = gen_fin_ctx(gen);
|
||||
if (!ctx)
|
||||
{
|
||||
free(args);
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
fzl_type_t ret;
|
||||
memset(ex_retcn, 0, sizeof(ex_retcn));
|
||||
|
||||
bool resolved = gen_resolve_type(gen, node->ext_fn.type, &ret, ex_retcn);
|
||||
if (!resolved)
|
||||
{
|
||||
free(args);
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
fzl_symtable_res_t res =
|
||||
fzl_symt_put(gen->syms[0], node, node->ext_fn.name, ret, 0);
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
free(args);
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
if (ret == FZL_TYPE_OBJECT || ret == FZL_TYPE_STRUCT ||
|
||||
ret == FZL_TYPE_HANDLE)
|
||||
{
|
||||
res = fzl_symt_setobjclass(gen->syms[0], node->ext_fn.name, ex_retcn);
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
free(args);
|
||||
return gen_error;
|
||||
}
|
||||
}
|
||||
|
||||
fzl_extern_fn_t *result = &gen->extern_fns[gen->extern_fns_count++];
|
||||
|
||||
ssize_t slot = gen_resolve_extern_type(gen, ex_retcn);
|
||||
if (slot == -1)
|
||||
result->ret_type =
|
||||
(fzl_extern_type_ref_t){.is_primitive = true, .primitive = ret};
|
||||
else
|
||||
result->ret_type =
|
||||
(fzl_extern_type_ref_t){.is_primitive = false, .type_slot = slot};
|
||||
|
||||
result->lib_slot = gen->extern_libs_count - 1;
|
||||
result->args = args;
|
||||
result->args_count = args_count;
|
||||
result->has_return = ret != FZL_TYPE_NULL;
|
||||
result->used_name = node->ext_fn.name;
|
||||
result->symbol_name = node->ext_fn.original_name ? node->ext_fn.original_name
|
||||
: node->ext_fn.name;
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// Field access
|
||||
gen_res_t gen_field(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t parent_res = gen_common(gen, node->memb.parent);
|
||||
if (parent_res.error)
|
||||
return gen_error;
|
||||
|
||||
fzl_ctable_t *ct = NULL;
|
||||
for (size_t i = 0; i < gen->classes_count; i++)
|
||||
{
|
||||
if (strcmp(parent_res.classname, gen->classes[i]->name) == 0)
|
||||
{
|
||||
ct = gen->classes[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ct)
|
||||
return gen_error;
|
||||
|
||||
fzl_token_t *ftok = node->memb.field->token;
|
||||
fzl_ctable_field_t ctf;
|
||||
bool field_exists = false;
|
||||
for (size_t i = 0; i < ct->fields_count; i++)
|
||||
{
|
||||
if (strcmp(ftok->value, ct->fields[i].name) == 0)
|
||||
{
|
||||
field_exists = true;
|
||||
ctf = ct->fields[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!field_exists)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_GET_FIELD);
|
||||
gen_word(gen, ctf.slot_index);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = ctf.type, .classname = parent_res.classname};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Format string
|
||||
gen_res_t gen_fmtstr(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
if (gen_common(gen, node->list.entries[i]).error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_BUILD_STR);
|
||||
gen_word(gen, node->list.entry_count);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = FZL_TYPE_STRING, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char fn_typecn[256];
|
||||
|
||||
// Function definition
|
||||
gen_res_t gen_fn(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_JMP);
|
||||
size_t skip_ip = gen->ops_count;
|
||||
gen_word(gen, (uint64_t)0);
|
||||
|
||||
fzl_type_t fntype;
|
||||
memset(fn_typecn, 0, sizeof(fn_typecn));
|
||||
|
||||
bool resolved = gen_resolve_type(gen, node->fn.type, &fntype, fn_typecn);
|
||||
if (!resolved)
|
||||
return gen_error;
|
||||
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(gen->syms[gen->syms_top - 1], node, node->fn.name->value,
|
||||
fntype, gen->ops_count);
|
||||
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
return gen_error;
|
||||
|
||||
if (fntype == FZL_TYPE_OBJECT)
|
||||
{
|
||||
fzl_symtable_res_t updres = fzl_symt_setobjclass(
|
||||
gen->syms[gen->syms_top - 1], node->fn.name->value, fn_typecn);
|
||||
if (updres != FZL_SYMTABLE_RESULT_OK)
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
fzl_gen_ctx_t *ctx = gen_ctx(gen, FZL_GEN_CTX_TYPE_FUNCTION, node);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_MK_ENVTABLE);
|
||||
|
||||
for (ssize_t i = node->fn.args_count - 1; i >= 0; i--)
|
||||
{
|
||||
if (gen_common(gen, node->fn.args[i]).error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_WRITE);
|
||||
}
|
||||
|
||||
if (gen_common(gen, node->fn.body).error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_RM_ENVTABLE);
|
||||
gen_op(gen, FZL_OPCODE_RET);
|
||||
gen->ops[skip_ip] = gen->ops_count;
|
||||
|
||||
ctx = gen_fin_ctx(gen);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
return (gen_res_t){.error = false, .type = fntype, .classname = fn_typecn};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <common/fzl_extern.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// Opaque pointer
|
||||
gen_res_t gen_handle(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
type_buf.kind = FZL_EXTERN_KIND_HANDLE;
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <compiler/internals/gen.h>
|
||||
|
||||
// Identifier lookup
|
||||
gen_res_t gen_identifier(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_sym_t sym;
|
||||
bool exists = false;
|
||||
ssize_t i = gen->syms_top - 1;
|
||||
for (; i >= 0; i--)
|
||||
if ((exists = fzl_symt_get(gen->syms[i], node->token->value, &sym)))
|
||||
break;
|
||||
|
||||
if (!exists)
|
||||
return gen_error;
|
||||
|
||||
if (!sym.slot_set)
|
||||
return gen_error;
|
||||
|
||||
size_t depth = gen->syms_top - 1 - i;
|
||||
gen_op(gen, FZL_OPCODE_LOAD_LOCAL);
|
||||
gen_word(gen, depth);
|
||||
gen_word(gen, sym.slot);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = sym.type, .classname = sym.classname};
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
gen_res_t gen_literal(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
if (node->token->type == FZL_TOKTYPE_STR_LITERAL)
|
||||
{
|
||||
size_t n = gen_add_const(
|
||||
gen, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = node->token->value,
|
||||
.as.string.length = node->token->value_sz});
|
||||
|
||||
gen_op(gen, FZL_OPCODE_LOADCONST);
|
||||
gen_word(gen, n);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = FZL_TYPE_STRING, .classname = NULL};
|
||||
}
|
||||
|
||||
if (node->token->type == FZL_TOKTYPE_NUM_LITERAL)
|
||||
{
|
||||
gen_op(gen, FZL_OPCODE_PUSHNUM);
|
||||
|
||||
uint64_t num = strtol(node->token->value, NULL, 10);
|
||||
gen_word(gen, num);
|
||||
|
||||
return (gen_res_t){.error = false, .type = FZL_TYPE_INT, .classname = NULL};
|
||||
}
|
||||
|
||||
if (node->token->type == FZL_TOKTYPE_BOOL_LITERAL)
|
||||
{
|
||||
fzl_token_t *tok = node->token;
|
||||
uint64_t val = strcmp(tok->value, "true") == 0 ? 1 : 0;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_PUSHBOOL);
|
||||
gen_word(gen, val);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
|
||||
if (node->token->type == FZL_TOKTYPE_NULL_LITERAL)
|
||||
{
|
||||
gen_op(gen, FZL_OPCODE_PUSHNUM);
|
||||
gen_word(gen, 0);
|
||||
gen_op(gen, FZL_OPCODE_DEREFERENCE);
|
||||
|
||||
return (gen_res_t){
|
||||
.error = false, .type = FZL_TYPE_OBJECT, .classname = null_name};
|
||||
}
|
||||
|
||||
return gen_error;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Binary logical operations (and / or)
|
||||
gen_res_t gen_logical(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t left = gen_common(gen, node->binary_op.left);
|
||||
if (left.error)
|
||||
return left;
|
||||
|
||||
gen_res_t right = gen_common(gen, node->binary_op.right);
|
||||
if (right.error)
|
||||
return right;
|
||||
|
||||
if (!gen_is_types_compatible(left.type, right.type, left.classname,
|
||||
right.classname))
|
||||
return gen_error;
|
||||
|
||||
if (node->type == FZL_NODETYPE_AND)
|
||||
gen_op(gen, FZL_OPCODE_AND);
|
||||
else
|
||||
gen_op(gen, FZL_OPCODE_OR);
|
||||
|
||||
return (gen_res_t){.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// While or for loops
|
||||
gen_res_t gen_loop(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_gen_ctx_t *ctx = gen_ctx(gen, FZL_GEN_CTX_TYPE_LOOP, node);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_MK_ENVTABLE);
|
||||
|
||||
if (node->loop.init)
|
||||
if (gen_common(gen, node->loop.init).error)
|
||||
return gen_error;
|
||||
|
||||
size_t start_ip = gen->ops_count;
|
||||
if (node->loop.cond)
|
||||
{
|
||||
if (gen_common(gen, node->loop.cond).error)
|
||||
return gen_error;
|
||||
}
|
||||
else
|
||||
{
|
||||
gen_op(gen, FZL_OPCODE_PUSHNUM);
|
||||
gen_word(gen, 1);
|
||||
}
|
||||
|
||||
gen_op(gen, FZL_OPCODE_JZ);
|
||||
ctx->loop.breaks[ctx->loop.breaks_count++] = gen->ops_count;
|
||||
gen_word(gen, 0);
|
||||
|
||||
if (gen_common(gen, node->loop.body).error)
|
||||
return gen_error;
|
||||
|
||||
if (node->loop.step)
|
||||
if (gen_common(gen, node->loop.step).error)
|
||||
return gen_error;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_JMP);
|
||||
gen_word(gen, start_ip);
|
||||
|
||||
// backpatch `break`s
|
||||
for (size_t i = 0; i < ctx->loop.breaks_count; i++)
|
||||
gen->ops[ctx->loop.breaks[i]] = gen->ops_count;
|
||||
|
||||
gen_op(gen, FZL_OPCODE_RM_ENVTABLE);
|
||||
|
||||
for (size_t i = gen->syms_top - 1; i > ctx->level + 1; i--)
|
||||
fzl_freesymt(gen->syms[--gen->syms_top]);
|
||||
|
||||
ctx = gen_fin_ctx(gen);
|
||||
if (!ctx)
|
||||
return gen_error;
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Binary math operations
|
||||
gen_res_t gen_math(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t lres = gen_common(gen, node->binary_op.left);
|
||||
if (lres.error)
|
||||
return lres;
|
||||
|
||||
gen_res_t rres = gen_common(gen, node->binary_op.right);
|
||||
if (rres.error)
|
||||
return rres;
|
||||
|
||||
if (lres.type < FZL_TYPE_INT8 || rres.type < FZL_TYPE_INT8)
|
||||
return gen_error;
|
||||
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_ADD:
|
||||
gen_op(gen, FZL_OPCODE_ADD);
|
||||
break;
|
||||
case FZL_NODETYPE_SUB:
|
||||
gen_op(gen, FZL_OPCODE_SUB);
|
||||
break;
|
||||
case FZL_NODETYPE_MUL:
|
||||
gen_op(gen, FZL_OPCODE_MUL);
|
||||
break;
|
||||
case FZL_NODETYPE_DIV:
|
||||
gen_op(gen, FZL_OPCODE_DIV);
|
||||
break;
|
||||
case FZL_NODETYPE_POW:
|
||||
gen_op(gen, FZL_OPCODE_POW);
|
||||
break;
|
||||
case FZL_NODETYPE_MOD:
|
||||
gen_op(gen, FZL_OPCODE_MOD);
|
||||
break;
|
||||
default:
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
return (gen_res_t){.error = false, .type = lres.type, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <common/fzl_extern.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Type reference (convert T => T*)
|
||||
gen_res_t gen_reference(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
char *refname = (char *)node->token->value;
|
||||
ssize_t slot = gen_resolve_extern_type(gen, refname);
|
||||
if (slot == -1)
|
||||
return gen_error;
|
||||
|
||||
type_buf.kind = FZL_EXTERN_KIND_REF;
|
||||
type_buf.ref_target =
|
||||
(fzl_extern_type_ref_t){.is_primitive = false, .type_slot = slot};
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char ret_typecn[256];
|
||||
|
||||
// Return keyword
|
||||
gen_res_t gen_return(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
fzl_gen_ctx_t *ctx = gen->ctx;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
if (ctx && ctx->type == FZL_GEN_CTX_TYPE_FUNCTION)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_GEN_CTX_TYPE_FUNCTION)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t result;
|
||||
bool returning_object = false;
|
||||
if (node->ret.value)
|
||||
{
|
||||
gen_res_t retres = gen_common(gen, node->ret.value);
|
||||
if (retres.error)
|
||||
return gen_error;
|
||||
|
||||
fzl_node_t *fn_node = ctx->function;
|
||||
fzl_type_t fntype;
|
||||
memset(ret_typecn, 0, sizeof(ret_typecn));
|
||||
|
||||
bool resolved =
|
||||
gen_resolve_type(gen, fn_node->fn.type, &fntype, ret_typecn);
|
||||
if (!resolved)
|
||||
return gen_error;
|
||||
|
||||
if (!gen_is_types_compatible(retres.type, fntype, retres.classname,
|
||||
ret_typecn))
|
||||
return gen_error;
|
||||
|
||||
if (retres.type == FZL_TYPE_OBJECT)
|
||||
returning_object = true;
|
||||
|
||||
result = retres;
|
||||
}
|
||||
else
|
||||
result = gen_ok_null;
|
||||
|
||||
bool is_method = false;
|
||||
fzl_gen_ctx_t *cur_ctx = gen->ctx;
|
||||
while (cur_ctx != NULL)
|
||||
{
|
||||
if (cur_ctx && cur_ctx->type == FZL_GEN_CTX_TYPE_CLASS)
|
||||
{
|
||||
is_method = true;
|
||||
break;
|
||||
}
|
||||
|
||||
cur_ctx = cur_ctx->parent;
|
||||
}
|
||||
|
||||
size_t method_dec = is_method;
|
||||
for (size_t i = 0; i < (gen->syms_top - ctx->level + method_dec); i++)
|
||||
{
|
||||
if (returning_object)
|
||||
gen_op(gen, FZL_OPCODE_ESCAPE_OBJECT);
|
||||
gen_op(gen, FZL_OPCODE_RM_ENVTABLE);
|
||||
}
|
||||
gen_op(gen, FZL_OPCODE_RET);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#include <common/fzl_extern.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char struct_typecn[256];
|
||||
|
||||
// Structure
|
||||
gen_res_t gen_struct(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
type_buf.kind = FZL_EXTERN_KIND_STRUCT;
|
||||
|
||||
size_t remainder = 2;
|
||||
size_t count = 0;
|
||||
fzl_extern_struct_field_t *fields =
|
||||
malloc(remainder * sizeof(fzl_extern_struct_field_t));
|
||||
if (!fields)
|
||||
return gen_error;
|
||||
|
||||
for (size_t i = 0; i < node->structure.entries_count; i++)
|
||||
{
|
||||
fzl_node_t *fnode = node->structure.entries[i];
|
||||
if (count >= remainder)
|
||||
{
|
||||
remainder *= 2;
|
||||
fzl_extern_struct_field_t *newfields =
|
||||
realloc(fields, remainder * sizeof(fzl_extern_struct_field_t));
|
||||
if (!newfields)
|
||||
{
|
||||
free(fields);
|
||||
return gen_error;
|
||||
}
|
||||
fields = newfields;
|
||||
}
|
||||
|
||||
fzl_type_t type;
|
||||
memset(struct_typecn, 0, sizeof(struct_typecn));
|
||||
|
||||
fzl_extern_struct_field_t *field = &fields[count++];
|
||||
|
||||
if (fnode->type == FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
field->name = (char *)fnode->var.identifier->token->value;
|
||||
|
||||
bool resolved =
|
||||
gen_resolve_type(gen, fnode->var.type, &type, struct_typecn);
|
||||
if (!resolved)
|
||||
{
|
||||
free(fields);
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
ssize_t slot = gen_resolve_extern_type(gen, struct_typecn);
|
||||
if (slot == -1)
|
||||
field->type =
|
||||
(fzl_extern_type_ref_t){.is_primitive = true, .primitive = type};
|
||||
else
|
||||
field->type =
|
||||
(fzl_extern_type_ref_t){.is_primitive = false, .type_slot = slot};
|
||||
}
|
||||
else if (fnode->type == FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
field->name = (char *)fnode->token->value;
|
||||
|
||||
ssize_t slot = gen_resolve_extern_type(gen, struct_typecn);
|
||||
if (slot == -1)
|
||||
{
|
||||
free(fields);
|
||||
return gen_error;
|
||||
}
|
||||
else
|
||||
field->type =
|
||||
(fzl_extern_type_ref_t){.is_primitive = false, .type_slot = slot};
|
||||
}
|
||||
else
|
||||
{
|
||||
free(fields);
|
||||
return gen_error;
|
||||
}
|
||||
}
|
||||
|
||||
type_buf.structure.fields = fields;
|
||||
type_buf.structure.fields_count = count;
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include <common/fzl_extern.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
fzl_extern_type_t type_buf;
|
||||
|
||||
// Type definition
|
||||
gen_res_t gen_typedef(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
memset(&type_buf, 0, sizeof(type_buf));
|
||||
if (gen_common(gen, node->typedefinition.type).error)
|
||||
return gen_error;
|
||||
|
||||
fzl_extern_type_t *newtype = &gen->extern_types[gen->extern_types_count++];
|
||||
newtype->lib_slot = gen->extern_libs_count - 1;
|
||||
newtype->name = node->typedefinition.typename;
|
||||
|
||||
fzl_type_t type;
|
||||
switch (type_buf.kind)
|
||||
{
|
||||
case FZL_EXTERN_KIND_REF:
|
||||
newtype->kind = FZL_EXTERN_KIND_REF;
|
||||
newtype->ref_target = type_buf.ref_target;
|
||||
type = FZL_TYPE_HANDLE;
|
||||
break;
|
||||
|
||||
case FZL_EXTERN_KIND_HANDLE:
|
||||
newtype->kind = FZL_EXTERN_KIND_HANDLE;
|
||||
type = FZL_TYPE_HANDLE;
|
||||
break;
|
||||
|
||||
case FZL_EXTERN_KIND_ALIAS:
|
||||
newtype->kind = FZL_EXTERN_KIND_ALIAS;
|
||||
newtype->alias_target = type_buf.alias_target;
|
||||
if (type_buf.alias_target.is_primitive)
|
||||
type = type_buf.alias_target.primitive;
|
||||
else
|
||||
{
|
||||
fzl_extern_type_t *aliased =
|
||||
&gen->extern_types[type_buf.alias_target.type_slot];
|
||||
if (aliased->kind == FZL_EXTERN_KIND_ALIAS)
|
||||
return gen_error;
|
||||
else if (aliased->kind == FZL_EXTERN_KIND_REF)
|
||||
type = FZL_TYPE_HANDLE;
|
||||
else if (aliased->kind == FZL_EXTERN_KIND_STRUCT)
|
||||
type = FZL_TYPE_STRUCT;
|
||||
else
|
||||
type = FZL_TYPE_HANDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_EXTERN_KIND_STRUCT:
|
||||
newtype->kind = FZL_EXTERN_KIND_STRUCT;
|
||||
newtype->structure.fields = type_buf.structure.fields;
|
||||
newtype->structure.fields_count = type_buf.structure.fields_count;
|
||||
type = FZL_TYPE_STRUCT;
|
||||
break;
|
||||
}
|
||||
|
||||
fzl_symtable_res_t res =
|
||||
fzl_symt_put(gen->syms[0], node, newtype->name, type, 0);
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
return gen_error;
|
||||
|
||||
if (type == FZL_TYPE_STRUCT || type == FZL_TYPE_HANDLE)
|
||||
{
|
||||
res = fzl_symt_setobjclass(gen->syms[0], newtype->name, newtype->name);
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
return gen_error;
|
||||
}
|
||||
|
||||
return gen_ok_null;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Currently, only NOT operator
|
||||
gen_res_t gen_unary_logical(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t operand_res = gen_common(gen, node->unary_op.operand);
|
||||
if (operand_res.error)
|
||||
return gen_error;
|
||||
|
||||
if (node->type == FZL_NODETYPE_NOT)
|
||||
gen_op(gen, FZL_OPCODE_NOT);
|
||||
else
|
||||
return gen_error;
|
||||
|
||||
return (gen_res_t){.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/internals/gen.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Unary math operations
|
||||
gen_res_t gen_unary_math(fzl_gen_t *gen, fzl_node_t *node)
|
||||
{
|
||||
if (!gen || !node)
|
||||
return gen_error;
|
||||
|
||||
gen_res_t res = gen_common(gen, node->unary_op.operand);
|
||||
if (res.error)
|
||||
return gen_error;
|
||||
|
||||
if (node->type == FZL_NODETYPE_UNARY_MINUS)
|
||||
gen_op(gen, FZL_OPCODE_NEG);
|
||||
|
||||
return (gen_res_t){.error = false, .type = res.type, .classname = NULL};
|
||||
}
|
||||
@@ -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,168 @@
|
||||
#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))
|
||||
{
|
||||
fzl_ctable_t *class = NULL;
|
||||
for (size_t i = 0; i < gen->classes_count; i++)
|
||||
{
|
||||
if (strcmp(name, gen->classes[i]->name) == 0)
|
||||
{
|
||||
class = gen->classes[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!class)
|
||||
return false;
|
||||
|
||||
*t = FZL_TYPE_OBJECT;
|
||||
if (c && class->name)
|
||||
strcpy(c, class->name);
|
||||
}
|
||||
else if (sym.node && sym.node->type == FZL_NODETYPE_CLASS)
|
||||
{
|
||||
*t = FZL_TYPE_OBJECT;
|
||||
if (c && sym.classname)
|
||||
strcpy(c, sym.classname);
|
||||
}
|
||||
else if (sym.node && sym.node->type == FZL_NODETYPE_TYPEDEF)
|
||||
{
|
||||
*t = sym.type;
|
||||
if (c && sym.classname)
|
||||
strcpy(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;
|
||||
}
|
||||
|
||||
// Resolve external type
|
||||
ssize_t gen_resolve_extern_type(fzl_gen_t *gen, char *name)
|
||||
{
|
||||
if (!gen || !name)
|
||||
return -1;
|
||||
|
||||
for (size_t i = 0; i < gen->extern_types_count; i++)
|
||||
{
|
||||
if (strcmp(name, gen->extern_types[i].name) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
+24
-19
@@ -1,9 +1,9 @@
|
||||
#include <common/fzl_colors.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_debug.h>
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <compiler/fzl_walker.h>
|
||||
#include <compiler/fzl_writer.h>
|
||||
#include <compiler/utils/fzl_linked.h>
|
||||
#include <compiler/utils/fzl_options.h>
|
||||
@@ -122,8 +122,7 @@ 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-> analyzing AST...%s\n", FZL_CDEBUG, FZL_CRST);
|
||||
printf("%s-> analyzing AST...%s", FZL_CDEBUG, FZL_CRST);
|
||||
}
|
||||
|
||||
fzl_analyzer_t *analyzer = fzl_mkanalyzer(parser->root);
|
||||
@@ -159,10 +158,16 @@ int main(int argc, char **argv)
|
||||
analyzer->warnings[i]);
|
||||
}
|
||||
|
||||
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
||||
if (!walker)
|
||||
if (opts->verbose)
|
||||
{
|
||||
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
|
||||
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||
printf("%s> generate\n-> generating bytecode...%s", FZL_CDEBUG, FZL_CRST);
|
||||
}
|
||||
|
||||
fzl_gen_t *gen = fzl_mkgen(parser->root);
|
||||
if (!gen)
|
||||
{
|
||||
printf("%sError%s: failed to allocate generator\n", FZL_CERROR, FZL_CRST);
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
@@ -172,12 +177,12 @@ int main(int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = fzl_walk(walker);
|
||||
if (result != 0)
|
||||
bool gen_res = fzl_gen_run(gen);
|
||||
if (!gen_res)
|
||||
{
|
||||
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_walker_geterror());
|
||||
printf("%sError%s: couldn't generate operations\n", FZL_CERROR, FZL_CRST);
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freewalker(walker);
|
||||
fzl_freegen(gen);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
@@ -191,16 +196,16 @@ int main(int argc, char **argv)
|
||||
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||
|
||||
printf("%s--> Constants table%s:\n", FZL_CDEBUG, FZL_CRST);
|
||||
for (size_t i = 0; i < walker->consts->size; i++)
|
||||
printf("---> %s\n", walker->consts->array[i].as.string.chars);
|
||||
for (size_t i = 0; i < gen->consts->size; i++)
|
||||
printf("---> %s\n", gen->consts->array[i].as.string.chars);
|
||||
|
||||
printf("%s--> Ops%s:\n", FZL_CDEBUG, FZL_CRST);
|
||||
printf("---> ");
|
||||
for (size_t i = 0; i < walker->op_count; i++)
|
||||
for (size_t i = 0; i < gen->ops_count; i++)
|
||||
{
|
||||
if (i > 0 && i < walker->op_count)
|
||||
if (i > 0 && i < gen->ops_count)
|
||||
printf("->");
|
||||
printf("%lu", walker->ops[i]);
|
||||
printf("%lX", gen->ops[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
@@ -224,7 +229,7 @@ int main(int argc, char **argv)
|
||||
{
|
||||
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
|
||||
fzl_freeoptions(opts);
|
||||
fzl_freewalker(walker);
|
||||
fzl_freegen(gen);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
@@ -233,13 +238,13 @@ int main(int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = fzl_write(writer, walker);
|
||||
result = fzl_write(writer, gen);
|
||||
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_freegen(gen);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
@@ -257,7 +262,7 @@ int main(int argc, char **argv)
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeanalyzer(analyzer);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freewalker(walker);
|
||||
fzl_freegen(gen);
|
||||
fzl_freewriter(writer);
|
||||
|
||||
printf("%sCompiled successfully%s.\n", FZL_COK, FZL_CRST);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user