removed color-factory for sake of terminalcolors

This commit is contained in:
Zander Thannhauser 2025-01-19 19:07:51 -06:00
parent d89f2d7b06
commit 9d0bdc9db1
60 changed files with 93 additions and 332 deletions

View file

@ -1,44 +0,0 @@
#include <stdlib.h>
#include <debug.h>
#include <defines/N.h>
#include <string/free.h>
#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;
}

View file

@ -1,7 +0,0 @@
struct color_factory;
void free_color_factory(
struct color_factory* this);

View file

@ -1,108 +0,0 @@
#include <limits.h>
#include <math.h>
#include <debug.h>
#include <defines/N.h>
#include <memory/smalloc.h>
#include <string/new.h>
#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;
}

View file

@ -1,3 +0,0 @@
struct color_factory* new_color_factory(void);

View file

@ -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;
};

View file

@ -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); \

View file

@ -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)

View file

@ -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);

View file

@ -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)(

View file

@ -3,7 +3,7 @@
#include <defines/N.h>
#include <color_factory/struct.h>
#include <globals/terminalcolors.h>
#include <stringtree/new.h>
#include <stringtree/prepend.h>
@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -3,13 +3,11 @@
#include <defines/N.h>
#include <color_factory/struct.h>
#include <stringtree/new.h>
#include <stringtree/prepend.h>
#include <stringtree/append.h>
/*#include <value/prettyprint.h>*/
#include <globals/terminalcolors.h>
#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;

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -1,7 +1,7 @@
#include <debug.h>
#include <color_factory/struct.h>
#include <globals/terminalcolors.h>
#include <stringtree/new.h>
#include <stringtree/prepend.h>
@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -1,4 +1,9 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <termios.h>
#include <unistd.h>
@ -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;

View file

@ -5,6 +5,5 @@ struct color_factory;
void handle_interactive(
struct environment** environment,
struct booleans* booleans,
struct color_factory* cfactory);
struct booleans* booleans);

View file

@ -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);

View file

@ -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);

19
main.c
View file

@ -1,4 +1,6 @@
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
@ -12,8 +14,8 @@
#include <booleans/new.h>
#include <booleans/free.h>
#include <color_factory/new.h>
#include <color_factory/free.h>
/*#include <color_factory/new.h>*/
/*#include <color_factory/free.h>*/
#include <environment/new.h>
#include <environment/declare_builtins.h>
@ -25,10 +27,6 @@
#include <handle_file.h>
#include <handle_interactive.h>
/*/ 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);

View file

@ -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}

View file

@ -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

View file

@ -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);

View file

@ -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);

View file

@ -3,13 +3,13 @@
#include <defines/N.h>
#include <color_factory/struct.h>
#include <stringtree/new.h>
#include <stringtree/prepend.h>
#include <stringtree/append.h>
#include <stringtree/free.h>
#include <globals/terminalcolors.h>
#include <expression/prettyprint.h>
#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;

View file

@ -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);

View file

@ -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;
}

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

View file

View file

@ -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;
}

View file

@ -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);

View file

@ -5,14 +5,13 @@
#include <defines/N.h>
#include <color_factory/struct.h>
/*#include <stringtree/struct.h>*/
#include <stringtree/new.h>
#include <stringtree/prepend.h>
#include <stringtree/append.h>
#include <stringtree/free.h>
#include <globals/terminalcolors.h>
#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;

View file

@ -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);

View file

@ -5,13 +5,13 @@
#include <defines/N.h>
#include <color_factory/struct.h>
#include <stringtree/new.h>
#include <stringtree/append.h>
#include <stringtree/prepend.h>
#include <stringtree/free.h>
#include <globals/terminalcolors.h>
#include <value/prettyprint.h>
#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;

View file

@ -2,6 +2,5 @@
struct stringtree* builtin_lambda_value_prettyprint(
int *out_chosen_color,
struct value* super,
struct color_factory* cfactory,
bool with_color);

View file

@ -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)(

View file

@ -1,8 +1,6 @@
#include <debug.h>
#include <color_factory/struct.h>
#include <defines/N.h>
#include <stringtree/new.h>
@ -10,7 +8,7 @@
#include <stringtree/prepend.h>
#include <stringtree/free.h>
/*#include <mpq/prettyprint.h>*/
#include <globals/terminalcolors.h>
#include <expression/prettyprint.h>
@ -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;

View file

@ -2,6 +2,5 @@
struct stringtree* lambda_value_prettyprint(
int *out_chosen_color,
struct value* super,
struct color_factory* cfactory,
bool with_color);

View file

@ -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(");*/

View file

@ -2,6 +2,5 @@
struct stringtree* lazy_value_prettyprint(
int *out_chosen_color,
struct value* super,
struct color_factory* cfactory,
bool with_color);

View file

@ -1,7 +1,7 @@
#include <debug.h>
#include <color_factory/struct.h>
#include <globals/terminalcolors.h>
#include <stringtree/new.h>
#include <stringtree/append.h>
@ -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;

View file

@ -2,6 +2,5 @@
struct stringtree* number_value_prettyprint(
int *out_chosen_color,
struct value* super,
struct color_factory* cfactory,
bool with_color);

View file

@ -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;

View file

@ -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);

26
zog.py
View file

@ -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, "<cog-snippet>", "exec"), myglobals);
eval(compile(source, "<zog-snippet>", "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;