From d201bf2bf253c80a8629f11e44da9daca67470aa Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sat, 17 Jun 2023 17:46:10 -0700 Subject: console library works now --- src/operetta.c | 77 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 21 deletions(-) (limited to 'src/operetta.c') diff --git a/src/operetta.c b/src/operetta.c index f754895..7e257f0 100644 --- a/src/operetta.c +++ b/src/operetta.c @@ -1,52 +1,87 @@ #include #include +#include #include "operetta.h" #include "lexer.h" #include "parse.h" #include "proposition.h" #include "eval.h" +#include "console.h" +#include "conio.h" int main(int argc, char** argv) { /* read file */ - char* path = argv[1]; + char* path; + if (argc > 0) { + path = argv[1]; + } + else { + exit(0); + } char* content = read_file(path); int lineCount = count_lines(content); lineSet_t lines = parse_lines(content, lineCount); + consoleLine_t* consoleLines = malloc(sizeof(consoleLine_t) * lines.count); for (int i = 0; i < lines.count; i++) { line_t line = lines.lines[i]; - printf("%d. ", i + 1); - for (int j = 0; j < line.scope.depth; j++) { - printf(" > "); + char* s = calloc(sizeof(char), 100); + s[0] = '\0'; + char* propString = write_prop(line.proposition); + char* justString = write_just(line.justification); + snprintf(s, 100, "%d. %s BECAUSE: %s", i + 1, propString, justString); + free(propString); + free(justString); + consoleLines[i] = init_simple_consoleline(CWHITE, s); + } + set_cache(init_consolelineset(consoleLines, lines.count)); + /* enter main loop */ + while (1) { + draw_screen(); + int c = getch(); + if (c == 'j') { + move_selection(1); + } + if (c == 'k') { + move_selection(-1); } - write_prop(line.proposition); - printf(" BECAUSE: "); - write_just(line.justification); - printf(just_is_sound(line, lines) ? " " : " "); - printf(" [%d]", line.scope.id); - printf("\n"); } free(content); } -void write_prop(prop_t prop) { +char* write_prop(prop_t prop) { + char* out = calloc(sizeof(char), 100); + out[0] = '\0'; if (prop.isNegative) { - printf("%s", "!"); + strcat(out, "!"); } if (prop.isSimple) { - printf("%s", prop.name); + strcat(out, prop.name); } else { - printf("("); - write_prop(*(prop.propA)); - printf(" %s ", tttos(prop.relation)); - write_prop(*(prop.propB)); - printf(")"); + strcat(out, "("); + char* c1 = write_prop(*prop.propA); + strcat(out, c1); + free(c1); + strcat(out, " "); + strcat(out, tttos(prop.relation)); + strcat(out, " "); + char* c2 = write_prop(*prop.propB); + strcat(out, c2); + free(c2); + strcat(out, ")"); } + return out; } -void write_just(just_t just) { - printf("%s", jrtos(just.rule)); +char* write_just(just_t just) { + char* out = calloc(sizeof(char), 50); + out[0] = '\0'; + strcat(out, jrtos(just.rule)); for (int i = 0; i < just.referencesCount; i++) { - printf(" %d", just.references[i]); + char* n = calloc(sizeof(char), 5); + snprintf(n, 5, " %d", just.references[i]); + strcat(out, n); + free(n); } + return out; } char* read_file(char* path) { FILE* file = fopen(path, "r"); -- cgit v1.3.1