summaryrefslogtreecommitdiff
path: root/precedent.c
blob: b609fff14a10ee1f9b4af311a04f82587ed3db63 (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "precedent.h"
#include "hashtable.h"
#include "parse.h"
#include "eval.h"

int main(int argc, char** argv) {
	int visual = 0;
	if (argc > 1) {
		for (int i = 1; i < argc; i++) {
			if (strcmp("--version", argv[i]) == 0) {
				printf("precedent, version 0.1\n");
				printf("Copyright 2023 Kai Stevenson\n");
				printf("All rights reserved. No part of the software may be sold\nor used in derivative works without explicit consent\n");
				return 0;
			}
			else if (strcmp("--help", argv[i]) == 0) {
				printf("usage: precedent <file> [--visualize] [--version] [--help]\n");
				return 0;
			}
			else if (strcmp("--visualize", argv[i]) == 0) {
				visual = 1;
			}
		}
	}
	else {
		printf("usage: precedent <file> [--visualize] [--version] [--help]\n");
		return 0;
	}
	sym_t* finalSymbol = parse_symbol_final(read_file(argv[1]));
	if (visual) {
		printf("--BEGIN VISUALIZATION--\n");
		write_symbol_visual(finalSymbol, 0);
		printf("--END VISUALIZATION--\n");
	}
	init_eval();
	eval(finalSymbol);
	return 0;
}
char* read_file(char* path) {
	FILE* file = fopen(path, "r");
	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;
}