summaryrefslogtreecommitdiff
path: root/precedent.c
blob: 81555b9cfd39c635aff64c4ab6087d3ecacbbbbc (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
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <libgen.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;
	}
	/* dirname may modify path, copy first */
	char* path = argv[1];
	char* pathCpy = malloc((strlen(path) + 1) * sizeof(char));
	strcpy(pathCpy, path);
	char* dirName = dirname(pathCpy);
	char* baseDir = malloc((strlen(dirName) + 2) * sizeof(char));
	strcpy(baseDir, dirName);
	strcat(baseDir, "/");
	sym_t* finalSymbol = parse_symbol_final(read_file(path));
	if (visual) {
		printf("--BEGIN VISUALIZATION--\n");
		write_symbol_visual(finalSymbol, 0);
		printf("--END VISUALIZATION--\n");
	}
	init_eval();
	eval(finalSymbol);
	free(dirName);
	free(pathCpy);
	free(baseDir);
	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;
}
/* wtf */
char* preprocess(char* str, char* baseDir) {
	char* final = str;
	while (1) {
		int i = 0;
		while (str[i] != '@') {
			i++;
			if (str[i] == '\0') {
				printf("%s\n", final);
				return final;
			}
		}
		i++;
		char* includeName = malloc(20 * sizeof(char));
		int f = 0;
		while (isalnum(str[i]) || str[i] == '/' || str[i] == '.' || str[i] == '_') {
			includeName[f] = str[i];
			str[i] = ' ';
			i++;
			f++;
		}
		final = str;
		includeName[f] = '\0';
		char* path = malloc((strlen(baseDir) + strlen(includeName) + 1) * sizeof(char));
		strcpy(path, baseDir);
		strcat(path, includeName);
		char* toInclude = preprocess(read_file(path), baseDir);
		char* newStr = malloc((strlen(toInclude) + strlen(final) + 1) * sizeof(char));
		strcpy(newStr, toInclude);
		strcat(newStr, final);
		/*
		free(includeName);
		free(path);
		free(str);
		free(toInclude);
		*/
		final = newStr;
	}
}