This commit is contained in:
Zander Thannhauser 2025-01-20 13:40:51 -06:00
parent 56f7748377
commit 4668736ce6
119 changed files with 1106 additions and 187 deletions

View file

@ -21,10 +21,14 @@ struct cmdln_flags* new_cmdln_flags(
bool read_init_file = true; bool read_init_file = true;
const char* custom_init_path = "~/.13rc"; const char* init_path = "~/.13rc";
const char* input_string = NULL; const char* input_string = NULL;
#ifndef RELEASE_BUILD
const char* test_interactive_using_input_file = NULL;
#endif
enum input_kind input_kind = ik_interactive; enum input_kind input_kind = ik_interactive;
static const struct option long_options[] = { static const struct option long_options[] = {
@ -36,13 +40,18 @@ struct cmdln_flags* new_cmdln_flags(
{"custom-init-path", required_argument, 0, 4}, {"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'}, {"command", required_argument, 0, 'c'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
for (int c; (c = getopt_long(argc, argv, "" 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; ) "", long_options, NULL)) >= 0; )
{ {
switch (c) switch (c)
@ -72,7 +81,14 @@ struct cmdln_flags* new_cmdln_flags(
case 4: // --custom-init-path 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; break;
} }
@ -143,12 +159,16 @@ struct cmdln_flags* new_cmdln_flags(
flags->read_init_file = read_init_file; 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_kind = input_kind;
flags->input_string = input_string; flags->input_string = input_string;
#ifndef RELEASE_BUILD
flags->test_interactive_using_input_file = test_interactive_using_input_file;
#endif
EXIT; EXIT;
return flags; return flags;
} }

View file

@ -18,12 +18,16 @@ struct cmdln_flags
bool read_init_file; bool read_init_file;
const char* custom_init_path; const char* init_path;
const char* input_string; const char* input_string;
const char* input_file; const char* input_file;
#ifndef RELEASE_BUILD
const char* test_interactive_using_input_file;
#endif
char* const* argv; char* const* argv;
}; };

View file

@ -77,11 +77,7 @@ extern int debug_depth;
#define TODO \ #define TODO \
{ \ { \
char filebuffer[PATH_MAX]; \ printf("TODO hit at %s:%i!\n", __FILE__, __LINE__); \
strcpy(filebuffer, __FILE__ + 4); \
filebuffer[strlen(filebuffer) - 2] = 0; \
\
printf("TODO hit at %s:%i!\n", filebuffer, __LINE__); \
\ \
pid_t __child = fork(); \ pid_t __child = fork(); \
\ \
@ -102,7 +98,7 @@ extern int debug_depth;
{\ {\
char __buffer[100]; \ char __buffer[100]; \
snprintf(__buffer, 100, "+%u", __LINE__); \ snprintf(__buffer, 100, "+%u", __LINE__); \
execlp("gedit", "gedit", filebuffer, __buffer, NULL); \ execlp("gedit", "gedit", __FILE__, __buffer, NULL); \
}\ }\
\ \
exit(1); \ exit(1); \

View file

@ -0,0 +1,7 @@
1
(1
(
)
+ 1 2
1 2
λx:x

View file

@ -47,7 +47,7 @@ fib 8
(𝑌 λ f x: (≤ x 1) x (+ (f (- x 1)) (f (- x 2)))) 10 (𝑌 λ f x: (≤ x 1) x (+ (f (- x 1)) (f (- x 2)))) 10
((((( ((((( 1 ))))) ))))) ((((( ((((( ((((( ((((( 1 ))))) ))))) ))))) )))))

View file

@ -90,7 +90,13 @@ struct value* application_expression_evaluate(
{ {
dpvu(lleft->kind); 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; TODO;
exit(1); exit(1);
break; break;
} }

View file

@ -0,0 +1,26 @@
#include <debug.h>
#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
}

View file

@ -0,0 +1,7 @@
struct value* error_expression_evaluate(
struct expression* this,
struct environment* environment,
struct booleans* booleans);

19
expression/error/free.c Normal file
View file

@ -0,0 +1,19 @@
#include <debug.h>
#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;
}

6
expression/error/free.h Normal file
View file

@ -0,0 +1,6 @@
struct expression;
void free_error_expression(
struct expression* super);

View file

@ -0,0 +1,20 @@
#include <debug.h>
#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
};

View file

@ -0,0 +1,2 @@
extern struct expression_inheritance error_expression_inheritance;

28
expression/error/new.c Normal file
View file

@ -0,0 +1,28 @@
#include <debug.h>
#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;
}

9
expression/error/new.h Normal file
View file

@ -0,0 +1,9 @@
struct value;
struct string;
struct expression* new_error_expression(
const wchar_t* message,
struct expression* subexpression);

View file

@ -0,0 +1,72 @@
#include <debug.h>
#include <defines/N.h>
#include <stringtree/new.h>
#include <stringtree/prepend.h>
#include <stringtree/append.h>
#include <globals/terminalcolors.h>
#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
}

View file

@ -0,0 +1,14 @@
#include <stdbool.h>
struct expression;
struct color_factory;
struct stringtree* error_expression_prettyprint(
int *out_chosen_color,
struct expression* this,
bool with_color);

View file

@ -0,0 +1,38 @@
#include <debug.h>
#include <stringtree/new.h>
#include <stringtree/append.h>
#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;
}

