summaryrefslogtreecommitdiff
path: root/src/operetta.c
blob: 9b7fea3f814a793cbb1b55bbe74098aad8cd454a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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;
}