Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b24cefee96 | ||
|
|
6498c73960 | ||
|
|
dc80de66e4 | ||
|
|
c61322718d | ||
|
|
411d291a2c | ||
|
|
aa7a06bd22 | ||
|
|
a263b6206f | ||
|
|
25991c6f15 | ||
|
|
d7630dbb6a | ||
|
|
a70ad011f3 | ||
|
|
62ae05b4cd | ||
|
|
4904f40037 | ||
|
|
562b1b2e0a | ||
|
|
6d1f1ce66e | ||
|
|
c03fddf47f | ||
|
|
3bb60071bb | ||
|
|
04fdd58e4f | ||
|
|
e00e609589 | ||
|
|
a37b8cfe6e | ||
|
|
c4eb148d17 | ||
|
|
dbca3129ab | ||
|
|
e3878047d7 | ||
|
|
7cfb8efef4 | ||
|
|
b5ae959d14 | ||
|
|
0e319308a7 | ||
|
|
a20743a835 | ||
|
|
5dde75523d | ||
|
|
cc5bbd61f2 | ||
|
|
0188991f4d | ||
|
|
73a2a50a0b | ||
|
|
4a366cb0ac | ||
|
|
c7cce94119 | ||
|
|
673a28fbc7 | ||
|
|
160c6c6488 | ||
|
|
c8f3cd61f4 | ||
|
|
138d503bce | ||
|
|
e49ebba346 |
@@ -5,6 +5,7 @@
|
|||||||
!/exl
|
!/exl
|
||||||
!/include
|
!/include
|
||||||
!/src
|
!/src
|
||||||
|
!/tests
|
||||||
|
|
||||||
# Files
|
# Files
|
||||||
!/.clang-format
|
!/.clang-format
|
||||||
|
|||||||
+48
-103
@@ -14,17 +14,39 @@ include_directories(include)
|
|||||||
|
|
||||||
# Compiler
|
# Compiler
|
||||||
add_executable(fzc
|
add_executable(fzc
|
||||||
src/compiler/fzl_token.c
|
|
||||||
src/compiler/fzl_reader.c
|
|
||||||
src/compiler/fzl_parser.c
|
|
||||||
src/compiler/fzl_debug.c
|
|
||||||
src/compiler/fzl_walker.c
|
|
||||||
src/compiler/fzl_writer.c
|
|
||||||
src/compiler/utils/fzl_const_pool.c
|
src/compiler/utils/fzl_const_pool.c
|
||||||
src/compiler/utils/fzl_symtable.c
|
src/compiler/utils/fzl_symtable.c
|
||||||
src/compiler/utils/fzl_linked.c
|
src/compiler/utils/fzl_linked.c
|
||||||
src/compiler/utils/fzl_options.c
|
src/compiler/utils/fzl_options.c
|
||||||
src/compiler/utils/fzl_ctable.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)
|
src/compiler/main.c)
|
||||||
|
|
||||||
# Virtual machine
|
# Virtual machine
|
||||||
@@ -35,108 +57,31 @@ add_executable(fz
|
|||||||
src/vm/main.c)
|
src/vm/main.c)
|
||||||
|
|
||||||
#
|
#
|
||||||
# ========== Compilation tests ==========
|
# ========== Tests ==========
|
||||||
#
|
#
|
||||||
|
|
||||||
enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
add_test(
|
file(GLOB TEST_PROGS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "tests/programs/*.fzs")
|
||||||
NAME compile_hw
|
foreach (TEST_FILE IN LISTS TEST_PROGS)
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/hw.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/hw.fzs"
|
get_filename_component(TEST_NAME "${TEST_FILE}" NAME_WE)
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_print_empty
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/print_empty.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/print_empty.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_problematic_hw
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/problematic_hw.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/problematic_hw.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_base_math
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/base_math.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/base_math.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_variables
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/variables.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/variables.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_conditions
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/conditions.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/conditions.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_fn
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/fn.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/fn.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_comment
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/comment.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/comment.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_class
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/class.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/class.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_raii
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/raii.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/raii.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_borrow
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/borrow.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/borrow.fzs"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME compile_hard_1
|
|
||||||
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/hard-1.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/hard-1.fzs"
|
|
||||||
)
|
|
||||||
|
|
||||||
set_tests_properties(compile_problematic_hw PROPERTIES WILL_FAIL TRUE)
|
add_test(
|
||||||
|
NAME "progs_compile_${TEST_NAME}"
|
||||||
|
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/p_${TEST_NAME}.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_FILE}"
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME "progs_runtime_${TEST_NAME}"
|
||||||
|
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/p_${TEST_NAME}.fzb"
|
||||||
|
)
|
||||||
|
|
||||||
#
|
if (TEST_NAME MATCHES "neg_")
|
||||||
# ========== Runtime tests ==========
|
set_tests_properties("progs_compile_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
|
||||||
#
|
set_tests_properties("progs_runtime_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
|
||||||
|
endif()
|
||||||
|
|
||||||
add_test(
|
if (TEST_NAME MATCHES "negr_")
|
||||||
NAME runtime_hw
|
set_tests_properties("progs_runtime_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/hw.fzb"
|
endif()
|
||||||
)
|
endforeach()
|
||||||
add_test(
|
|
||||||
NAME runtime_print_empty
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/print_empty.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_base_math
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/base_math.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_variables
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/variables.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_conditions
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/conditions.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_fn
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/fn.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_comment
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/comment.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_class
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/class.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_raii
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/raii.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_borrow
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/borrow.fzb"
|
|
||||||
)
|
|
||||||
add_test(
|
|
||||||
NAME runtime_hard_1
|
|
||||||
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/hard-1.fzb"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
.PHONY: configure build debug-c debug-vm test
|
.PHONY: build debug-c debug-vm test
|
||||||
|
|
||||||
configure:
|
build:
|
||||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
|
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
|
||||||
|
cmake --build build
|
||||||
|
|
||||||
build: configure
|
build-release:
|
||||||
|
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
||||||
cmake --build build
|
cmake --build build
|
||||||
|
|
||||||
debug-c: build
|
debug-c: build
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ Working examples of code can be found in `exl/` directory.
|
|||||||
## Building
|
## Building
|
||||||
|
|
||||||
1. Clone the repository: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
1. Clone the repository: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||||
2. Build: `make build`
|
2. Build: `make build-release`
|
||||||
|
|
||||||
After this, a "build" directory will appear in the project root. It will contain two executable files:
|
After this, a "build" directory will appear in the project root. It will contain two executable files:
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@
|
|||||||
## Инструкции по сборке
|
## Инструкции по сборке
|
||||||
|
|
||||||
1. Склонировать проект: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
1. Склонировать проект: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||||
2. Собрать проект: `make build`
|
2. Собрать проект: `make build-release`
|
||||||
|
|
||||||
После этого в корне появится директория **"build"**. В ней будут лежать два скомпилированных бинарных файла:
|
После этого в корне появится директория **"build"**. В ней будут лежать два скомпилированных бинарных файла:
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
// There are two ways to work with variables
|
||||||
|
// 1. Declare variable:
|
||||||
|
str someString = "world, hello!";
|
||||||
|
|
||||||
|
// 2. Change variable value:
|
||||||
|
someString = "hello, world!";
|
||||||
|
|
||||||
|
// Variables help you store values to use it later
|
||||||
|
int a = 2;
|
||||||
|
int b = a + a * a;
|
||||||
|
int c = a * b;
|
||||||
|
|
||||||
|
printline("Now we got 12:");
|
||||||
|
printline(c);
|
||||||
|
|
||||||
|
// Also, there are many different variable types to help your program
|
||||||
|
// work with many things:
|
||||||
|
int signedInteger;
|
||||||
|
uint unsignedInteger;
|
||||||
|
str string;
|
||||||
|
bool boolean;
|
||||||
|
|
||||||
|
i8 integer1; // 8-byte int
|
||||||
|
i16 integer2; // 16-byte int
|
||||||
|
i32 integer3; // 32-byte int
|
||||||
|
i64 integer4; // 64-byte int
|
||||||
|
|
||||||
|
u8 integer5; // unsigned 8-byte integer
|
||||||
|
u16 integer6; // unsigned 16-byte integer
|
||||||
|
u32 integer7; // unsigned 32-byte integer
|
||||||
|
u64 integer8; // unsigned 64-byte integer
|
||||||
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
// When you need to check some value
|
||||||
|
// a conditional statement might help you
|
||||||
|
|
||||||
|
int valueToCheck = 2 + 2;
|
||||||
|
if (valueToCheck == 4)
|
||||||
|
{
|
||||||
|
printline("Yay! 2 + 2 = 4");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conditional statement uses condition in parenthesis
|
||||||
|
// after `if` keyword and executes command(s) after
|
||||||
|
// if condition turns out to be true
|
||||||
|
|
||||||
|
// Otherwise, to execute block when condition turns
|
||||||
|
// out to be false, you can use `else` keyword:
|
||||||
|
|
||||||
|
int age = 30;
|
||||||
|
|
||||||
|
if (age <= 20)
|
||||||
|
printline("You're too young");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
age = 20;
|
||||||
|
printline("You look too young!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: If you need to execute only one statement
|
||||||
|
// inside `if` or `else` block, then the curly
|
||||||
|
// braces can be omitted.
|
||||||
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
// If you have some code section which is
|
||||||
|
// duplicated in program several times
|
||||||
|
// then it means your program needs some
|
||||||
|
// functions!
|
||||||
|
|
||||||
|
fn doSomeWork()
|
||||||
|
{
|
||||||
|
printline("Some work");
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the function makes some kind of
|
||||||
|
// calculations then you should specify
|
||||||
|
// the result type
|
||||||
|
|
||||||
|
fn sum(int a, int b) int
|
||||||
|
{
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The `return` keyword can be used to
|
||||||
|
// actually return the value from the
|
||||||
|
// function. Also, you can specify list
|
||||||
|
// of arguments that you will use inside
|
||||||
|
// the function
|
||||||
|
|
||||||
|
int first = 5;
|
||||||
|
int second = 6;
|
||||||
|
int result = sum(first, second);
|
||||||
|
printline(result);
|
||||||
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
class A
|
|
||||||
{
|
|
||||||
uint some;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn doSome(A a) A
|
|
||||||
{
|
|
||||||
// NOTE: section below won't change `a.some` value
|
|
||||||
|
|
||||||
if (a.some < 1)
|
|
||||||
{
|
|
||||||
a.some = 1;
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn doSomeNew() A
|
|
||||||
{
|
|
||||||
if (1 == 1)
|
|
||||||
{
|
|
||||||
return new A;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
A a = new A;
|
|
||||||
a.some = 0;
|
|
||||||
if (a.some != 0)
|
|
||||||
{
|
|
||||||
printline("Test failed (first a.some != 0)");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
else printline(a);
|
|
||||||
|
|
||||||
a = doSome(a);
|
|
||||||
if (a.some != 1)
|
|
||||||
{
|
|
||||||
printline("Test failed (2nd a.some != 1)");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
else printline(a);
|
|
||||||
|
|
||||||
A b = doSomeNew();
|
|
||||||
printline(b);
|
|
||||||
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
class Point
|
|
||||||
{
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
|
|
||||||
// Move point by [dx,dy]
|
|
||||||
fn move(int dx, int dy) {
|
|
||||||
this.x = this.x + dx;
|
|
||||||
this.y = this.y + dy;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Point obj = new Point;
|
|
||||||
obj.x = 1;
|
|
||||||
obj.y = 2;
|
|
||||||
|
|
||||||
printline("Current X,Y:");
|
|
||||||
printline(obj.x);
|
|
||||||
printline(obj.y);
|
|
||||||
|
|
||||||
// NOTE: WHY THE FUCK THIS FUCKING COMPILER REQUIRES ';' AFTER '='?!
|
|
||||||
// |
|
|
||||||
// v
|
|
||||||
obj.move(9, 8);
|
|
||||||
printline("X,Y after move:");
|
|
||||||
printline(obj.x);
|
|
||||||
printline(obj.y);
|
|
||||||
|
|
||||||
if (obj.x == 10 && obj.y == 10)
|
|
||||||
printline("Test passed");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printline("Test failed");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
// I'm a comment
|
|
||||||
printline("Comment is skipped");;;;;
|
|
||||||
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
bool flag = true;
|
|
||||||
|
|
||||||
if (!flag)
|
|
||||||
{
|
|
||||||
printline("buhhh, flag is not true");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
else if (2 + 2 >= 4)
|
|
||||||
{
|
|
||||||
printline ("two plus two is four");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printline("gotcha");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
printline("block after");
|
|
||||||
}
|
|
||||||
|
|
||||||
-32
@@ -1,32 +0,0 @@
|
|||||||
fn fib(int n) int
|
|
||||||
{
|
|
||||||
if (n <= 1)
|
|
||||||
return n;
|
|
||||||
|
|
||||||
return fib(n - 1) + fib(n - 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sum(uint a, uint b) uint
|
|
||||||
{
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
printline("Test #1: fibonacci");
|
|
||||||
int result = fib(7);
|
|
||||||
if (result == 13)
|
|
||||||
printline("Recurse test passed");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printline("Recurse test failed");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printline("Test #2: sum");
|
|
||||||
if (sum(2, 2) == 4)
|
|
||||||
printline("Sum test passed");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printline("Sum test failed");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
printline('hello, world');
|
|
||||||
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
printline("");
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
printline("hello);
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
class Animal
|
|
||||||
{
|
|
||||||
str type;
|
|
||||||
str name;
|
|
||||||
int age;
|
|
||||||
|
|
||||||
fn copy() Animal
|
|
||||||
{
|
|
||||||
Animal cp = new Animal;
|
|
||||||
cp.type = this.type;
|
|
||||||
cp.name = this.name;
|
|
||||||
cp.age = this.age;
|
|
||||||
|
|
||||||
return cp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Animal a = new Animal;
|
|
||||||
a.type = "Cow";
|
|
||||||
a.name = "Buryatka";
|
|
||||||
a.age = 5;
|
|
||||||
|
|
||||||
Animal b = a.copy();
|
|
||||||
b.name = "Gavryusha";
|
|
||||||
b.age = 10;
|
|
||||||
|
|
||||||
printline(a);
|
|
||||||
printline(b);
|
|
||||||
|
|
||||||
printline("Types are same:");
|
|
||||||
printline(a.type == b.type);
|
|
||||||
|
|
||||||
printline("Names are NOT the same");
|
|
||||||
printline(a.name != b.name);
|
|
||||||
|
|
||||||
printline("1st age less than 2nd");
|
|
||||||
printline(a.age < b.age);
|
|
||||||
|
|
||||||
if (a.type == b.type && a.name != b.name && a.age < b.age)
|
|
||||||
printline("Test passed");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printline("Test failed");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
#ifndef FZL_ANALYZER_H
|
||||||
|
#define FZL_ANALYZER_H
|
||||||
|
|
||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// Analyzing context type
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_AN_CTX_TYPE_UNNECESSARY, // default type
|
||||||
|
FZL_AN_CTX_TYPE_FUNCTION, // when analyzing function
|
||||||
|
FZL_AN_CTX_TYPE_LOOP, // when analyzing loop
|
||||||
|
FZL_AN_CTX_TYPE_CLASS, // when analyzing class
|
||||||
|
} fzl_an_ctx_type_t;
|
||||||
|
|
||||||
|
// Inner node analyzing result descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
bool error;
|
||||||
|
|
||||||
|
fzl_type_t type;
|
||||||
|
char *classname;
|
||||||
|
} fzl_an_res_t;
|
||||||
|
|
||||||
|
// Current node context descriptor
|
||||||
|
typedef struct fzl_an_ctx
|
||||||
|
{
|
||||||
|
struct fzl_an_ctx *parent;
|
||||||
|
|
||||||
|
fzl_an_ctx_type_t type;
|
||||||
|
fzl_symtable_t *symbols;
|
||||||
|
size_t symbols_level;
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
fzl_node_t *fn;
|
||||||
|
char *classname;
|
||||||
|
};
|
||||||
|
} fzl_an_ctx_t;
|
||||||
|
|
||||||
|
// Analyzer descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *warnings[256];
|
||||||
|
size_t warnings_top;
|
||||||
|
|
||||||
|
char *errors[256];
|
||||||
|
size_t errors_top;
|
||||||
|
|
||||||
|
fzl_symtable_t *symbols[256];
|
||||||
|
size_t symbols_top;
|
||||||
|
|
||||||
|
char *alloc_names[256];
|
||||||
|
size_t alloc_names_top;
|
||||||
|
|
||||||
|
fzl_node_t *root;
|
||||||
|
fzl_an_ctx_t *ctx;
|
||||||
|
} fzl_analyzer_t;
|
||||||
|
|
||||||
|
// Allocate analyzer
|
||||||
|
fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node);
|
||||||
|
|
||||||
|
// Push error to errors list
|
||||||
|
bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error);
|
||||||
|
|
||||||
|
// Push warning to warnings list
|
||||||
|
bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning);
|
||||||
|
|
||||||
|
// Analyze AST
|
||||||
|
void fzl_analyze(fzl_analyzer_t *analyzer);
|
||||||
|
|
||||||
|
// Free analyzer
|
||||||
|
void fzl_freeanalyzer(fzl_analyzer_t *analyzer);
|
||||||
|
|
||||||
|
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
|
||||||
@@ -18,6 +18,8 @@ typedef enum
|
|||||||
FZL_NODETYPE_CLASS,
|
FZL_NODETYPE_CLASS,
|
||||||
FZL_NODETYPE_FIELD,
|
FZL_NODETYPE_FIELD,
|
||||||
FZL_NODETYPE_CONSTRUCT,
|
FZL_NODETYPE_CONSTRUCT,
|
||||||
|
FZL_NODETYPE_LOOP,
|
||||||
|
FZL_NODETYPE_BREAK,
|
||||||
|
|
||||||
FZL_NODETYPE_ADD,
|
FZL_NODETYPE_ADD,
|
||||||
FZL_NODETYPE_SUB,
|
FZL_NODETYPE_SUB,
|
||||||
@@ -140,6 +142,16 @@ typedef struct fzl_node
|
|||||||
{
|
{
|
||||||
struct fzl_node *name;
|
struct fzl_node *name;
|
||||||
} construct;
|
} construct;
|
||||||
|
|
||||||
|
// Loop
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct fzl_node *init;
|
||||||
|
struct fzl_node *cond;
|
||||||
|
struct fzl_node *step;
|
||||||
|
|
||||||
|
struct fzl_node *body;
|
||||||
|
} loop;
|
||||||
};
|
};
|
||||||
} fzl_node_t;
|
} fzl_node_t;
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ typedef enum
|
|||||||
FZL_TOKTYPE_FUNCTION, // fn
|
FZL_TOKTYPE_FUNCTION, // fn
|
||||||
FZL_TOKTYPE_RETURN, // return
|
FZL_TOKTYPE_RETURN, // return
|
||||||
|
|
||||||
|
FZL_TOKTYPE_FOR, // for
|
||||||
|
FZL_TOKTYPE_WHILE, // while
|
||||||
|
FZL_TOKTYPE_BREAK, // break
|
||||||
|
|
||||||
FZL_TOKTYPE_IF, // if
|
FZL_TOKTYPE_IF, // if
|
||||||
FZL_TOKTYPE_ELSE, // else
|
FZL_TOKTYPE_ELSE, // else
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ typedef enum
|
|||||||
FZL_WALK_CTX_TYPE_GLOBAL,
|
FZL_WALK_CTX_TYPE_GLOBAL,
|
||||||
FZL_WALK_CTX_TYPE_FN,
|
FZL_WALK_CTX_TYPE_FN,
|
||||||
FZL_WALK_CTX_TYPE_CLASS,
|
FZL_WALK_CTX_TYPE_CLASS,
|
||||||
FZL_WALK_CTX_TYPE_ASSIGN
|
FZL_WALK_CTX_TYPE_ASSIGN,
|
||||||
|
FZL_WALK_CTX_TYPE_LOOP,
|
||||||
} fzl_walk_ctx_type_t;
|
} fzl_walk_ctx_type_t;
|
||||||
|
|
||||||
// Internal walk function result
|
// Internal walk function result
|
||||||
@@ -36,8 +37,23 @@ typedef struct fzl_walk_ctx
|
|||||||
fzl_walk_ctx_type_t type;
|
fzl_walk_ctx_type_t type;
|
||||||
struct fzl_walk_ctx *parent;
|
struct fzl_walk_ctx *parent;
|
||||||
|
|
||||||
fzl_node_t *fn;
|
|
||||||
size_t initial_env_lvl;
|
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;
|
} fzl_walk_ctx_t;
|
||||||
|
|
||||||
// Walker descriptor
|
// Walker descriptor
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define FZL_SYMTABLE_H
|
#define FZL_SYMTABLE_H
|
||||||
|
|
||||||
#include <common/fzl_types.h>
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -19,6 +20,7 @@ typedef struct
|
|||||||
{
|
{
|
||||||
char *name;
|
char *name;
|
||||||
fzl_type_t type;
|
fzl_type_t type;
|
||||||
|
fzl_node_t *node;
|
||||||
size_t ip;
|
size_t ip;
|
||||||
|
|
||||||
char *classname;
|
char *classname;
|
||||||
@@ -37,8 +39,8 @@ typedef struct
|
|||||||
fzl_symtable_t *fzl_mksymt();
|
fzl_symtable_t *fzl_mksymt();
|
||||||
|
|
||||||
// Put new entry into symtable
|
// Put new entry into symtable
|
||||||
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, fzl_node_t *node,
|
||||||
fzl_type_t type, uint64_t ip);
|
char *name, fzl_type_t type, uint64_t ip);
|
||||||
|
|
||||||
// Assign class name to object type
|
// Assign class name to object type
|
||||||
fzl_symtable_res_t fzl_symt_setobjclass(fzl_symtable_t *symt, char *name,
|
fzl_symtable_res_t fzl_symt_setobjclass(fzl_symtable_t *symt, char *name,
|
||||||
|
|||||||
+1
-1
@@ -36,7 +36,7 @@ typedef struct
|
|||||||
fzl_vm_t *fzl_mkvm(const char *filepath);
|
fzl_vm_t *fzl_mkvm(const char *filepath);
|
||||||
|
|
||||||
// Load and run code
|
// Load and run code
|
||||||
int fzl_vm_run(fzl_vm_t *vm);
|
int fzl_vm_run(fzl_vm_t *vm, bool allow_infinite, uint64_t max_iter);
|
||||||
|
|
||||||
// Get last VM error message
|
// Get last VM error message
|
||||||
char *fzl_vm_geterror();
|
char *fzl_vm_geterror();
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze assigns
|
||||||
|
fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!assign_node || assign_node->type != FZL_NODETYPE_ASSIGN)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not assignment");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *left = assign_node->binary_op.left;
|
||||||
|
fzl_an_res_t leftside = analyze_common(analyzer, left);
|
||||||
|
if (leftside.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
fzl_an_res_t rightside =
|
||||||
|
analyze_common(analyzer, assign_node->binary_op.right);
|
||||||
|
if (rightside.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (left->type != FZL_NODETYPE_DECLARATION &&
|
||||||
|
left->type != FZL_NODETYPE_IDENTIFIER && left->type != FZL_NODETYPE_FIELD)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, left->token,
|
||||||
|
"left side must be one of these: declaration, identifier or "
|
||||||
|
"field access");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!an_is_compatible(leftside.type, rightside.type, leftside.classname,
|
||||||
|
rightside.classname))
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"incompatible types - '%s' is expected, got '%s'",
|
||||||
|
an_typetostr(leftside.type, leftside.classname),
|
||||||
|
an_typetostr(rightside.type, rightside.classname));
|
||||||
|
an_push_error(analyzer, assign_node->binary_op.right->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze binary arithmetics
|
||||||
|
fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!bin_node || (bin_node->type != FZL_NODETYPE_ADD &&
|
||||||
|
bin_node->type != FZL_NODETYPE_SUB &&
|
||||||
|
bin_node->type != FZL_NODETYPE_MUL &&
|
||||||
|
bin_node->type != FZL_NODETYPE_DIV &&
|
||||||
|
bin_node->type != FZL_NODETYPE_POW &&
|
||||||
|
bin_node->type != FZL_NODETYPE_MOD))
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not binary math operator");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t left = analyze_common(analyzer, bin_node->binary_op.left);
|
||||||
|
if (left.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
fzl_an_res_t right = analyze_common(analyzer, bin_node->binary_op.right);
|
||||||
|
if (right.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (left.type < FZL_TYPE_INT8)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"integer type is expected, got '%s'",
|
||||||
|
an_typetostr(left.type, left.classname));
|
||||||
|
an_push_error(analyzer, bin_node->binary_op.left->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (right.type < FZL_TYPE_INT8)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"integer type is expected, got '%s'",
|
||||||
|
an_typetostr(right.type, right.classname));
|
||||||
|
an_push_error(analyzer, bin_node->binary_op.right->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = false, .type = left.type, .classname = NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze block
|
||||||
|
fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!block_node || block_node->type != FZL_NODETYPE_BLOCK)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "node is not a block");
|
||||||
|
an_push_error(analyzer, NULL, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY);
|
||||||
|
|
||||||
|
bool has_return = false;
|
||||||
|
bool error = false;
|
||||||
|
for (size_t i = 0; i < block_node->list.entry_count; i++)
|
||||||
|
{
|
||||||
|
if (has_return)
|
||||||
|
an_push_warning(analyzer, block_node->list.entries[i]->token,
|
||||||
|
"this statement will never execute");
|
||||||
|
|
||||||
|
fzl_an_res_t entry_res =
|
||||||
|
analyze_common(analyzer, block_node->list.entries[i]);
|
||||||
|
if (entry_res.error)
|
||||||
|
error = true;
|
||||||
|
|
||||||
|
if (block_node->list.entries[i]->type == FZL_NODETYPE_RETURN)
|
||||||
|
has_return = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_ctx_t *fnctx = analyzer->ctx;
|
||||||
|
while (fnctx != NULL)
|
||||||
|
{
|
||||||
|
if (fnctx && fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||||
|
break;
|
||||||
|
|
||||||
|
fnctx = fnctx->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fnctx && fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||||
|
{
|
||||||
|
fzl_type_t rettype = an_toktotype(fnctx->fn->fn.type);
|
||||||
|
char *classname = NULL;
|
||||||
|
char *rettype_str = an_typetostr(rettype, classname);
|
||||||
|
|
||||||
|
if (rettype == FZL_TYPE_OBJECT)
|
||||||
|
classname = (char *)fnctx->fn->fn.type->value;
|
||||||
|
|
||||||
|
if (rettype != FZL_TYPE_NULL)
|
||||||
|
{
|
||||||
|
if (!has_return || block_node->list.entry_count < 1)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"'return <%s>' is expected in the function with non-null "
|
||||||
|
"return type",
|
||||||
|
rettype_str);
|
||||||
|
an_push_error(analyzer, block_node->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *last_node =
|
||||||
|
block_node->list.entries[block_node->list.entry_count - 1];
|
||||||
|
if (last_node->type != FZL_NODETYPE_RETURN)
|
||||||
|
{
|
||||||
|
// NOTE: replace with ghost walker to check if all branch return an
|
||||||
|
// expected value type
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"expected last statement in block to be 'return <%s>'",
|
||||||
|
rettype_str);
|
||||||
|
an_push_error(analyzer, last_node->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
|
||||||
|
// Analyze break node
|
||||||
|
fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!break_node || break_node->type != FZL_NODETYPE_BREAK)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not a break statement");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_ctx_t *ctx = analyzer->ctx;
|
||||||
|
while (ctx != NULL)
|
||||||
|
{
|
||||||
|
if (ctx && ctx->type == FZL_AN_CTX_TYPE_LOOP)
|
||||||
|
break;
|
||||||
|
|
||||||
|
ctx = ctx->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, break_node->token,
|
||||||
|
"cannot use 'break' outside the loop");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,238 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char *tmp_method_name = NULL;
|
||||||
|
char *tmp_fnname = NULL;
|
||||||
|
char *tmp_classname = NULL;
|
||||||
|
|
||||||
|
// Try to get symbol name from callee
|
||||||
|
char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||||
|
{
|
||||||
|
*method = false;
|
||||||
|
|
||||||
|
if (callee->type == FZL_NODETYPE_IDENTIFIER)
|
||||||
|
return (char *)callee->token->value;
|
||||||
|
|
||||||
|
if (callee->type != FZL_NODETYPE_FIELD)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, callee->token, "unknown call format");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*method = true;
|
||||||
|
fzl_an_res_t parent_res = analyze_common(analyzer, callee->memb.parent);
|
||||||
|
if (parent_res.type != FZL_TYPE_OBJECT || !parent_res.classname)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
char *fn_name = callee->memb.field->token->value;
|
||||||
|
tmp_fnname = fn_name;
|
||||||
|
tmp_classname = parent_res.classname;
|
||||||
|
size_t classname_len = strlen(parent_res.classname);
|
||||||
|
size_t methodname_len = strlen(fn_name);
|
||||||
|
tmp_method_name = malloc(classname_len + methodname_len + 2);
|
||||||
|
if (!tmp_method_name)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, callee->token, "out of memory");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(tmp_method_name, parent_res.classname);
|
||||||
|
tmp_method_name[classname_len] = '_';
|
||||||
|
strcpy(tmp_method_name + classname_len + 1, fn_name);
|
||||||
|
tmp_method_name[classname_len + methodname_len + 1] = 0;
|
||||||
|
|
||||||
|
return tmp_method_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Analyze call node
|
||||||
|
fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||||
|
{
|
||||||
|
if (!call_node || call_node->type != FZL_NODETYPE_CALL)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not call");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *callee = call_node->call.callee;
|
||||||
|
|
||||||
|
bool is_method = false;
|
||||||
|
char *callee_name = resolve_callee(analyzer, callee, &is_method);
|
||||||
|
if (!callee_name)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, callee->token, "expression is not callable");
|
||||||
|
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: move into `compiler/analyzer/builtins.c`
|
||||||
|
if (strcmp(callee_name, "printline") == 0 || strcmp(callee_name, "exit") == 0)
|
||||||
|
{
|
||||||
|
if (call_node->call.args_count != 1)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"'%s' builtin should be called with 1 argument instead of %zu",
|
||||||
|
callee_name, call_node->call.args_count);
|
||||||
|
an_push_error(analyzer, call_node->token, an_problem);
|
||||||
|
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool sym_found = false;
|
||||||
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((sym_found = fzl_symt_get(analyzer->symbols[i], callee_name, &sym)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sym_found)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "cannot resolve symbol '%s'",
|
||||||
|
callee_name);
|
||||||
|
an_push_error(analyzer, call_node->token, an_problem);
|
||||||
|
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sym.node->type != FZL_NODETYPE_FUNCTION)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "'%s' is not callable",
|
||||||
|
callee_name);
|
||||||
|
an_push_error(analyzer, call_node->token, an_problem);
|
||||||
|
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t offset = is_method ? 1 : 0;
|
||||||
|
if ((sym.node->fn.args_count - offset) != call_node->call.args_count)
|
||||||
|
{
|
||||||
|
if (is_method)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"method '%s.%s' is expecting %zu argument(s), got %zu",
|
||||||
|
tmp_classname, tmp_fnname, sym.node->fn.args_count,
|
||||||
|
call_node->call.args_count - offset);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"function '%s' is expecting %zu argument(s), got %zu",
|
||||||
|
callee_name, sym.node->fn.args_count,
|
||||||
|
call_node->call.args_count - offset);
|
||||||
|
}
|
||||||
|
an_push_error(analyzer, call_node->token, an_problem);
|
||||||
|
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = offset; i < sym.node->fn.args_count; i++)
|
||||||
|
{
|
||||||
|
fzl_node_t *arg = sym.node->fn.args[i];
|
||||||
|
fzl_type_t req_type = an_toktotype(arg->var.type);
|
||||||
|
char *req_classname = NULL;
|
||||||
|
if (req_type == FZL_TYPE_OBJECT)
|
||||||
|
req_classname = arg->var.type->value;
|
||||||
|
|
||||||
|
fzl_an_res_t got =
|
||||||
|
analyze_common(analyzer, call_node->call.args[i - offset]);
|
||||||
|
if (got.error)
|
||||||
|
{
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!an_is_compatible(req_type, got.type, req_classname, got.classname))
|
||||||
|
{
|
||||||
|
if (is_method)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"method '%s.%s' is expecting %zu argument to have type '%s', "
|
||||||
|
"got '%s'",
|
||||||
|
tmp_classname, tmp_fnname, i + 1 - offset,
|
||||||
|
an_typetostr(req_type, req_classname),
|
||||||
|
an_typetostr(got.type, got.classname));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"function '%s' is expecting %zu argument to have type '%s', "
|
||||||
|
"got '%s'",
|
||||||
|
callee_name, i + 1 - offset,
|
||||||
|
an_typetostr(req_type, req_classname),
|
||||||
|
an_typetostr(got.type, got.classname));
|
||||||
|
}
|
||||||
|
an_push_error(analyzer, call_node->token, an_problem);
|
||||||
|
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_type_t type = an_toktotype(sym.node->fn.type);
|
||||||
|
char *classname = sym.classname;
|
||||||
|
|
||||||
|
if (tmp_method_name != NULL)
|
||||||
|
{
|
||||||
|
free(tmp_method_name);
|
||||||
|
tmp_method_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze class declaration
|
||||||
|
fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!class_node || class_node->type != FZL_NODETYPE_CLASS)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not class");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
char *classname = (char *)class_node->class.name->value;
|
||||||
|
fzl_an_ctx_t *ctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_CLASS);
|
||||||
|
ctx->classname = classname;
|
||||||
|
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool sym_found = false;
|
||||||
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((sym_found = fzl_symt_get(analyzer->symbols[i], classname, &sym)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_symtable_res_t res =
|
||||||
|
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], class_node,
|
||||||
|
classname, FZL_TYPE_NULL, 0);
|
||||||
|
|
||||||
|
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||||
|
{
|
||||||
|
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"name '%s' already has been declared", classname);
|
||||||
|
an_push_error(analyzer, class_node->token, an_problem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "cannot store name '%s'",
|
||||||
|
classname);
|
||||||
|
an_push_error(analyzer, class_node->token, an_problem);
|
||||||
|
}
|
||||||
|
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool error = false;
|
||||||
|
for (size_t i = 0; i < class_node->class.fields_count; i++)
|
||||||
|
{
|
||||||
|
fzl_an_res_t res = analyze_common(analyzer, class_node->class.fields[i]);
|
||||||
|
if (res.error)
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < class_node->class.methods_count; i++)
|
||||||
|
{
|
||||||
|
fzl_an_res_t res = analyze_common(analyzer, class_node->class.methods[i]);
|
||||||
|
if (res.error)
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Analyzer main function
|
||||||
|
fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node)
|
||||||
|
{
|
||||||
|
if (!analyzer || !node)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
fzl_an_res_t res = {.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||||
|
switch (node->type)
|
||||||
|
{
|
||||||
|
case FZL_NODETYPE_ASSIGN:
|
||||||
|
res = analyze_assign(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_NOT:
|
||||||
|
res = analyze_unarycond(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_OR:
|
||||||
|
case FZL_NODETYPE_AND:
|
||||||
|
case FZL_NODETYPE_LESS:
|
||||||
|
case FZL_NODETYPE_LESS_EQUAL:
|
||||||
|
case FZL_NODETYPE_GREATER:
|
||||||
|
case FZL_NODETYPE_GREATER_EQUAL:
|
||||||
|
case FZL_NODETYPE_EQUAL:
|
||||||
|
case FZL_NODETYPE_NEQUAL:
|
||||||
|
res = analyze_condition(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_UNARY_MINUS:
|
||||||
|
case FZL_NODETYPE_UNARY_PLUS:
|
||||||
|
res = analyze_unarymath(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_ADD:
|
||||||
|
case FZL_NODETYPE_SUB:
|
||||||
|
case FZL_NODETYPE_MUL:
|
||||||
|
case FZL_NODETYPE_DIV:
|
||||||
|
case FZL_NODETYPE_POW:
|
||||||
|
case FZL_NODETYPE_MOD:
|
||||||
|
res = analyze_binmath(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_BREAK:
|
||||||
|
res = analyze_break(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_LOOP:
|
||||||
|
res = analyze_loop(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_CONSTRUCT:
|
||||||
|
res = analyze_construct(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_DECLARATION:
|
||||||
|
res = analyze_declaration(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_FIELD:
|
||||||
|
res = analyze_field(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_CLASS:
|
||||||
|
res = analyze_class(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_RETURN:
|
||||||
|
res = analyze_return(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_FUNCTION:
|
||||||
|
res = analyze_function(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_CONDITIONAL:
|
||||||
|
res = analyze_conditional(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_IDENTIFIER:
|
||||||
|
res = analyze_identifier(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_BLOCK:
|
||||||
|
res = analyze_block(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_CALL:
|
||||||
|
res = analyze_call(analyzer, node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_LITERAL:
|
||||||
|
switch (node->token->type)
|
||||||
|
{
|
||||||
|
case FZL_TOKTYPE_STR_LITERAL:
|
||||||
|
res.type = FZL_TYPE_STRING;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_TOKTYPE_NUM_LITERAL:
|
||||||
|
res.type = FZL_TYPE_INT;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_TOKTYPE_BOOL_LITERAL:
|
||||||
|
res.type = FZL_TYPE_BOOL;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_TOKTYPE_NULL_LITERAL:
|
||||||
|
res.type = FZL_TYPE_OBJECT;
|
||||||
|
res.classname = "null";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
an_push_error(analyzer, node->token, "unknown literal type");
|
||||||
|
res.error = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze binary conditional nodes
|
||||||
|
fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!cond_node || (cond_node->type != FZL_NODETYPE_OR &&
|
||||||
|
cond_node->type != FZL_NODETYPE_AND &&
|
||||||
|
cond_node->type != FZL_NODETYPE_LESS &&
|
||||||
|
cond_node->type != FZL_NODETYPE_LESS_EQUAL &&
|
||||||
|
cond_node->type != FZL_NODETYPE_GREATER &&
|
||||||
|
cond_node->type != FZL_NODETYPE_GREATER_EQUAL &&
|
||||||
|
cond_node->type != FZL_NODETYPE_EQUAL &&
|
||||||
|
cond_node->type != FZL_NODETYPE_NEQUAL))
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not a binary conditional operator");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t left = analyze_common(analyzer, cond_node->binary_op.left);
|
||||||
|
if (left.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
fzl_an_res_t right = analyze_common(analyzer, cond_node->binary_op.right);
|
||||||
|
if (right.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (cond_node->type == FZL_NODETYPE_OR || cond_node->type == FZL_NODETYPE_AND)
|
||||||
|
{
|
||||||
|
if (left.type != FZL_TYPE_BOOL)
|
||||||
|
an_push_warning(analyzer, cond_node->binary_op.left->token,
|
||||||
|
"left operand is not resulting with boolean type");
|
||||||
|
|
||||||
|
if (right.type != FZL_TYPE_BOOL)
|
||||||
|
an_push_warning(analyzer, cond_node->binary_op.right->token,
|
||||||
|
"right operand is not resulting with boolean type");
|
||||||
|
}
|
||||||
|
else if (cond_node->type == FZL_NODETYPE_LESS ||
|
||||||
|
cond_node->type == FZL_NODETYPE_LESS_EQUAL ||
|
||||||
|
cond_node->type == FZL_NODETYPE_GREATER ||
|
||||||
|
cond_node->type == FZL_NODETYPE_GREATER_EQUAL)
|
||||||
|
{
|
||||||
|
if (left.type < FZL_TYPE_INT8)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||||
|
"left operand must have integer type");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left.type < FZL_TYPE_INT8)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||||
|
"right operand must have integer type");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (left.type >= FZL_TYPE_INT8)
|
||||||
|
{
|
||||||
|
if (right.type < FZL_TYPE_INT8)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||||
|
"right operand must have integer type");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||||
|
}
|
||||||
|
else if (left.type == FZL_TYPE_OBJECT)
|
||||||
|
{
|
||||||
|
if (!left.classname || !right.classname)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||||
|
"unhandled error :(");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!an_is_compatible(left.type, right.type, left.classname,
|
||||||
|
right.classname))
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "'%s' and '%s' are incompatible",
|
||||||
|
an_typetostr(left.type, left.classname),
|
||||||
|
an_typetostr(right.type, right.classname));
|
||||||
|
an_push_error(analyzer, cond_node->binary_op.right->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (left.type == FZL_TYPE_STRING)
|
||||||
|
{
|
||||||
|
if (right.type != FZL_TYPE_STRING)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||||
|
"right operand must be a string");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!an_is_compatible(left.type, right.type, left.classname,
|
||||||
|
right.classname))
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"equality not supported between %s and %s",
|
||||||
|
an_typetostr(left.type, left.classname),
|
||||||
|
an_typetostr(right.type, right.classname));
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze conditional statement
|
||||||
|
fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!cond || cond->type != FZL_NODETYPE_CONDITIONAL)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "provided node is a condition");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t cond_res = analyze_common(analyzer, cond->cond.condition);
|
||||||
|
fzl_an_res_t true_res = analyze_common(analyzer, cond->cond.true_block);
|
||||||
|
|
||||||
|
if (cond->cond.else_block)
|
||||||
|
{
|
||||||
|
fzl_an_res_t else_res = analyze_common(analyzer, cond->cond.else_block);
|
||||||
|
if (else_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cond_res.error || true_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (cond_res.type != FZL_TYPE_BOOL)
|
||||||
|
an_push_warning(analyzer, cond->cond.condition->token,
|
||||||
|
"resulting type is not bool");
|
||||||
|
|
||||||
|
if (cond->cond.condition->token->type == FZL_TOKTYPE_BOOL_LITERAL)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "condition is always %s",
|
||||||
|
(char *)cond->cond.condition->token->value);
|
||||||
|
an_push_warning(analyzer, cond->cond.condition->token, an_problem);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze class construct
|
||||||
|
fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!construct || construct->type != FZL_NODETYPE_CONSTRUCT)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not a construct");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
char *classname = (char *)construct->construct.name->token->value;
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool sym_found = false;
|
||||||
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((sym_found = fzl_symt_get(analyzer->symbols[i], classname, &sym)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sym_found)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "class '%s' not found", classname);
|
||||||
|
an_push_error(analyzer, construct->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = FZL_TYPE_OBJECT, .classname = classname};
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze variable/field/argument declaration
|
||||||
|
fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!decl || decl->type != FZL_NODETYPE_DECLARATION)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not a declaration");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
char *name = (char *)decl->var.identifier->token->value;
|
||||||
|
fzl_type_t type = an_toktotype(decl->var.type);
|
||||||
|
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);
|
||||||
|
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||||
|
{
|
||||||
|
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "'%s' already has been declared",
|
||||||
|
name);
|
||||||
|
else
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "cannot store '%s'", name);
|
||||||
|
|
||||||
|
an_push_error(analyzer, decl->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == FZL_TYPE_OBJECT)
|
||||||
|
{
|
||||||
|
fzl_sym_t class;
|
||||||
|
bool class_found = false;
|
||||||
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((class_found = fzl_symt_get(analyzer->symbols[i],
|
||||||
|
decl->var.type->value, &class)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!class_found || class.node->type != FZL_NODETYPE_CLASS)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "class '%s' does not exists",
|
||||||
|
(char *)decl->var.type->value);
|
||||||
|
an_push_error(analyzer, decl->var.type, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_symt_setobjclass(analyzer->symbols[analyzer->symbols_top - 1], name,
|
||||||
|
classname);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Analyze field access
|
||||||
|
fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!field || field->type != FZL_NODETYPE_FIELD)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not field access");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t parent = analyze_common(analyzer, field->memb.parent);
|
||||||
|
if (parent.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (parent.type != FZL_TYPE_OBJECT || parent.classname == NULL)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "access to non-object value (%s)",
|
||||||
|
an_typetostr(parent.type, parent.classname));
|
||||||
|
an_push_error(analyzer, field->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
char *field_name = field->memb.field->token->value;
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool sym_found = false;
|
||||||
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((sym_found =
|
||||||
|
fzl_symt_get(analyzer->symbols[i], parent.classname, &sym)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sym_found)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "class '%s' not found",
|
||||||
|
parent.classname);
|
||||||
|
an_push_error(analyzer, field->parent->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *field_node = NULL;
|
||||||
|
for (size_t i = 0; i < sym.node->class.fields_count; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(sym.node->class.fields[i]->var.identifier->token->value,
|
||||||
|
field_name) == 0)
|
||||||
|
{
|
||||||
|
field_node = sym.node->class.fields[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (field_node == NULL)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "field '%s.%s' does not exists",
|
||||||
|
parent.classname, field_name);
|
||||||
|
an_push_error(analyzer, field->memb.field->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_type_t field_type = an_toktotype(field_node->var.type);
|
||||||
|
char *classname = NULL;
|
||||||
|
if (field_type == FZL_TYPE_OBJECT)
|
||||||
|
classname = field_node->var.type->value;
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = field_type, .classname = classname};
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!fn_node || fn_node->type != FZL_NODETYPE_FUNCTION)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not a function");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_ctx_t *fnctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_FUNCTION);
|
||||||
|
fnctx->fn = fn_node;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < fn_node->fn.args_count; i++)
|
||||||
|
{
|
||||||
|
fzl_an_res_t res = analyze_common(analyzer, fn_node->fn.args[i]);
|
||||||
|
if (res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_ctx_t *ctx = analyzer->ctx;
|
||||||
|
while (ctx != NULL)
|
||||||
|
{
|
||||||
|
if (ctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||||
|
break;
|
||||||
|
|
||||||
|
ctx = ctx->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_type_t rettype = an_toktotype(fn_node->fn.type);
|
||||||
|
char *classname = NULL;
|
||||||
|
if (rettype == FZL_TYPE_OBJECT)
|
||||||
|
classname = (char *)fn_node->fn.type->value;
|
||||||
|
|
||||||
|
if (ctx != NULL && ctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < fn_node->fn.args_count; i++)
|
||||||
|
{
|
||||||
|
fzl_node_t *decl = fn_node->fn.args[i];
|
||||||
|
if (decl->type != FZL_NODETYPE_DECLARATION)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, decl->token, "argument must be a declaration");
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
char *argname = (char *)decl->var.identifier->token->value;
|
||||||
|
if (strcmp(argname, "this") == 0 && i > 0)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, decl->token,
|
||||||
|
"manual declaration of 'this' is not allowed in methods");
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t classname_len = strlen(ctx->classname);
|
||||||
|
size_t methodname_len = strlen((char *)fn_node->fn.name->value);
|
||||||
|
char *methodname = malloc(classname_len + methodname_len + 2);
|
||||||
|
|
||||||
|
strcpy(methodname, ctx->classname);
|
||||||
|
methodname[classname_len] = '_';
|
||||||
|
strcpy(methodname + classname_len + 1, fn_node->fn.name->value);
|
||||||
|
methodname[classname_len + methodname_len + 1] = 0;
|
||||||
|
|
||||||
|
analyzer->alloc_names[analyzer->alloc_names_top++] = methodname;
|
||||||
|
|
||||||
|
fzl_symtable_res_t putres =
|
||||||
|
fzl_symt_put(analyzer->symbols[ctx->symbols_level - 1], fn_node,
|
||||||
|
methodname, rettype, 0);
|
||||||
|
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||||
|
{
|
||||||
|
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"method '%s.%s' already has been declared", classname,
|
||||||
|
(char *)fn_node->fn.name->value);
|
||||||
|
an_push_error(analyzer, fn_node->token, an_problem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
an_push_error(analyzer, fn_node->token, "failed to store method");
|
||||||
|
|
||||||
|
free(analyzer->alloc_names[--analyzer->alloc_names_top]);
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
fzl_symt_setobjclass(analyzer->symbols[ctx->symbols_level - 1], methodname,
|
||||||
|
classname);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fzl_symtable_res_t putres =
|
||||||
|
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], fn_node,
|
||||||
|
(char *)fn_node->fn.name->value, rettype, 0);
|
||||||
|
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||||
|
{
|
||||||
|
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"function '%s' already has been declared",
|
||||||
|
(char *)fn_node->fn.name->value);
|
||||||
|
an_push_error(analyzer, fn_node->token, an_problem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
an_push_error(analyzer, fn_node->token, "failed to store function");
|
||||||
|
|
||||||
|
free(analyzer->alloc_names[--analyzer->alloc_names_top]);
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t body_res = analyze_common(analyzer, fn_node->fn.body);
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = body_res.error, .type = rettype, .classname = classname};
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze identifier
|
||||||
|
fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!id || id->type != FZL_NODETYPE_IDENTIFIER)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not an identifier");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
char *symname = id->token->value;
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool sym_found = false;
|
||||||
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((sym_found = fzl_symt_get(analyzer->symbols[i], symname, &sym)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sym_found)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "cannot find symbol '%s'",
|
||||||
|
symname);
|
||||||
|
an_push_error(analyzer, id->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = sym.type, .classname = sym.classname};
|
||||||
|
}
|
||||||
@@ -0,0 +1,455 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Init block analysis result
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
bool has_var;
|
||||||
|
char *name;
|
||||||
|
fzl_type_t type;
|
||||||
|
char *classname;
|
||||||
|
} init_analysis;
|
||||||
|
|
||||||
|
// Check init block
|
||||||
|
init_analysis check_init(fzl_analyzer_t *analyzer, fzl_node_t *init)
|
||||||
|
{
|
||||||
|
if (!analyzer || !init)
|
||||||
|
return (init_analysis){.has_var = false};
|
||||||
|
|
||||||
|
if (init->type == FZL_NODETYPE_IDENTIFIER)
|
||||||
|
{
|
||||||
|
char *name = init->token->value;
|
||||||
|
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool sym_found = false;
|
||||||
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((sym_found = fzl_symt_get(analyzer->symbols[i], name, &sym)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sym_found)
|
||||||
|
return (init_analysis){.has_var = false};
|
||||||
|
|
||||||
|
char *classname = NULL;
|
||||||
|
if (sym.type == FZL_TYPE_OBJECT)
|
||||||
|
classname = sym.classname;
|
||||||
|
|
||||||
|
return (init_analysis){.has_var = true,
|
||||||
|
.name = name,
|
||||||
|
.type = sym.type,
|
||||||
|
.classname = classname};
|
||||||
|
}
|
||||||
|
else if (init->type == FZL_NODETYPE_DECLARATION)
|
||||||
|
{
|
||||||
|
char *name = init->var.identifier->token->value;
|
||||||
|
fzl_type_t type = an_toktotype(init->var.type);
|
||||||
|
char *classname = NULL;
|
||||||
|
if (type == FZL_TYPE_OBJECT)
|
||||||
|
classname = init->var.type->value;
|
||||||
|
|
||||||
|
return (init_analysis){
|
||||||
|
.has_var = true, .name = name, .type = type, .classname = classname};
|
||||||
|
}
|
||||||
|
else if (init->type == FZL_NODETYPE_ASSIGN)
|
||||||
|
{
|
||||||
|
return (init_analysis)check_init(analyzer, init->binary_op.left);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (init_analysis){.has_var = false};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Condition block analysis result type
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
COND_TYPE_CONST_TRUE, // `true` is used as condition
|
||||||
|
COND_TYPE_CONST_FALSE, // `false` is used as condition
|
||||||
|
COND_TYPE_VAR_LESS, // until variable value is less
|
||||||
|
COND_TYPE_VAR_GREATER, // until variable value is greater
|
||||||
|
COND_TYPE_UNKNOWN, // cannot determine in compilation time
|
||||||
|
} cond_type;
|
||||||
|
|
||||||
|
// Condition block analysis result
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
cond_type type;
|
||||||
|
bool result_bool;
|
||||||
|
char *varname;
|
||||||
|
fzl_type_t vartype;
|
||||||
|
char *classname;
|
||||||
|
} cond_analysis;
|
||||||
|
|
||||||
|
// Check condition block
|
||||||
|
cond_analysis check_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond,
|
||||||
|
bool report)
|
||||||
|
{
|
||||||
|
if (!cond)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||||
|
|
||||||
|
switch (cond->type)
|
||||||
|
{
|
||||||
|
case FZL_NODETYPE_LITERAL:
|
||||||
|
if (cond->token->type == FZL_TOKTYPE_STR_LITERAL)
|
||||||
|
{
|
||||||
|
if (report)
|
||||||
|
an_push_warning(analyzer, cond->token,
|
||||||
|
"string literal is used as condition");
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||||
|
}
|
||||||
|
else if (cond->token->type == FZL_TOKTYPE_NULL_LITERAL)
|
||||||
|
{
|
||||||
|
if (report)
|
||||||
|
an_push_warning(analyzer, cond->token,
|
||||||
|
"null literal is used as condition");
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||||
|
}
|
||||||
|
else if (cond->token->type == FZL_TOKTYPE_BOOL_LITERAL)
|
||||||
|
{
|
||||||
|
if (strcmp(cond->token->value, "true") == 0)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_CONST_TRUE,
|
||||||
|
.result_bool = true};
|
||||||
|
else
|
||||||
|
return (cond_analysis){.type = COND_TYPE_CONST_FALSE,
|
||||||
|
.result_bool = true};
|
||||||
|
}
|
||||||
|
else if (cond->token->type == FZL_TOKTYPE_NUM_LITERAL)
|
||||||
|
{
|
||||||
|
if (strcmp(cond->token->value, "0") == 0)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_CONST_FALSE,
|
||||||
|
.result_bool = false};
|
||||||
|
else
|
||||||
|
return (cond_analysis){.type = COND_TYPE_CONST_TRUE,
|
||||||
|
.result_bool = false};
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_LESS:
|
||||||
|
case FZL_NODETYPE_LESS_EQUAL:
|
||||||
|
{
|
||||||
|
fzl_node_t *comp_left = cond->binary_op.left;
|
||||||
|
cond_analysis left_check = check_condition(analyzer, comp_left, false);
|
||||||
|
if (left_check.varname != NULL)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_VAR_LESS,
|
||||||
|
.result_bool = true,
|
||||||
|
.varname = left_check.varname,
|
||||||
|
.vartype = left_check.vartype,
|
||||||
|
.classname = left_check.classname};
|
||||||
|
|
||||||
|
fzl_node_t *comp_right = cond->binary_op.right;
|
||||||
|
cond_analysis right_check = check_condition(analyzer, comp_right, false);
|
||||||
|
if (right_check.varname != NULL)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_VAR_GREATER,
|
||||||
|
.result_bool = true,
|
||||||
|
.varname = right_check.varname,
|
||||||
|
.vartype = right_check.vartype,
|
||||||
|
.classname = right_check.classname};
|
||||||
|
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN};
|
||||||
|
}
|
||||||
|
|
||||||
|
case FZL_NODETYPE_GREATER:
|
||||||
|
case FZL_NODETYPE_GREATER_EQUAL:
|
||||||
|
{
|
||||||
|
fzl_node_t *comp_left = cond->binary_op.left;
|
||||||
|
cond_analysis left_check = check_condition(analyzer, comp_left, false);
|
||||||
|
if (left_check.varname != NULL)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_VAR_GREATER,
|
||||||
|
.result_bool = true,
|
||||||
|
.varname = left_check.varname,
|
||||||
|
.vartype = left_check.vartype,
|
||||||
|
.classname = left_check.classname};
|
||||||
|
|
||||||
|
fzl_node_t *comp_right = cond->binary_op.right;
|
||||||
|
cond_analysis right_check = check_condition(analyzer, comp_right, false);
|
||||||
|
if (right_check.varname != NULL)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_VAR_LESS,
|
||||||
|
.result_bool = true,
|
||||||
|
.varname = right_check.varname,
|
||||||
|
.vartype = right_check.vartype,
|
||||||
|
.classname = right_check.classname};
|
||||||
|
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN};
|
||||||
|
}
|
||||||
|
|
||||||
|
case FZL_NODETYPE_IDENTIFIER:
|
||||||
|
{
|
||||||
|
char *name = cond->token->value;
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool sym_found = false;
|
||||||
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((sym_found = fzl_symt_get(analyzer->symbols[i], name, &sym)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sym_found)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .varname = NULL};
|
||||||
|
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN,
|
||||||
|
.varname = name,
|
||||||
|
.vartype = sym.type,
|
||||||
|
.classname = sym.classname};
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t custom_res = analyze_common(analyzer, cond);
|
||||||
|
if (custom_res.type == FZL_TYPE_BOOL)
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = true};
|
||||||
|
|
||||||
|
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step block change direction
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
STEP_DIR_TYPE_INCREASE,
|
||||||
|
STEP_DIR_TYPE_DECREASE,
|
||||||
|
STEP_DIR_TYPE_STALL,
|
||||||
|
STEP_DIR_UNKNOWN,
|
||||||
|
} step_dir_type;
|
||||||
|
|
||||||
|
// Step block analysis
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
bool changing;
|
||||||
|
char *changing_var;
|
||||||
|
step_dir_type dir;
|
||||||
|
} step_analysis;
|
||||||
|
|
||||||
|
// Check step block
|
||||||
|
step_analysis check_step(fzl_node_t *step)
|
||||||
|
{
|
||||||
|
if (!step)
|
||||||
|
return (step_analysis){.changing = false};
|
||||||
|
|
||||||
|
if (step->type == FZL_NODETYPE_ASSIGN)
|
||||||
|
{
|
||||||
|
if (step->binary_op.left->type != FZL_NODETYPE_IDENTIFIER)
|
||||||
|
return (step_analysis){
|
||||||
|
.changing = true, .changing_var = "-", .dir = STEP_DIR_UNKNOWN};
|
||||||
|
|
||||||
|
int dir = 0;
|
||||||
|
fzl_node_t *right = step->binary_op.right;
|
||||||
|
fzl_node_t *num = NULL;
|
||||||
|
char *varname = NULL;
|
||||||
|
if (right->binary_op.left->type != FZL_NODETYPE_IDENTIFIER)
|
||||||
|
{
|
||||||
|
num = right->binary_op.left;
|
||||||
|
if (right->binary_op.right->type == FZL_NODETYPE_IDENTIFIER)
|
||||||
|
varname = right->binary_op.right->token->value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
num = right->binary_op.right;
|
||||||
|
if (right->binary_op.left->type == FZL_NODETYPE_IDENTIFIER)
|
||||||
|
varname = right->binary_op.left->token->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (right->type == FZL_NODETYPE_ADD || right->type == FZL_NODETYPE_MUL ||
|
||||||
|
right->type == FZL_NODETYPE_POW)
|
||||||
|
dir = 1;
|
||||||
|
else if (right->type == FZL_NODETYPE_SUB ||
|
||||||
|
right->type == FZL_NODETYPE_DIV || right->type == FZL_NODETYPE_MOD)
|
||||||
|
dir = -1;
|
||||||
|
|
||||||
|
if (num && num->type == FZL_NODETYPE_UNARY_MINUS)
|
||||||
|
dir *= -1;
|
||||||
|
|
||||||
|
step_dir_type dirtype = varname == NULL ? STEP_DIR_UNKNOWN
|
||||||
|
: dir > 0 ? STEP_DIR_TYPE_INCREASE
|
||||||
|
: dir < 0 ? STEP_DIR_TYPE_DECREASE
|
||||||
|
: STEP_DIR_TYPE_STALL;
|
||||||
|
|
||||||
|
return (step_analysis){.changing = varname != NULL && dir != 0,
|
||||||
|
.changing_var = varname,
|
||||||
|
.dir = dirtype};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (step_analysis){.changing = false, .dir = STEP_DIR_UNKNOWN};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body analysis result
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
bool has_exit;
|
||||||
|
} body_analysis;
|
||||||
|
|
||||||
|
body_analysis check_body(fzl_node_t *body, bool nested_loop)
|
||||||
|
{
|
||||||
|
body_analysis result = {.has_exit = false};
|
||||||
|
switch (body->type)
|
||||||
|
{
|
||||||
|
case FZL_NODETYPE_RETURN:
|
||||||
|
result.has_exit = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_BREAK:
|
||||||
|
if (!nested_loop)
|
||||||
|
result.has_exit = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_LOOP:
|
||||||
|
if (body->loop.body)
|
||||||
|
{
|
||||||
|
body_analysis res = check_body(body->loop.body, true);
|
||||||
|
if (res.has_exit)
|
||||||
|
result.has_exit = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_CONDITIONAL:
|
||||||
|
if (check_body(body->cond.true_block, nested_loop).has_exit)
|
||||||
|
{
|
||||||
|
result.has_exit = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_BLOCK:
|
||||||
|
for (size_t i = 0; i < body->list.entry_count; i++)
|
||||||
|
{
|
||||||
|
body_analysis res = check_body(body->list.entries[i], nested_loop);
|
||||||
|
if (res.has_exit)
|
||||||
|
{
|
||||||
|
result.has_exit = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Analyze loop
|
||||||
|
fzl_an_res_t analyze_loop(fzl_analyzer_t *analyzer, fzl_node_t *loop_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!loop_node || loop_node->type != FZL_NODETYPE_LOOP)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not typeof LOOP");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_LOOP);
|
||||||
|
|
||||||
|
init_analysis init_data = {
|
||||||
|
.has_var = false, .type = FZL_TYPE_NULL, .name = NULL, .classname = NULL};
|
||||||
|
if (loop_node->loop.init)
|
||||||
|
{
|
||||||
|
fzl_an_res_t init_res = analyze_common(analyzer, loop_node->loop.init);
|
||||||
|
if (init_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
init_data = check_init(analyzer, loop_node->loop.init);
|
||||||
|
}
|
||||||
|
|
||||||
|
cond_analysis cond_data = {.type = COND_TYPE_CONST_TRUE,
|
||||||
|
.varname = NULL,
|
||||||
|
.vartype = FZL_TYPE_NULL,
|
||||||
|
.classname = NULL};
|
||||||
|
if (loop_node->loop.cond)
|
||||||
|
{
|
||||||
|
fzl_an_res_t cond_res = analyze_common(analyzer, loop_node->loop.cond);
|
||||||
|
if (cond_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
cond_data = check_condition(analyzer, loop_node->loop.cond, true);
|
||||||
|
|
||||||
|
if (!cond_data.result_bool && cond_data.type != COND_TYPE_UNKNOWN)
|
||||||
|
an_push_warning(analyzer, loop_node->loop.cond->token,
|
||||||
|
"resulting type is not boolean");
|
||||||
|
|
||||||
|
if (cond_data.type == COND_TYPE_CONST_FALSE)
|
||||||
|
an_push_warning(analyzer, loop_node->loop.cond->token,
|
||||||
|
"loop will never execute");
|
||||||
|
|
||||||
|
if (init_data.name && cond_data.varname &&
|
||||||
|
strcmp(init_data.name, cond_data.varname) != 0)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"'%s' is used in condition but '%s' is declared. Maybe there is "
|
||||||
|
"mistake?",
|
||||||
|
cond_data.varname, init_data.name);
|
||||||
|
an_push_warning(analyzer, loop_node->loop.cond->token, an_problem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
step_analysis step_data = {
|
||||||
|
.changing = false, .changing_var = NULL, .dir = STEP_DIR_UNKNOWN};
|
||||||
|
if (loop_node->loop.step)
|
||||||
|
{
|
||||||
|
fzl_an_res_t step_res = analyze_common(analyzer, loop_node->loop.step);
|
||||||
|
if (step_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
step_data = check_step(loop_node->loop.step);
|
||||||
|
|
||||||
|
if (step_data.dir != STEP_DIR_UNKNOWN)
|
||||||
|
{
|
||||||
|
if (step_data.changing_var && cond_data.varname &&
|
||||||
|
strcmp(step_data.changing_var, cond_data.varname) != 0)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"changing '%s' but condition checking '%s'",
|
||||||
|
step_data.changing_var, cond_data.varname);
|
||||||
|
an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cond_data.type == COND_TYPE_VAR_GREATER &&
|
||||||
|
step_data.dir == STEP_DIR_TYPE_INCREASE)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"'%s' is increasing while condition is 'greater than'",
|
||||||
|
step_data.changing_var);
|
||||||
|
an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
|
||||||
|
}
|
||||||
|
else if (cond_data.type == COND_TYPE_VAR_LESS &&
|
||||||
|
step_data.dir == STEP_DIR_TYPE_DECREASE)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"'%s' is decreasing while condition is 'less than'",
|
||||||
|
step_data.changing_var);
|
||||||
|
an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
|
||||||
|
}
|
||||||
|
else if ((cond_data.type == COND_TYPE_VAR_LESS ||
|
||||||
|
cond_data.type == COND_TYPE_VAR_GREATER) &&
|
||||||
|
step_data.dir == STEP_DIR_TYPE_STALL)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem), "'%s' is stalled",
|
||||||
|
cond_data.varname);
|
||||||
|
an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body_analysis body_data = {.has_exit = false};
|
||||||
|
if (cond_data.type != COND_TYPE_CONST_FALSE)
|
||||||
|
{
|
||||||
|
fzl_an_res_t body_res = analyze_common(analyzer, loop_node->loop.body);
|
||||||
|
if (body_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
body_data = check_body(loop_node->loop.body, false);
|
||||||
|
if (cond_data.type == COND_TYPE_CONST_TRUE && !body_data.has_exit)
|
||||||
|
an_push_warning(analyzer, loop_node->token, "possibly infinite loop");
|
||||||
|
}
|
||||||
|
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
|
||||||
|
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Analyze return statement
|
||||||
|
fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!ret_node || ret_node->type != FZL_NODETYPE_RETURN)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not a return statement");
|
||||||
|
return (fzl_an_res_t){.error = false};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_ctx_t *fnctx = analyzer->ctx;
|
||||||
|
while (fnctx != NULL)
|
||||||
|
{
|
||||||
|
if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||||
|
break;
|
||||||
|
|
||||||
|
fnctx = fnctx->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_ctx_t *classctx = analyzer->ctx;
|
||||||
|
while (classctx != NULL)
|
||||||
|
{
|
||||||
|
if (classctx && classctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||||
|
break;
|
||||||
|
|
||||||
|
classctx = classctx->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_in_method =
|
||||||
|
classctx != NULL && classctx->type == FZL_AN_CTX_TYPE_CLASS;
|
||||||
|
|
||||||
|
if (!fnctx || fnctx->type != FZL_AN_CTX_TYPE_FUNCTION)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, ret_node->token,
|
||||||
|
"cannot use 'return' outside the function");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_type_t rettype = an_toktotype(fnctx->fn->fn.type);
|
||||||
|
char *classname = NULL;
|
||||||
|
if (rettype == FZL_TYPE_OBJECT)
|
||||||
|
classname = (char *)fnctx->fn->fn.type->value;
|
||||||
|
|
||||||
|
char *rettype_str = an_typetostr(rettype, classname);
|
||||||
|
if (rettype != FZL_TYPE_NULL)
|
||||||
|
{
|
||||||
|
if (ret_node->ret.value == NULL)
|
||||||
|
{
|
||||||
|
if (is_in_method)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"cannot return null as method '%s.%s' have return type '%s'",
|
||||||
|
classctx->classname, (char *)fnctx->fn->fn.name->value,
|
||||||
|
rettype_str);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"cannot return null as function '%s' have return type '%s'",
|
||||||
|
(char *)fnctx->fn->fn.name->value, rettype_str);
|
||||||
|
}
|
||||||
|
an_push_error(analyzer, ret_node->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t ret_res = analyze_common(analyzer, ret_node->ret.value);
|
||||||
|
if (ret_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname))
|
||||||
|
{
|
||||||
|
if (is_in_method)
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"method '%s.%s' is expecting return type '%s', got '%s'",
|
||||||
|
classctx->classname, (char *)fnctx->fn->fn.name->value,
|
||||||
|
rettype_str, an_typetostr(ret_res.type, ret_res.classname));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(an_problem, sizeof(an_problem),
|
||||||
|
"function '%s' is expecting return type '%s', got '%s'",
|
||||||
|
(char *)fnctx->fn->fn.name->value, rettype_str,
|
||||||
|
an_typetostr(ret_res.type, ret_res.classname));
|
||||||
|
}
|
||||||
|
an_push_error(analyzer, ret_node->token, an_problem);
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = rettype, .classname = classname};
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
|
||||||
|
// Analyze unary condition
|
||||||
|
fzl_an_res_t analyze_unarycond(fzl_analyzer_t *analyzer, fzl_node_t *unary)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!unary || unary->type != FZL_NODETYPE_NOT)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not an unary condition operator");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
|
||||||
|
if (operand_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (operand_res.type != FZL_TYPE_BOOL && operand_res.type < FZL_TYPE_INT8)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, unary->unary_op.operand->token,
|
||||||
|
"unary condition only support bool and integer types");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// Analyze unary arithmetic
|
||||||
|
fzl_an_res_t analyze_unarymath(fzl_analyzer_t *analyzer, fzl_node_t *unary)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (!unary || (unary->type != FZL_NODETYPE_UNARY_MINUS &&
|
||||||
|
unary->type != FZL_NODETYPE_UNARY_PLUS))
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "node is not an unary math operator");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
|
||||||
|
if (operand_res.error)
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
|
||||||
|
if (operand_res.type < FZL_TYPE_INT8)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, unary->unary_op.operand->token,
|
||||||
|
"unary operator only support integer types");
|
||||||
|
return (fzl_an_res_t){.error = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fzl_an_res_t){
|
||||||
|
.error = false, .type = operand_res.type, .classname = NULL};
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Nest context
|
||||||
|
fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
fzl_an_ctx_t *ctx = malloc(sizeof(fzl_an_ctx_t));
|
||||||
|
if (!ctx)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memset(ctx, 0, sizeof(fzl_an_ctx_t));
|
||||||
|
|
||||||
|
ctx->type = type;
|
||||||
|
if (analyzer->ctx != NULL)
|
||||||
|
ctx->parent = analyzer->ctx;
|
||||||
|
else
|
||||||
|
ctx->parent = NULL;
|
||||||
|
|
||||||
|
ctx->symbols = fzl_mksymt();
|
||||||
|
ctx->symbols_level = analyzer->symbols_top;
|
||||||
|
analyzer->ctx = ctx;
|
||||||
|
analyzer->symbols[analyzer->symbols_top++] = ctx->symbols;
|
||||||
|
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close context and return parent
|
||||||
|
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer)
|
||||||
|
{
|
||||||
|
if (!analyzer || analyzer->ctx == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
fzl_an_ctx_t *current = analyzer->ctx;
|
||||||
|
fzl_an_ctx_t *parent = current->parent;
|
||||||
|
analyzer->ctx = parent;
|
||||||
|
|
||||||
|
free(current);
|
||||||
|
fzl_freesymt(analyzer->symbols[--analyzer->symbols_top]);
|
||||||
|
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Check compatibility between A and B types (and classnames)
|
||||||
|
bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc)
|
||||||
|
{
|
||||||
|
if (a == b)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (a == FZL_TYPE_OBJECT && b == FZL_TYPE_OBJECT)
|
||||||
|
{
|
||||||
|
if (strcmp(ac, "null") == 0 || strcmp(bc, "null") == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return strcmp(ac, bc) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a >= FZL_TYPE_INT8 && b >= FZL_TYPE_INT8)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert type to string
|
||||||
|
char *an_typetostr(fzl_type_t type, char *classname)
|
||||||
|
{
|
||||||
|
if (classname)
|
||||||
|
return classname;
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case FZL_TYPE_NULL:
|
||||||
|
return "null";
|
||||||
|
|
||||||
|
case FZL_TYPE_BOOL:
|
||||||
|
return "boolean";
|
||||||
|
|
||||||
|
case FZL_TYPE_STRING:
|
||||||
|
return "string";
|
||||||
|
|
||||||
|
case FZL_TYPE_CHAR:
|
||||||
|
return "char";
|
||||||
|
|
||||||
|
case FZL_TYPE_INT8:
|
||||||
|
return "i8";
|
||||||
|
|
||||||
|
case FZL_TYPE_INT16:
|
||||||
|
return "i16";
|
||||||
|
|
||||||
|
case FZL_TYPE_INT32:
|
||||||
|
return "i32";
|
||||||
|
|
||||||
|
case FZL_TYPE_INT64:
|
||||||
|
return "i64";
|
||||||
|
|
||||||
|
case FZL_TYPE_UINT8:
|
||||||
|
return "u8";
|
||||||
|
|
||||||
|
case FZL_TYPE_UINT16:
|
||||||
|
return "u16";
|
||||||
|
|
||||||
|
case FZL_TYPE_UINT32:
|
||||||
|
return "u32";
|
||||||
|
|
||||||
|
case FZL_TYPE_UINT64:
|
||||||
|
return "u64";
|
||||||
|
|
||||||
|
case FZL_TYPE_INT:
|
||||||
|
return "int";
|
||||||
|
|
||||||
|
case FZL_TYPE_UINT:
|
||||||
|
return "uint";
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "<unknown>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert type token to type
|
||||||
|
fzl_type_t an_toktotype(fzl_token_t *tok)
|
||||||
|
{
|
||||||
|
if (!tok)
|
||||||
|
return FZL_TYPE_NULL;
|
||||||
|
|
||||||
|
if (tok->type == FZL_TOKTYPE_NAME)
|
||||||
|
return FZL_TYPE_OBJECT;
|
||||||
|
|
||||||
|
if (strcmp(tok->value, "null") == 0)
|
||||||
|
return FZL_TYPE_NULL;
|
||||||
|
|
||||||
|
if (strcmp((char *)tok->value, "i8") == 0)
|
||||||
|
return FZL_TYPE_INT8;
|
||||||
|
if (strcmp((char *)tok->value, "i16") == 0)
|
||||||
|
return FZL_TYPE_INT16;
|
||||||
|
if (strcmp((char *)tok->value, "i32") == 0)
|
||||||
|
return FZL_TYPE_INT32;
|
||||||
|
if (strcmp((char *)tok->value, "i64") == 0)
|
||||||
|
return FZL_TYPE_INT64;
|
||||||
|
|
||||||
|
if (strcmp((char *)tok->value, "u8") == 0)
|
||||||
|
return FZL_TYPE_UINT8;
|
||||||
|
if (strcmp((char *)tok->value, "u16") == 0)
|
||||||
|
return FZL_TYPE_UINT16;
|
||||||
|
if (strcmp((char *)tok->value, "u32") == 0)
|
||||||
|
return FZL_TYPE_UINT32;
|
||||||
|
if (strcmp((char *)tok->value, "u64") == 0)
|
||||||
|
return FZL_TYPE_UINT64;
|
||||||
|
|
||||||
|
if (strcmp((char *)tok->value, "int") == 0)
|
||||||
|
return FZL_TYPE_INT;
|
||||||
|
if (strcmp((char *)tok->value, "uint") == 0)
|
||||||
|
return FZL_TYPE_UINT;
|
||||||
|
|
||||||
|
if (strcmp((char *)tok->value, "bool") == 0)
|
||||||
|
return FZL_TYPE_BOOL;
|
||||||
|
if (strcmp((char *)tok->value, "str") == 0)
|
||||||
|
return FZL_TYPE_STRING;
|
||||||
|
if (strcmp((char *)tok->value, "char") == 0)
|
||||||
|
return FZL_TYPE_CHAR;
|
||||||
|
|
||||||
|
return FZL_TYPE_NULL;
|
||||||
|
}
|
||||||
@@ -0,0 +1,229 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char aerrors[256][256];
|
||||||
|
size_t aerrors_top;
|
||||||
|
|
||||||
|
char awarnings[256][256];
|
||||||
|
size_t awarnings_top;
|
||||||
|
|
||||||
|
char an_problem[256];
|
||||||
|
|
||||||
|
fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node)
|
||||||
|
{
|
||||||
|
if (!node)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
fzl_analyzer_t *analyzer = malloc(sizeof(fzl_analyzer_t));
|
||||||
|
if (!analyzer)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
analyzer->root = node;
|
||||||
|
analyzer->warnings_top = 0;
|
||||||
|
analyzer->errors_top = 0;
|
||||||
|
analyzer->symbols_top = 0;
|
||||||
|
analyzer->alloc_names_top = 0;
|
||||||
|
|
||||||
|
memset(analyzer->errors, 0, sizeof(analyzer->errors));
|
||||||
|
memset(analyzer->warnings, 0, sizeof(analyzer->warnings));
|
||||||
|
memset(analyzer->symbols, 0, sizeof(analyzer->symbols));
|
||||||
|
memset(analyzer->alloc_names, 0, sizeof(analyzer->alloc_names));
|
||||||
|
|
||||||
|
memset(aerrors, 0, sizeof(aerrors));
|
||||||
|
memset(awarnings, 0, sizeof(awarnings));
|
||||||
|
|
||||||
|
return analyzer;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error)
|
||||||
|
{
|
||||||
|
if (analyzer->errors_top >= 256 || !analyzer || !error)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
size_t ei = aerrors_top++;
|
||||||
|
if (tok != NULL)
|
||||||
|
snprintf(aerrors[ei], sizeof(aerrors[ei]), "'%s':%zu:%zu => %s",
|
||||||
|
tok->filename, tok->ln, tok->cn, error);
|
||||||
|
else
|
||||||
|
snprintf(aerrors[ei], sizeof(aerrors[ei]), "<unknown location> => %s",
|
||||||
|
error);
|
||||||
|
|
||||||
|
analyzer->errors[analyzer->errors_top++] = aerrors[ei];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning)
|
||||||
|
{
|
||||||
|
if (analyzer->warnings_top >= 256 || !analyzer || !warning)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
size_t wi = awarnings_top++;
|
||||||
|
if (tok != NULL)
|
||||||
|
snprintf(awarnings[wi], sizeof(awarnings[wi]), "'%s':%zu:%zu => %s",
|
||||||
|
tok->filename, tok->ln, tok->cn, warning);
|
||||||
|
else
|
||||||
|
snprintf(awarnings[wi], sizeof(awarnings[wi]), "<unknown location> => %s",
|
||||||
|
warning);
|
||||||
|
|
||||||
|
analyzer->warnings[analyzer->warnings_top++] = awarnings[wi];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_analyze(fzl_analyzer_t *analyzer)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY) == NULL)
|
||||||
|
{
|
||||||
|
an_push_error(analyzer, NULL, "failed to make global context");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *current = analyzer->root;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
fzl_an_res_t res = analyze_common(analyzer, current);
|
||||||
|
if (res.error)
|
||||||
|
break;
|
||||||
|
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ssize_t i = analyzer->symbols_top; i > 0; i--)
|
||||||
|
an_fin_ctx(analyzer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_freeanalyzer(fzl_analyzer_t *analyzer)
|
||||||
|
{
|
||||||
|
if (!analyzer)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (analyzer->alloc_names_top > 0)
|
||||||
|
for (ssize_t i = analyzer->alloc_names_top - 1; i >= 0; i--)
|
||||||
|
free(analyzer->alloc_names[i]);
|
||||||
|
|
||||||
|
memset(analyzer->errors, 0, sizeof(analyzer->errors));
|
||||||
|
memset(analyzer->warnings, 0, sizeof(analyzer->warnings));
|
||||||
|
memset(analyzer->symbols, 0, sizeof(analyzer->symbols));
|
||||||
|
memset(analyzer->alloc_names, 0, sizeof(analyzer->alloc_names));
|
||||||
|
free(analyzer);
|
||||||
|
}
|
||||||
@@ -31,6 +31,22 @@ void print_node(fzl_node_t *node, int level)
|
|||||||
|
|
||||||
switch (node->type)
|
switch (node->type)
|
||||||
{
|
{
|
||||||
|
case FZL_NODETYPE_LOOP:
|
||||||
|
printf("%s Loop:\n", levelbuf);
|
||||||
|
printf("-%s Init:\n", levelbuf);
|
||||||
|
if (node->loop.init)
|
||||||
|
print_node(node->loop.init, level + 2);
|
||||||
|
printf("-%s Condition:\n", levelbuf);
|
||||||
|
if (node->loop.cond)
|
||||||
|
print_node(node->loop.cond, level + 2);
|
||||||
|
printf("-%s Step:\n", levelbuf);
|
||||||
|
if (node->loop.step)
|
||||||
|
print_node(node->loop.step, level + 2);
|
||||||
|
printf("-%s Body:\n", levelbuf);
|
||||||
|
if (node->loop.body)
|
||||||
|
print_node(node->loop.body, level + 2);
|
||||||
|
break;
|
||||||
|
|
||||||
case FZL_NODETYPE_CONSTRUCT:
|
case FZL_NODETYPE_CONSTRUCT:
|
||||||
printf("%s Construct class '%s':\n", levelbuf,
|
printf("%s Construct class '%s':\n", levelbuf,
|
||||||
(char *)node->construct.name->token->value);
|
(char *)node->construct.name->token->value);
|
||||||
@@ -169,7 +185,7 @@ void fzl_debug_print_ast(fzl_node_t *root)
|
|||||||
fzl_node_t *curr = root;
|
fzl_node_t *curr = root;
|
||||||
while (curr != NULL)
|
while (curr != NULL)
|
||||||
{
|
{
|
||||||
print_node(curr, 2);
|
print_node(curr, 3);
|
||||||
curr = curr->next;
|
curr = curr->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+308
-69
@@ -30,10 +30,7 @@ fzl_token_t this_tok = {.type = FZL_TOKTYPE_NAME,
|
|||||||
fzl_parser_t *fzl_mkparser(fzl_reader_ll_t *first)
|
fzl_parser_t *fzl_mkparser(fzl_reader_ll_t *first)
|
||||||
{
|
{
|
||||||
if (!first)
|
if (!first)
|
||||||
{
|
|
||||||
snprintf(plast_err, sizeof(plast_err), "token is null");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
fzl_parser_t *parser = malloc(sizeof(fzl_parser_t));
|
fzl_parser_t *parser = malloc(sizeof(fzl_parser_t));
|
||||||
if (!parser)
|
if (!parser)
|
||||||
@@ -161,7 +158,9 @@ fzl_node_t *parse_primary(fzl_parser_t *parser)
|
|||||||
if (!is(parser, FZL_TOKTYPE_RPAREN))
|
if (!is(parser, FZL_TOKTYPE_RPAREN))
|
||||||
{
|
{
|
||||||
snprintf(plast_err, sizeof(plast_err), "'%s': expected ')' at %ld:%ld",
|
snprintf(plast_err, sizeof(plast_err), "'%s': expected ')' at %ld:%ld",
|
||||||
peek(parser)->filename, peek(parser)->ln, peek(parser)->cn);
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
|
fzl_freeast(node);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
consume(parser);
|
consume(parser);
|
||||||
@@ -183,11 +182,12 @@ fzl_node_t *parse_primary(fzl_parser_t *parser)
|
|||||||
{
|
{
|
||||||
fzl_token_t *type = consume(parser);
|
fzl_token_t *type = consume(parser);
|
||||||
fzl_token_t *name = consume(parser);
|
fzl_token_t *name = consume(parser);
|
||||||
if (name->type != FZL_TOKTYPE_NAME)
|
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||||
{
|
{
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected name after type at %ld:%ld", type->filename,
|
"'%s': expected name after type at %ld:%ld",
|
||||||
type->ln, type->cn);
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,8 +199,8 @@ fzl_node_t *parse_primary(fzl_parser_t *parser)
|
|||||||
|
|
||||||
if (!plast_err[0])
|
if (!plast_err[0])
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': failed to parse primary at %ld:%ld", peek(parser)->filename,
|
"'%s': expected expression at %ld:%ld", last_known_tok->filename,
|
||||||
peek(parser)->ln, peek(parser)->cn);
|
last_known_tok->ln, last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,13 +213,7 @@ fzl_node_t *parse_postfix(fzl_parser_t *parser)
|
|||||||
fzl_node_t *left = parse_primary(parser);
|
fzl_node_t *left = parse_primary(parser);
|
||||||
|
|
||||||
if (!left)
|
if (!left)
|
||||||
{
|
|
||||||
if (!plast_err[0])
|
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
|
||||||
"'%s': failed to parse postfix at %ld:%ld",
|
|
||||||
peek(parser)->filename, peek(parser)->ln, peek(parser)->cn);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
if (is(parser, FZL_TOKTYPE_LPAREN) || is(parser, FZL_TOKTYPE_DOT))
|
if (is(parser, FZL_TOKTYPE_LPAREN) || is(parser, FZL_TOKTYPE_DOT))
|
||||||
{
|
{
|
||||||
@@ -238,7 +232,10 @@ fzl_node_t *parse_postfix(fzl_parser_t *parser)
|
|||||||
fzl_node_t *arg = parse_expression(parser);
|
fzl_node_t *arg = parse_expression(parser);
|
||||||
if (!arg)
|
if (!arg)
|
||||||
{
|
{
|
||||||
|
for (size_t i = 0; i < args_count; i++)
|
||||||
|
fzl_freeast(args[i]);
|
||||||
free(args);
|
free(args);
|
||||||
|
fzl_freeast(left);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,10 +255,14 @@ fzl_node_t *parse_postfix(fzl_parser_t *parser)
|
|||||||
fzl_token_t *rparen = consume(parser);
|
fzl_token_t *rparen = consume(parser);
|
||||||
if (!rparen || rparen->type != FZL_TOKTYPE_RPAREN)
|
if (!rparen || rparen->type != FZL_TOKTYPE_RPAREN)
|
||||||
{
|
{
|
||||||
|
for (size_t i = 0; i < args_count; i++)
|
||||||
|
fzl_freeast(args[i]);
|
||||||
free(args);
|
free(args);
|
||||||
|
fzl_freeast(left);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected ')' after argument list at %ld:%ld",
|
"'%s': expected ')' after argument list at %ld:%ld",
|
||||||
peek(parser)->filename, peek(parser)->ln, peek(parser)->cn);
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,6 +277,7 @@ fzl_node_t *parse_postfix(fzl_parser_t *parser)
|
|||||||
{
|
{
|
||||||
if (!is(parser, FZL_TOKTYPE_NAME))
|
if (!is(parser, FZL_TOKTYPE_NAME))
|
||||||
{
|
{
|
||||||
|
fzl_freeast(left);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected name after '.' at %ld:%ld",
|
"'%s': expected name after '.' at %ld:%ld",
|
||||||
last_known_tok->filename, last_known_tok->ln,
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
@@ -327,13 +329,7 @@ fzl_node_t *parse_power(fzl_parser_t *parser)
|
|||||||
fzl_node_t *right = NULL;
|
fzl_node_t *right = NULL;
|
||||||
|
|
||||||
if (!left)
|
if (!left)
|
||||||
{
|
|
||||||
if (!plast_err[0])
|
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
|
||||||
"'%s': failed to parse power at %ld:%ld", peek(parser)->filename,
|
|
||||||
peek(parser)->ln, peek(parser)->cn);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
if (is(parser, FZL_TOKTYPE_CARET))
|
if (is(parser, FZL_TOKTYPE_CARET))
|
||||||
{
|
{
|
||||||
@@ -342,10 +338,12 @@ fzl_node_t *parse_power(fzl_parser_t *parser)
|
|||||||
right = parse_unary(parser);
|
right = parse_unary(parser);
|
||||||
if (!right)
|
if (!right)
|
||||||
{
|
{
|
||||||
|
fzl_freeast(left);
|
||||||
if (!plast_err[0])
|
if (!plast_err[0])
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': failed to parse right side of power at %ld:%ld",
|
"'%s': expected expression at %ld:%ld",
|
||||||
peek(parser)->filename, peek(parser)->ln, peek(parser)->cn);
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,13 +367,7 @@ fzl_node_t *parse_factor(fzl_parser_t *parser)
|
|||||||
fzl_node_t *right = NULL;
|
fzl_node_t *right = NULL;
|
||||||
|
|
||||||
if (!left)
|
if (!left)
|
||||||
{
|
|
||||||
if (!plast_err[0])
|
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
|
||||||
"'%s': invalid expression at %ld:%ld", peek(parser)->filename,
|
|
||||||
peek(parser)->ln, peek(parser)->cn);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
while (is(parser, FZL_TOKTYPE_STAR) || is(parser, FZL_TOKTYPE_SLASH) ||
|
while (is(parser, FZL_TOKTYPE_STAR) || is(parser, FZL_TOKTYPE_SLASH) ||
|
||||||
is(parser, FZL_TOKTYPE_PERCENT))
|
is(parser, FZL_TOKTYPE_PERCENT))
|
||||||
@@ -385,10 +377,12 @@ fzl_node_t *parse_factor(fzl_parser_t *parser)
|
|||||||
right = parse_factor(parser);
|
right = parse_factor(parser);
|
||||||
if (!right)
|
if (!right)
|
||||||
{
|
{
|
||||||
|
fzl_freeast(left);
|
||||||
if (!plast_err[0])
|
if (!plast_err[0])
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': unfinished factor (mul/div) expression at %ld:%ld",
|
"'%s': expected expression at %ld:%ld",
|
||||||
left->token->filename, left->token->ln, left->token->cn);
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -411,13 +405,7 @@ fzl_node_t *parse_term(fzl_parser_t *parser)
|
|||||||
fzl_node_t *right = NULL;
|
fzl_node_t *right = NULL;
|
||||||
|
|
||||||
if (!left)
|
if (!left)
|
||||||
{
|
|
||||||
if (!plast_err[0])
|
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
|
||||||
"'%s': invalid expression at %ld:%ld", peek(parser)->filename,
|
|
||||||
peek(parser)->ln, peek(parser)->cn);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
while (is(parser, FZL_TOKTYPE_PLUS) || is(parser, FZL_TOKTYPE_MINUS))
|
while (is(parser, FZL_TOKTYPE_PLUS) || is(parser, FZL_TOKTYPE_MINUS))
|
||||||
{
|
{
|
||||||
@@ -426,10 +414,12 @@ fzl_node_t *parse_term(fzl_parser_t *parser)
|
|||||||
right = parse_factor(parser);
|
right = parse_factor(parser);
|
||||||
if (!right)
|
if (!right)
|
||||||
{
|
{
|
||||||
|
fzl_freeast(left);
|
||||||
if (!plast_err[0])
|
if (!plast_err[0])
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': unfinished term (add/sub) expression at %ld:%ld",
|
"'%s': expected expression at %ld:%ld",
|
||||||
left->token->filename, left->token->ln, left->token->cn);
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -462,10 +452,7 @@ fzl_node_t *parse_cond(fzl_parser_t *parser)
|
|||||||
right = parse_term(parser);
|
right = parse_term(parser);
|
||||||
if (!right)
|
if (!right)
|
||||||
{
|
{
|
||||||
if (!plast_err[0])
|
fzl_freeast(left);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
|
||||||
"'%s': unfinished expression at %ld:%ld", op->filename, op->ln,
|
|
||||||
op->cn);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -520,10 +507,7 @@ fzl_node_t *parse_and(fzl_parser_t *parser)
|
|||||||
right = parse_cond(parser);
|
right = parse_cond(parser);
|
||||||
if (!right)
|
if (!right)
|
||||||
{
|
{
|
||||||
if (!plast_err[0])
|
fzl_freeast(left);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
|
||||||
"'%s': unfinished expression at %ld:%ld", or->filename, or->ln,
|
|
||||||
or->cn);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -554,10 +538,7 @@ fzl_node_t *parse_or(fzl_parser_t *parser)
|
|||||||
right = parse_and(parser);
|
right = parse_and(parser);
|
||||||
if (!right)
|
if (!right)
|
||||||
{
|
{
|
||||||
if (!plast_err[0])
|
fzl_freeast(left);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
|
||||||
"'%s': unfinished expression at %ld:%ld", or->filename, or->ln,
|
|
||||||
or->cn);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -581,8 +562,9 @@ fzl_node_t *parse_new(fzl_parser_t *parser)
|
|||||||
if (objname->type != FZL_NODETYPE_IDENTIFIER &&
|
if (objname->type != FZL_NODETYPE_IDENTIFIER &&
|
||||||
objname->type != FZL_NODETYPE_FIELD)
|
objname->type != FZL_NODETYPE_FIELD)
|
||||||
{
|
{
|
||||||
|
fzl_freeast(objname);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expect class name after 'new' keyword at %ld:%ld",
|
"'%s': expected class name after 'new' keyword at %ld:%ld",
|
||||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -603,19 +585,24 @@ fzl_node_t *parse_assignment(fzl_parser_t *parser)
|
|||||||
if (is(parser, FZL_TOKTYPE_EQ))
|
if (is(parser, FZL_TOKTYPE_EQ))
|
||||||
{
|
{
|
||||||
fzl_token_t *asn = consume(parser);
|
fzl_token_t *asn = consume(parser);
|
||||||
if (left->type != FZL_NODETYPE_IDENTIFIER &&
|
if (!left || (left->type != FZL_NODETYPE_IDENTIFIER &&
|
||||||
left->type != FZL_NODETYPE_DECLARATION &&
|
left->type != FZL_NODETYPE_DECLARATION &&
|
||||||
left->type != FZL_NODETYPE_FIELD)
|
left->type != FZL_NODETYPE_FIELD))
|
||||||
{
|
{
|
||||||
|
fzl_freeast(left);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': cannot assign to '%s' at %ld:%ld", left->token->filename,
|
"'%s': cannot assign to '%s' at %ld:%ld",
|
||||||
(char *)left->token->value, asn->ln, asn->cn);
|
last_known_tok->filename, (char *)left->token->value,
|
||||||
|
last_known_tok->ln, last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
fzl_node_t *right = parse_assignment(parser);
|
fzl_node_t *right = parse_assignment(parser);
|
||||||
if (!right)
|
if (!right)
|
||||||
|
{
|
||||||
|
fzl_freeast(left);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_ASSIGN, asn);
|
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_ASSIGN, asn);
|
||||||
node->binary_op.left = left;
|
node->binary_op.left = left;
|
||||||
@@ -643,20 +630,20 @@ fzl_node_t *parse_class(fzl_parser_t *parser)
|
|||||||
|
|
||||||
fzl_token_t *class = consume(parser);
|
fzl_token_t *class = consume(parser);
|
||||||
fzl_token_t *name = consume(parser);
|
fzl_token_t *name = consume(parser);
|
||||||
if (name->type != FZL_TOKTYPE_NAME)
|
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||||
{
|
{
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected class name at %ld:%ld", name->filename, name->ln,
|
"'%s': expected class name at %ld:%ld", last_known_tok->filename,
|
||||||
name->cn);
|
last_known_tok->ln, last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
fzl_token_t *lbrace = consume(parser);
|
fzl_token_t *lbrace = consume(parser);
|
||||||
if (lbrace->type != FZL_TOKTYPE_LBRACE)
|
if (!lbrace || lbrace->type != FZL_TOKTYPE_LBRACE)
|
||||||
{
|
{
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected '{' after class name at %ld:%ld", lbrace->filename,
|
"'%s': expected '{' after class name at %ld:%ld",
|
||||||
lbrace->ln, lbrace->cn);
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -697,6 +684,7 @@ fzl_node_t *parse_class(fzl_parser_t *parser)
|
|||||||
{
|
{
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"failed to resize fields array");
|
"failed to resize fields array");
|
||||||
|
fzl_freeast(node);
|
||||||
free(methods);
|
free(methods);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -714,6 +702,7 @@ fzl_node_t *parse_class(fzl_parser_t *parser)
|
|||||||
{
|
{
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"failed to resize methods array");
|
"failed to resize methods array");
|
||||||
|
fzl_freeast(node);
|
||||||
free(fields);
|
free(fields);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -726,6 +715,7 @@ fzl_node_t *parse_class(fzl_parser_t *parser)
|
|||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"failed to reallocate method args list");
|
"failed to reallocate method args list");
|
||||||
free(fields);
|
free(fields);
|
||||||
|
fzl_freeast(node);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -744,9 +734,11 @@ fzl_node_t *parse_class(fzl_parser_t *parser)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
fzl_freeast(node);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': unknown type of class member at %ld:%ld",
|
"'%s': unknown type of class member at %ld:%ld",
|
||||||
node->token->filename, node->token->ln, node->token->cn);
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
free(fields);
|
free(fields);
|
||||||
free(methods);
|
free(methods);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -780,7 +772,7 @@ fzl_node_t *parse_function(fzl_parser_t *parser)
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
fzl_token_t *fn = consume(parser);
|
fzl_token_t *fn = consume(parser);
|
||||||
if (fn->type != FZL_TOKTYPE_FUNCTION)
|
if (!fn || fn->type != FZL_TOKTYPE_FUNCTION)
|
||||||
{
|
{
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected keyword 'fn' at %ld:%ld", last_known_tok->filename,
|
"'%s': expected keyword 'fn' at %ld:%ld", last_known_tok->filename,
|
||||||
@@ -830,6 +822,7 @@ fzl_node_t *parse_function(fzl_parser_t *parser)
|
|||||||
fzl_token_t *rpar = consume(parser);
|
fzl_token_t *rpar = consume(parser);
|
||||||
if (!rpar || rpar->type != FZL_TOKTYPE_RPAREN)
|
if (!rpar || rpar->type != FZL_TOKTYPE_RPAREN)
|
||||||
{
|
{
|
||||||
|
free(args);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected ')' after argument list at %ld:%ld",
|
"'%s': expected ')' after argument list at %ld:%ld",
|
||||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
@@ -845,6 +838,7 @@ fzl_node_t *parse_function(fzl_parser_t *parser)
|
|||||||
|
|
||||||
if (!is(parser, FZL_TOKTYPE_LBRACE))
|
if (!is(parser, FZL_TOKTYPE_LBRACE))
|
||||||
{
|
{
|
||||||
|
free(args);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected '{' after function declaration at %ld:%ld",
|
"'%s': expected '{' after function declaration at %ld:%ld",
|
||||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
@@ -853,7 +847,10 @@ fzl_node_t *parse_function(fzl_parser_t *parser)
|
|||||||
|
|
||||||
fzl_node_t *body = parse_statement(parser);
|
fzl_node_t *body = parse_statement(parser);
|
||||||
if (!body)
|
if (!body)
|
||||||
|
{
|
||||||
|
free(args);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_FUNCTION, name);
|
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_FUNCTION, name);
|
||||||
node->fn.args = args;
|
node->fn.args = args;
|
||||||
@@ -878,7 +875,10 @@ fzl_node_t *parse_return(fzl_parser_t *parser)
|
|||||||
{
|
{
|
||||||
fzl_node_t *retval = parse_expression(parser);
|
fzl_node_t *retval = parse_expression(parser);
|
||||||
if (!retval)
|
if (!retval)
|
||||||
|
{
|
||||||
|
fzl_freeast(node);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
node->ret.value = retval;
|
node->ret.value = retval;
|
||||||
}
|
}
|
||||||
@@ -908,6 +908,7 @@ fzl_node_t *parse_if(fzl_parser_t *parser)
|
|||||||
fzl_token_t *rparen = consume(parser);
|
fzl_token_t *rparen = consume(parser);
|
||||||
if (rparen->type != FZL_TOKTYPE_RPAREN)
|
if (rparen->type != FZL_TOKTYPE_RPAREN)
|
||||||
{
|
{
|
||||||
|
fzl_freeast(condition);
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': expected ')' after condition at %ld:%ld", rparen->filename,
|
"'%s': expected ')' after condition at %ld:%ld", rparen->filename,
|
||||||
rparen->ln, rparen->cn);
|
rparen->ln, rparen->cn);
|
||||||
@@ -917,14 +918,21 @@ fzl_node_t *parse_if(fzl_parser_t *parser)
|
|||||||
fzl_node_t *else_block = NULL;
|
fzl_node_t *else_block = NULL;
|
||||||
fzl_node_t *true_block = parse_statement(parser);
|
fzl_node_t *true_block = parse_statement(parser);
|
||||||
if (!true_block)
|
if (!true_block)
|
||||||
|
{
|
||||||
|
fzl_freeast(condition);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (is(parser, FZL_TOKTYPE_ELSE))
|
if (is(parser, FZL_TOKTYPE_ELSE))
|
||||||
{
|
{
|
||||||
consume(parser); // else
|
consume(parser); // else
|
||||||
else_block = parse_statement(parser);
|
else_block = parse_statement(parser);
|
||||||
if (!else_block)
|
if (!else_block)
|
||||||
|
{
|
||||||
|
fzl_freeast(condition);
|
||||||
|
fzl_freeast(true_block);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fzl_node_t *stmt_node = fzl_mknode(FZL_NODETYPE_CONDITIONAL, if_tok);
|
fzl_node_t *stmt_node = fzl_mknode(FZL_NODETYPE_CONDITIONAL, if_tok);
|
||||||
@@ -951,7 +959,10 @@ fzl_node_t *parse_block(fzl_parser_t *parser)
|
|||||||
{
|
{
|
||||||
fzl_node_t *node = parse_statement(parser);
|
fzl_node_t *node = parse_statement(parser);
|
||||||
if (!node)
|
if (!node)
|
||||||
|
{
|
||||||
|
free(stmts);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (remainder <= count)
|
if (remainder <= count)
|
||||||
{
|
{
|
||||||
@@ -962,11 +973,15 @@ fzl_node_t *parse_block(fzl_parser_t *parser)
|
|||||||
stmts[count++] = node;
|
stmts[count++] = node;
|
||||||
}
|
}
|
||||||
fzl_token_t *rbrace = consume(parser);
|
fzl_token_t *rbrace = consume(parser);
|
||||||
if (rbrace && rbrace->type != FZL_TOKTYPE_RBRACE)
|
if (!rbrace || rbrace->type != FZL_TOKTYPE_RBRACE)
|
||||||
{
|
{
|
||||||
|
for (size_t i = 0; i < count; i++)
|
||||||
|
fzl_freeast(stmts[i]);
|
||||||
|
free(stmts);
|
||||||
|
|
||||||
snprintf(plast_err, sizeof(plast_err),
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
"'%s': missing '}' after block at %ld:%ld", rbrace->filename,
|
"'%s': missing '}' after block at %ld:%ld",
|
||||||
rbrace->ln, rbrace->cn);
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -976,6 +991,204 @@ fzl_node_t *parse_block(fzl_parser_t *parser)
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For-loop parsing
|
||||||
|
fzl_node_t *parse_for(fzl_parser_t *parser)
|
||||||
|
{
|
||||||
|
fzl_token_t *tok = consume(parser);
|
||||||
|
fzl_token_t *lpar = consume(parser);
|
||||||
|
if (!lpar || lpar->type != FZL_TOKTYPE_LPAREN)
|
||||||
|
{
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': expected '(' after for keyword at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *start = NULL;
|
||||||
|
if (!is(parser, FZL_TOKTYPE_SEMICOLON))
|
||||||
|
{
|
||||||
|
start = parse_expression(parser);
|
||||||
|
if (!start)
|
||||||
|
{
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': expected loop init or ';' at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_token_t *sc = consume(parser);
|
||||||
|
if (!sc || sc->type != FZL_TOKTYPE_SEMICOLON)
|
||||||
|
{
|
||||||
|
fzl_freeast(start);
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': missing ';' after init section at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
consume(parser);
|
||||||
|
|
||||||
|
fzl_node_t *condition = NULL;
|
||||||
|
if (!is(parser, FZL_TOKTYPE_SEMICOLON))
|
||||||
|
{
|
||||||
|
condition = parse_expression(parser);
|
||||||
|
if (!condition)
|
||||||
|
{
|
||||||
|
fzl_freeast(start);
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': expected loop condition or ';' at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_token_t *sc = consume(parser);
|
||||||
|
if (!sc || sc->type != FZL_TOKTYPE_SEMICOLON)
|
||||||
|
{
|
||||||
|
fzl_freeast(start);
|
||||||
|
fzl_freeast(condition);
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': missing ';' after condition at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
consume(parser);
|
||||||
|
|
||||||
|
fzl_node_t *step = NULL;
|
||||||
|
if (!is(parser, FZL_TOKTYPE_RPAREN))
|
||||||
|
{
|
||||||
|
step = parse_expression(parser);
|
||||||
|
if (!step)
|
||||||
|
{
|
||||||
|
fzl_freeast(start);
|
||||||
|
fzl_freeast(condition);
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': expected loop step end or ')' at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln,
|
||||||
|
last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_token_t *rpar = consume(parser);
|
||||||
|
if (!rpar || rpar->type != FZL_TOKTYPE_RPAREN)
|
||||||
|
{
|
||||||
|
fzl_freeast(start);
|
||||||
|
fzl_freeast(condition);
|
||||||
|
fzl_freeast(step);
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': missing ')' after loop setup at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *body = parse_statement(parser);
|
||||||
|
if (!body)
|
||||||
|
{
|
||||||
|
fzl_freeast(start);
|
||||||
|
fzl_freeast(condition);
|
||||||
|
fzl_freeast(step);
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': missing body of for loop at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_LOOP, tok);
|
||||||
|
node->loop.init = start;
|
||||||
|
node->loop.cond = condition;
|
||||||
|
node->loop.step = step;
|
||||||
|
node->loop.body = body;
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// While-loop parsing
|
||||||
|
fzl_node_t *parse_while(fzl_parser_t *parser)
|
||||||
|
{
|
||||||
|
fzl_token_t *tok = consume(parser);
|
||||||
|
fzl_token_t *lpar = consume(parser);
|
||||||
|
if (!lpar || lpar->type != FZL_TOKTYPE_LPAREN)
|
||||||
|
{
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': missing '(' after while keyword at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *condition = parse_expression(parser);
|
||||||
|
if (!condition)
|
||||||
|
{
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': missing condition for 'while' at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_token_t *rpar = consume(parser);
|
||||||
|
if (!rpar || rpar->type != FZL_TOKTYPE_RPAREN)
|
||||||
|
{
|
||||||
|
fzl_freeast(condition);
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': missing ')' after while loop condition at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *body = parse_statement(parser);
|
||||||
|
if (!body)
|
||||||
|
{
|
||||||
|
fzl_freeast(condition);
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': missing while loop body at %ld:%ld",
|
||||||
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_LOOP, tok);
|
||||||
|
node->loop.cond = condition;
|
||||||
|
node->loop.body = body;
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// `while` and `for` loops
|
||||||
|
fzl_node_t *parse_loop(fzl_parser_t *parser)
|
||||||
|
{
|
||||||
|
if (!peek(parser))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (is(parser, FZL_TOKTYPE_FOR))
|
||||||
|
return parse_for(parser);
|
||||||
|
else if (is(parser, FZL_TOKTYPE_WHILE))
|
||||||
|
return parse_while(parser);
|
||||||
|
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': unknown type of loop at %ld:%ld", last_known_tok->filename,
|
||||||
|
last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// `break` keyword parsing
|
||||||
|
fzl_node_t *parse_break(fzl_parser_t *parser)
|
||||||
|
{
|
||||||
|
fzl_token_t *tok = consume(parser);
|
||||||
|
if (!tok)
|
||||||
|
{
|
||||||
|
snprintf(plast_err, sizeof(plast_err), "'%s': EOF",
|
||||||
|
last_known_tok->filename);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fzl_mknode(FZL_NODETYPE_BREAK, tok);
|
||||||
|
}
|
||||||
|
|
||||||
// Statement parsing
|
// Statement parsing
|
||||||
fzl_node_t *parse_statement(fzl_parser_t *parser)
|
fzl_node_t *parse_statement(fzl_parser_t *parser)
|
||||||
{
|
{
|
||||||
@@ -994,12 +1207,23 @@ fzl_node_t *parse_statement(fzl_parser_t *parser)
|
|||||||
return parse_if(parser);
|
return parse_if(parser);
|
||||||
else if (is(parser, FZL_TOKTYPE_LBRACE))
|
else if (is(parser, FZL_TOKTYPE_LBRACE))
|
||||||
return parse_block(parser);
|
return parse_block(parser);
|
||||||
|
else if (is(parser, FZL_TOKTYPE_FOR) || is(parser, FZL_TOKTYPE_WHILE))
|
||||||
|
return parse_loop(parser);
|
||||||
|
else if (is(parser, FZL_TOKTYPE_BREAK))
|
||||||
|
res = parse_break(parser);
|
||||||
|
|
||||||
if (!res)
|
if (!res)
|
||||||
res = parse_expression(parser);
|
res = parse_expression(parser);
|
||||||
|
|
||||||
|
if (!res)
|
||||||
|
{
|
||||||
|
fzl_freeast(res);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (!is(parser, FZL_TOKTYPE_SEMICOLON))
|
if (!is(parser, FZL_TOKTYPE_SEMICOLON))
|
||||||
{
|
{
|
||||||
|
fzl_freeast(res);
|
||||||
snprintf(plast_err, sizeof(plast_err), "'%s': missing ';' at %ld:%ld",
|
snprintf(plast_err, sizeof(plast_err), "'%s': missing ';' at %ld:%ld",
|
||||||
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
last_known_tok->filename, last_known_tok->ln, last_known_tok->cn);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -1079,6 +1303,7 @@ void fzl_freeast(fzl_node_t *root)
|
|||||||
{
|
{
|
||||||
switch (root->type)
|
switch (root->type)
|
||||||
{
|
{
|
||||||
|
case FZL_NODETYPE_BREAK:
|
||||||
case FZL_NODETYPE_IDENTIFIER:
|
case FZL_NODETYPE_IDENTIFIER:
|
||||||
case FZL_NODETYPE_LITERAL:
|
case FZL_NODETYPE_LITERAL:
|
||||||
break;
|
break;
|
||||||
@@ -1119,6 +1344,20 @@ void fzl_freeast(fzl_node_t *root)
|
|||||||
fzl_freeast(root->cond.else_block);
|
fzl_freeast(root->cond.else_block);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case FZL_NODETYPE_LOOP:
|
||||||
|
if (root->loop.init)
|
||||||
|
fzl_freeast(root->loop.init);
|
||||||
|
|
||||||
|
if (root->loop.cond)
|
||||||
|
fzl_freeast(root->loop.cond);
|
||||||
|
|
||||||
|
if (root->loop.step)
|
||||||
|
fzl_freeast(root->loop.step);
|
||||||
|
|
||||||
|
if (root->loop.body)
|
||||||
|
fzl_freeast(root->loop.body);
|
||||||
|
break;
|
||||||
|
|
||||||
case FZL_NODETYPE_ADD:
|
case FZL_NODETYPE_ADD:
|
||||||
case FZL_NODETYPE_SUB:
|
case FZL_NODETYPE_SUB:
|
||||||
case FZL_NODETYPE_MUL:
|
case FZL_NODETYPE_MUL:
|
||||||
|
|||||||
@@ -73,6 +73,14 @@ fzl_token_t *parse_name(fzl_reader_t *reader)
|
|||||||
if (strcmp(value, "class") == 0)
|
if (strcmp(value, "class") == 0)
|
||||||
tok =
|
tok =
|
||||||
fzl_mktoken(FZL_TOKTYPE_CLASS, l.line, l.col, value, size, l.filename);
|
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)
|
else if (strcmp(value, "new") == 0)
|
||||||
tok = fzl_mktoken(FZL_TOKTYPE_NEW, l.line, l.col, value, size, l.filename);
|
tok = fzl_mktoken(FZL_TOKTYPE_NEW, l.line, l.col, value, size, l.filename);
|
||||||
else if (strcmp(value, "fn") == 0)
|
else if (strcmp(value, "fn") == 0)
|
||||||
|
|||||||
+174
-6
@@ -14,6 +14,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
extern bool is_infinite_loop(fzl_walker_t *walker, fzl_node_t *node, size_t ls,
|
||||||
|
size_t le);
|
||||||
|
|
||||||
char *null_name = "<null>";
|
char *null_name = "<null>";
|
||||||
char wlast_err[256];
|
char wlast_err[256];
|
||||||
|
|
||||||
@@ -200,6 +203,146 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
{
|
{
|
||||||
switch (node->type)
|
switch (node->type)
|
||||||
{
|
{
|
||||||
|
case FZL_NODETYPE_BREAK:
|
||||||
|
{
|
||||||
|
fzl_walk_ctx_t *ctx = walker->ctx;
|
||||||
|
while (ctx)
|
||||||
|
{
|
||||||
|
if (ctx->type == FZL_WALK_CTX_TYPE_LOOP)
|
||||||
|
break;
|
||||||
|
|
||||||
|
ctx = ctx->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ctx || ctx->type != FZL_WALK_CTX_TYPE_LOOP)
|
||||||
|
{
|
||||||
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
|
"'%s': usage of 'break' outsude the loop at %ld:%ld",
|
||||||
|
node->token->filename, node->token->ln, node->token->cn);
|
||||||
|
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = walker->sym_top - 1; i > ctx->initial_env_lvl + 1; i--)
|
||||||
|
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||||
|
|
||||||
|
if (ctx->breaks_remainder <= ctx->breaks_count)
|
||||||
|
{
|
||||||
|
ctx->breaks_remainder *= 2;
|
||||||
|
ctx->breaks =
|
||||||
|
realloc(ctx->breaks, ctx->breaks_remainder * sizeof(size_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
push_op(walker, FZL_OPCODE_JMP);
|
||||||
|
ctx->breaks[ctx->breaks_count++] = walker->op_count;
|
||||||
|
push_word(walker, 0);
|
||||||
|
|
||||||
|
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: add new levels for infinite loop check:
|
||||||
|
// 1. Step direction (f.e. `for (; i > 5; i = i - 1)`)
|
||||||
|
case FZL_NODETYPE_LOOP:
|
||||||
|
{
|
||||||
|
fzl_walk_ctx_t *newctx = nest_ctx(walker, FZL_WALK_CTX_TYPE_LOOP);
|
||||||
|
if (!newctx)
|
||||||
|
{
|
||||||
|
snprintf(wlast_err, sizeof(wlast_err), "failed to nest context");
|
||||||
|
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
|
}
|
||||||
|
|
||||||
|
newctx->initial_env_lvl = walker->sym_top - 1;
|
||||||
|
newctx->breaks_count = 0;
|
||||||
|
newctx->breaks_remainder = 2;
|
||||||
|
newctx->breaks = malloc(newctx->breaks_remainder * sizeof(size_t));
|
||||||
|
|
||||||
|
push_op(walker, FZL_OPCODE_MK_ENVTABLE);
|
||||||
|
walker->syms[walker->sym_top++] = fzl_mksymt();
|
||||||
|
// size_t loop_begin_ip = walker->op_count;
|
||||||
|
|
||||||
|
if (node->loop.init)
|
||||||
|
{
|
||||||
|
fzl_walk_res_t res = walk(walker, node->loop.init);
|
||||||
|
if (res.error)
|
||||||
|
{
|
||||||
|
free(newctx->breaks);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t start_ip = walker->op_count;
|
||||||
|
if (node->loop.cond)
|
||||||
|
{
|
||||||
|
fzl_walk_res_t res = walk(walker, node->loop.cond);
|
||||||
|
if (res.error)
|
||||||
|
{
|
||||||
|
free(newctx->breaks);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
push_op(walker, FZL_OPCODE_PUSHNUM);
|
||||||
|
push_word(walker, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
push_op(walker, FZL_OPCODE_JZ);
|
||||||
|
newctx->breaks[newctx->breaks_count++] = walker->op_count;
|
||||||
|
push_word(walker, 0);
|
||||||
|
|
||||||
|
fzl_walk_res_t loop_body_res = walk(walker, node->loop.body);
|
||||||
|
if (loop_body_res.error)
|
||||||
|
{
|
||||||
|
free(newctx->breaks);
|
||||||
|
return loop_body_res;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node->loop.step)
|
||||||
|
{
|
||||||
|
fzl_walk_res_t res = walk(walker, node->loop.step);
|
||||||
|
if (res.error)
|
||||||
|
{
|
||||||
|
free(newctx->breaks);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
push_op(walker, FZL_OPCODE_JMP);
|
||||||
|
push_word(walker, start_ip);
|
||||||
|
|
||||||
|
// backpatch `break`s
|
||||||
|
for (size_t i = 0; i < newctx->breaks_count; i++)
|
||||||
|
walker->ops[newctx->breaks[i]] = walker->op_count;
|
||||||
|
|
||||||
|
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||||
|
|
||||||
|
// bool is_infinite =
|
||||||
|
// is_infinite_loop(walker, node, loop_begin_ip, walker->op_count -
|
||||||
|
// 2);
|
||||||
|
// if (is_infinite)
|
||||||
|
// {
|
||||||
|
// free(newctx->breaks);
|
||||||
|
// snprintf(wlast_err, sizeof(wlast_err),
|
||||||
|
// "'%s': infinite loop detected at %ld:%ld",
|
||||||
|
// node->token->filename, node->token->ln, node->token->cn);
|
||||||
|
// return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
|
// }
|
||||||
|
|
||||||
|
for (size_t i = walker->sym_top - 1; i > newctx->initial_env_lvl; i--)
|
||||||
|
fzl_freesymt(walker->syms[--walker->sym_top]);
|
||||||
|
|
||||||
|
free(newctx->breaks);
|
||||||
|
if (!fin_ctx(walker))
|
||||||
|
{
|
||||||
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
|
"failed to finish loop context");
|
||||||
|
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
|
}
|
||||||
|
|
||||||
|
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case FZL_NODETYPE_CLASS:
|
case FZL_NODETYPE_CLASS:
|
||||||
{
|
{
|
||||||
fzl_walk_ctx_t *newctx = nest_ctx(walker, FZL_WALK_CTX_TYPE_CLASS);
|
fzl_walk_ctx_t *newctx = nest_ctx(walker, FZL_WALK_CTX_TYPE_CLASS);
|
||||||
@@ -370,7 +513,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
|
|
||||||
fzl_type_t fntype = tok_to_type(node->fn.type);
|
fzl_type_t fntype = tok_to_type(node->fn.type);
|
||||||
fzl_symtable_res_t putres =
|
fzl_symtable_res_t putres =
|
||||||
fzl_symt_put(walker->syms[walker->sym_top - 1],
|
fzl_symt_put(walker->syms[walker->sym_top - 1], node,
|
||||||
node->fn.name->value, fntype, walker->op_count);
|
node->fn.name->value, fntype, walker->op_count);
|
||||||
|
|
||||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||||
@@ -461,7 +604,16 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
|
|
||||||
case FZL_NODETYPE_RETURN:
|
case FZL_NODETYPE_RETURN:
|
||||||
{
|
{
|
||||||
if (!walker->ctx || walker->ctx->type != FZL_WALK_CTX_TYPE_FN)
|
fzl_walk_ctx_t *ctx = walker->ctx;
|
||||||
|
while (ctx)
|
||||||
|
{
|
||||||
|
if (ctx && ctx->type == FZL_WALK_CTX_TYPE_FN)
|
||||||
|
break;
|
||||||
|
|
||||||
|
ctx = ctx->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ctx || ctx->type != FZL_WALK_CTX_TYPE_FN)
|
||||||
{
|
{
|
||||||
snprintf(
|
snprintf(
|
||||||
wlast_err, sizeof(wlast_err),
|
wlast_err, sizeof(wlast_err),
|
||||||
@@ -731,6 +883,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
}
|
}
|
||||||
fzl_freesymt(walker->syms[--walker->sym_top]);
|
fzl_freesymt(walker->syms[--walker->sym_top]);
|
||||||
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||||
|
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FZL_NODETYPE_CALL:
|
case FZL_NODETYPE_CALL:
|
||||||
@@ -1045,7 +1198,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fzl_symtable_res_t res = fzl_symt_put(
|
fzl_symtable_res_t res = fzl_symt_put(
|
||||||
walker->syms[walker->sym_top - 1],
|
walker->syms[walker->sym_top - 1], node,
|
||||||
node->var.identifier->token->value,
|
node->var.identifier->token->value,
|
||||||
ct != NULL ? FZL_TYPE_OBJECT : tok_to_type(node->var.type), 0);
|
ct != NULL ? FZL_TYPE_OBJECT : tok_to_type(node->var.type), 0);
|
||||||
|
|
||||||
@@ -1056,9 +1209,19 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||||
{
|
{
|
||||||
fzl_token_t *tok = node->var.identifier->token;
|
fzl_token_t *tok = node->var.identifier->token;
|
||||||
snprintf(wlast_err, sizeof(wlast_err),
|
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||||
"'%s': cannot store '%s' at %ld:%ld", tok->filename,
|
{
|
||||||
(char *)tok->value, tok->ln, tok->cn);
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
|
"'%s': cannot redefine '%s' at %ld:%ld", tok->filename,
|
||||||
|
(char *)tok->value, tok->ln, tok->cn);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
|
"'%s': cannot store '%s' at %ld:%ld", tok->filename,
|
||||||
|
(char *)tok->value, tok->ln, tok->cn);
|
||||||
|
}
|
||||||
|
|
||||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1225,6 +1388,11 @@ int fzl_walk(fzl_walker_t *walker)
|
|||||||
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||||
|
|
||||||
fin_ctx(walker);
|
fin_ctx(walker);
|
||||||
|
if (walker->ctx)
|
||||||
|
{
|
||||||
|
while (walker->ctx)
|
||||||
|
fin_ctx(walker);
|
||||||
|
}
|
||||||
|
|
||||||
if (result.error && !wlast_err[0])
|
if (result.error && !wlast_err[0])
|
||||||
snprintf(wlast_err, sizeof(wlast_err), "%s", result.error_msg);
|
snprintf(wlast_err, sizeof(wlast_err), "%s", result.error_msg);
|
||||||
|
|||||||
+78
-4
@@ -1,4 +1,5 @@
|
|||||||
#include <common/fzl_colors.h>
|
#include <common/fzl_colors.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
#include <compiler/fzl_debug.h>
|
#include <compiler/fzl_debug.h>
|
||||||
#include <compiler/fzl_parser.h>
|
#include <compiler/fzl_parser.h>
|
||||||
#include <compiler/fzl_reader.h>
|
#include <compiler/fzl_reader.h>
|
||||||
@@ -96,11 +97,21 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fzl_parser_t *parser = fzl_mkparser(rd->first);
|
fzl_parser_t *parser = fzl_mkparser(rd->first);
|
||||||
result = fzl_parse(parser);
|
if (!parser)
|
||||||
|
{
|
||||||
|
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = fzl_parse(parser);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_parser_geterror());
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_parser_geterror());
|
||||||
fzl_freeoptions(opts);
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freeast(parser->root);
|
||||||
fzl_freeparser(parser);
|
fzl_freeparser(parser);
|
||||||
fzl_freereader(rd);
|
fzl_freereader(rd);
|
||||||
fzl_freelinked(linked);
|
fzl_freelinked(linked);
|
||||||
@@ -112,16 +123,63 @@ int main(int argc, char **argv)
|
|||||||
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||||
fzl_debug_print_ast(parser->root);
|
fzl_debug_print_ast(parser->root);
|
||||||
printf("%s> building%s\n", FZL_CDEBUG, FZL_CRST);
|
printf("%s> building%s\n", FZL_CDEBUG, FZL_CRST);
|
||||||
printf("%s-> walking AST...%s\n", FZL_CDEBUG, FZL_CRST);
|
printf("%s-> analyzing AST...%s\n", FZL_CDEBUG, FZL_CRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_analyzer_t *analyzer = fzl_mkanalyzer(parser->root);
|
||||||
|
if (!analyzer)
|
||||||
|
{
|
||||||
|
printf("%sError%s: failed to allocate analyzer\n", FZL_CERROR, FZL_CRST);
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freeast(parser->root);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_analyze(analyzer);
|
||||||
|
if (analyzer->errors_top > 0)
|
||||||
|
{
|
||||||
|
for (ssize_t i = analyzer->errors_top - 1; i >= 0; i--)
|
||||||
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, analyzer->errors[i]);
|
||||||
|
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freeanalyzer(analyzer);
|
||||||
|
fzl_freeast(parser->root);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (analyzer->warnings_top > 0)
|
||||||
|
{
|
||||||
|
for (ssize_t i = analyzer->warnings_top - 1; i >= 0; i--)
|
||||||
|
printf("%sWarning%s: %s\n", FZL_CWARNING, FZL_CRST,
|
||||||
|
analyzer->warnings[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
||||||
result = fzl_walk(walker);
|
if (!walker)
|
||||||
|
{
|
||||||
|
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freeast(parser->root);
|
||||||
|
fzl_freeanalyzer(analyzer);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = fzl_walk(walker);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_walker_geterror());
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_walker_geterror());
|
||||||
fzl_freeoptions(opts);
|
fzl_freeoptions(opts);
|
||||||
fzl_freewalker(walker);
|
fzl_freewalker(walker);
|
||||||
|
fzl_freeast(parser->root);
|
||||||
|
fzl_freeanalyzer(analyzer);
|
||||||
fzl_freeparser(parser);
|
fzl_freeparser(parser);
|
||||||
fzl_freereader(rd);
|
fzl_freereader(rd);
|
||||||
fzl_freelinked(linked);
|
fzl_freelinked(linked);
|
||||||
@@ -162,13 +220,28 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fzl_writer_t *writer = fzl_mkwriter(outpath);
|
fzl_writer_t *writer = fzl_mkwriter(outpath);
|
||||||
result = fzl_write(writer, walker);
|
if (!writer)
|
||||||
|
{
|
||||||
|
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
|
||||||
|
fzl_freeoptions(opts);
|
||||||
|
fzl_freewalker(walker);
|
||||||
|
fzl_freeast(parser->root);
|
||||||
|
fzl_freeanalyzer(analyzer);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freelinked(linked);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = fzl_write(writer, walker);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_writer_geterror());
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_writer_geterror());
|
||||||
fzl_freeoptions(opts);
|
fzl_freeoptions(opts);
|
||||||
fzl_freewriter(writer);
|
fzl_freewriter(writer);
|
||||||
fzl_freewalker(walker);
|
fzl_freewalker(walker);
|
||||||
|
fzl_freeast(parser->root);
|
||||||
|
fzl_freeanalyzer(analyzer);
|
||||||
fzl_freeparser(parser);
|
fzl_freeparser(parser);
|
||||||
fzl_freereader(rd);
|
fzl_freereader(rd);
|
||||||
fzl_freelinked(linked);
|
fzl_freelinked(linked);
|
||||||
@@ -182,6 +255,7 @@ int main(int argc, char **argv)
|
|||||||
fzl_freelinked(linked);
|
fzl_freelinked(linked);
|
||||||
fzl_freereader(rd);
|
fzl_freereader(rd);
|
||||||
fzl_freeast(parser->root);
|
fzl_freeast(parser->root);
|
||||||
|
fzl_freeanalyzer(analyzer);
|
||||||
fzl_freeparser(parser);
|
fzl_freeparser(parser);
|
||||||
fzl_freewalker(walker);
|
fzl_freewalker(walker);
|
||||||
fzl_freewriter(writer);
|
fzl_freewriter(writer);
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ bool fzl_linked_add_files(fzl_linked_t *l, char **files, size_t files_count)
|
|||||||
rewind(fd);
|
rewind(fd);
|
||||||
|
|
||||||
source = malloc(file_sz + 1);
|
source = malloc(file_sz + 1);
|
||||||
fread(source, file_sz, 1, fd);
|
fread(source, file_sz, 1, fd); // NOTE: add red bytes check
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
source[file_sz] = 0;
|
source[file_sz] = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <common/fzl_types.h>
|
#include <common/fzl_types.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
#include <compiler/utils/fzl_symtable.h>
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -21,8 +22,8 @@ fzl_symtable_t *fzl_mksymt()
|
|||||||
return tbl;
|
return tbl;
|
||||||
}
|
}
|
||||||
|
|
||||||
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, fzl_node_t *node,
|
||||||
fzl_type_t type, uint64_t ip)
|
char *name, fzl_type_t type, uint64_t ip)
|
||||||
{
|
{
|
||||||
if (!symt)
|
if (!symt)
|
||||||
return FZL_SYMTABLE_RESULT_ERR;
|
return FZL_SYMTABLE_RESULT_ERR;
|
||||||
@@ -37,8 +38,8 @@ fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
|||||||
symt->symbols = realloc(symt->symbols, symt->remainder * sizeof(fzl_sym_t));
|
symt->symbols = realloc(symt->symbols, symt->remainder * sizeof(fzl_sym_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
symt->symbols[symt->size++] =
|
symt->symbols[symt->size++] = (fzl_sym_t){
|
||||||
(fzl_sym_t){.name = name, .type = type, .ip = ip, .classname = NULL};
|
.name = name, .node = node, .type = type, .ip = ip, .classname = NULL};
|
||||||
return FZL_SYMTABLE_RESULT_OK;
|
return FZL_SYMTABLE_RESULT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+36
-6
@@ -4,6 +4,7 @@
|
|||||||
#include <common/fzl_value.h>
|
#include <common/fzl_value.h>
|
||||||
#include <compiler/utils/fzl_const_pool.h>
|
#include <compiler/utils/fzl_const_pool.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -13,6 +14,8 @@
|
|||||||
#include <vm/fzl_vm_object.h>
|
#include <vm/fzl_vm_object.h>
|
||||||
|
|
||||||
char last_err[256];
|
char last_err[256];
|
||||||
|
size_t last_jmp_ip = 0;
|
||||||
|
size_t total_jmp_on_same = 0;
|
||||||
|
|
||||||
static char *expect_hdr = "FZL\0\1";
|
static char *expect_hdr = "FZL\0\1";
|
||||||
|
|
||||||
@@ -47,7 +50,8 @@ fzl_vm_t *fzl_mkvm(const char *filepath)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char hdr[5];
|
char hdr[5];
|
||||||
fread(hdr, sizeof(char), 5, fd);
|
fread(hdr, sizeof(char), 5,
|
||||||
|
fd); // NOTE: add red bytes check (also for freads below)
|
||||||
if (memcmp(hdr, expect_hdr, 5) != 0)
|
if (memcmp(hdr, expect_hdr, 5) != 0)
|
||||||
{
|
{
|
||||||
snprintf(last_err, sizeof(last_err), "magic check error");
|
snprintf(last_err, sizeof(last_err), "magic check error");
|
||||||
@@ -202,7 +206,7 @@ void print_object(fzl_vm_object_t *obj)
|
|||||||
printf("<unknown>");
|
printf("<unknown>");
|
||||||
}
|
}
|
||||||
|
|
||||||
printf(">\n");
|
printf(">");
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_err_data(fzl_vm_t *vm)
|
void print_err_data(fzl_vm_t *vm)
|
||||||
@@ -273,7 +277,7 @@ void print_err_data(fzl_vm_t *vm)
|
|||||||
printf("\t> [%zd] %lu\n", i, vm->cstack[i]);
|
printf("\t> [%zd] %lu\n", i, vm->cstack[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fzl_vm_run(fzl_vm_t *vm)
|
int fzl_vm_run(fzl_vm_t *vm, bool allow_infinite, uint64_t max_iter)
|
||||||
{
|
{
|
||||||
if (!vm)
|
if (!vm)
|
||||||
{
|
{
|
||||||
@@ -427,8 +431,11 @@ int fzl_vm_run(fzl_vm_t *vm)
|
|||||||
{
|
{
|
||||||
uint64_t values_count = vm->ops[++vm->ip];
|
uint64_t values_count = vm->ops[++vm->ip];
|
||||||
fzl_vm_object_t *obj = malloc(sizeof(fzl_vm_object_t));
|
fzl_vm_object_t *obj = malloc(sizeof(fzl_vm_object_t));
|
||||||
obj->values = malloc(values_count * sizeof(fzl_value_t));
|
|
||||||
obj->values_count = values_count;
|
if (values_count > 0)
|
||||||
|
obj->values = malloc(values_count * sizeof(fzl_value_t));
|
||||||
|
obj->values_count = values_count;
|
||||||
|
memset(obj->values, 0, values_count * sizeof(fzl_value_t));
|
||||||
|
|
||||||
fzl_vm_env_t *current_env = vm->estack[vm->etop - 1];
|
fzl_vm_env_t *current_env = vm->estack[vm->etop - 1];
|
||||||
if (current_env->owned_objects_remainder <=
|
if (current_env->owned_objects_remainder <=
|
||||||
@@ -666,8 +673,28 @@ int fzl_vm_run(fzl_vm_t *vm)
|
|||||||
"runtime error - expected JMP addr");
|
"runtime error - expected JMP addr");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t jmp_ip = vm->ops[++vm->ip];
|
uint64_t jmp_ip = vm->ops[++vm->ip];
|
||||||
vm->ip = jmp_ip - 1;
|
if (!allow_infinite)
|
||||||
|
{
|
||||||
|
if (jmp_ip == last_jmp_ip)
|
||||||
|
total_jmp_on_same++;
|
||||||
|
else
|
||||||
|
total_jmp_on_same = 0;
|
||||||
|
|
||||||
|
if (total_jmp_on_same > max_iter)
|
||||||
|
{
|
||||||
|
snprintf(
|
||||||
|
last_err, sizeof(last_err),
|
||||||
|
"runtime error - exceeded max loop jumps (infinite loop?)\n"
|
||||||
|
"If you thinks there's mistake, change jumps limit with "
|
||||||
|
"--max-iter=N");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vm->ip = jmp_ip - 1;
|
||||||
|
last_jmp_ip = jmp_ip;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -837,7 +864,10 @@ int fzl_vm_run(fzl_vm_t *vm)
|
|||||||
else if (arg.type == FZL_TYPE_NULL)
|
else if (arg.type == FZL_TYPE_NULL)
|
||||||
printf("<null>\n");
|
printf("<null>\n");
|
||||||
else if (arg.type == FZL_TYPE_OBJECT)
|
else if (arg.type == FZL_TYPE_OBJECT)
|
||||||
|
{
|
||||||
print_object(arg.as.obj);
|
print_object(arg.as.obj);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (fn_id == FZL_BUILTIN_EXIT)
|
else if (fn_id == FZL_BUILTIN_EXIT)
|
||||||
{
|
{
|
||||||
|
|||||||
+44
-11
@@ -1,5 +1,7 @@
|
|||||||
|
#include <common/fzl_colors.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -10,14 +12,18 @@ typedef struct
|
|||||||
char *filepath;
|
char *filepath;
|
||||||
bool help;
|
bool help;
|
||||||
bool debug;
|
bool debug;
|
||||||
|
bool allow_infinite;
|
||||||
|
uint64_t max_iter;
|
||||||
} run_args_t;
|
} run_args_t;
|
||||||
|
|
||||||
void print_hint()
|
void print_hint()
|
||||||
{
|
{
|
||||||
printf("Usage: fz [flags] <file path>\n");
|
printf("Usage: fz [flags] <file path>\n");
|
||||||
printf("Flags:\n");
|
printf("Flags:\n");
|
||||||
printf("\t-h\t--help\tprint usage information\n");
|
printf("\t-h\t--help\t\t\tprint usage information\n");
|
||||||
printf("\t-d\t--debug\tdisplay program opcodes\n");
|
printf("\t-d\t--debug\t\t\tdisplay program opcodes\n");
|
||||||
|
printf("\t\t--allow-infinite\tallow infinite loops\n");
|
||||||
|
printf("\t\t--max-iter N\t\trestrict loops to maximum N iterations\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse passed arguments and build args setup structure
|
// Parse passed arguments and build args setup structure
|
||||||
@@ -29,10 +35,12 @@ run_args_t *parse_args(int argc, char **argv)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
run_args_t *args = malloc(sizeof(run_args_t));
|
run_args_t *args = malloc(sizeof(run_args_t));
|
||||||
args->filepath = NULL;
|
args->filepath = NULL;
|
||||||
args->help = false;
|
args->help = false;
|
||||||
args->debug = false;
|
args->debug = false;
|
||||||
|
args->allow_infinite = false;
|
||||||
|
args->max_iter = 100000;
|
||||||
|
|
||||||
for (int i = 0; i < argc; i++)
|
for (int i = 0; i < argc; i++)
|
||||||
{
|
{
|
||||||
@@ -46,9 +54,33 @@ run_args_t *parse_args(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0)
|
else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0)
|
||||||
args->debug = true;
|
args->debug = true;
|
||||||
|
else if (strcmp(argv[i], "--allow-infinite") == 0)
|
||||||
|
args->allow_infinite = true;
|
||||||
|
else if (strcmp(argv[i], "--max-iter") == 0)
|
||||||
|
{
|
||||||
|
char *n = argv[++i];
|
||||||
|
if (!n || argc <= i || !(n[0] >= '0' && n[0] <= '9'))
|
||||||
|
{
|
||||||
|
printf("%sError%s: --max-iter options requires number after it\n",
|
||||||
|
FZL_CERROR, FZL_CRST);
|
||||||
|
free(args);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t iters = strtol(n, NULL, 10);
|
||||||
|
if (iters < 1)
|
||||||
|
{
|
||||||
|
printf("%sError%s: '--max-iter 0' is not allowed\n", FZL_CERROR,
|
||||||
|
FZL_CRST);
|
||||||
|
free(args);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
args->max_iter = iters;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Error: unknown flag '%s'.\n", argv[i]);
|
printf("%sError%s: unknown flag '%s'.\n", FZL_CERROR, FZL_CRST, argv[i]);
|
||||||
printf("Use 'fz --help' to read the usage\n");
|
printf("Use 'fz --help' to read the usage\n");
|
||||||
free(args);
|
free(args);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -57,7 +89,8 @@ run_args_t *parse_args(int argc, char **argv)
|
|||||||
|
|
||||||
if (argv[argc - 1][0] == '-')
|
if (argv[argc - 1][0] == '-')
|
||||||
{
|
{
|
||||||
printf("Error: last argument MUST be a file path\n");
|
printf("%sError%s: last argument MUST be a file path\n", FZL_CERROR,
|
||||||
|
FZL_CRST);
|
||||||
printf("Use 'fz --help' for help\n");
|
printf("Use 'fz --help' for help\n");
|
||||||
free(args);
|
free(args);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -83,7 +116,7 @@ int main(int argc, char **argv)
|
|||||||
fzl_vm_t *vm = fzl_mkvm(args->filepath);
|
fzl_vm_t *vm = fzl_mkvm(args->filepath);
|
||||||
if (!vm)
|
if (!vm)
|
||||||
{
|
{
|
||||||
printf("Error: %s\n", fzl_vm_geterror());
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_vm_geterror());
|
||||||
free(args);
|
free(args);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -92,12 +125,12 @@ int main(int argc, char **argv)
|
|||||||
fzl_vm_print_ops(vm);
|
fzl_vm_print_ops(vm);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int result = fzl_vm_run(vm);
|
int result = fzl_vm_run(vm, args->allow_infinite, args->max_iter);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
char *err = fzl_vm_geterror();
|
char *err = fzl_vm_geterror();
|
||||||
if (err && err[0])
|
if (err && err[0])
|
||||||
printf("Error: %s\n", err);
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, err);
|
||||||
|
|
||||||
free(args);
|
free(args);
|
||||||
fzl_freevm(vm);
|
fzl_freevm(vm);
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
fn choose(bool flag) int
|
||||||
|
{
|
||||||
|
if (flag)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int invalid = "not an integer";
|
||||||
|
exit(1);
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
printline(1 + );
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
int value = 42
|
||||||
|
printline(value);
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
if (true)
|
||||||
|
{
|
||||||
|
printline("missing closing brace");
|
||||||
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
printline("unterminated string);
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
for (int i = 0; i < 5; i = i + 0)
|
||||||
|
{}
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
while (true)
|
||||||
|
{}
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
|
// Arithmetic precedence and operators.
|
||||||
printline(2 + 2 * 2);
|
printline(2 + 2 * 2);
|
||||||
printline(-1 + 1);
|
printline(-1 + 1);
|
||||||
printline(2 ^ 2);
|
printline(2 ^ 2);
|
||||||
printline(1 + 1 * 1 / 1);
|
|
||||||
printline(4 % 2);
|
printline(4 % 2);
|
||||||
printline(10 / 2);
|
printline(10 / 2);
|
||||||
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
class Point
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
fn move(int dx, int dy)
|
||||||
|
{
|
||||||
|
this.x = this.x + dx;
|
||||||
|
this.y = this.y + dy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Point point = new Point;
|
||||||
|
point.x = 1;
|
||||||
|
point.y = 2;
|
||||||
|
point.move(9, 8);
|
||||||
|
|
||||||
|
if (point.x == 10 && point.y == 10)
|
||||||
|
printline("class method passed");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("class method failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// Line comments are ignored by the parser.
|
||||||
|
printline('single-quoted string');;;;;
|
||||||
|
printline("");
|
||||||
|
printline("double-quoted string");
|
||||||
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
bool flag = true;
|
||||||
|
|
||||||
|
if (!flag)
|
||||||
|
{
|
||||||
|
printline("unexpected false");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else if (2 + 2 >= 4)
|
||||||
|
{
|
||||||
|
printline("condition passed");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("unexpected branch");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
printline("standalone block");
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
fn doSomeWork()
|
||||||
|
{
|
||||||
|
printline("Some work");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum(int a, int b) int
|
||||||
|
{
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
int result = sum(5, 6);
|
||||||
|
printline(result);
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
fn fib(int n) int
|
||||||
|
{
|
||||||
|
if (n <= 1)
|
||||||
|
return n;
|
||||||
|
|
||||||
|
return fib(n - 1) + fib(n - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum(uint a, uint b) uint
|
||||||
|
{
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fib(7) == 13 && sum(2, 2) == 4)
|
||||||
|
printline("functions passed");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("functions failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
int i = 0;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
for (int i = 0; i < 10; i = i + 1)
|
||||||
|
printline("check");
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (i = 0; i <= 5; i = i + 1)
|
||||||
|
{
|
||||||
|
printline("i is:");
|
||||||
|
printline(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i < 5)
|
||||||
|
{
|
||||||
|
printline("external declared `i` != 5");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i > 0)
|
||||||
|
{
|
||||||
|
i = i - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
printline("decreased in while loop `i` > 0");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
class Node
|
||||||
|
{
|
||||||
|
Node next;
|
||||||
|
|
||||||
|
fn ping()
|
||||||
|
{
|
||||||
|
printline("ping");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pingNext()
|
||||||
|
{
|
||||||
|
this.next.ping();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Node head = new Node;
|
||||||
|
Node tail = new Node;
|
||||||
|
head.next = tail;
|
||||||
|
head.pingNext();
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
class Item
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn maybe_item(bool create) Item
|
||||||
|
{
|
||||||
|
if (create)
|
||||||
|
return new Item;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Item empty = maybe_item(false);
|
||||||
|
Item present = maybe_item(true);
|
||||||
|
if (empty == null && present != null)
|
||||||
|
printline("null handling passed");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printline("null handling failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
uint work_it = 21 * 2 ^ 2;
|
uint work_it = 21 * 2 ^ 2;
|
||||||
int make_it = 10;
|
int make_it = 10;
|
||||||
|
u64 do_it;
|
||||||
u64 do_it;
|
|
||||||
do_it = 15;
|
do_it = 15;
|
||||||
|
char make_us;
|
||||||
|
str floyd = "hello, Pink Floyd!";
|
||||||
|
bool flag = true;
|
||||||
|
|
||||||
printline(work_it);
|
printline(work_it);
|
||||||
printline(make_it + do_it);
|
printline(make_it + do_it);
|
||||||
|
|
||||||
char make_us;
|
|
||||||
str floyd = "hello, Pink Floyd!";
|
|
||||||
printline(floyd);
|
printline(floyd);
|
||||||
|
printline(flag);
|
||||||
|
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
class Some {
|
||||||
|
Some next;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some l4 = new Some;
|
||||||
|
|
||||||
|
Some l3 = new Some;
|
||||||
|
l3.next = l4;
|
||||||
|
|
||||||
|
Some l2 = new Some;
|
||||||
|
l2.next = l3;
|
||||||
|
|
||||||
|
Some l1 = new Some;
|
||||||
|
l1.next = l2;
|
||||||
|
|
||||||
|
Some root = new Some;
|
||||||
|
root.next = l1;
|
||||||
|
|
||||||
|
Some obj = root;
|
||||||
|
while (obj != null)
|
||||||
|
{
|
||||||
|
obj = obj.next;
|
||||||
|
printline("Now object is:");
|
||||||
|
printline(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
printline(obj);
|
||||||
|
|
||||||
Reference in New Issue
Block a user