View file

@ -0,0 +1,4 @@
struct stringtree* error_expression_prettyprint_errors(
struct expression* super);

12
expression/error/struct.h Normal file
View file

@ -0,0 +1,12 @@
#include "../struct.h"
struct error_expression
{
struct expression super;
const wchar_t* message;
struct expression* subexpression;
};

51
globals/terminalcolors.c Normal file
View file

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

View file

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

19
globals/terminalcolors.h Normal file
View file

@ -0,0 +1,19 @@
#include <wchar.h>
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;

View file

@ -23,14 +23,14 @@
#include <parse/position/new.h> #include <parse/position/new.h>
#include <parse/position/free.h> #include <parse/position/free.h>
#include <parse/istream/string/new.h> #include <istream/string/new.h>
#include <parse/istream/file/new.h> #include <istream/file/new.h>
#include <parse/istream/read.h> #include <istream/read.h>
#include <parse/istream/free.h> #include <istream/free.h>
#include <parse/wcistream/istream/new.h> #include <wcistream/istream/new.h>
#include <parse/wcistream/read.h> #include <wcistream/read.h>
#include <parse/wcistream/free.h> #include <wcistream/free.h>
#include <parse/token/struct.h> #include <parse/token/struct.h>
@ -60,7 +60,8 @@ void handle_file(
struct booleans* booleans, struct booleans* booleans,
const char* input_file, const char* input_file,
bool echo, bool echo,
bool print_with_colors) bool print_with_colors,
struct wcostream* wc_stdout)
{ {
ENTER; ENTER;
@ -68,12 +69,6 @@ void handle_file(
assert(input_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( struct istream* stream = new_file_istream(
/* path: */ input_file); /* path: */ input_file);
@ -163,14 +158,10 @@ void handle_file(
free_string(filename); free_string(filename);
free_wcostream(wc_stdout);
free_wcistream(wcstream); free_wcistream(wcstream);
free_istream(stream); free_istream(stream);
free_ostream(b_stdout);
EXIT; EXIT;
} }

View file

@ -4,6 +4,7 @@ void handle_file(
struct booleans* booleans, struct booleans* booleans,
const char* input_file, const char* input_file,
bool echo, bool echo,
bool isatty); bool isatty,
struct wcostream* wc_stdout);

View file

@ -16,13 +16,13 @@
#include <parse/position/new.h> #include <parse/position/new.h>
#include <parse/position/free.h> #include <parse/position/free.h>
#include <parse/istream/file/new.h> #include <istream/file/new.h>
#include <parse/istream/read.h> #include <istream/read.h>
#include <parse/istream/free.h> #include <istream/free.h>
#include <parse/wcistream/istream/new.h> #include <wcistream/istream/new.h>
#include <parse/wcistream/read.h> #include <wcistream/read.h>
#include <parse/wcistream/free.h> #include <wcistream/free.h>
#include <parse/parse.h> #include <parse/parse.h>

View file

@ -13,6 +13,35 @@
#include <memory/smalloc.h> #include <memory/smalloc.h>
#include <memory/srealloc.h> #include <memory/srealloc.h>
#include <string/new.h>
#include <string/free.h>
#include <stringtree/new.h>
#include <stringtree/append.h>
#include <stringtree/print.h>
#include <stringtree/free.h>
#include <parse/position/new.h>
#include <parse/position/free.h>
#include <parse/tokenizer/new.h>
#include <parse/tokenizer/next.h>
#include <parse/tokenizer/free.h>
#include <parse/parse.h>
#include <statement/prettyprint.h>
#include <statement/prettyprint_errors.h>
#include <statement/execute.h>
#include <statement/free.h>
#include <stringtree/println.h>
#include <stringtree/free.h>
#include <wcistream/string/new.h>
#include <wcistream/read.h>
#include <wcistream/free.h>
#include "handle_interactive.h" #include "handle_interactive.h"
static const enum state { static const enum state {
@ -24,6 +53,10 @@ static const enum state {
s_backspace, s_backspace,
s_enter,
s_insert_lambda,
s_insert_letter, s_insert_letter,
s_up, s_up,
@ -36,6 +69,8 @@ static const enum state {
s_start, s_start,
s_sync_idle,
s_esc, s_esc,
s_csi, s_csi,
@ -45,10 +80,25 @@ static const enum state {
[s_start][0x7F] = s_backspace, [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]['0' ... '9'] = s_insert_letter,
[s_start]['a' ... 'z'] = s_insert_letter, [s_start]['a' ... 'z'] = 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_start][0x1B] = s_esc,
[s_esc]['['] = s_csi, [s_esc]['['] = s_csi,
[s_start][0x9B] = s_csi, [s_start][0x9B] = s_csi,
@ -65,28 +115,48 @@ static const enum state {
void handle_interactive( void handle_interactive(
struct environment** environment, 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; ENTER;
int fd;
struct termios termios; struct termios termios;
if (test_interactive_using_input_file)
{
if ((fd = open(test_interactive_using_input_file, O_RDONLY)) < 0)
{
TODO;
exit(1);
}
}
else
{
fd = 0;
if (tcgetattr(/* fd: */ 1, /* struct termios: */ &termios) < 0) if (tcgetattr(/* fd: */ 1, /* struct termios: */ &termios) < 0)
{ {
TODO; TODO;
exit(1); exit(1);
} }
struct termios original = termios; struct termios modified_termios = termios;
termios.c_lflag &= ~(unsigned) ICANON; // disable ICANON. modified_termios.c_lflag &= ~(unsigned) ICANON; // disable ICANON.
termios.c_lflag &= ~(unsigned) ECHO; // disable ECHO. modified_termios.c_lflag &= ~(unsigned) ECHO; // disable ECHO.
if (tcsetattr(/* fd: */ 1, /* when?: */ TCSADRAIN, /* struct termios: */ &termios) < 0) if (tcsetattr(/* fd: */ 1, /* when?: */ TCSADRAIN, /* struct termios: */ &modified_termios) < 0)
{ {
TODO; TODO;
exit(1); exit(1);
} }
}
size_t width = 50; size_t width = 50;
@ -109,6 +179,8 @@ void handle_interactive(
{ {
live.data = smalloc(sizeof(*live.data) * width); live.data = smalloc(sizeof(*live.data) * width);
live.pos = 0;
live.cap = width; live.cap = width;
memset(live.data, 0, sizeof(*live.data) * width); memset(live.data, 0, sizeof(*live.data) * width);
@ -148,7 +220,7 @@ void handle_interactive(
{ {
assert(line.pos <= line.n); assert(line.pos <= line.n);
ensure_capacity(line.cap + 1); ensure_capacity(line.n + 1);
memmove( memmove(
/* dest: */ line.data + line.pos + 1, /* dest: */ line.data + line.pos + 1,
@ -172,6 +244,8 @@ void handle_interactive(
assert(line.n <= width); assert(line.n <= width);
struct stringtree* tree = new_stringtree();
size_t i = 0; size_t i = 0;
for (; i < width; i++) for (; i < width; i++)
@ -184,26 +258,33 @@ void handle_interactive(
{ {
if (pos != i) 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; 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; live_letter->code = intended_letter->code;
pos++; pos++;
free_string(s);
} }
} }
if (pos != line.pos) 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; live.pos = pos;
} }
fflush(stdout); stringtree_print(tree, wc_stdout);
free_stringtree(tree);
} }
struct { struct {
@ -249,11 +330,18 @@ void handle_interactive(
fflush(stdout); fflush(stdout);
} }
uint8_t buffer[4096]; struct string* filename = new_string(L"<cmdln>", (unsigned) -1);
struct position* position = new_position(
/* filename: */ filename,
/* line: */ 1,
/* column: */ 1);
for (bool keep_going = true, error = false; !error && keep_going; ) 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) if (retval < 0)
{ {
@ -261,7 +349,7 @@ void handle_interactive(
} }
else if (!retval) else if (!retval)
{ {
TODO; keep_going = false;
} }
else for (ssize_t i = 0; i < retval; i++) else for (ssize_t i = 0; i < retval; i++)
{ {
@ -303,6 +391,83 @@ void handle_interactive(
break; 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: case s_delete:
{ {
if (line.pos < line.n) if (line.pos < line.n)
@ -315,6 +480,15 @@ void handle_interactive(
break; break;
} }
case s_insert_lambda:
{
insert_into_line_at_pos(L'λ'), line.pos++;
redraw();
break;
}
case s_insert_letter: case s_insert_letter:
{ {
insert_into_line_at_pos(buffer[i]), line.pos++; insert_into_line_at_pos(buffer[i]), line.pos++;
@ -387,15 +561,26 @@ void handle_interactive(
} }
} }
// running them through a tokenizer-like state machine? if (test_interactive_using_input_file)
{
// different states have corasponding actions close(fd);
}
if (tcsetattr(/* fd: */ 1, /* when?: */ TCSADRAIN, /* struct termios: */ &original) < 0) else
{
if (tcsetattr(/* fd: */ 1, /* when?: */ TCSADRAIN, /* struct termios: */ &termios) < 0)
{ {
TODO; TODO;
exit(1); exit(1);
} }
}
free_position(position);
free_string(filename);
free(token.data);
free(live.data);
free(line.data); free(line.data);

View file

@ -5,5 +5,10 @@ struct color_factory;
void handle_interactive( void handle_interactive(
struct environment** environment, struct environment** environment,
struct booleans* booleans); struct booleans* booleans,
struct wcostream* wc_stdout
#ifndef RELEASE_BUILD
, const char* test_interactive_input_file
#endif
);

View file

@ -9,28 +9,18 @@
#include <stringtree/println.h> #include <stringtree/println.h>
#include <stringtree/free.h> #include <stringtree/free.h>
/*#include <booleans/new.h>*/
/*#include <booleans/free.h>*/
/*#include <color_factory/new.h>*/
/*#include <color_factory/free.h>*/
/*#include <environment/new.h>*/
/*#include <environment/declare_builtins.h>*/
/*#include <environment/free.h>*/
#include <parse/position/struct.h> #include <parse/position/struct.h>
#include <parse/position/new.h> #include <parse/position/new.h>
#include <parse/position/free.h> #include <parse/position/free.h>
#include <parse/istream/string/new.h> #include <istream/string/new.h>
#include <parse/istream/file/new.h> #include <istream/file/new.h>
#include <parse/istream/read.h> #include <istream/read.h>
#include <parse/istream/free.h> #include <istream/free.h>
#include <parse/wcistream/istream/new.h> #include <wcistream/istream/new.h>
#include <parse/wcistream/read.h> #include <wcistream/read.h>
#include <parse/wcistream/free.h> #include <wcistream/free.h>
#include <parse/token/struct.h> #include <parse/token/struct.h>
@ -60,18 +50,13 @@ void handle_string(
struct booleans* booleans, struct booleans* booleans,
const char* input_string, const char* input_string,
bool echo, bool echo,
bool print_with_colors) bool print_with_colors,
struct wcostream* wc_stdout)
{ {
ENTER; ENTER;
assert(input_string); 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( struct istream* stream = new_string_istream(
/* read-only text: */ (const uint8_t*) input_string); /* read-only text: */ (const uint8_t*) input_string);
@ -156,14 +141,10 @@ void handle_string(
free_string(filename); free_string(filename);
free_wcostream(wc_stdout);
free_wcistream(wcstream); free_wcistream(wcstream);
free_istream(stream); free_istream(stream);
free_ostream(b_stdout);
EXIT; EXIT;
} }

View file

@ -1,4 +1,5 @@
struct wcostream;
struct color_factory; struct color_factory;
struct environment; struct environment;
struct booleans; struct booleans;
@ -8,5 +9,6 @@ void handle_string(
struct booleans* booleans, struct booleans* booleans,
const char* input_file, const char* input_file,
bool echo, bool echo,
bool print_with_colors); bool print_with_colors,
struct wcostream* wc_stdout);

39
main.c
View file

@ -14,8 +14,11 @@
#include <booleans/new.h> #include <booleans/new.h>
#include <booleans/free.h> #include <booleans/free.h>
/*#include <color_factory/new.h>*/ #include <ostream/file/new.h>
/*#include <color_factory/free.h>*/ #include <ostream/free.h>
#include <wcostream/ostream/new.h>
#include <wcostream/free.h>
#include <environment/new.h> #include <environment/new.h>
#include <environment/declare_builtins.h> #include <environment/declare_builtins.h>
@ -44,9 +47,15 @@ int main(int argc, char* const* argv)
if (flags->read_init_file) 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) switch (flags->input_kind)
{ {
case ik_string: case ik_string:
@ -55,8 +64,9 @@ int main(int argc, char* const* argv)
/* environment, may by modified/replaced: */ &environment, /* environment, may by modified/replaced: */ &environment,
/* booleans: */ booleans, /* booleans: */ booleans,
/* input string: */ flags->input_string, /* input string: */ flags->input_string,
/* echo? */ flags->echo, /* echo statements? */ flags->echo,
/* colors?: */ flags->print_with_colors); /* colors?: */ flags->print_with_colors,
/* wide-character output stream: */ wc_stdout);
break; break;
} }
@ -67,23 +77,32 @@ int main(int argc, char* const* argv)
/* environment, may by modified/replaced: */ &environment, /* environment, may by modified/replaced: */ &environment,
/* booleans: */ booleans, /* booleans: */ booleans,
/* input file: */ flags->input_file, /* input file: */ flags->input_file,
/* echo? */ flags->echo, /* echo statements? */ flags->echo,
/* colors?: */ flags->print_with_colors); /* colors?: */ flags->print_with_colors,
/* wide-character output stream: */ wc_stdout);
break; break;
} }
case ik_interactive: case ik_interactive:
{ {
#ifdef RELEASE_BUILD
if (!isatty(1)) if (!isatty(1))
{ {
TODO; TODO;
exit(1); exit(1);
} }
#endif
handle_interactive( handle_interactive(
/* environment, may by modified/replaced: */ &environment, /* 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; break;
} }
@ -95,6 +114,10 @@ int main(int argc, char* const* argv)
} }
} }
free_wcostream(wc_stdout);
free_ostream(b_stdout);
free_environment(environment); free_environment(environment);
free_booleans(booleans); free_booleans(booleans);

View file

@ -25,7 +25,7 @@ endif
srclist.mk: srclist.mk:
@echo "searching for source files ..." @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' \ | sed 's/.cpp$$/.c/g;s/^/srcs += /g' \
| sort -Vu > ${@} | sort -Vu > ${@}
@ -33,9 +33,9 @@ include srclist.mk
.PRECIOUS: %.c .PRECIOUS: %.c
%.c: %.cpp zog.py %.c: %.cpp zog.py | bin/
@echo "preprocessing ${<} ..." @echo "preprocessing ${<} ..."
@python3.10 ./zog.py -i $< -o $@ @./zog.py -i $< --snippet-directory=bin -o $@
${prefix}/%.o ${prefix}/%.d: %.c ${optionset} | ${prefix}/%/ ${prefix}/%.o ${prefix}/%.d: %.c ${optionset} | ${prefix}/%/
@echo "compiling (${buildtype}) ${*}.c ..." @echo "compiling (${buildtype}) ${*}.c ..."
@ -56,7 +56,9 @@ args += --custom-init-path ./examples/init.txt
#args += -c '1' #args += -c '1'
args += ./examples/sandbox.txt #args += ./examples/sandbox.txt
args += --test-interactive-using-input-file ./examples/sandbox-interactive.txt
run: ${prefix}/13 run: ${prefix}/13
$< ${args} $< ${args}

View file

@ -1,4 +1,5 @@
#include <stdio.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
@ -12,6 +13,8 @@ void* srealloc(
if (!newptr) if (!newptr)
{ {
printf("oldptr = %p\n", oldptr);
printf("size = %zu\n", size);
assert(!"TODO"); assert(!"TODO");
} }

View file

@ -24,12 +24,14 @@
#include <expression/lambda/new.h> #include <expression/lambda/new.h>
#include <expression/application/new.h> #include <expression/application/new.h>
#include <expression/parenthesis/new.h> #include <expression/parenthesis/new.h>
#include <expression/error/new.h>
#include <expression/inc.h> #include <expression/inc.h>
#include <expression/free.h> #include <expression/free.h>
#include <statement/expression/new.h> #include <statement/expression/new.h>
#include <statement/substatements/new.h> #include <statement/substatements/new.h>
#include <statement/assignment/new.h> #include <statement/assignment/new.h>
#include <statement/error/new.h>
#include <statement/inc.h> #include <statement/inc.h>
#include <statement/free.h> #include <statement/free.h>
@ -244,7 +246,34 @@ struct expression* parse_primary_expression(
parse_application_expression(tokenizer, parse_application_expression(tokenizer,
/* in parenthesis: */ true); /* in parenthesis: */ true);
switch (tokenizer->token->kind)
{
case tk_cparen:
retval = new_parenthesis_expression(subexpression); 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); tokenizer_next(tokenizer);
@ -331,6 +360,13 @@ struct expression* parse_application_expression(
TODO; TODO;
break; break;
case tk_EOF:
{
retval = new_error_expression(L"Unexpected EOF.", NULL);
break;
}
default: default:
TODO; TODO;
break; break;
@ -371,14 +407,6 @@ struct expression* parse_application_expression(
case tk_semicolon: case tk_semicolon:
case tk_EOF: case tk_EOF:
{
if (in_parentheses)
{
TODO;
}
break;
}
case tk_cparen: case tk_cparen:
{ {
break; break;
@ -504,6 +532,14 @@ struct statement* parse_statement(
TODO; TODO;
break; break;
case tk_cparen:
{
retval = new_error_statement(
L"Unexpected close parenthesis when reading statement", NULL);
break;
}
case tk_error: case tk_error:
TODO; TODO;
// create error statement ("unknown token") // create error statement ("unknown token")
@ -550,12 +586,18 @@ struct statement* parse_statements(
left = inc_statement(right); 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); tokenizer_next(tokenizer);
} }

View file

@ -3,7 +3,7 @@
#include <debug.h> #include <debug.h>
#include "../wcistream/free.h" #include <wcistream/free.h>
#include "../position/free.h" #include "../position/free.h"

View file

@ -1,7 +1,7 @@
#include <debug.h> #include <debug.h>
#include <parse/wcistream/inc.h> #include <wcistream/inc.h>
#include <parse/position/inc.h> #include <parse/position/inc.h>

View file

@ -10,8 +10,8 @@
#include <string/new.h> #include <string/new.h>
#include <string/free.h> #include <string/free.h>
#include "../wcistream/struct.h" #include <wcistream/struct.h>
#include "../wcistream/read.h" #include <wcistream/read.h>
#include "../position/struct.h" #include "../position/struct.h"
#include "../position/clone.h" #include "../position/clone.h"

View file

@ -28,6 +28,12 @@ srcs += ./expression/application/inheritance.c
srcs += ./expression/application/new.c srcs += ./expression/application/new.c
srcs += ./expression/application/prettyprint.c srcs += ./expression/application/prettyprint.c
srcs += ./expression/application/prettyprint_errors.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/evaluate.c
srcs += ./expression/free.c srcs += ./expression/free.c
srcs += ./expression/inc.c srcs += ./expression/inc.c
@ -67,6 +73,18 @@ srcs += ./handle_file.c
srcs += ./handle_init_file.c srcs += ./handle_init_file.c
srcs += ./handle_interactive.c srcs += ./handle_interactive.c
srcs += ./handle_string.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 += ./main.c
srcs += ./memory/smalloc.c srcs += ./memory/smalloc.c
srcs += ./memory/srealloc.c srcs += ./memory/srealloc.c
@ -89,18 +107,6 @@ srcs += ./ostream/free.c
srcs += ./ostream/inc.c srcs += ./ostream/inc.c
srcs += ./ostream/new.c srcs += ./ostream/new.c
srcs += ./ostream/write.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/parse.c
srcs += ./parse/position/assign.c srcs += ./parse/position/assign.c
srcs += ./parse/position/clone.c srcs += ./parse/position/clone.c
@ -116,28 +122,18 @@ srcs += ./parse/tokenizer/put_back.c
srcs += ./parse/token/free.c srcs += ./parse/token/free.c
srcs += ./parse/token/inc.c srcs += ./parse/token/inc.c
srcs += ./parse/token/new.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/execute.c
srcs += ./statement/assignment/free.c srcs += ./statement/assignment/free.c
srcs += ./statement/assignment/inheritance.c srcs += ./statement/assignment/inheritance.c
srcs += ./statement/assignment/new.c srcs += ./statement/assignment/new.c
srcs += ./statement/assignment/prettyprint.c srcs += ./statement/assignment/prettyprint.c
srcs += ./statement/assignment/prettyprint_errors.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/execute.c
srcs += ./statement/expression/execute.c srcs += ./statement/expression/execute.c
srcs += ./statement/expression/free.c srcs += ./statement/expression/free.c
@ -190,6 +186,22 @@ srcs += ./value/number/inheritance.c
srcs += ./value/number/new.c srcs += ./value/number/new.c
srcs += ./value/number/prettyprint.c srcs += ./value/number/prettyprint.c
srcs += ./value/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/free.c
srcs += ./wcostream/file/new.c srcs += ./wcostream/file/new.c
srcs += ./wcostream/file/write.c srcs += ./wcostream/file/write.c

69
statement/error/execute.c Normal file
View file

@ -0,0 +1,69 @@
#include <stddef.h>
#include <debug.h>
#include <stringtree/new.h>
#include <stringtree/println.h>
#include <stringtree/free.h>
#include <value/prettyprint.h>
#include <value/lazy/unlazy.h>
#include <value/free.h>
#include <statement/execute.h>
#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;
}

12
statement/error/execute.h Normal file
View file

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

20
statement/error/free.c Normal file
View file

@ -0,0 +1,20 @@
#include <debug.h>
#include <statement/free.h>
#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;
}

5
statement/error/free.h Normal file
View file

@ -0,0 +1,5 @@
struct statement;
void free_error_statement(
struct statement* this);

View file

@ -0,0 +1,21 @@
#include <debug.h>
#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,
};

View file

@ -0,0 +1,3 @@
extern struct statement_inheritance error_statement_inheritance;

28
statement/error/new.c Normal file
View file

@ -0,0 +1,28 @@
#include <debug.h>
#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;
}

7
statement/error/new.h Normal file
View file

@ -0,0 +1,7 @@
struct error;
struct statement* new_error_statement(
const wchar_t* message,
struct statement* statement);

View file

@ -0,0 +1,28 @@
#include <debug.h>
#include <statement/prettyprint.h>
#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
}

View file

@ -0,0 +1,10 @@
#include <stdbool.h>
struct color_factory;
struct stringtree* error_statement_prettyprint(
int *out_chosen_color,
struct statement* this,
bool with_color);

View file

@ -0,0 +1,36 @@
#include <debug.h>
#include <stringtree/new.h>
#include <stringtree/append.h>
#include <statement/prettyprint_errors.h>
#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;
}

View file

@ -0,0 +1,4 @@
struct stringtree* error_statement_prettyprint_errors(
struct statement* super);

12
statement/error/struct.h Normal file
View file

@ -0,0 +1,12 @@
#include "../struct.h"
struct error_statement
{
struct statement super;
const wchar_t* message;
struct statement* substatement;
};

View file

@ -106,6 +106,30 @@ struct string* new_string_from_ascii_format(
return this; 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;
}

View file

@ -14,3 +14,6 @@ struct string* new_string_from_ascii(
struct string* new_string_from_ascii_format( struct string* new_string_from_ascii_format(
const char* fmt, ...); const char* fmt, ...);
struct string* new_string_from_ascii_format_vargs(
const char* fmt, va_list arg);

View file

@ -48,20 +48,17 @@ void stringtree_append_formatstr(
{ {
ENTER; ENTER;
TODO;
#if 0
va_list va; va_list va;
va_start(va, fmt); 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); stringtree_append_string(this, string);
free_string(string); free_string(string);
va_end(va); va_end(va);
#endif
EXIT; EXIT;
} }

View file

@ -3,7 +3,7 @@
#include <debug.h> #include <debug.h>
#include <parse/istream/free.h> #include <istream/free.h>
#include "struct.h" #include "struct.h"
#include "free.h" #include "free.h"

Some files were not shown because too many files have changed in this diff Show more