6 Commits
21 changed files with 283 additions and 111 deletions
+1
View File
@@ -5,6 +5,7 @@
!/exl
!/include
!/src
!/tests
# Files
!/.clang-format
+18 -99
View File
@@ -35,108 +35,27 @@ add_executable(fz
src/vm/main.c)
#
# ========== Compilation tests ==========
# ========== Tests ==========
#
enable_testing()
add_test(
NAME compile_hw
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/hw.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/hw.fzs"
)
add_test(
NAME compile_print_empty
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/print_empty.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/print_empty.fzs"
)
add_test(
NAME compile_problematic_hw
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/problematic_hw.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/problematic_hw.fzs"
)
add_test(
NAME compile_base_math
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/base_math.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/base_math.fzs"
)
add_test(
NAME compile_variables
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/variables.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/variables.fzs"
)
add_test(
NAME compile_conditions
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/conditions.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/conditions.fzs"
)
add_test(
NAME compile_fn
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/fn.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/fn.fzs"
)
add_test(
NAME compile_comment
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/comment.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/comment.fzs"
)
add_test(
NAME compile_class
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/class.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/class.fzs"
)
add_test(
NAME compile_raii
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/raii.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/raii.fzs"
)
add_test(
NAME compile_borrow
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/borrow.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/borrow.fzs"
)
add_test(
NAME compile_hard_1
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/hard-1.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/exl/hard-1.fzs"
)
file(GLOB TEST_PROGS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "tests/programs/*.fzs")
foreach (TEST_FILE IN LISTS TEST_PROGS)
get_filename_component(TEST_NAME "${TEST_FILE}" NAME_WE)
add_test(
NAME "progs_compile_${TEST_NAME}"
COMMAND fzc -o "${CMAKE_CURRENT_BINARY_DIR}/p_${TEST_NAME}.fzb" "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_FILE}"
)
add_test(
NAME "progs_runtime_${TEST_NAME}"
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/p_${TEST_NAME}.fzb"
)
set_tests_properties(compile_problematic_hw PROPERTIES WILL_FAIL TRUE)
#
# ========== Runtime tests ==========
#
add_test(
NAME runtime_hw
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/hw.fzb"
)
add_test(
NAME runtime_print_empty
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/print_empty.fzb"
)
add_test(
NAME runtime_base_math
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/base_math.fzb"
)
add_test(
NAME runtime_variables
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/variables.fzb"
)
add_test(
NAME runtime_conditions
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/conditions.fzb"
)
add_test(
NAME runtime_fn
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/fn.fzb"
)
add_test(
NAME runtime_comment
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/comment.fzb"
)
add_test(
NAME runtime_class
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/class.fzb"
)
add_test(
NAME runtime_raii
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/raii.fzb"
)
add_test(
NAME runtime_borrow
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/borrow.fzb"
)
add_test(
NAME runtime_hard_1
COMMAND fz "${CMAKE_CURRENT_BINARY_DIR}/hard-1.fzb"
)
if (TEST_NAME MATCHES "neg_")
set_tests_properties("progs_compile_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
set_tests_properties("progs_runtime_${TEST_NAME}" PROPERTIES WILL_FAIL TRUE)
endif()
endforeach()
+5 -3
View File
@@ -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 --build build
build: configure
build-release:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
debug-c: build
+1 -1
View File
@@ -27,7 +27,7 @@ Working examples of code can be found in `exl/` directory.
## Building
1. Clone the repository: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
2. Build: `make build`
2. Build: `make build-release`
After this, a "build" directory will appear in the project root. It will contain two executable files:
+1 -1
View File
@@ -27,7 +27,7 @@
## Инструкции по сборке
1. Склонировать проект: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
2. Собрать проект: `make build`
2. Собрать проект: `make build-release`
После этого в корне появится директория **"build"**. В ней будут лежать два скомпилированных бинарных файла:
+32
View File
@@ -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 helps 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 is many different 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
+30
View File
@@ -0,0 +1,30 @@
// When you need to check some value
// a conditional statements might help you
int valueToCheck = 2 + 2;
if (valueToCheck == 4)
{
printline("Yay! 2 + 2 = 4");
}
// Conditional statement uses condition in pairs
// 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 looks too young!");
}
// NOTE: If you need to execute only one statement
// inside `if` or `else` block, then the curly
// braces can be omitted.
+30
View File
@@ -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 -4
View File
@@ -30,10 +30,7 @@ fzl_token_t this_tok = {.type = FZL_TOKTYPE_NAME,
fzl_parser_t *fzl_mkparser(fzl_reader_ll_t *first)
{
if (!first)
{
snprintf(plast_err, sizeof(plast_err), "token is null");
return NULL;
}
fzl_parser_t *parser = malloc(sizeof(fzl_parser_t));
if (!parser)
@@ -962,7 +959,7 @@ fzl_node_t *parse_block(fzl_parser_t *parser)
stmts[count++] = node;
}
fzl_token_t *rbrace = consume(parser);
if (rbrace && rbrace->type != FZL_TOKTYPE_RBRACE)
if (!rbrace || rbrace->type != FZL_TOKTYPE_RBRACE)
{
snprintf(plast_err, sizeof(plast_err),
"'%s': missing '}' after block at %ld:%ld", rbrace->filename,
+38 -3
View File
@@ -96,11 +96,21 @@ int main(int argc, char **argv)
}
fzl_parser_t *parser = fzl_mkparser(rd->first);
result = fzl_parse(parser);
if (!parser)
{
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
fzl_freeoptions(opts);
fzl_freereader(rd);
fzl_freelinked(linked);
return -1;
}
result = fzl_parse(parser);
if (result != 0)
{
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_parser_geterror());
fzl_freeoptions(opts);
fzl_freeast(parser->root);
fzl_freeparser(parser);
fzl_freereader(rd);
fzl_freelinked(linked);
@@ -116,12 +126,24 @@ int main(int argc, char **argv)
}
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_freeparser(parser);
fzl_freereader(rd);
fzl_freelinked(linked);
return -1;
}
result = fzl_walk(walker);
if (result != 0)
{
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_walker_geterror());
fzl_freeoptions(opts);
fzl_freewalker(walker);
fzl_freeast(parser->root);
fzl_freeparser(parser);
fzl_freereader(rd);
fzl_freelinked(linked);
@@ -162,13 +184,26 @@ int main(int argc, char **argv)
}
fzl_writer_t *writer = fzl_mkwriter(outpath);
result = fzl_write(writer, walker);
if (!writer)
{
printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST);
fzl_freeoptions(opts);
fzl_freewalker(walker);
fzl_freeast(parser->root);
fzl_freeparser(parser);
fzl_freereader(rd);
fzl_freelinked(linked);
return -1;
}
result = fzl_write(writer, walker);
if (result != 0)
{
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_writer_geterror());
fzl_freeoptions(opts);
fzl_freewriter(writer);
fzl_freewalker(walker);
fzl_freeast(parser->root);
fzl_freeparser(parser);
fzl_freereader(rd);
fzl_freelinked(linked);
+2
View File
@@ -0,0 +1,2 @@
printline(1 + );
+3
View File
@@ -0,0 +1,3 @@
int value = 42
printline(value);
+4
View File
@@ -0,0 +1,4 @@
if (true)
{
printline("missing closing brace");
@@ -0,0 +1,2 @@
printline("unterminated string);
+7
View File
@@ -0,0 +1,7 @@
// Arithmetic precedence and operators.
printline(2 + 2 * 2);
printline(-1 + 1);
printline(2 ^ 2);
printline(4 % 2);
printline(10 / 2);
+25
View File
@@ -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);
}
+5
View File
@@ -0,0 +1,5 @@
// Line comments are ignored by the parser.
printline('single-quoted string');;;;;
printline("");
printline("double-quoted string");
+21
View File
@@ -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");
}
+21
View File
@@ -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);
}
+23
View File
@@ -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);
}
+13
View File
@@ -0,0 +1,13 @@
uint work_it = 21 * 2 ^ 2;
int make_it = 10;
u64 do_it;
do_it = 15;
char make_us;
str floyd = "hello, Pink Floyd!";
bool flag = true;
printline(work_it);
printline(make_it + do_it);
printline(floyd);
printline(flag);