#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; 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]; char* s = calloc(sizeof(char), 100); s[0] = '\0'; char* propString = write_prop(line.proposition); char* justString = write_just(line.justification); char isValid = just_is_sound(line, lines); snprintf(s, 100, "%d. %s BECAUSE: %s <%s>", i + 1, propString, justString, isValid ? "SOUND" : "UNSOUND"); 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); } } free(content); } char* write_prop(prop_t prop) { if (prop.isContradiction) { char* s = tttos(CONTRADICTION); char* sp = malloc(sizeof(char) * strlen(s) + 1); strcpy(sp, s); return sp; } char* out = calloc(sizeof(char), 100); out[0] = '\0'; if (prop.isNegative) { strcat(out, "!"); } if (prop.isSimple) { strcat(out, prop.name); } else { 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 realloc(out, sizeof(char) * strlen(out) + 1); } 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++) { char* n = calloc(sizeof(char), 5); snprintf(n, 5, " %d", just.references[i]); strcat(out, n); free(n); } return realloc(out, sizeof(char) * strlen(out) + 1); } char* read_file(char* path) { FILE* file = fopen(path, "r"); if (file == NULL) { printf("file %s does not exist\n", path); } fseek(file, 0, SEEK_END); unsigned long int length = ftell(file); fseek(file, 0, SEEK_SET); char* out = malloc((length + 1) * sizeof(char)); fread(out, length, 1, file); fclose(file); out[length] = '\0'; return out; }