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
107
108
109
110
111
112
113
114
115
116
117
118
|
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <libgen.h>
#include "precedent.h"
#include "hashtable.h"
#include "parse.h"
#include "eval.h"
int main(int argc, char** argv) {
clock_t initTime = clock();
int visual = 0;
int debug = 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] [--debug] [--version] [--help]\n");
return 0;
}
else if (strcmp("--visualize", argv[i]) == 0) {
visual = 1;
}
else if (strcmp("--debug", argv[i]) == 0) {
debug = 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, "/");
char* finalContent = preprocess(read_file(path), baseDir);
sym_t* finalSymbol = parse_symbol_final(finalContent);
free(pathCpy);
free(baseDir);
free(finalContent);
if (visual) {
printf("--BEGIN VISUALIZATION--\n");
write_symbol_visual(finalSymbol, 0);
printf("--END VISUALIZATION--\n");
}
hashtable_t* rootTable = init_hashtable(500, NULL);
clock_t setupTime = clock();
sym_t* sym = eval(finalSymbol, rootTable);
clock_t evalTime = clock();
if (debug) {
printf("time taken to set up environment: %.5f secs\n", (float)(setupTime - initTime) / (float)CLOCKS_PER_SEC);
printf("time taken to execute program: %.5f secs\n", ((float)(evalTime - setupTime) - initTime) / (float)CLOCKS_PER_SEC);
}
destroy_sym_tree(finalSymbol);
destroy_sym(sym);
destroy_hashtable(rootTable);
return 0;
}
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;
}
char* preprocess(char* str, char* baseDir) {
while (1) {
int i = 0;
while (str[i] != '@') {
i++;
if (str[i] == '\0') {
return str;
}
}
str[i] = ' ';
i++;
char* includeName = malloc(20 * sizeof(char));
int f = 0;
while (isalnum((unsigned char)str[i]) || str[i] == '/' || str[i] == '.' || str[i] == '_') {
includeName[f] = str[i];
str[i] = ' ';
i++;
f++;
}
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(str) + 1) * sizeof(char));
strcpy(newStr, toInclude);
strcat(newStr, str);
free(str);
free(includeName);
free(path);
free(toInclude);
str = newStr;
}
}
|