From a5a86dc080ab6c3619051c0d81d242787e3f7aba Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Sat, 1 Feb 2025 18:40:43 -0500 Subject: [PATCH] Basic blocks now work. --- Makefile | 2 +- src/dlist.c | 4 +- src/dstr.c | 6 +-- src/grammar.y | 65 +++++++++++++++++++++--------- src/lexer.c | 8 ++-- test/test_ast.c | 2 +- test/{validation => val}/test.bats | 25 ++++++------ 7 files changed, 70 insertions(+), 42 deletions(-) rename test/{validation => val}/test.bats (85%) diff --git a/Makefile b/Makefile index 40a9a18..9487a63 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ UNITY_OBJ = $(TEST_BUILD_DIR)/unity.o TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c) TEST_OBJ_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ_DIR)/%.o, $(TEST_SRC_FILES)) TEST_BIN_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_BUILD_DIR)/%.out, $(TEST_SRC_FILES)) -TEST_VAL_DIR = $(TEST_DIR)/validation +TEST_VAL_DIR = $(TEST_DIR)/val RESETCOLOR = \033[0m WHITE = $(RESETCOLOR)\033[37m diff --git a/src/dlist.c b/src/dlist.c index 376212e..b87d926 100644 --- a/src/dlist.c +++ b/src/dlist.c @@ -22,7 +22,7 @@ void dlist_destroy(DList* dlist) { void dlist_destroypsv(DList* dlist) { free(dlist); } // Check whether the buffer is overflowing and resize it if necessary. -void check_resz(DList* dlist, size_t ln) { +void dlist_check_resz(DList* dlist, size_t ln) { while (dlist->ln + ln + 1 > dlist->sz) { // Double the buffer size when overflown. dlist->sz *= 2; @@ -34,7 +34,7 @@ void check_resz(DList* dlist, size_t ln) { } void dlist_append(DList* dest, void* src) { - check_resz(dest, 1); + dlist_check_resz(dest, 1); dest->buf[dest->ln] = src; dest->ln += 1; diff --git a/src/dstr.c b/src/dstr.c index 7b0d9f6..fcc0a4f 100644 --- a/src/dstr.c +++ b/src/dstr.c @@ -24,7 +24,7 @@ void dstr_destroy(Dstr* dstr) { void dstr_destroypsv(Dstr* dstr) { free(dstr); } // Check whether the buffer is overflowing and resize it if necessary. -void check_resz(Dstr* dstr, size_t ln) { +void dstr_check_resz(Dstr* dstr, size_t ln) { while (dstr->ln + ln + 1 > dstr->sz) { // Double the buffer size when overflown. dstr->sz *= 2; @@ -36,7 +36,7 @@ void check_resz(Dstr* dstr, size_t ln) { } void dstr_append(Dstr* dest, char* src, size_t ln) { - check_resz(dest, ln); + dstr_check_resz(dest, ln); // Overwrites the \0 at the end of the string, keeps the null from the given // string. @@ -45,7 +45,7 @@ void dstr_append(Dstr* dest, char* src, size_t ln) { } void dstr_appendch(Dstr* dest, char ch) { - check_resz(dest, 1); + dstr_check_resz(dest, 1); // Overwrites the preexisting null terminator, and adds one of its own. dest->buf[dest->ln] = ch; diff --git a/src/grammar.y b/src/grammar.y index 6c2092a..946ec05 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -3,6 +3,7 @@ #include #include "../../src/include/ast.h" #include "../../src/include/lexer.h" + #include "../../src/include/dlist.h" int yylex(void); void yyerror(char const*); @@ -12,6 +13,7 @@ %code requires { #include "../../src/include/ast.h" + #include "../../src/include/dlist.h" } %union { @@ -19,36 +21,39 @@ char* strval; AST* ast; ArgArr* argarr; + DList* exps; } %define parse.error verbose -%token BLOCKS -%token BLOCKE +%token BLOCKS // Block start {. +%token BLOCKE // Block end }. -%token GROUPS -%token GROUPE -%token SEP +%token GROUPS // Group start (. +%token GROUPE // Group end ). +%token SEP // Seperator ,. -%token EXPSEP +%token EXPSEP // Expression seperator ;. -%token WORD -%token NUM +%token WORD // Word, i.e. keyword. +%token NUM // Number. -%token SUB -%token PLUS -%token MULT -%token DIV +%token SUB // Subtract -. +%token ADD // Addition *. +%token MUL // Multiplication *. +%token DIV // Division /. -%token NL +%token NL // Newline. -%left PLUS SUB -%left MULT DIV +%left ADD SUB +%left MUL DIV %precedence NEG %type exp; %type arg; %type argstart; +%type blockstart; +%type block; %% @@ -58,7 +63,6 @@ input: | input EXPSEP exp { root = $3; } ; - argstart: exp { ArgArr* argarr = argarr_init(); @@ -75,9 +79,32 @@ arg: } ; +blockstart: + exp { + DList* exps = dlist_init(); // List of expressions. + dlist_append(exps, $1); + $$ = exps; + } + ; + +block: + blockstart { $$ = $1; } + | block EXPSEP exp { + dlist_append($1, $3); + $$ = $1; + } + ; + exp: NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); } + | BLOCKS exp BLOCKE { $$ = $2; } + + | BLOCKS block BLOCKE { + size_t i = $2->ln - 1; + $$ = $2->buf[i]; + } + | SUB exp { AST** argv = calloc(2, sizeof(AST*)); argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1)); @@ -87,7 +114,7 @@ exp: $$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv)); } - | LGROUP exp RGROUP { $$ = $2; } + | GROUPS exp GROUPE { $$ = $2; } // Variable reference. | WORD { @@ -101,7 +128,7 @@ exp: $$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, argc, argv)); } - | exp PLUS exp { + | exp ADD exp { AST** argv = calloc(2, sizeof(AST*)); argv[0] = $1; argv[1] = $3; @@ -119,7 +146,7 @@ exp: $$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv)); } - | exp MULT exp { + | exp MUL exp { AST** argv = calloc(2, sizeof(AST*)); argv[0] = $1; argv[1] = $3; diff --git a/src/lexer.c b/src/lexer.c index 052a4c4..b3bcee9 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -104,13 +104,13 @@ int yylex() { } switch (c) { - case '+': return PLUS; + case '+': return ADD; case '\n': return NL; case '-': return SUB; - case '*': return MULT; + case '*': return MUL; case '/': return DIV; - case '(': return LGROUP; - case ')': return RGROUP; + case '(': return GROUPS; + case ')': return GROUPE; case ',': return SEP; case ';': return EXPSEP; case '{': return BLOCKS; diff --git a/test/test_ast.c b/test/test_ast.c index 54a2ab2..ba8d1f0 100644 --- a/test/test_ast.c +++ b/test/test_ast.c @@ -52,7 +52,7 @@ void test_ast_vref() { int main() { UNITY_BEGIN(); - //RUN_TEST(test_ast_num); + RUN_TEST(test_ast_num); //RUN_TEST(test_ast_call); //RUN_TEST(test_ast_vref); return UNITY_END(); diff --git a/test/validation/test.bats b/test/val/test.bats similarity index 85% rename from test/validation/test.bats rename to test/val/test.bats index a9dba5a..f0bcf03 100644 --- a/test/validation/test.bats +++ b/test/val/test.bats @@ -7,6 +7,7 @@ bin() { ./scl.out $1 | tail -n1; } [ "$output" = "= 2.000000" ] run bin "-1+1" + echo $output [ "$output" = "= 0.000000" ] run bin "1+-1" @@ -88,15 +89,15 @@ bin() { ./scl.out $1 | tail -n1; } [ "$output" = "= 2.000000" ] } -@test "variable definition" { - run bin "x = 1" - [ "$output" = "= 1.000000" ] - - run bin "x = 1; x + 1" - [ "$output" = "= 2.000000" ] -} - -@test "function definition" { - run bin "f(n)=2*n; f(2)" - [ "$output" = "= 4.000000" ] -} +#@test "variable definition" { +# run bin "x = 1" +# [ "$output" = "= 1.000000" ] +# +# run bin "x = 1; x + 1" +# [ "$output" = "= 2.000000" ] +#} +# +#@test "function definition" { +# run bin "f(n)=2*n; f(2)" +# [ "$output" = "= 4.000000" ] +#}