From 9d0bdc9db1cc5584bffca37169d2c59670d34ac4 Mon Sep 17 00:00:00 2001 From: Zander Thannhauser Date: Sun, 19 Jan 2025 19:07:51 -0600 Subject: [PATCH] removed color-factory for sake of terminalcolors --- color_factory/free.c | 44 ----------- color_factory/free.h | 7 -- color_factory/new.c | 108 -------------------------- color_factory/new.h | 3 - color_factory/struct.h | 16 ---- debug.h | 8 +- expression/application/prettyprint.c | 3 - expression/application/prettyprint.h | 1 - expression/inheritance.h | 1 - expression/lambda/prettyprint.c | 10 +-- expression/lambda/prettyprint.h | 1 - expression/literal/prettyprint.c | 3 +- expression/literal/prettyprint.h | 1 - expression/parenthesis/prettyprint.c | 16 ++-- expression/parenthesis/prettyprint.h | 1 - expression/prettyprint.c | 3 +- expression/prettyprint.h | 1 - expression/variable/prettyprint.c | 7 +- expression/variable/prettyprint.h | 1 - handle_file.c | 3 - handle_file.h | 1 - handle_init_file.c | 1 - handle_interactive.c | 8 +- handle_interactive.h | 3 +- handle_string.c | 3 - handle_string.h | 1 - main.c | 19 ++--- makefile | 12 ++- srclist.mk | 4 +- statement/assignment/execute.c | 2 - statement/assignment/execute.h | 1 - statement/assignment/prettyprint.c | 12 ++- statement/assignment/prettyprint.h | 1 - statement/execute.c | 3 +- statement/execute.h | 1 - statement/expression/execute.c | 2 - statement/expression/execute.h | 1 - statement/expression/prettyprint.c | 3 +- statement/expression/prettyprint.h | 1 - statement/inheritance.h | 2 - statement/prettyprint.c | 2 - statement/prettyprint.h | 1 - statement/print.c | 0 statement/print.h | 0 statement/substatements/execute.c | 5 +- statement/substatements/execute.h | 1 - statement/substatements/prettyprint.c | 15 ++-- statement/substatements/prettyprint.h | 1 - value/builtin_lambda/prettyprint.c | 25 +++--- value/builtin_lambda/prettyprint.h | 1 - value/inheritance.h | 1 - value/lambda/prettyprint.c | 12 +-- value/lambda/prettyprint.h | 1 - value/lazy/prettyprint.c | 2 - value/lazy/prettyprint.h | 1 - value/number/prettyprint.c | 7 +- value/number/prettyprint.h | 1 - value/prettyprint.c | 3 +- value/prettyprint.h | 1 - zog.py | 26 +++++-- 60 files changed, 93 insertions(+), 332 deletions(-) delete mode 100644 color_factory/free.c delete mode 100644 color_factory/free.h delete mode 100644 color_factory/new.c delete mode 100644 color_factory/new.h delete mode 100644 color_factory/struct.h delete mode 100644 statement/print.c delete mode 100644 statement/print.h diff --git a/color_factory/free.c b/color_factory/free.c deleted file mode 100644 index 248e495..0000000 --- a/color_factory/free.c +++ /dev/null @@ -1,44 +0,0 @@ - -#include - -#include - -#include - -#include - -#include "struct.h" -#include "free.h" - -void free_color_factory( - struct color_factory* this) -{ - ENTER; - - if (this) - { - for (unsigned i = 0; i < N(this->colors); i++) - { - free_string(this->colors[i]); - } - - free_string(this->numeric); - - free_string(this->string); - - free_string(this->variable); - - free_string(this->builtin); - - free_string(this->reset); - - free(this); - } - - EXIT; -} - - - - - diff --git a/color_factory/free.h b/color_factory/free.h deleted file mode 100644 index 7082ac0..0000000 --- a/color_factory/free.h +++ /dev/null @@ -1,7 +0,0 @@ - -struct color_factory; - -void free_color_factory( - struct color_factory* this); - - diff --git a/color_factory/new.c b/color_factory/new.c deleted file mode 100644 index 3af547d..0000000 --- a/color_factory/new.c +++ /dev/null @@ -1,108 +0,0 @@ - -#include - -#include - -#include - -#include - -#include - -#include - -#include "struct.h" -#include "new.h" - -struct rgb -{ - uint8_t r, g, b; -}; - -static struct rgb hsv_to_rgb(double hue, double sat, double val) -{ - double c = val * sat; - double x = c * (1 - fabs(fmod(hue / (M_PI / 3), 2) - 1)); - double m = val - c; - - double r, g, b; - - if (hue < 1 * M_PI / 3) r = c, g = x, b = 0; - else if (hue < 2 * M_PI / 3) r = x, g = c, b = 0; - else if (hue < 3 * M_PI / 3) r = 0, g = c, b = x; - else if (hue < 4 * M_PI / 3) r = 0, g = x, b = c; - else if (hue < 5 * M_PI / 3) r = x, g = 0, b = c; - else r = c, g = 0, b = x; - - return (struct rgb) { - (uint8_t) ((r + m) * 255), - (uint8_t) ((g + m) * 255), - (uint8_t) ((b + m) * 255)}; -} - - -static struct string* new_color_from_hue( - double hue, - double sat, - double val) -{ - ENTER; - - dpvlf(hue); - dpvlf(sat); - dpvlf(val); - - struct rgb rgb = hsv_to_rgb(hue, sat, val); - - struct string* string = new_string_from_ascii_format( - "\e[38;2;%hhu;%hhu;%hhum", rgb.r, rgb.g, rgb.b); - - EXIT; - return string; -} - -struct color_factory* new_color_factory(void) -{ - ENTER; - - struct color_factory* this = smalloc(sizeof(*this)); - - for (unsigned i = 0, n = N(this->colors); i < n; i++) - { - this->colors[i] = new_color_from_hue( - /* hue: */ (double) i / N(this->colors) * 2 * M_PI, - /* sat: */ 0.9, - /* val: */ 1.0); - } - - this->numeric = new_color_from_hue(31.0 / 180 * M_PI, 0.80, 1.0); - - this->string = new_color_from_hue(300.0 / 180 * M_PI, 0.80, 1.0); - - this->variable = new_color_from_hue(263.0 / 180 * M_PI, 0.80, 1.0); - - this->builtin = new_color_from_hue(0.0, 0.0, 0.52); - - this->reset = new_string(L"\e[0m", ULONG_MAX); - - EXIT; - return this; -} - - - - - - - - - - - - - - - - - - diff --git a/color_factory/new.h b/color_factory/new.h deleted file mode 100644 index 044433b..0000000 --- a/color_factory/new.h +++ /dev/null @@ -1,3 +0,0 @@ - -struct color_factory* new_color_factory(void); - diff --git a/color_factory/struct.h b/color_factory/struct.h deleted file mode 100644 index 461aa65..0000000 --- a/color_factory/struct.h +++ /dev/null @@ -1,16 +0,0 @@ - -struct color_factory -{ - struct string* colors[20]; - - struct string* numeric; - - struct string* string; - - struct string* variable; - - struct string* builtin; - - struct string* reset; -}; - diff --git a/debug.h b/debug.h index 7af367e..2f53df6 100644 --- a/debug.h +++ b/debug.h @@ -77,7 +77,11 @@ extern int debug_depth; #define TODO \ { \ - printf("TODO hit at %s:%i!\n", __FILE__, __LINE__); \ + char filebuffer[PATH_MAX]; \ + strcpy(filebuffer, __FILE__ + 4); \ + filebuffer[strlen(filebuffer) - 2] = 0; \ + \ + printf("TODO hit at %s:%i!\n", filebuffer, __LINE__); \ \ pid_t __child = fork(); \ \ @@ -98,7 +102,7 @@ extern int debug_depth; {\ char __buffer[100]; \ snprintf(__buffer, 100, "+%u", __LINE__); \ - execlp("gedit", "gedit", __FILE__, __buffer, NULL); \ + execlp("gedit", "gedit", filebuffer, __buffer, NULL); \ }\ \ exit(1); \ diff --git a/expression/application/prettyprint.c b/expression/application/prettyprint.c index 694dd39..a910edf 100644 --- a/expression/application/prettyprint.c +++ b/expression/application/prettyprint.c @@ -18,7 +18,6 @@ struct stringtree* application_expression_prettyprint( int *out_chosen_color, struct expression* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -30,13 +29,11 @@ struct stringtree* application_expression_prettyprint( struct stringtree* left = expression_prettyprint( &left_color, this->left, - cfactory, with_color); struct stringtree* right = expression_prettyprint( &right_color, this->right, - cfactory, with_color); if (with_color && out_chosen_color) diff --git a/expression/application/prettyprint.h b/expression/application/prettyprint.h index 399f4d1..75b163e 100644 --- a/expression/application/prettyprint.h +++ b/expression/application/prettyprint.h @@ -8,7 +8,6 @@ struct color_factory; struct stringtree* application_expression_prettyprint( int *out_chosen_color, struct expression* this, - struct color_factory* cfactory, bool with_color); diff --git a/expression/inheritance.h b/expression/inheritance.h index 7601c9b..921c88a 100644 --- a/expression/inheritance.h +++ b/expression/inheritance.h @@ -11,7 +11,6 @@ struct expression_inheritance struct stringtree* (*prettyprint)( int *out_chosen_color, struct expression* this, - struct color_factory* cfactory, bool with_color); struct stringtree* (*prettyprint_errors)( diff --git a/expression/lambda/prettyprint.c b/expression/lambda/prettyprint.c index e4ea54c..30a20c3 100644 --- a/expression/lambda/prettyprint.c +++ b/expression/lambda/prettyprint.c @@ -3,7 +3,7 @@ #include -#include +#include #include #include @@ -18,7 +18,6 @@ struct stringtree* lambda_expression_prettyprint( int *out_chosen_color, struct expression* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -38,16 +37,15 @@ struct stringtree* lambda_expression_prettyprint( struct stringtree* subtree = expression_prettyprint( &chosen_color, this->body, - cfactory, with_color); if (with_color) { - int my_color = (chosen_color + 1) % (int) N(cfactory->colors); + int my_color = (chosen_color + 1) % (int) N(terminalcolors.precedences); - stringtree_prepend_string(tree, cfactory->colors[my_color]); + stringtree_prepend_cstr(tree, terminalcolors.precedences[my_color]); - stringtree_append_string(tree, cfactory->reset); + stringtree_append_cstr(tree, terminalcolors.reset); if (out_chosen_color) *out_chosen_color = my_color; diff --git a/expression/lambda/prettyprint.h b/expression/lambda/prettyprint.h index 4c12a73..f253127 100644 --- a/expression/lambda/prettyprint.h +++ b/expression/lambda/prettyprint.h @@ -8,7 +8,6 @@ struct color_factory; struct stringtree* lambda_expression_prettyprint( int *out_chosen_color, struct expression* this, - struct color_factory* cfactory, bool with_color); diff --git a/expression/literal/prettyprint.c b/expression/literal/prettyprint.c index f73dfb3..9c1176b 100644 --- a/expression/literal/prettyprint.c +++ b/expression/literal/prettyprint.c @@ -9,7 +9,6 @@ struct stringtree* literal_expression_prettyprint( int *out_chosen_color, struct expression* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -18,7 +17,7 @@ struct stringtree* literal_expression_prettyprint( struct stringtree* tree = value_prettyprint( out_chosen_color, - this->value, cfactory, with_color); + this->value, with_color); EXIT; return tree; diff --git a/expression/literal/prettyprint.h b/expression/literal/prettyprint.h index 46272de..66508f8 100644 --- a/expression/literal/prettyprint.h +++ b/expression/literal/prettyprint.h @@ -6,7 +6,6 @@ struct color_factory; struct stringtree* literal_expression_prettyprint( int *out_chosen_color, struct expression* this, - struct color_factory* cfactory, bool with_color); diff --git a/expression/parenthesis/prettyprint.c b/expression/parenthesis/prettyprint.c index e0c7eed..35158dd 100644 --- a/expression/parenthesis/prettyprint.c +++ b/expression/parenthesis/prettyprint.c @@ -3,13 +3,11 @@ #include -#include - #include #include #include -/*#include */ +#include #include "../prettyprint.h" @@ -19,7 +17,6 @@ struct stringtree* parenthesis_expression_prettyprint( int *out_chosen_color, struct expression* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -31,20 +28,19 @@ struct stringtree* parenthesis_expression_prettyprint( struct stringtree* tree = expression_prettyprint( &inner_color, this->subexpression, - cfactory, with_color); if (with_color) { - int my_color = (inner_color + 1) % (int) N(cfactory->colors); + int my_color = (inner_color + 1) % (int) N(terminalcolors.precedences); - stringtree_prepend_string(tree, cfactory->reset); + stringtree_prepend_cstr(tree, terminalcolors.reset); stringtree_prepend_cstr(tree, L"("); - stringtree_prepend_string(tree, cfactory->colors[my_color]); + stringtree_prepend_cstr(tree, terminalcolors.precedences[my_color]); - stringtree_append_string(tree, cfactory->colors[my_color]); + stringtree_append_cstr(tree, terminalcolors.precedences[my_color]); stringtree_append_cstr(tree, L")"); - stringtree_append_string(tree, cfactory->reset); + stringtree_append_cstr(tree, terminalcolors.reset); if (out_chosen_color) *out_chosen_color = my_color; diff --git a/expression/parenthesis/prettyprint.h b/expression/parenthesis/prettyprint.h index 21381e0..865a39a 100644 --- a/expression/parenthesis/prettyprint.h +++ b/expression/parenthesis/prettyprint.h @@ -8,7 +8,6 @@ struct color_factory; struct stringtree* parenthesis_expression_prettyprint( int *out_chosen_color, struct expression* this, - struct color_factory* cfactory, bool with_color); diff --git a/expression/prettyprint.c b/expression/prettyprint.c index a716e2b..6982ec6 100644 --- a/expression/prettyprint.c +++ b/expression/prettyprint.c @@ -10,7 +10,6 @@ struct stringtree* expression_prettyprint( int *out_chosen_color, struct expression* this, - struct color_factory* cfactory, unsigned precedence_level) { ENTER; @@ -21,7 +20,7 @@ struct stringtree* expression_prettyprint( struct stringtree* tree = (this->inheritance->prettyprint)( out_chosen_color, - this, cfactory, precedence_level); + this, precedence_level); EXIT; return tree; diff --git a/expression/prettyprint.h b/expression/prettyprint.h index 5af151e..fca6c96 100644 --- a/expression/prettyprint.h +++ b/expression/prettyprint.h @@ -6,7 +6,6 @@ struct color_factory; struct stringtree* expression_prettyprint( int *out_chosen_color, struct expression* this, - struct color_factory* cfactory, unsigned precedence_level); diff --git a/expression/variable/prettyprint.c b/expression/variable/prettyprint.c index 1f24609..b278eec 100644 --- a/expression/variable/prettyprint.c +++ b/expression/variable/prettyprint.c @@ -1,7 +1,7 @@ #include -#include +#include #include #include @@ -15,7 +15,6 @@ struct stringtree* variable_expression_prettyprint( int *out_chosen_color, struct expression* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -28,9 +27,9 @@ struct stringtree* variable_expression_prettyprint( if (with_color) { - stringtree_prepend_string(tree, cfactory->variable); + stringtree_prepend_cstr(tree, terminalcolors.variable); - stringtree_append_string(tree, cfactory->reset); + stringtree_append_cstr(tree, terminalcolors.reset); if (out_chosen_color) *out_chosen_color = -1; diff --git a/expression/variable/prettyprint.h b/expression/variable/prettyprint.h index 1bfc8eb..ca4b78e 100644 --- a/expression/variable/prettyprint.h +++ b/expression/variable/prettyprint.h @@ -6,7 +6,6 @@ struct color_factory; struct stringtree* variable_expression_prettyprint( int *out_chosen_color, struct expression* this, - struct color_factory* cfactory, bool with_color); diff --git a/handle_file.c b/handle_file.c index 59fc371..840fdba 100644 --- a/handle_file.c +++ b/handle_file.c @@ -58,7 +58,6 @@ void handle_file( struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, const char* input_file, bool echo, bool print_with_colors) @@ -133,7 +132,6 @@ void handle_file( struct stringtree* stree = statement_prettyprint( /* chosen color reference: */ NULL, /* instance: */ statement, - /* color factory: */ cfactory, /* print with color? */ print_with_colors); stringtree_println(stree, wc_stdout); @@ -145,7 +143,6 @@ void handle_file( /* instance: */ statement, /* environment: */ environment, /* booleans: */ booleans, - /* color factory: */ cfactory, /* wide-character stdout: */ wc_stdout, /* print value: */ true, /* print with color?: */ print_with_colors); diff --git a/handle_file.h b/handle_file.h index 5d92d56..81d4ef3 100644 --- a/handle_file.h +++ b/handle_file.h @@ -2,7 +2,6 @@ void handle_file( struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, const char* input_file, bool echo, bool isatty); diff --git a/handle_init_file.c b/handle_init_file.c index 0268680..fea3b01 100644 --- a/handle_init_file.c +++ b/handle_init_file.c @@ -102,7 +102,6 @@ void handle_init_file( /* instance: */ statement, /* environment: */ environment, /* booleans: */ booleans, - /* color factory: */ NULL, /* wide-character stdout: */ NULL, /* print value: */ false, /* print with color?: */ false); diff --git a/handle_interactive.c b/handle_interactive.c index 15e4e7d..de9a023 100644 --- a/handle_interactive.c +++ b/handle_interactive.c @@ -1,4 +1,9 @@ +#include +#include +#include +#include +#include #include #include #include @@ -60,8 +65,7 @@ static const enum state { void handle_interactive( struct environment** environment, - struct booleans* booleans, - struct color_factory* cfactory) + struct booleans* booleans) { ENTER; diff --git a/handle_interactive.h b/handle_interactive.h index df1604e..a7dbf65 100644 --- a/handle_interactive.h +++ b/handle_interactive.h @@ -5,6 +5,5 @@ struct color_factory; void handle_interactive( struct environment** environment, - struct booleans* booleans, - struct color_factory* cfactory); + struct booleans* booleans); diff --git a/handle_string.c b/handle_string.c index ccbe90f..b3e8219 100644 --- a/handle_string.c +++ b/handle_string.c @@ -58,7 +58,6 @@ void handle_string( struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, const char* input_string, bool echo, bool print_with_colors) @@ -126,7 +125,6 @@ void handle_string( struct stringtree* stree = statement_prettyprint( /* chosen color reference: */ NULL, /* instance: */ statement, - /* color factory: */ cfactory, /* print with color? */ print_with_colors); stringtree_println(stree, wc_stdout); @@ -138,7 +136,6 @@ void handle_string( /* instance: */ statement, /* environment: */ environment, /* booleans: */ booleans, - /* color factory: */ cfactory, /* wide-character stdout: */ wc_stdout, /* print value: */ true, /* print with color?: */ print_with_colors); diff --git a/handle_string.h b/handle_string.h index 94c9f6d..8879d83 100644 --- a/handle_string.h +++ b/handle_string.h @@ -6,7 +6,6 @@ struct booleans; void handle_string( struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, const char* input_file, bool echo, bool print_with_colors); diff --git a/main.c b/main.c index 0029ed8..8bc41b7 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,6 @@ +#include +#include #include #include @@ -12,8 +14,8 @@ #include #include -#include -#include +/*#include */ +/*#include */ #include #include @@ -25,10 +27,6 @@ #include #include -/*/ python -print("static int x = 3;"); -/*/ - int main(int argc, char* const* argv) { ENTER; @@ -37,8 +35,6 @@ int main(int argc, char* const* argv) struct booleans* booleans = new_booleans(); - struct color_factory* cfactory = new_color_factory(); - struct environment* environment = new_environment( /* fallback: */ NULL); @@ -58,7 +54,6 @@ int main(int argc, char* const* argv) handle_string( /* environment, may by modified/replaced: */ &environment, /* booleans: */ booleans, - /* colors: */ cfactory, /* input string: */ flags->input_string, /* echo? */ flags->echo, /* colors?: */ flags->print_with_colors); @@ -71,7 +66,6 @@ int main(int argc, char* const* argv) handle_file( /* environment, may by modified/replaced: */ &environment, /* booleans: */ booleans, - /* colors: */ cfactory, /* input file: */ flags->input_file, /* echo? */ flags->echo, /* colors?: */ flags->print_with_colors); @@ -89,8 +83,7 @@ int main(int argc, char* const* argv) handle_interactive( /* environment, may by modified/replaced: */ &environment, - /* booleans: */ booleans, - /* colors: */ cfactory); + /* booleans: */ booleans); break; } @@ -104,8 +97,6 @@ int main(int argc, char* const* argv) free_environment(environment); - free_color_factory(cfactory); - free_booleans(booleans); free_cmdln_flags(flags); diff --git a/makefile b/makefile index 42934c2..85dc5a5 100644 --- a/makefile +++ b/makefile @@ -13,7 +13,7 @@ on_error ?= do_nothing ifeq ($(on_error), do_nothing) on_error_command = else ifeq ($(on_error), open_editor) -on_error_command += || ($$EDITOR $<; false) +on_error_command += || ($$EDITOR ${*}.c; false) else $(error "invalid on_error option!"); endif @@ -24,7 +24,7 @@ endif @mkdir -p $@ srclist.mk: - find -name '*.c' -! -path '*/bin/*'-! -path '*/junk/*' | sed "s/^/srcs += /" | sort -V > ${@} + find -name '*.c' -! -path '*/bin/*' -! -path '*/junk/*' | sed "s/^/srcs += /" | sort -V > ${@} include srclist.mk @@ -38,9 +38,7 @@ bin/%.c.c: %.c zog.py | bin/%/ ${prefix}/%.o ${prefix}/%.d: bin/%.c.c ${optionset} | ${prefix}/%/ @echo "compiling (${buildtype}) ${*}.c ..." - @gcc -c @${optionset} -I $(*D) $< -MD -MF ${prefix}/${*}.d -o ${prefix}/${*}.o - -# $(on_error_command) + @gcc -c @${optionset} -I $(*D) $< -MD -MF ${prefix}/${*}.d -o ${prefix}/${*}.o $(on_error_command) objs = $(patsubst %.c,${prefix}/%.o,${srcs}) @@ -55,9 +53,9 @@ args += --color args += --custom-init-path ./examples/init.txt -args += -c '1' +#args += -c '1' -#args += ./examples/sandbox.txt +args += ./examples/sandbox.txt run: ${prefix}/13 $< ${args} diff --git a/srclist.mk b/srclist.mk index 40d3e5a..00e8fa3 100644 --- a/srclist.mk +++ b/srclist.mk @@ -12,8 +12,6 @@ srcs += ./builtin/numeric/multiply.c srcs += ./builtin/numeric/subtract.c srcs += ./cmdln/free.c srcs += ./cmdln/new.c -srcs += ./color_factory/free.c -srcs += ./color_factory/new.c srcs += ./debug.c srcs += ./environment/declare.c srcs += ./environment/declare_builtins.c @@ -64,6 +62,7 @@ srcs += ./expression/variable/prettyprint_errors.c srcs += ./extern/avl/avl.c srcs += ./extern/gmp/mini-gmp.c srcs += ./extern/gmp/mini-mpq.c +srcs += ./globals/terminalcolors.c srcs += ./handle_file.c srcs += ./handle_init_file.c srcs += ./handle_interactive.c @@ -151,7 +150,6 @@ srcs += ./statement/inc.c srcs += ./statement/new.c srcs += ./statement/prettyprint.c srcs += ./statement/prettyprint_errors.c -srcs += ./statement/print.c srcs += ./statement/substatements/execute.c srcs += ./statement/substatements/free.c srcs += ./statement/substatements/inheritance.c diff --git a/statement/assignment/execute.c b/statement/assignment/execute.c index c81900e..7a4d497 100644 --- a/statement/assignment/execute.c +++ b/statement/assignment/execute.c @@ -24,7 +24,6 @@ void assignment_statement_execute( struct statement* super, struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, struct wcostream* ostream, bool print_value, bool print_with_color) @@ -53,7 +52,6 @@ void assignment_statement_execute( struct stringtree* tree = value_prettyprint( /* outgoing chosen color: */ NULL, /* value: */ vvalue, - /* color factory: */ cfactory, /* print with color? */ print_with_color); stringtree_println(tree, ostream); diff --git a/statement/assignment/execute.h b/statement/assignment/execute.h index 0174d44..ca3e2e7 100644 --- a/statement/assignment/execute.h +++ b/statement/assignment/execute.h @@ -7,7 +7,6 @@ void assignment_statement_execute( struct statement* this, struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, struct wcostream* ostream, bool print_value, bool print_with_color); diff --git a/statement/assignment/prettyprint.c b/statement/assignment/prettyprint.c index 942cbd0..c329ea2 100644 --- a/statement/assignment/prettyprint.c +++ b/statement/assignment/prettyprint.c @@ -3,13 +3,13 @@ #include -#include - #include #include #include #include +#include + #include #include "struct.h" @@ -18,7 +18,6 @@ struct stringtree* assignment_statement_prettyprint( int *out_chosen_color, struct statement* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -36,16 +35,15 @@ struct stringtree* assignment_statement_prettyprint( struct stringtree* tree = expression_prettyprint( &inner_color, this->expression, - cfactory, with_color); if (with_color) { - int my_color = (inner_color + 1) % (int) N(cfactory->colors); + int my_color = (inner_color + 1) % (int) N(terminalcolors.precedences); - stringtree_prepend_string(header, cfactory->colors[my_color]); + stringtree_prepend_cstr(header, terminalcolors.precedences[my_color]); - stringtree_append_string(header, cfactory->reset); + stringtree_append_cstr(header, terminalcolors.reset); if (out_chosen_color) *out_chosen_color = my_color; diff --git a/statement/assignment/prettyprint.h b/statement/assignment/prettyprint.h index 40fa604..7b2cdb6 100644 --- a/statement/assignment/prettyprint.h +++ b/statement/assignment/prettyprint.h @@ -6,6 +6,5 @@ struct color_factory; struct stringtree* assignment_statement_prettyprint( int *out_chosen_color, struct statement* this, - struct color_factory* cfactory, bool with_color); diff --git a/statement/execute.c b/statement/execute.c index 6a5cfb4..79eb97a 100644 --- a/statement/execute.c +++ b/statement/execute.c @@ -11,7 +11,6 @@ void statement_execute( struct statement* this, struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, struct wcostream* ostream, bool print_value, bool print_with_color) @@ -23,7 +22,7 @@ void statement_execute( assert(this->inheritance->execute); (this->inheritance->execute)( - this, environment, booleans, cfactory, ostream, print_value, print_with_color); + this, environment, booleans, ostream, print_value, print_with_color); EXIT; } diff --git a/statement/execute.h b/statement/execute.h index f981f3f..1a523e1 100644 --- a/statement/execute.h +++ b/statement/execute.h @@ -11,7 +11,6 @@ void statement_execute( struct statement* this, struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, struct wcostream* ostream, bool print_value, bool print_with_color); diff --git a/statement/expression/execute.c b/statement/expression/execute.c index aee81b3..b4d1c22 100644 --- a/statement/expression/execute.c +++ b/statement/expression/execute.c @@ -20,7 +20,6 @@ void expression_statement_execute( struct statement* super, struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, struct wcostream* ostream, bool print_value, bool print_with_color) @@ -41,7 +40,6 @@ void expression_statement_execute( struct stringtree* tree = value_prettyprint( /* outgoing chosen color: */ NULL, /* value: */ vvalue, - /* color factory: */ cfactory, /* print with color? */ print_with_color); stringtree_println(tree, ostream); diff --git a/statement/expression/execute.h b/statement/expression/execute.h index 77f998c..4cd8f12 100644 --- a/statement/expression/execute.h +++ b/statement/expression/execute.h @@ -7,7 +7,6 @@ void expression_statement_execute( struct statement* this, struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, struct wcostream* ostream, bool print_value, bool print_with_color); diff --git a/statement/expression/prettyprint.c b/statement/expression/prettyprint.c index af74d2e..1da5442 100644 --- a/statement/expression/prettyprint.c +++ b/statement/expression/prettyprint.c @@ -9,7 +9,6 @@ struct stringtree* expression_statement_prettyprint( int *out_chosen_color, struct statement* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -18,7 +17,7 @@ struct stringtree* expression_statement_prettyprint( struct stringtree* tree = expression_prettyprint( out_chosen_color, - this->expression, cfactory, with_color); + this->expression, with_color); EXIT; return tree; diff --git a/statement/expression/prettyprint.h b/statement/expression/prettyprint.h index 8fe5712..0223d30 100644 --- a/statement/expression/prettyprint.h +++ b/statement/expression/prettyprint.h @@ -6,6 +6,5 @@ struct color_factory; struct stringtree* expression_statement_prettyprint( int *out_chosen_color, struct statement* this, - struct color_factory* cfactory, bool with_color); diff --git a/statement/inheritance.h b/statement/inheritance.h index c3b1d78..1d3b7da 100644 --- a/statement/inheritance.h +++ b/statement/inheritance.h @@ -12,7 +12,6 @@ struct statement_inheritance struct stringtree* (*prettyprint)( int *out_chosen_color, struct statement* this, - struct color_factory* cfactory, bool with_color); struct stringtree* (*prettyprint_errors)( @@ -22,7 +21,6 @@ struct statement_inheritance struct statement*, struct environment**, struct booleans* booleans, - struct color_factory*, struct wcostream*, bool print_value, bool print_with_color); diff --git a/statement/prettyprint.c b/statement/prettyprint.c index 110e7e2..f8e06bf 100644 --- a/statement/prettyprint.c +++ b/statement/prettyprint.c @@ -10,7 +10,6 @@ struct stringtree* statement_prettyprint( int *out_chosen_color, struct statement* this, - struct color_factory* cfactory, unsigned precedence_level) { ENTER; @@ -22,7 +21,6 @@ struct stringtree* statement_prettyprint( struct stringtree* tree = (this->inheritance->prettyprint)( /* out: */ out_chosen_color, /* this: */ this, - /* color factory: */ cfactory, /* precedence level: */ precedence_level); EXIT; diff --git a/statement/prettyprint.h b/statement/prettyprint.h index 8198fde..b3686f5 100644 --- a/statement/prettyprint.h +++ b/statement/prettyprint.h @@ -6,6 +6,5 @@ struct color_factory; struct stringtree* statement_prettyprint( int *out_chosen_color, struct statement* this, - struct color_factory* cfactory, unsigned precedence_level); diff --git a/statement/print.c b/statement/print.c deleted file mode 100644 index e69de29..0000000 diff --git a/statement/print.h b/statement/print.h deleted file mode 100644 index e69de29..0000000 diff --git a/statement/substatements/execute.c b/statement/substatements/execute.c index 5f0b0e5..6b3df37 100644 --- a/statement/substatements/execute.c +++ b/statement/substatements/execute.c @@ -10,7 +10,6 @@ void substatements_statement_execute( struct statement* super, struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, struct wcostream* ostream, bool print_value, bool print_with_color) @@ -19,9 +18,9 @@ void substatements_statement_execute( struct substatements_statement* this = (void*) super; - statement_execute(this->left, environment, booleans, cfactory, ostream, print_value, print_with_color); + statement_execute(this->left, environment, booleans, ostream, print_value, print_with_color); - statement_execute(this->right, environment, booleans, cfactory, ostream, print_value, print_with_color); + statement_execute(this->right, environment, booleans, ostream, print_value, print_with_color); EXIT; } diff --git a/statement/substatements/execute.h b/statement/substatements/execute.h index 5f5d167..71009fa 100644 --- a/statement/substatements/execute.h +++ b/statement/substatements/execute.h @@ -5,7 +5,6 @@ void substatements_statement_execute( struct statement* this, struct environment** environment, struct booleans* booleans, - struct color_factory* cfactory, struct wcostream* ostream, bool print_value, bool print_with_color); diff --git a/statement/substatements/prettyprint.c b/statement/substatements/prettyprint.c index d267da1..0ceb699 100644 --- a/statement/substatements/prettyprint.c +++ b/statement/substatements/prettyprint.c @@ -5,14 +5,13 @@ #include -#include - -/*#include */ #include #include #include #include +#include + #include "../prettyprint.h" #include "struct.h" @@ -21,7 +20,6 @@ struct stringtree* substatements_statement_prettyprint( int *out_chosen_color, struct statement* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -33,13 +31,11 @@ struct stringtree* substatements_statement_prettyprint( struct stringtree* left = statement_prettyprint( &left_color, /* instance: */ this->left, - /* color factory: */ cfactory, with_color); struct stringtree* right = statement_prettyprint( &right_color, /* instance: */ this->right, - /* color factory: */ cfactory, with_color); struct stringtree* semicolon = new_stringtree(); @@ -48,13 +44,14 @@ struct stringtree* substatements_statement_prettyprint( if (with_color) { - int my_color = (MAX(left_color, right_color) + 1) % (int) N(cfactory->colors); + int my_color = + (MAX(left_color, right_color) + 1) % (int) N(terminalcolors.precedences); dpvu(my_color); - stringtree_prepend_string(semicolon, cfactory->colors[my_color]); + stringtree_prepend_cstr(semicolon, terminalcolors.precedences[my_color]); - stringtree_prepend_string(semicolon, cfactory->reset); + stringtree_prepend_cstr(semicolon, terminalcolors.reset); if (out_chosen_color) *out_chosen_color = my_color; diff --git a/statement/substatements/prettyprint.h b/statement/substatements/prettyprint.h index 088623f..8b17936 100644 --- a/statement/substatements/prettyprint.h +++ b/statement/substatements/prettyprint.h @@ -6,7 +6,6 @@ struct color_factory; struct stringtree* substatements_statement_prettyprint( int *out_chosen_color, struct statement* this, - struct color_factory* cfactory, bool with_color); diff --git a/value/builtin_lambda/prettyprint.c b/value/builtin_lambda/prettyprint.c index 8b0ebc7..59ce923 100644 --- a/value/builtin_lambda/prettyprint.c +++ b/value/builtin_lambda/prettyprint.c @@ -5,13 +5,13 @@ #include -#include - #include #include #include #include +#include + #include #include "struct.h" @@ -21,7 +21,6 @@ struct stringtree* builtin_lambda_value_prettyprint( int *out_chosen_color, struct value* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -41,8 +40,8 @@ struct stringtree* builtin_lambda_value_prettyprint( struct stringtree* left = helper(&left_color, this->prev, false); - struct stringtree* right = value_prettyprint(&right_color, this->value, - cfactory, with_color); + struct stringtree* right = value_prettyprint(&right_color, + this->value, with_color); stringtree_append_stringtree(tree, left); stringtree_append_cstr(tree, L" "); @@ -52,15 +51,17 @@ struct stringtree* builtin_lambda_value_prettyprint( { if (with_color) { - int my_color = (MAX(left_color, right_color) + 1) % (int) N(cfactory->colors); + int my_color = + (MAX(left_color, right_color) + 1) + % (int) N(terminalcolors.precedences); - stringtree_prepend_string(tree, cfactory->reset); + stringtree_prepend_cstr(tree, terminalcolors.reset); stringtree_prepend_cstr(tree, L"("); - stringtree_prepend_string(tree, cfactory->colors[my_color]); + stringtree_prepend_cstr(tree, terminalcolors.precedences[my_color]); - stringtree_append_string(tree, cfactory->colors[my_color]); + stringtree_append_cstr(tree, terminalcolors.precedences[my_color]); stringtree_append_cstr(tree, L")"); - stringtree_append_string(tree, cfactory->reset); + stringtree_append_cstr(tree, terminalcolors.reset); if (out) { @@ -91,9 +92,9 @@ struct stringtree* builtin_lambda_value_prettyprint( if (with_color) { - stringtree_prepend_string(tree, cfactory->variable); + stringtree_prepend_cstr(tree, terminalcolors.variable); - stringtree_append_string(tree, cfactory->reset); + stringtree_append_cstr(tree, terminalcolors.reset); if (out) *out = -1; diff --git a/value/builtin_lambda/prettyprint.h b/value/builtin_lambda/prettyprint.h index 85a946d..542f11d 100644 --- a/value/builtin_lambda/prettyprint.h +++ b/value/builtin_lambda/prettyprint.h @@ -2,6 +2,5 @@ struct stringtree* builtin_lambda_value_prettyprint( int *out_chosen_color, struct value* super, - struct color_factory* cfactory, bool with_color); diff --git a/value/inheritance.h b/value/inheritance.h index 6916097..53a4c69 100644 --- a/value/inheritance.h +++ b/value/inheritance.h @@ -13,7 +13,6 @@ struct value_inheritance struct stringtree* (*prettyprint)( int *out_chosen_color, struct value* this, - struct color_factory* cfactory, bool with_color); void (*free)( diff --git a/value/lambda/prettyprint.c b/value/lambda/prettyprint.c index 18ac4c7..25bbbc4 100644 --- a/value/lambda/prettyprint.c +++ b/value/lambda/prettyprint.c @@ -1,8 +1,6 @@ #include -#include - #include #include @@ -10,7 +8,7 @@ #include #include -/*#include */ +#include #include @@ -21,7 +19,6 @@ struct stringtree* lambda_value_prettyprint( int *out_chosen_color, struct value* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -41,16 +38,15 @@ struct stringtree* lambda_value_prettyprint( struct stringtree* subtree = expression_prettyprint( &chosen_color, this->body, - cfactory, with_color); if (with_color) { - int my_color = (chosen_color + 1) % (int) N(cfactory->colors); + int my_color = (chosen_color + 1) % (int) N(terminalcolors.precedences); - stringtree_prepend_string(tree, cfactory->colors[my_color]); + stringtree_prepend_cstr(tree, terminalcolors.precedences[my_color]); - stringtree_append_string(tree, cfactory->reset); + stringtree_append_cstr(tree, terminalcolors.reset); if (out_chosen_color) *out_chosen_color = my_color; diff --git a/value/lambda/prettyprint.h b/value/lambda/prettyprint.h index 89553fc..90b7ff5 100644 --- a/value/lambda/prettyprint.h +++ b/value/lambda/prettyprint.h @@ -2,6 +2,5 @@ struct stringtree* lambda_value_prettyprint( int *out_chosen_color, struct value* super, - struct color_factory* cfactory, bool with_color); diff --git a/value/lazy/prettyprint.c b/value/lazy/prettyprint.c index a92adc7..22af4c7 100644 --- a/value/lazy/prettyprint.c +++ b/value/lazy/prettyprint.c @@ -16,7 +16,6 @@ struct stringtree* lazy_value_prettyprint( int *out_chosen_color, struct value* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -26,7 +25,6 @@ struct stringtree* lazy_value_prettyprint( struct stringtree* tree = expression_prettyprint( out_chosen_color, this->expression, - cfactory, with_color); /* stringtree_prepend_cstr(tree, L"lazy(");*/ diff --git a/value/lazy/prettyprint.h b/value/lazy/prettyprint.h index 379b20f..920cbd6 100644 --- a/value/lazy/prettyprint.h +++ b/value/lazy/prettyprint.h @@ -2,6 +2,5 @@ struct stringtree* lazy_value_prettyprint( int *out_chosen_color, struct value* super, - struct color_factory* cfactory, bool with_color); diff --git a/value/number/prettyprint.c b/value/number/prettyprint.c index 717b96d..a6cffb3 100644 --- a/value/number/prettyprint.c +++ b/value/number/prettyprint.c @@ -1,7 +1,7 @@ #include -#include +#include #include #include @@ -16,7 +16,6 @@ struct stringtree* number_value_prettyprint( int *out_chosen_color, struct value* super, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -27,9 +26,9 @@ struct stringtree* number_value_prettyprint( if (with_color) { - stringtree_prepend_string(tree, cfactory->numeric); + stringtree_prepend_cstr(tree, terminalcolors.numeric); - stringtree_append_string(tree, cfactory->reset); + stringtree_append_cstr(tree, terminalcolors.reset); if (out_chosen_color) *out_chosen_color = -1; diff --git a/value/number/prettyprint.h b/value/number/prettyprint.h index cc4dea7..4c6b500 100644 --- a/value/number/prettyprint.h +++ b/value/number/prettyprint.h @@ -2,6 +2,5 @@ struct stringtree* number_value_prettyprint( int *out_chosen_color, struct value* super, - struct color_factory* cfactory, bool with_color); diff --git a/value/prettyprint.c b/value/prettyprint.c index dda45d2..9b3de78 100644 --- a/value/prettyprint.c +++ b/value/prettyprint.c @@ -10,7 +10,6 @@ struct stringtree* value_prettyprint( int *out_chosen_color, struct value* this, - struct color_factory* cfactory, bool with_color) { ENTER; @@ -20,7 +19,7 @@ struct stringtree* value_prettyprint( assert(this->inheritance->prettyprint); struct stringtree* tree = (this->inheritance->prettyprint)( - out_chosen_color, this, cfactory, with_color); + out_chosen_color, this, with_color); EXIT; return tree; diff --git a/value/prettyprint.h b/value/prettyprint.h index ff23074..7fa2b50 100644 --- a/value/prettyprint.h +++ b/value/prettyprint.h @@ -8,6 +8,5 @@ struct color_factory; struct stringtree* value_prettyprint( int *out_chosen_color, struct value* this, - struct color_factory* cfactory, bool with_color); diff --git a/zog.py b/zog.py index c894a7d..c544dbb 100755 --- a/zog.py +++ b/zog.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3.10 +import os; import sys; import io; @@ -55,34 +56,43 @@ n = len(lines); while i < n: if "/*/" in lines[i]: - i += 1; + indentation = lines[i].index("/*/"); + start_index = i; + i += 1; + + source = "" + while "/*/" not in lines[i]: + source += lines[i][indentation:]; i += 1; end_index = i; i += 1; - source = "".join(lines[start_index:end_index]); - real_stdout = sys.stdout sys.stdout = captured_stdout = io.StringIO() - eval(compile(source, "", "exec"), myglobals); + eval(compile(source, "", "exec"), myglobals); sys.stdout = real_stdout 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"; + tempdir = temppath[:temppath.rindex('/')]; + + os.makedirs(tempdir, exist_ok = True); + with open(temppath, "w") as stream: - stream.write(outstring); + for outline in outstring.split("\n"): + stream.write((" " * indentation) + outline + '\n'); ostream.write(f'#include "{temppath}"' + "\n"); - ostream.write("\n" * (end_index - start_index + 1)); + ostream.write("\n" * (end_index - start_index)); else: ostream.write(lines[i]); i += 1;