From 4668736ce606806368cf234096d705f79353f947 Mon Sep 17 00:00:00 2001 From: Zander Thannhauser Date: Mon, 20 Jan 2025 13:40:51 -0600 Subject: [PATCH] . --- cmdln/new.c | 28 +- cmdln/struct.h | 6 +- debug.h | 8 +- examples/sandbox-interactive.txt | 7 + examples/sandbox.txt | 2 +- expression/application/evaluate.c | 6 + expression/error/evaluate.c | 26 ++ expression/error/evaluate.h | 7 + expression/error/free.c | 19 ++ expression/error/free.h | 6 + expression/error/inheritance.c | 20 ++ expression/error/inheritance.h | 2 + expression/error/new.c | 28 ++ expression/error/new.h | 9 + expression/error/prettyprint.c | 72 ++++++ expression/error/prettyprint.h | 14 + expression/error/prettyprint_errors.c | 38 +++ expression/error/prettyprint_errors.h | 4 + expression/error/struct.h | 12 + globals/terminalcolors.c | 51 ++++ globals/terminalcolors.cpp | 51 ++++ globals/terminalcolors.h | 19 ++ handle_file.c | 27 +- handle_file.h | 3 +- handle_init_file.c | 12 +- handle_interactive.c | 241 ++++++++++++++++-- handle_interactive.h | 7 +- handle_string.c | 37 +-- handle_string.h | 4 +- {parse/istream => istream}/file/free.c | 0 {parse/istream => istream}/file/free.h | 0 {parse/istream => istream}/file/inheritance.c | 0 {parse/istream => istream}/file/inheritance.h | 0 {parse/istream => istream}/file/new.c | 0 {parse/istream => istream}/file/new.h | 0 {parse/istream => istream}/file/read.c | 0 {parse/istream => istream}/file/read.h | 0 {parse/istream => istream}/file/struct.h | 0 {parse/istream => istream}/free.c | 0 {parse/istream => istream}/free.h | 0 {parse/istream => istream}/inc.c | 0 {parse/istream => istream}/inc.h | 0 {parse/istream => istream}/inheritance.h | 0 {parse/istream => istream}/new.c | 0 {parse/istream => istream}/new.h | 0 {parse/istream => istream}/read.c | 0 {parse/istream => istream}/read.h | 0 {parse/istream => istream}/string/free.c | 0 {parse/istream => istream}/string/free.h | 0 .../istream => istream}/string/inheritance.c | 0 .../istream => istream}/string/inheritance.h | 0 {parse/istream => istream}/string/new.c | 0 {parse/istream => istream}/string/new.h | 0 {parse/istream => istream}/string/read.c | 0 {parse/istream => istream}/string/read.h | 0 {parse/istream => istream}/string/struct.h | 0 {parse/istream => istream}/struct.h | 0 main.c | 39 ++- makefile | 10 +- memory/srealloc.c | 3 + parse/parse.c | 66 ++++- parse/tokenizer/free.c | 2 +- parse/tokenizer/new.c | 2 +- parse/tokenizer/next.c | 4 +- srclist.mk | 68 +++-- statement/error/execute.c | 69 +++++ statement/error/execute.h | 12 + statement/error/free.c | 20 ++ statement/error/free.h | 5 + statement/error/inheritance.c | 21 ++ statement/error/inheritance.h | 3 + statement/error/new.c | 28 ++ statement/error/new.h | 7 + statement/error/prettyprint.c | 28 ++ statement/error/prettyprint.h | 10 + statement/error/prettyprint_errors.c | 36 +++ statement/error/prettyprint_errors.h | 4 + statement/error/struct.h | 12 + string/new.c | 24 ++ string/new.h | 3 + stringtree/append.c | 5 +- {parse/wcistream => wcistream}/file/free.c | 0 {parse/wcistream => wcistream}/file/free.h | 0 .../file/inheritance.c | 0 .../file/inheritance.h | 0 {parse/wcistream => wcistream}/file/new.c | 0 {parse/wcistream => wcistream}/file/new.h | 0 {parse/wcistream => wcistream}/file/read.c | 0 {parse/wcistream => wcistream}/file/read.h | 0 {parse/wcistream => wcistream}/file/struct.h | 0 {parse/wcistream => wcistream}/free.c | 0 {parse/wcistream => wcistream}/free.h | 0 {parse/wcistream => wcistream}/inc.c | 0 {parse/wcistream => wcistream}/inc.h | 0 {parse/wcistream => wcistream}/inheritance.h | 0 {parse/wcistream => wcistream}/istream/free.c | 2 +- {parse/wcistream => wcistream}/istream/free.h | 0 .../istream/inheritance.c | 0 .../istream/inheritance.h | 0 {parse/wcistream => wcistream}/istream/new.c | 0 {parse/wcistream => wcistream}/istream/new.h | 0 {parse/wcistream => wcistream}/istream/read.c | 0 {parse/wcistream => wcistream}/istream/read.h | 0 .../wcistream => wcistream}/istream/struct.h | 0 {parse/wcistream => wcistream}/new.c | 0 {parse/wcistream => wcistream}/new.h | 0 {parse/wcistream => wcistream}/read.c | 0 {parse/wcistream => wcistream}/read.h | 0 {parse/wcistream => wcistream}/string/free.c | 0 {parse/wcistream => wcistream}/string/free.h | 0 .../string/inheritance.c | 0 .../string/inheritance.h | 0 {parse/wcistream => wcistream}/string/new.c | 0 {parse/wcistream => wcistream}/string/new.h | 0 {parse/wcistream => wcistream}/string/read.c | 0 {parse/wcistream => wcistream}/string/read.h | 0 .../wcistream => wcistream}/string/struct.h | 0 {parse/wcistream => wcistream}/struct.h | 0 zog.py | 44 +--- 119 files changed, 1106 insertions(+), 187 deletions(-) create mode 100644 examples/sandbox-interactive.txt create mode 100644 expression/error/evaluate.c create mode 100644 expression/error/evaluate.h create mode 100644 expression/error/free.c create mode 100644 expression/error/free.h create mode 100644 expression/error/inheritance.c create mode 100644 expression/error/inheritance.h create mode 100644 expression/error/new.c create mode 100644 expression/error/new.h create mode 100644 expression/error/prettyprint.c create mode 100644 expression/error/prettyprint.h create mode 100644 expression/error/prettyprint_errors.c create mode 100644 expression/error/prettyprint_errors.h create mode 100644 expression/error/struct.h create mode 100644 globals/terminalcolors.c create mode 100644 globals/terminalcolors.cpp create mode 100644 globals/terminalcolors.h rename {parse/istream => istream}/file/free.c (100%) rename {parse/istream => istream}/file/free.h (100%) rename {parse/istream => istream}/file/inheritance.c (100%) rename {parse/istream => istream}/file/inheritance.h (100%) rename {parse/istream => istream}/file/new.c (100%) rename {parse/istream => istream}/file/new.h (100%) rename {parse/istream => istream}/file/read.c (100%) rename {parse/istream => istream}/file/read.h (100%) rename {parse/istream => istream}/file/struct.h (100%) rename {parse/istream => istream}/free.c (100%) rename {parse/istream => istream}/free.h (100%) rename {parse/istream => istream}/inc.c (100%) rename {parse/istream => istream}/inc.h (100%) rename {parse/istream => istream}/inheritance.h (100%) rename {parse/istream => istream}/new.c (100%) rename {parse/istream => istream}/new.h (100%) rename {parse/istream => istream}/read.c (100%) rename {parse/istream => istream}/read.h (100%) rename {parse/istream => istream}/string/free.c (100%) rename {parse/istream => istream}/string/free.h (100%) rename {parse/istream => istream}/string/inheritance.c (100%) rename {parse/istream => istream}/string/inheritance.h (100%) rename {parse/istream => istream}/string/new.c (100%) rename {parse/istream => istream}/string/new.h (100%) rename {parse/istream => istream}/string/read.c (100%) rename {parse/istream => istream}/string/read.h (100%) rename {parse/istream => istream}/string/struct.h (100%) rename {parse/istream => istream}/struct.h (100%) create mode 100644 statement/error/execute.c create mode 100644 statement/error/execute.h create mode 100644 statement/error/free.c create mode 100644 statement/error/free.h create mode 100644 statement/error/inheritance.c create mode 100644 statement/error/inheritance.h create mode 100644 statement/error/new.c create mode 100644 statement/error/new.h create mode 100644 statement/error/prettyprint.c create mode 100644 statement/error/prettyprint.h create mode 100644 statement/error/prettyprint_errors.c create mode 100644 statement/error/prettyprint_errors.h create mode 100644 statement/error/struct.h rename {parse/wcistream => wcistream}/file/free.c (100%) rename {parse/wcistream => wcistream}/file/free.h (100%) rename {parse/wcistream => wcistream}/file/inheritance.c (100%) rename {parse/wcistream => wcistream}/file/inheritance.h (100%) rename {parse/wcistream => wcistream}/file/new.c (100%) rename {parse/wcistream => wcistream}/file/new.h (100%) rename {parse/wcistream => wcistream}/file/read.c (100%) rename {parse/wcistream => wcistream}/file/read.h (100%) rename {parse/wcistream => wcistream}/file/struct.h (100%) rename {parse/wcistream => wcistream}/free.c (100%) rename {parse/wcistream => wcistream}/free.h (100%) rename {parse/wcistream => wcistream}/inc.c (100%) rename {parse/wcistream => wcistream}/inc.h (100%) rename {parse/wcistream => wcistream}/inheritance.h (100%) rename {parse/wcistream => wcistream}/istream/free.c (89%) rename {parse/wcistream => wcistream}/istream/free.h (100%) rename {parse/wcistream => wcistream}/istream/inheritance.c (100%) rename {parse/wcistream => wcistream}/istream/inheritance.h (100%) rename {parse/wcistream => wcistream}/istream/new.c (100%) rename {parse/wcistream => wcistream}/istream/new.h (100%) rename {parse/wcistream => wcistream}/istream/read.c (100%) rename {parse/wcistream => wcistream}/istream/read.h (100%) rename {parse/wcistream => wcistream}/istream/struct.h (100%) rename {parse/wcistream => wcistream}/new.c (100%) rename {parse/wcistream => wcistream}/new.h (100%) rename {parse/wcistream => wcistream}/read.c (100%) rename {parse/wcistream => wcistream}/read.h (100%) rename {parse/wcistream => wcistream}/string/free.c (100%) rename {parse/wcistream => wcistream}/string/free.h (100%) rename {parse/wcistream => wcistream}/string/inheritance.c (100%) rename {parse/wcistream => wcistream}/string/inheritance.h (100%) rename {parse/wcistream => wcistream}/string/new.c (100%) rename {parse/wcistream => wcistream}/string/new.h (100%) rename {parse/wcistream => wcistream}/string/read.c (100%) rename {parse/wcistream => wcistream}/string/read.h (100%) rename {parse/wcistream => wcistream}/string/struct.h (100%) rename {parse/wcistream => wcistream}/struct.h (100%) diff --git a/cmdln/new.c b/cmdln/new.c index 4ecf495..b2c9c1a 100644 --- a/cmdln/new.c +++ b/cmdln/new.c @@ -21,10 +21,14 @@ struct cmdln_flags* new_cmdln_flags( bool read_init_file = true; - const char* custom_init_path = "~/.13rc"; + const char* init_path = "~/.13rc"; const char* input_string = NULL; + #ifndef RELEASE_BUILD + const char* test_interactive_using_input_file = NULL; + #endif + enum input_kind input_kind = ik_interactive; static const struct option long_options[] = { @@ -36,13 +40,18 @@ struct cmdln_flags* new_cmdln_flags( {"custom-init-path", required_argument, 0, 4}, + #ifndef RELEASE_BUILD + {"test-interactive-using-input-file", required_argument, 0, 5}, + #endif + {"command", required_argument, 0, 'c'}, {0, 0, 0, 0} }; for (int c; (c = getopt_long(argc, argv, "" - "\1\2\3\4c:\5\6" + "\1\2\3\4\5:\6" + "c:" "", long_options, NULL)) >= 0; ) { switch (c) @@ -72,7 +81,14 @@ struct cmdln_flags* new_cmdln_flags( case 4: // --custom-init-path { - custom_init_path = optarg; + init_path = optarg; + + break; + } + + case 5: // --test-interactive-using-input-file + { + test_interactive_using_input_file = optarg; break; } @@ -143,12 +159,16 @@ struct cmdln_flags* new_cmdln_flags( flags->read_init_file = read_init_file; - flags->custom_init_path = custom_init_path; + flags->init_path = init_path; flags->input_kind = input_kind; flags->input_string = input_string; + #ifndef RELEASE_BUILD + flags->test_interactive_using_input_file = test_interactive_using_input_file; + #endif + EXIT; return flags; } diff --git a/cmdln/struct.h b/cmdln/struct.h index 8e2d774..6ed44b1 100644 --- a/cmdln/struct.h +++ b/cmdln/struct.h @@ -18,12 +18,16 @@ struct cmdln_flags bool read_init_file; - const char* custom_init_path; + const char* init_path; const char* input_string; const char* input_file; + #ifndef RELEASE_BUILD + const char* test_interactive_using_input_file; + #endif + char* const* argv; }; diff --git a/debug.h b/debug.h index 2f53df6..7af367e 100644 --- a/debug.h +++ b/debug.h @@ -77,11 +77,7 @@ extern int debug_depth; #define TODO \ { \ - char filebuffer[PATH_MAX]; \ - strcpy(filebuffer, __FILE__ + 4); \ - filebuffer[strlen(filebuffer) - 2] = 0; \ - \ - printf("TODO hit at %s:%i!\n", filebuffer, __LINE__); \ + printf("TODO hit at %s:%i!\n", __FILE__, __LINE__); \ \ pid_t __child = fork(); \ \ @@ -102,7 +98,7 @@ extern int debug_depth; {\ char __buffer[100]; \ snprintf(__buffer, 100, "+%u", __LINE__); \ - execlp("gedit", "gedit", filebuffer, __buffer, NULL); \ + execlp("gedit", "gedit", __FILE__, __buffer, NULL); \ }\ \ exit(1); \ diff --git a/examples/sandbox-interactive.txt b/examples/sandbox-interactive.txt new file mode 100644 index 0000000..c882194 --- /dev/null +++ b/examples/sandbox-interactive.txt @@ -0,0 +1,7 @@ +1 +(1 +( +) ++ 1 2 +1 2 +λx:x diff --git a/examples/sandbox.txt b/examples/sandbox.txt index 02c7dd8..28652b9 100644 --- a/examples/sandbox.txt +++ b/examples/sandbox.txt @@ -47,7 +47,7 @@ fib 8 (𝑌 λ f x: (≤ x 1) x (+ (f (- x 1)) (f (- x 2)))) 10 -((((( ((((( 1 ))))) ))))) +((((( ((((( ((((( ((((( 1 ))))) ))))) ))))) ))))) diff --git a/expression/application/evaluate.c b/expression/application/evaluate.c index 48ea8c7..5e5ca06 100644 --- a/expression/application/evaluate.c +++ b/expression/application/evaluate.c @@ -90,7 +90,13 @@ struct value* application_expression_evaluate( { dpvu(lleft->kind); + // make some kind of 'error-value'? + // that's contagious, like NaN? + + // maybe error values can link to other error values? + // if more than on seperate error was encountered? TODO; + exit(1); break; } diff --git a/expression/error/evaluate.c b/expression/error/evaluate.c new file mode 100644 index 0000000..271e14b --- /dev/null +++ b/expression/error/evaluate.c @@ -0,0 +1,26 @@ + +#include + +#include "../evaluate.h" + +#include "struct.h" +#include "evaluate.h" + +struct value* error_expression_evaluate( + struct expression* super, + struct environment* environment, + struct booleans* booleans) +{ + ENTER; + + TODO; + #if 0 + struct error_expression* this = (void*) super; + + struct value* result = expression_evaluate( + this->subexpression, environment, booleans); + + EXIT; + return result; + #endif +} diff --git a/expression/error/evaluate.h b/expression/error/evaluate.h new file mode 100644 index 0000000..a4257da --- /dev/null +++ b/expression/error/evaluate.h @@ -0,0 +1,7 @@ + +struct value* error_expression_evaluate( + struct expression* this, + struct environment* environment, + struct booleans* booleans); + + diff --git a/expression/error/free.c b/expression/error/free.c new file mode 100644 index 0000000..6049ac7 --- /dev/null +++ b/expression/error/free.c @@ -0,0 +1,19 @@ + +#include + +#include "../free.h" + +#include "struct.h" +#include "free.h" + +void free_error_expression( + struct expression* super) +{ + ENTER; + + struct error_expression* this = (void*) super; + + free_expression(this->subexpression); + + EXIT; +} diff --git a/expression/error/free.h b/expression/error/free.h new file mode 100644 index 0000000..eb7a03c --- /dev/null +++ b/expression/error/free.h @@ -0,0 +1,6 @@ + +struct expression; + +void free_error_expression( + struct expression* super); + diff --git a/expression/error/inheritance.c b/expression/error/inheritance.c new file mode 100644 index 0000000..73746c4 --- /dev/null +++ b/expression/error/inheritance.c @@ -0,0 +1,20 @@ + +#include + +#include "../inheritance.h" + +#include "prettyprint_errors.h" +#include "prettyprint.h" +#include "evaluate.h" +#include "free.h" +#include "inheritance.h" + +struct expression_inheritance error_expression_inheritance = { + .prettyprint_errors = error_expression_prettyprint_errors, + + .prettyprint = error_expression_prettyprint, + + .evaluate = error_expression_evaluate, + + .free = free_error_expression +}; diff --git a/expression/error/inheritance.h b/expression/error/inheritance.h new file mode 100644 index 0000000..aff9689 --- /dev/null +++ b/expression/error/inheritance.h @@ -0,0 +1,2 @@ + +extern struct expression_inheritance error_expression_inheritance; diff --git a/expression/error/new.c b/expression/error/new.c new file mode 100644 index 0000000..2e5d9fd --- /dev/null +++ b/expression/error/new.c @@ -0,0 +1,28 @@ + +#include + +#include "../new.h" +#include "../inc.h" + +#include "inheritance.h" +#include "struct.h" +#include "new.h" + +struct expression* new_error_expression( + const wchar_t* message, + struct expression* subexpression) +{ + ENTER; + + struct error_expression* this = (void*) new_expression( + /* inheritance: */ &error_expression_inheritance, + /* alloc size: */ sizeof(*this)); + + this->message = message; + + this->subexpression = inc_expression(subexpression); + + EXIT; + return (void*) this; +} + diff --git a/expression/error/new.h b/expression/error/new.h new file mode 100644 index 0000000..4bd1bd1 --- /dev/null +++ b/expression/error/new.h @@ -0,0 +1,9 @@ + +struct value; +struct string; + +struct expression* new_error_expression( + const wchar_t* message, + struct expression* subexpression); + + diff --git a/expression/error/prettyprint.c b/expression/error/prettyprint.c new file mode 100644 index 0000000..cb71766 --- /dev/null +++ b/expression/error/prettyprint.c @@ -0,0 +1,72 @@ + +#include + +#include + +#include +#include +#include + +#include + +#include "../prettyprint.h" + +#include "struct.h" +#include "prettyprint.h" + +struct stringtree* error_expression_prettyprint( + int *out_chosen_color, + struct expression* super, + bool with_color) +{ + ENTER; + + TODO; + #if 0 + struct error_expression* const this = (void*) super; + + int inner_color; + + struct stringtree* tree = expression_prettyprint( + &inner_color, + this->subexpression, + with_color); + + if (with_color) + { + int my_color = (inner_color + 1) % (int) N(terminalcolors.precedences); + + stringtree_prepend_cstr(tree, terminalcolors.reset); + stringtree_prepend_cstr(tree, L"("); + stringtree_prepend_cstr(tree, terminalcolors.precedences[my_color]); + + stringtree_append_cstr(tree, terminalcolors.precedences[my_color]); + stringtree_append_cstr(tree, L")"); + stringtree_append_cstr(tree, terminalcolors.reset); + + if (out_chosen_color) + *out_chosen_color = my_color; + } + else + { + stringtree_prepend_cstr(tree, L"("); + stringtree_append_cstr(tree, L")"); + } + + EXIT; + return tree; + #endif +} + + + + + + + + + + + + + diff --git a/expression/error/prettyprint.h b/expression/error/prettyprint.h new file mode 100644 index 0000000..86797c5 --- /dev/null +++ b/expression/error/prettyprint.h @@ -0,0 +1,14 @@ + +#include + +struct expression; + +struct color_factory; + +struct stringtree* error_expression_prettyprint( + int *out_chosen_color, + struct expression* this, + bool with_color); + + + diff --git a/expression/error/prettyprint_errors.c b/expression/error/prettyprint_errors.c new file mode 100644 index 0000000..b59a8e7 --- /dev/null +++ b/expression/error/prettyprint_errors.c @@ -0,0 +1,38 @@ + +#include + +#include +#include + +#include "../prettyprint_errors.h" + +#include "struct.h" +#include "prettyprint_errors.h" + +struct stringtree* error_expression_prettyprint_errors( + struct expression* super) +{ + ENTER; + + struct error_expression* const this = (void*) super; + + struct stringtree* tree = NULL; + + if (this->subexpression) + { + tree = expression_prettyprint_errors( + this->subexpression); + } + + if (!tree) + { + tree = new_stringtree(); + } + + stringtree_append_cstr(tree, this->message); + stringtree_append_cstr(tree, L"\n"); + + EXIT; + return tree; +} + diff --git a/expression/error/prettyprint_errors.h b/expression/error/prettyprint_errors.h new file mode 100644 index 0000000..630a4c6 --- /dev/null +++ b/expression/error/prettyprint_errors.h @@ -0,0 +1,4 @@ + +struct stringtree* error_expression_prettyprint_errors( + struct expression* super); + diff --git a/expression/error/struct.h b/expression/error/struct.h new file mode 100644 index 0000000..aa7bd61 --- /dev/null +++ b/expression/error/struct.h @@ -0,0 +1,12 @@ + +#include "../struct.h" + +struct error_expression +{ + struct expression super; + + const wchar_t* message; + + struct expression* subexpression; +}; + diff --git a/globals/terminalcolors.c b/globals/terminalcolors.c new file mode 100644 index 0000000..2921255 --- /dev/null +++ b/globals/terminalcolors.c @@ -0,0 +1,51 @@ + +#include "terminalcolors.h" + +#include "bin/globals/terminalcolors.cpp-4.c" + + + + + + + + + + + +struct colors terminalcolors = { + .precedences = { +#include "bin/globals/terminalcolors.cpp-18.c" + + + + }, + +#include "bin/globals/terminalcolors.cpp-24.c" + + + + + + + + + + .reset = L"\e[0m", +}; + + + + + + + + + + + + + + + + diff --git a/globals/terminalcolors.cpp b/globals/terminalcolors.cpp new file mode 100644 index 0000000..2f83e80 --- /dev/null +++ b/globals/terminalcolors.cpp @@ -0,0 +1,51 @@ + +#include "terminalcolors.h" + +/*/ python + +import colorsys; + +def make_color(hue, sat, val): + t = colorsys.hsv_to_rgb(hue, sat, val); + + r, g, b = map(lambda x: int(x * 255), t); + + return f"\\e[38;2;{r};{g};{b}m"; +/*/ + +struct colors terminalcolors = { + .precedences = { + /*/ + for i in range(20): + print(f'L"{make_color(i / 20, 0.9, 1.0)}",'); + /*/ + }, + + /*/ + print(f'.numeric = L"{make_color(31 / 360, 0.8, 1.0)}",'); + + print(f'.string = L"{make_color(300 / 360, 0.8, 1.0)}",'); + + print(f'.variable = L"{make_color(263 / 360, 0.8, 1.0)}",'); + + print(f'.builtin = L"{make_color(0 / 360, 0.0, 0.52)}",'); + /*/ + + .reset = L"\e[0m", +}; + + + + + + + + + + + + + + + + diff --git a/globals/terminalcolors.h b/globals/terminalcolors.h new file mode 100644 index 0000000..09364ae --- /dev/null +++ b/globals/terminalcolors.h @@ -0,0 +1,19 @@ + +#include + +extern struct colors { + const wchar_t* precedences[20]; + + const wchar_t* numeric; + + const wchar_t* variable; + + const wchar_t* string; + + const wchar_t* builtin; + + const wchar_t* reset; +} terminalcolors; + + + diff --git a/handle_file.c b/handle_file.c index 840fdba..644e052 100644 --- a/handle_file.c +++ b/handle_file.c @@ -23,14 +23,14 @@ #include #include -#include -#include -#include -#include +#include +#include +#include +#include -#include -#include -#include +#include +#include +#include #include @@ -60,7 +60,8 @@ void handle_file( struct booleans* booleans, const char* input_file, bool echo, - bool print_with_colors) + bool print_with_colors, + struct wcostream* wc_stdout) { ENTER; @@ -68,12 +69,6 @@ void handle_file( assert(input_file); - struct ostream* b_stdout = new_file_ostream( - /* file descriptor: */ 1); - - struct wcostream* wc_stdout = new_ostream_wcostream( - /* binary stream: */ b_stdout); - struct istream* stream = new_file_istream( /* path: */ input_file); @@ -163,14 +158,10 @@ void handle_file( free_string(filename); - free_wcostream(wc_stdout); - free_wcistream(wcstream); free_istream(stream); - free_ostream(b_stdout); - EXIT; } diff --git a/handle_file.h b/handle_file.h index 81d4ef3..fcdcda6 100644 --- a/handle_file.h +++ b/handle_file.h @@ -4,6 +4,7 @@ void handle_file( struct booleans* booleans, const char* input_file, bool echo, - bool isatty); + bool isatty, + struct wcostream* wc_stdout); diff --git a/handle_init_file.c b/handle_init_file.c index fea3b01..0ff3797 100644 --- a/handle_init_file.c +++ b/handle_init_file.c @@ -16,13 +16,13 @@ #include #include -#include -#include -#include +#include +#include +#include -#include -#include -#include +#include +#include +#include #include diff --git a/handle_interactive.c b/handle_interactive.c index de9a023..044ab6c 100644 --- a/handle_interactive.c +++ b/handle_interactive.c @@ -13,6 +13,35 @@ #include #include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + #include "handle_interactive.h" static const enum state { @@ -24,6 +53,10 @@ static const enum state { s_backspace, + s_enter, + + s_insert_lambda, + s_insert_letter, s_up, @@ -36,6 +69,8 @@ static const enum state { s_start, + s_sync_idle, + s_esc, s_csi, @@ -45,10 +80,25 @@ static const enum state { [s_start][0x7F] = s_backspace, + [s_start][0x16] = s_sync_idle, + [s_sync_idle]['\\'] = s_insert_lambda, + + [s_start]['\n'] = s_enter, + + [s_start][' '] = s_insert_letter, [s_start]['0' ... '9'] = s_insert_letter, [s_start]['a' ... 'z'] = s_insert_letter, [s_start]['A' ... 'Z'] = s_insert_letter, + [s_start][':'] = s_insert_letter, + [s_start]['+'] = s_insert_letter, + [s_start]['-'] = s_insert_letter, + [s_start]['/'] = s_insert_letter, + [s_start]['.'] = s_insert_letter, + [s_start]['('] = s_insert_letter, + [s_start][')'] = s_insert_letter, + [s_start]['\\'] = s_insert_letter, + [s_start][0x1B] = s_esc, [s_esc]['['] = s_csi, [s_start][0x9B] = s_csi, @@ -65,27 +115,47 @@ static const enum state { void handle_interactive( struct environment** environment, - struct booleans* booleans) + struct booleans* booleans, + struct wcostream* wc_stdout + #ifndef RELEASE_BUILD + , const char* test_interactive_using_input_file + #endif + ) { ENTER; + int fd; + struct termios termios; - if (tcgetattr(/* fd: */ 1, /* struct termios: */ &termios) < 0) + if (test_interactive_using_input_file) { - TODO; - exit(1); + if ((fd = open(test_interactive_using_input_file, O_RDONLY)) < 0) + { + TODO; + exit(1); + } } - - struct termios original = termios; - - termios.c_lflag &= ~(unsigned) ICANON; // disable ICANON. - termios.c_lflag &= ~(unsigned) ECHO; // disable ECHO. - - if (tcsetattr(/* fd: */ 1, /* when?: */ TCSADRAIN, /* struct termios: */ &termios) < 0) + else { - TODO; - exit(1); + fd = 0; + + if (tcgetattr(/* fd: */ 1, /* struct termios: */ &termios) < 0) + { + TODO; + exit(1); + } + + struct termios modified_termios = termios; + + modified_termios.c_lflag &= ~(unsigned) ICANON; // disable ICANON. + modified_termios.c_lflag &= ~(unsigned) ECHO; // disable ECHO. + + if (tcsetattr(/* fd: */ 1, /* when?: */ TCSADRAIN, /* struct termios: */ &modified_termios) < 0) + { + TODO; + exit(1); + } } size_t width = 50; @@ -109,6 +179,8 @@ void handle_interactive( { live.data = smalloc(sizeof(*live.data) * width); + live.pos = 0; + live.cap = width; memset(live.data, 0, sizeof(*live.data) * width); @@ -148,7 +220,7 @@ void handle_interactive( { assert(line.pos <= line.n); - ensure_capacity(line.cap + 1); + ensure_capacity(line.n + 1); memmove( /* dest: */ line.data + line.pos + 1, @@ -172,6 +244,8 @@ void handle_interactive( assert(line.n <= width); + struct stringtree* tree = new_stringtree(); + size_t i = 0; for (; i < width; i++) @@ -184,26 +258,33 @@ void handle_interactive( { if (pos != i) { - printf("\e[%zuG", i + 1); // Move cursor to indicated column in current row. + stringtree_append_formatstr(tree, "\e[%zuG", i + 1); // Move cursor to indicated column in current row. + pos = i; } - printf("%c", (uint8_t) intended_letter->code); + struct string* s = new_string(&intended_letter->code, 1); + + stringtree_append_string(tree, s); live_letter->code = intended_letter->code; pos++; + + free_string(s); } } if (pos != line.pos) { - printf("\e[%zuG", line.pos + 1); // Move cursor to indicated column in current row. + stringtree_append_formatstr(tree, "\e[%zuG", line.pos + 1); // Move cursor to indicated column in current row. live.pos = pos; } - fflush(stdout); + stringtree_print(tree, wc_stdout); + + free_stringtree(tree); } struct { @@ -249,11 +330,18 @@ void handle_interactive( fflush(stdout); } - uint8_t buffer[4096]; + struct string* filename = new_string(L"", (unsigned) -1); + + struct position* position = new_position( + /* filename: */ filename, + /* line: */ 1, + /* column: */ 1); for (bool keep_going = true, error = false; !error && keep_going; ) { - ssize_t retval = read(0, buffer, sizeof(buffer)); + uint8_t buffer[4096]; + + ssize_t retval = read(fd, buffer, sizeof(buffer)); if (retval < 0) { @@ -261,7 +349,7 @@ void handle_interactive( } else if (!retval) { - TODO; + keep_going = false; } else for (ssize_t i = 0; i < retval; i++) { @@ -303,6 +391,83 @@ void handle_interactive( break; } + case s_enter: + { + puts(""); + + if (line.n) + { + wchar_t* string = smalloc(sizeof(*string) * (line.n + 1)); + + for (size_t j = 0, m = line.n; j < m; j++) + { + string[j] = line.data[j].code; + } + + string[line.n] = 0; + + struct wcistream* stream = new_string_wcistream( + /* input_string: */ string); + + wcistream_read(stream); + + struct tokenizer* tokenizer = new_tokenizer( + /* istream: */ stream, + /* position: */ position); + + tokenizer_next(tokenizer); + + struct statement* statement = parse( + /* tokenizer: */ tokenizer); + + struct stringtree* etree = statement_prettyprint_errors( + /* instance: */ statement); + + if (etree) + { + stringtree_println(etree, + /* wide-character stdout: */ wc_stdout); + } + else + { + struct stringtree* stree = statement_prettyprint( + /* chosen color reference: */ NULL, + /* instance: */ statement, + /* print with color? */ true); + + stringtree_println(stree, + /* wide-character stdout: */ wc_stdout); + + statement_execute( + /* instance: */ statement, + /* environment: */ environment, + /* booleans: */ booleans, + /* wide-character stdout: */ wc_stdout, + /* print value: */ true, + /* print with color?: */ true); + + free_stringtree(stree); + } + + line.n = 0; + line.pos = 0; + + redraw(); + + free_stringtree(etree); + + free_statement(statement); + + free_tokenizer(tokenizer); + + free_wcistream(stream); + + free(string); + } + + break; + } + case s_delete: { if (line.pos < line.n) @@ -315,6 +480,15 @@ void handle_interactive( break; } + case s_insert_lambda: + { + insert_into_line_at_pos(L'λ'), line.pos++; + + redraw(); + + break; + } + case s_insert_letter: { insert_into_line_at_pos(buffer[i]), line.pos++; @@ -387,15 +561,26 @@ void handle_interactive( } } - // running them through a tokenizer-like state machine? - - // different states have corasponding actions - - if (tcsetattr(/* fd: */ 1, /* when?: */ TCSADRAIN, /* struct termios: */ &original) < 0) + if (test_interactive_using_input_file) { - TODO; - exit(1); + close(fd); } + else + { + if (tcsetattr(/* fd: */ 1, /* when?: */ TCSADRAIN, /* struct termios: */ &termios) < 0) + { + TODO; + exit(1); + } + } + + free_position(position); + + free_string(filename); + + free(token.data); + + free(live.data); free(line.data); diff --git a/handle_interactive.h b/handle_interactive.h index a7dbf65..e7da932 100644 --- a/handle_interactive.h +++ b/handle_interactive.h @@ -5,5 +5,10 @@ struct color_factory; void handle_interactive( struct environment** environment, - struct booleans* booleans); + struct booleans* booleans, + struct wcostream* wc_stdout + #ifndef RELEASE_BUILD + , const char* test_interactive_input_file + #endif + ); diff --git a/handle_string.c b/handle_string.c index b3e8219..886443c 100644 --- a/handle_string.c +++ b/handle_string.c @@ -9,28 +9,18 @@ #include #include -/*#include */ -/*#include */ - -/*#include */ -/*#include */ - -/*#include */ -/*#include */ -/*#include */ - #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include -#include -#include -#include +#include +#include +#include #include @@ -60,18 +50,13 @@ void handle_string( struct booleans* booleans, const char* input_string, bool echo, - bool print_with_colors) + bool print_with_colors, + struct wcostream* wc_stdout) { ENTER; assert(input_string); - struct ostream* b_stdout = new_file_ostream( - /* file descriptor: */ 1); - - struct wcostream* wc_stdout = new_ostream_wcostream( - /* binary stream: */ b_stdout); - struct istream* stream = new_string_istream( /* read-only text: */ (const uint8_t*) input_string); @@ -156,14 +141,10 @@ void handle_string( free_string(filename); - free_wcostream(wc_stdout); - free_wcistream(wcstream); free_istream(stream); - free_ostream(b_stdout); - EXIT; } diff --git a/handle_string.h b/handle_string.h index 8879d83..038d7e4 100644 --- a/handle_string.h +++ b/handle_string.h @@ -1,4 +1,5 @@ +struct wcostream; struct color_factory; struct environment; struct booleans; @@ -8,5 +9,6 @@ void handle_string( struct booleans* booleans, const char* input_file, bool echo, - bool print_with_colors); + bool print_with_colors, + struct wcostream* wc_stdout); diff --git a/parse/istream/file/free.c b/istream/file/free.c similarity index 100% rename from parse/istream/file/free.c rename to istream/file/free.c diff --git a/parse/istream/file/free.h b/istream/file/free.h similarity index 100% rename from parse/istream/file/free.h rename to istream/file/free.h diff --git a/parse/istream/file/inheritance.c b/istream/file/inheritance.c similarity index 100% rename from parse/istream/file/inheritance.c rename to istream/file/inheritance.c diff --git a/parse/istream/file/inheritance.h b/istream/file/inheritance.h similarity index 100% rename from parse/istream/file/inheritance.h rename to istream/file/inheritance.h diff --git a/parse/istream/file/new.c b/istream/file/new.c similarity index 100% rename from parse/istream/file/new.c rename to istream/file/new.c diff --git a/parse/istream/file/new.h b/istream/file/new.h similarity index 100% rename from parse/istream/file/new.h rename to istream/file/new.h diff --git a/parse/istream/file/read.c b/istream/file/read.c similarity index 100% rename from parse/istream/file/read.c rename to istream/file/read.c diff --git a/parse/istream/file/read.h b/istream/file/read.h similarity index 100% rename from parse/istream/file/read.h rename to istream/file/read.h diff --git a/parse/istream/file/struct.h b/istream/file/struct.h similarity index 100% rename from parse/istream/file/struct.h rename to istream/file/struct.h diff --git a/parse/istream/free.c b/istream/free.c similarity index 100% rename from parse/istream/free.c rename to istream/free.c diff --git a/parse/istream/free.h b/istream/free.h similarity index 100% rename from parse/istream/free.h rename to istream/free.h diff --git a/parse/istream/inc.c b/istream/inc.c similarity index 100% rename from parse/istream/inc.c rename to istream/inc.c diff --git a/parse/istream/inc.h b/istream/inc.h similarity index 100% rename from parse/istream/inc.h rename to istream/inc.h diff --git a/parse/istream/inheritance.h b/istream/inheritance.h similarity index 100% rename from parse/istream/inheritance.h rename to istream/inheritance.h diff --git a/parse/istream/new.c b/istream/new.c similarity index 100% rename from parse/istream/new.c rename to istream/new.c diff --git a/parse/istream/new.h b/istream/new.h similarity index 100% rename from parse/istream/new.h rename to istream/new.h diff --git a/parse/istream/read.c b/istream/read.c similarity index 100% rename from parse/istream/read.c rename to istream/read.c diff --git a/parse/istream/read.h b/istream/read.h similarity index 100% rename from parse/istream/read.h rename to istream/read.h diff --git a/parse/istream/string/free.c b/istream/string/free.c similarity index 100% rename from parse/istream/string/free.c rename to istream/string/free.c diff --git a/parse/istream/string/free.h b/istream/string/free.h similarity index 100% rename from parse/istream/string/free.h rename to istream/string/free.h diff --git a/parse/istream/string/inheritance.c b/istream/string/inheritance.c similarity index 100% rename from parse/istream/string/inheritance.c rename to istream/string/inheritance.c diff --git a/parse/istream/string/inheritance.h b/istream/string/inheritance.h similarity index 100% rename from parse/istream/string/inheritance.h rename to istream/string/inheritance.h diff --git a/parse/istream/string/new.c b/istream/string/new.c similarity index 100% rename from parse/istream/string/new.c rename to istream/string/new.c diff --git a/parse/istream/string/new.h b/istream/string/new.h similarity index 100% rename from parse/istream/string/new.h rename to istream/string/new.h diff --git a/parse/istream/string/read.c b/istream/string/read.c similarity index 100% rename from parse/istream/string/read.c rename to istream/string/read.c diff --git a/parse/istream/string/read.h b/istream/string/read.h similarity index 100% rename from parse/istream/string/read.h rename to istream/string/read.h diff --git a/parse/istream/string/struct.h b/istream/string/struct.h similarity index 100% rename from parse/istream/string/struct.h rename to istream/string/struct.h diff --git a/parse/istream/struct.h b/istream/struct.h similarity index 100% rename from parse/istream/struct.h rename to istream/struct.h diff --git a/main.c b/main.c index 8bc41b7..83f6583 100644 --- a/main.c +++ b/main.c @@ -14,8 +14,11 @@ #include #include -/*#include */ -/*#include */ +#include +#include + +#include +#include #include #include @@ -44,9 +47,15 @@ int main(int argc, char* const* argv) if (flags->read_init_file) { - handle_init_file(&environment, booleans, flags->custom_init_path); + handle_init_file(&environment, booleans, flags->init_path); } + struct ostream* b_stdout = new_file_ostream( + /* file descriptor: */ 1); + + struct wcostream* wc_stdout = new_ostream_wcostream( + /* binary stream: */ b_stdout); + switch (flags->input_kind) { case ik_string: @@ -55,8 +64,9 @@ int main(int argc, char* const* argv) /* environment, may by modified/replaced: */ &environment, /* booleans: */ booleans, /* input string: */ flags->input_string, - /* echo? */ flags->echo, - /* colors?: */ flags->print_with_colors); + /* echo statements? */ flags->echo, + /* colors?: */ flags->print_with_colors, + /* wide-character output stream: */ wc_stdout); break; } @@ -67,23 +77,32 @@ int main(int argc, char* const* argv) /* environment, may by modified/replaced: */ &environment, /* booleans: */ booleans, /* input file: */ flags->input_file, - /* echo? */ flags->echo, - /* colors?: */ flags->print_with_colors); + /* echo statements? */ flags->echo, + /* colors?: */ flags->print_with_colors, + /* wide-character output stream: */ wc_stdout); break; } case ik_interactive: { + #ifdef RELEASE_BUILD if (!isatty(1)) { TODO; exit(1); } + #endif handle_interactive( /* environment, may by modified/replaced: */ &environment, - /* booleans: */ booleans); + /* booleans: */ booleans, + /* wide-character output stream: */ wc_stdout + #ifndef RELEASE_BUILD + , /* test interactive using input file: */ + flags->test_interactive_using_input_file + #endif + ); break; } @@ -95,6 +114,10 @@ int main(int argc, char* const* argv) } } + free_wcostream(wc_stdout); + + free_ostream(b_stdout); + free_environment(environment); free_booleans(booleans); diff --git a/makefile b/makefile index 50ad5cb..db1a99b 100644 --- a/makefile +++ b/makefile @@ -25,7 +25,7 @@ endif srclist.mk: @echo "searching for source files ..." - @find -name '*.c*' -! -path '*/junk/*' \ + @find -name '*.c*' -! -path '*/bin/*' -! -path '*/junk/*' \ | sed 's/.cpp$$/.c/g;s/^/srcs += /g' \ | sort -Vu > ${@} @@ -33,9 +33,9 @@ include srclist.mk .PRECIOUS: %.c -%.c: %.cpp zog.py +%.c: %.cpp zog.py | bin/ @echo "preprocessing ${<} ..." - @python3.10 ./zog.py -i $< -o $@ + @./zog.py -i $< --snippet-directory=bin -o $@ ${prefix}/%.o ${prefix}/%.d: %.c ${optionset} | ${prefix}/%/ @echo "compiling (${buildtype}) ${*}.c ..." @@ -56,7 +56,9 @@ args += --custom-init-path ./examples/init.txt #args += -c '1' -args += ./examples/sandbox.txt +#args += ./examples/sandbox.txt + +args += --test-interactive-using-input-file ./examples/sandbox-interactive.txt run: ${prefix}/13 $< ${args} diff --git a/memory/srealloc.c b/memory/srealloc.c index 8b7673b..d537dbc 100644 --- a/memory/srealloc.c +++ b/memory/srealloc.c @@ -1,4 +1,5 @@ +#include #include #include @@ -12,6 +13,8 @@ void* srealloc( if (!newptr) { + printf("oldptr = %p\n", oldptr); + printf("size = %zu\n", size); assert(!"TODO"); } diff --git a/parse/parse.c b/parse/parse.c index 3c04bbb..4c49717 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -24,12 +24,14 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -244,7 +246,34 @@ struct expression* parse_primary_expression( parse_application_expression(tokenizer, /* in parenthesis: */ true); - retval = new_parenthesis_expression(subexpression); + switch (tokenizer->token->kind) + { + case tk_cparen: + retval = new_parenthesis_expression(subexpression); + break; + + case tk_EOF: + { + retval = new_error_expression( + L"Unexpected EOF when reading parenthesis. " + L"Expecting close parenthesis.", + subexpression); + + break; + } + + case tk_semicolon: + TODO; + break; + + case tk_error: + TODO; + break; + + default: + TODO; + break; + } tokenizer_next(tokenizer); @@ -330,7 +359,14 @@ struct expression* parse_application_expression( case tk_error: TODO; break; + + case tk_EOF: + { + retval = new_error_expression(L"Unexpected EOF.", NULL); + break; + } + default: TODO; break; @@ -371,14 +407,6 @@ struct expression* parse_application_expression( case tk_semicolon: case tk_EOF: - { - if (in_parentheses) - { - TODO; - } - break; - } - case tk_cparen: { break; @@ -504,6 +532,14 @@ struct statement* parse_statement( TODO; break; + case tk_cparen: + { + retval = new_error_statement( + L"Unexpected close parenthesis when reading statement", NULL); + + break; + } + case tk_error: TODO; // create error statement ("unknown token") @@ -550,12 +586,18 @@ struct statement* parse_statements( left = inc_statement(right); } - if (tokenizer->token->kind == tk_cparen) + while (tokenizer->token->kind == tk_cparen) { - TODO; + struct statement* oldleft = left; + + left = new_error_statement(L"Extra close parenthesis.", left); + + free_statement(oldleft); + + tokenizer_next(tokenizer); } - if (tokenizer->token->kind == tk_semicolon) + while (tokenizer->token->kind == tk_semicolon) { tokenizer_next(tokenizer); } diff --git a/parse/tokenizer/free.c b/parse/tokenizer/free.c index 4b156e7..c9ca4d4 100644 --- a/parse/tokenizer/free.c +++ b/parse/tokenizer/free.c @@ -3,7 +3,7 @@ #include -#include "../wcistream/free.h" +#include #include "../position/free.h" diff --git a/parse/tokenizer/new.c b/parse/tokenizer/new.c index 7480190..c57a65e 100644 --- a/parse/tokenizer/new.c +++ b/parse/tokenizer/new.c @@ -1,7 +1,7 @@ #include -#include +#include #include diff --git a/parse/tokenizer/next.c b/parse/tokenizer/next.c index e4c59b4..5ae9904 100644 --- a/parse/tokenizer/next.c +++ b/parse/tokenizer/next.c @@ -10,8 +10,8 @@ #include #include -#include "../wcistream/struct.h" -#include "../wcistream/read.h" +#include +#include #include "../position/struct.h" #include "../position/clone.h" diff --git a/srclist.mk b/srclist.mk index 00e8fa3..5083662 100644 --- a/srclist.mk +++ b/srclist.mk @@ -28,6 +28,12 @@ srcs += ./expression/application/inheritance.c srcs += ./expression/application/new.c srcs += ./expression/application/prettyprint.c srcs += ./expression/application/prettyprint_errors.c +srcs += ./expression/error/evaluate.c +srcs += ./expression/error/free.c +srcs += ./expression/error/inheritance.c +srcs += ./expression/error/new.c +srcs += ./expression/error/prettyprint.c +srcs += ./expression/error/prettyprint_errors.c srcs += ./expression/evaluate.c srcs += ./expression/free.c srcs += ./expression/inc.c @@ -67,6 +73,18 @@ srcs += ./handle_file.c srcs += ./handle_init_file.c srcs += ./handle_interactive.c srcs += ./handle_string.c +srcs += ./istream/file/free.c +srcs += ./istream/file/inheritance.c +srcs += ./istream/file/new.c +srcs += ./istream/file/read.c +srcs += ./istream/free.c +srcs += ./istream/inc.c +srcs += ./istream/new.c +srcs += ./istream/read.c +srcs += ./istream/string/free.c +srcs += ./istream/string/inheritance.c +srcs += ./istream/string/new.c +srcs += ./istream/string/read.c srcs += ./main.c srcs += ./memory/smalloc.c srcs += ./memory/srealloc.c @@ -89,18 +107,6 @@ srcs += ./ostream/free.c srcs += ./ostream/inc.c srcs += ./ostream/new.c srcs += ./ostream/write.c -srcs += ./parse/istream/file/free.c -srcs += ./parse/istream/file/inheritance.c -srcs += ./parse/istream/file/new.c -srcs += ./parse/istream/file/read.c -srcs += ./parse/istream/free.c -srcs += ./parse/istream/inc.c -srcs += ./parse/istream/new.c -srcs += ./parse/istream/read.c -srcs += ./parse/istream/string/free.c -srcs += ./parse/istream/string/inheritance.c -srcs += ./parse/istream/string/new.c -srcs += ./parse/istream/string/read.c srcs += ./parse/parse.c srcs += ./parse/position/assign.c srcs += ./parse/position/clone.c @@ -116,28 +122,18 @@ srcs += ./parse/tokenizer/put_back.c srcs += ./parse/token/free.c srcs += ./parse/token/inc.c srcs += ./parse/token/new.c -srcs += ./parse/wcistream/file/free.c -srcs += ./parse/wcistream/file/inheritance.c -srcs += ./parse/wcistream/file/new.c -srcs += ./parse/wcistream/file/read.c -srcs += ./parse/wcistream/free.c -srcs += ./parse/wcistream/inc.c -srcs += ./parse/wcistream/istream/free.c -srcs += ./parse/wcistream/istream/inheritance.c -srcs += ./parse/wcistream/istream/new.c -srcs += ./parse/wcistream/istream/read.c -srcs += ./parse/wcistream/new.c -srcs += ./parse/wcistream/read.c -srcs += ./parse/wcistream/string/free.c -srcs += ./parse/wcistream/string/inheritance.c -srcs += ./parse/wcistream/string/new.c -srcs += ./parse/wcistream/string/read.c srcs += ./statement/assignment/execute.c srcs += ./statement/assignment/free.c srcs += ./statement/assignment/inheritance.c srcs += ./statement/assignment/new.c srcs += ./statement/assignment/prettyprint.c srcs += ./statement/assignment/prettyprint_errors.c +srcs += ./statement/error/execute.c +srcs += ./statement/error/free.c +srcs += ./statement/error/inheritance.c +srcs += ./statement/error/new.c +srcs += ./statement/error/prettyprint.c +srcs += ./statement/error/prettyprint_errors.c srcs += ./statement/execute.c srcs += ./statement/expression/execute.c srcs += ./statement/expression/free.c @@ -190,6 +186,22 @@ srcs += ./value/number/inheritance.c srcs += ./value/number/new.c srcs += ./value/number/prettyprint.c srcs += ./value/prettyprint.c +srcs += ./wcistream/file/free.c +srcs += ./wcistream/file/inheritance.c +srcs += ./wcistream/file/new.c +srcs += ./wcistream/file/read.c +srcs += ./wcistream/free.c +srcs += ./wcistream/inc.c +srcs += ./wcistream/istream/free.c +srcs += ./wcistream/istream/inheritance.c +srcs += ./wcistream/istream/new.c +srcs += ./wcistream/istream/read.c +srcs += ./wcistream/new.c +srcs += ./wcistream/read.c +srcs += ./wcistream/string/free.c +srcs += ./wcistream/string/inheritance.c +srcs += ./wcistream/string/new.c +srcs += ./wcistream/string/read.c srcs += ./wcostream/file/free.c srcs += ./wcostream/file/new.c srcs += ./wcostream/file/write.c diff --git a/statement/error/execute.c b/statement/error/execute.c new file mode 100644 index 0000000..419b00b --- /dev/null +++ b/statement/error/execute.c @@ -0,0 +1,69 @@ + +#include + +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include "struct.h" +#include "execute.h" + +void error_statement_execute( + struct statement* super, + struct environment** environment, + struct booleans* booleans, + struct wcostream* ostream, + bool print_value, + bool print_with_color) +{ + ENTER; + + TODO; + #if 0 + struct error_statement* this = (void*) super; + + struct value* value = error_evaluate( + /* error: */ this->error, + /* environment: */ *environment, + /* booleans: */ booleans); + + struct value* vvalue = lazy_value_unlazy(value, booleans); + + if (print_value) + { + struct stringtree* tree = value_prettyprint( + /* outgoing chosen color: */ NULL, + /* value: */ vvalue, + /* print with color? */ print_with_color); + + stringtree_println(tree, ostream); + + free_stringtree(tree); + } + + free_value(vvalue); + + free_value(value); + #endif + + EXIT; +} + + + + + + + + + + + diff --git a/statement/error/execute.h b/statement/error/execute.h new file mode 100644 index 0000000..6c0a998 --- /dev/null +++ b/statement/error/execute.h @@ -0,0 +1,12 @@ + +struct statement; +struct environment; +struct color_factory; + +void error_statement_execute( + struct statement* this, + struct environment** environment, + struct booleans* booleans, + struct wcostream* ostream, + bool print_value, + bool print_with_color); diff --git a/statement/error/free.c b/statement/error/free.c new file mode 100644 index 0000000..9f47990 --- /dev/null +++ b/statement/error/free.c @@ -0,0 +1,20 @@ + +#include + +#include + +#include "struct.h" +#include "free.h" + +void free_error_statement( + struct statement* super) +{ + ENTER; + + struct error_statement* this = (void*) super; + + free_statement(this->substatement); + + EXIT; +} + diff --git a/statement/error/free.h b/statement/error/free.h new file mode 100644 index 0000000..12f7bb0 --- /dev/null +++ b/statement/error/free.h @@ -0,0 +1,5 @@ + +struct statement; + +void free_error_statement( + struct statement* this); diff --git a/statement/error/inheritance.c b/statement/error/inheritance.c new file mode 100644 index 0000000..2e51223 --- /dev/null +++ b/statement/error/inheritance.c @@ -0,0 +1,21 @@ + +#include + +#include "../inheritance.h" + +#include "inheritance.h" + +#include "prettyprint.h" +#include "prettyprint_errors.h" +#include "execute.h" +#include "free.h" + +struct statement_inheritance error_statement_inheritance = { + .prettyprint_errors = error_statement_prettyprint_errors, + + .prettyprint = error_statement_prettyprint, + + .execute = error_statement_execute, + + .free = free_error_statement, +}; diff --git a/statement/error/inheritance.h b/statement/error/inheritance.h new file mode 100644 index 0000000..81ca494 --- /dev/null +++ b/statement/error/inheritance.h @@ -0,0 +1,3 @@ + +extern struct statement_inheritance error_statement_inheritance; + diff --git a/statement/error/new.c b/statement/error/new.c new file mode 100644 index 0000000..ac6a565 --- /dev/null +++ b/statement/error/new.c @@ -0,0 +1,28 @@ + +#include + +#include "../new.h" +#include "../inc.h" + +#include "inheritance.h" +#include "struct.h" +#include "new.h" + +struct statement* new_error_statement( + const wchar_t* message, + struct statement* substatement) +{ + ENTER; + + struct error_statement* this = (void*) new_statement( + /* inheritance: */ &error_statement_inheritance, + /* alloc size: */ sizeof(*this)); + + this->message = message; + + this->substatement = inc_statement(substatement); + + EXIT; + return (void*) this; +} + diff --git a/statement/error/new.h b/statement/error/new.h new file mode 100644 index 0000000..f42578a --- /dev/null +++ b/statement/error/new.h @@ -0,0 +1,7 @@ + +struct error; + +struct statement* new_error_statement( + const wchar_t* message, + struct statement* statement); + diff --git a/statement/error/prettyprint.c b/statement/error/prettyprint.c new file mode 100644 index 0000000..6ec5ae8 --- /dev/null +++ b/statement/error/prettyprint.c @@ -0,0 +1,28 @@ + +#include + +#include + +#include "struct.h" +#include "prettyprint.h" + +struct stringtree* error_statement_prettyprint( + int *out_chosen_color, + struct statement* super, + bool with_color) +{ + ENTER; + + TODO; + #if 0 + struct error_statement* const this = (void*) super; + + struct stringtree* tree = error_prettyprint( + out_chosen_color, + this->error, with_color); + + EXIT; + return tree; + #endif +} + diff --git a/statement/error/prettyprint.h b/statement/error/prettyprint.h new file mode 100644 index 0000000..0b9c7d2 --- /dev/null +++ b/statement/error/prettyprint.h @@ -0,0 +1,10 @@ + +#include + +struct color_factory; + +struct stringtree* error_statement_prettyprint( + int *out_chosen_color, + struct statement* this, + bool with_color); + diff --git a/statement/error/prettyprint_errors.c b/statement/error/prettyprint_errors.c new file mode 100644 index 0000000..53ed41a --- /dev/null +++ b/statement/error/prettyprint_errors.c @@ -0,0 +1,36 @@ + +#include + +#include +#include + +#include + +#include "struct.h" +#include "prettyprint_errors.h" + +struct stringtree* error_statement_prettyprint_errors( + struct statement* super) +{ + ENTER; + + struct error_statement* this = (void*) super; + + struct stringtree* tree = NULL; + + if (this->substatement) + { + tree = statement_prettyprint_errors(this->substatement); + } + + if (!tree) + { + tree = new_stringtree(); + } + + stringtree_append_cstr(tree, this->message); + stringtree_append_cstr(tree, L"\n"); + + EXIT; + return tree; +} diff --git a/statement/error/prettyprint_errors.h b/statement/error/prettyprint_errors.h new file mode 100644 index 0000000..68c52ee --- /dev/null +++ b/statement/error/prettyprint_errors.h @@ -0,0 +1,4 @@ + +struct stringtree* error_statement_prettyprint_errors( + struct statement* super); + diff --git a/statement/error/struct.h b/statement/error/struct.h new file mode 100644 index 0000000..0ff539e --- /dev/null +++ b/statement/error/struct.h @@ -0,0 +1,12 @@ + +#include "../struct.h" + +struct error_statement +{ + struct statement super; + + const wchar_t* message; + + struct statement* substatement; +}; + diff --git a/string/new.c b/string/new.c index ed5afbc..7455973 100644 --- a/string/new.c +++ b/string/new.c @@ -106,6 +106,30 @@ struct string* new_string_from_ascii_format( return this; } +struct string* new_string_from_ascii_format_vargs( + const char* fmt, va_list arg) +{ + ENTER; + + char *out = NULL; + + int len = vasprintf(&out, fmt, arg); + + if (len < 0) + { + TODO; + } + + dpvs(out); + + struct string* this = new_string_from_ascii(out, (size_t) len); + + free(out); + + EXIT; + return this; +} + diff --git a/string/new.h b/string/new.h index d4c160e..659f48d 100644 --- a/string/new.h +++ b/string/new.h @@ -14,3 +14,6 @@ struct string* new_string_from_ascii( struct string* new_string_from_ascii_format( const char* fmt, ...); +struct string* new_string_from_ascii_format_vargs( + const char* fmt, va_list arg); + diff --git a/stringtree/append.c b/stringtree/append.c index effe3e0..df31b3e 100644 --- a/stringtree/append.c +++ b/stringtree/append.c @@ -48,20 +48,17 @@ void stringtree_append_formatstr( { ENTER; - TODO; - #if 0 va_list va; va_start(va, fmt); - struct string* string = new_string_from_vargs(fmt, va); + struct string* string = new_string_from_ascii_format_vargs(fmt, va); stringtree_append_string(this, string); free_string(string); va_end(va); - #endif EXIT; } diff --git a/parse/wcistream/file/free.c b/wcistream/file/free.c similarity index 100% rename from parse/wcistream/file/free.c rename to wcistream/file/free.c diff --git a/parse/wcistream/file/free.h b/wcistream/file/free.h similarity index 100% rename from parse/wcistream/file/free.h rename to wcistream/file/free.h diff --git a/parse/wcistream/file/inheritance.c b/wcistream/file/inheritance.c similarity index 100% rename from parse/wcistream/file/inheritance.c rename to wcistream/file/inheritance.c diff --git a/parse/wcistream/file/inheritance.h b/wcistream/file/inheritance.h similarity index 100% rename from parse/wcistream/file/inheritance.h rename to wcistream/file/inheritance.h diff --git a/parse/wcistream/file/new.c b/wcistream/file/new.c similarity index 100% rename from parse/wcistream/file/new.c rename to wcistream/file/new.c diff --git a/parse/wcistream/file/new.h b/wcistream/file/new.h similarity index 100% rename from parse/wcistream/file/new.h rename to wcistream/file/new.h diff --git a/parse/wcistream/file/read.c b/wcistream/file/read.c similarity index 100% rename from parse/wcistream/file/read.c rename to wcistream/file/read.c diff --git a/parse/wcistream/file/read.h b/wcistream/file/read.h similarity index 100% rename from parse/wcistream/file/read.h rename to wcistream/file/read.h diff --git a/parse/wcistream/file/struct.h b/wcistream/file/struct.h similarity index 100% rename from parse/wcistream/file/struct.h rename to wcistream/file/struct.h diff --git a/parse/wcistream/free.c b/wcistream/free.c similarity index 100% rename from parse/wcistream/free.c rename to wcistream/free.c diff --git a/parse/wcistream/free.h b/wcistream/free.h similarity index 100% rename from parse/wcistream/free.h rename to wcistream/free.h diff --git a/parse/wcistream/inc.c b/wcistream/inc.c similarity index 100% rename from parse/wcistream/inc.c rename to wcistream/inc.c diff --git a/parse/wcistream/inc.h b/wcistream/inc.h similarity index 100% rename from parse/wcistream/inc.h rename to wcistream/inc.h diff --git a/parse/wcistream/inheritance.h b/wcistream/inheritance.h similarity index 100% rename from parse/wcistream/inheritance.h rename to wcistream/inheritance.h diff --git a/parse/wcistream/istream/free.c b/wcistream/istream/free.c similarity index 89% rename from parse/wcistream/istream/free.c rename to wcistream/istream/free.c index 5007185..5182d93 100644 --- a/parse/wcistream/istream/free.c +++ b/wcistream/istream/free.c @@ -3,7 +3,7 @@ #include -#include +#include #include "struct.h" #include "free.h" diff --git a/parse/wcistream/istream/free.h b/wcistream/istream/free.h similarity index 100% rename from parse/wcistream/istream/free.h rename to wcistream/istream/free.h diff --git a/parse/wcistream/istream/inheritance.c b/wcistream/istream/inheritance.c similarity index 100% rename from parse/wcistream/istream/inheritance.c rename to wcistream/istream/inheritance.c diff --git a/parse/wcistream/istream/inheritance.h b/wcistream/istream/inheritance.h similarity index 100% rename from parse/wcistream/istream/inheritance.h rename to wcistream/istream/inheritance.h diff --git a/parse/wcistream/istream/new.c b/wcistream/istream/new.c similarity index 100% rename from parse/wcistream/istream/new.c rename to wcistream/istream/new.c diff --git a/parse/wcistream/istream/new.h b/wcistream/istream/new.h similarity index 100% rename from parse/wcistream/istream/new.h rename to wcistream/istream/new.h diff --git a/parse/wcistream/istream/read.c b/wcistream/istream/read.c similarity index 100% rename from parse/wcistream/istream/read.c rename to wcistream/istream/read.c diff --git a/parse/wcistream/istream/read.h b/wcistream/istream/read.h similarity index 100% rename from parse/wcistream/istream/read.h rename to wcistream/istream/read.h diff --git a/parse/wcistream/istream/struct.h b/wcistream/istream/struct.h similarity index 100% rename from parse/wcistream/istream/struct.h rename to wcistream/istream/struct.h diff --git a/parse/wcistream/new.c b/wcistream/new.c similarity index 100% rename from parse/wcistream/new.c rename to wcistream/new.c diff --git a/parse/wcistream/new.h b/wcistream/new.h similarity index 100% rename from parse/wcistream/new.h rename to wcistream/new.h diff --git a/parse/wcistream/read.c b/wcistream/read.c similarity index 100% rename from parse/wcistream/read.c rename to wcistream/read.c diff --git a/parse/wcistream/read.h b/wcistream/read.h similarity index 100% rename from parse/wcistream/read.h rename to wcistream/read.h diff --git a/parse/wcistream/string/free.c b/wcistream/string/free.c similarity index 100% rename from parse/wcistream/string/free.c rename to wcistream/string/free.c diff --git a/parse/wcistream/string/free.h b/wcistream/string/free.h similarity index 100% rename from parse/wcistream/string/free.h rename to wcistream/string/free.h diff --git a/parse/wcistream/string/inheritance.c b/wcistream/string/inheritance.c similarity index 100% rename from parse/wcistream/string/inheritance.c rename to wcistream/string/inheritance.c diff --git a/parse/wcistream/string/inheritance.h b/wcistream/string/inheritance.h similarity index 100% rename from parse/wcistream/string/inheritance.h rename to wcistream/string/inheritance.h diff --git a/parse/wcistream/string/new.c b/wcistream/string/new.c similarity index 100% rename from parse/wcistream/string/new.c rename to wcistream/string/new.c diff --git a/parse/wcistream/string/new.h b/wcistream/string/new.h similarity index 100% rename from parse/wcistream/string/new.h rename to wcistream/string/new.h diff --git a/parse/wcistream/string/read.c b/wcistream/string/read.c similarity index 100% rename from parse/wcistream/string/read.c rename to wcistream/string/read.c diff --git a/parse/wcistream/string/read.h b/wcistream/string/read.h similarity index 100% rename from parse/wcistream/string/read.h rename to wcistream/string/read.h diff --git a/parse/wcistream/string/struct.h b/wcistream/string/struct.h similarity index 100% rename from parse/wcistream/string/struct.h rename to wcistream/string/struct.h diff --git a/parse/wcistream/struct.h b/wcistream/struct.h similarity index 100% rename from parse/wcistream/struct.h rename to wcistream/struct.h diff --git a/zog.py b/zog.py index c544dbb..695cad7 100755 --- a/zog.py +++ b/zog.py @@ -1,39 +1,21 @@ -#!/usr/bin/env python3.10 +#!/usr/bin/env python3 import os; import sys; import io; +import argparse; -input_file = None; +parser = argparse.ArgumentParser(prog = 'zog'); -output_file = None; +parser.add_argument('-i', '--input', required = True); +parser.add_argument('-o', '--output', required = True); +parser.add_argument('-s', '--snippet-directory', default = "/tmp/"); -snippet_directory = "/tmp" +args = parser.parse_args(sys.argv[1:]); -i = 1; -n = len(sys.argv); - -while i < n: - arg = sys.argv[i]; - - match arg: - case "-i": - input_file = sys.argv[i + 1] - i += 2; - - case "-o": - output_file = sys.argv[i + 1] - i += 2; - - case "-s": - snippet_directory = sys.argv[i + 1] - i += 2; - - case _: - assert(not "TODO"); - -if (not input_file) or (not output_file): - assert(not "TODO"); +input_file = args.input; +output_file = args.output; +snippet_directory = args.snippet_directory; while input_file.startswith("./"): input_file = input_file[2:]; @@ -78,10 +60,10 @@ while i < n: outstring = captured_stdout.getvalue(); -# if outstring and outstring[-1] != "\n": -# outstring += "\n" + if outstring and outstring[-1] != "\n": + outstring += "\n" - temppath = f"{snippet_directory}/{input_file}-{start_index}.c"; + temppath = f"{snippet_directory}/{input_file}-{start_index+1}.c"; tempdir = temppath[:temppath.rindex('/')];