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
119
120
121
122
|
#include <stdlib.h>
#include <string.h>
#include <utils/file.h>
#include <utils/time.h>
#include <vm/runner/runner.h>
#include "compiler/tree_parser/tree_parser.h"
#include "utils/types.h"
static const char *codes[] = {
"print(\"Hello\\n\");",
};
Clock runWithPrint(char const *restrict code) {
Clock sum = 0;
Clock diff = 0;
fprintf(stderr, "----code:\n%s\n----\n", code);
Clock start = getTimeInNano();
const Nodes nodes = lexer(code);
diff += getTimeInNano() - start;
sum += diff;
if (nodes.size != ERROR_SIZE) {
printNodes(nodes);
fprintf(stderr, "----lexing in %ldns\n", diff);
start = getTimeInNano();
ParsedNode *parsedNode = _parser(nodes, code);
diff = getTimeInNano() - start;
sum += diff;
if (parsedNode != NULL) {
printParsedNode(parsedNode);
fprintf(stderr, "----node parsing in %ldns\n", diff);
start = getTimeInNano();
ParsedTree *parsedTree = _treeParser(parsedNode, code);
diff = getTimeInNano() - start;
sum += diff;
if (parsedTree != NULL) {
printParsedTreeNode(parsedTree);
fprintf(stderr, "----tree parsing in %ldns\n", diff);
start = getTimeInNano();
Instructions instructions = _codeGenerator(parsedTree, code);
diff = getTimeInNano() - start;
sum += diff;
if (instructions.size != ERROR_SIZE) {
printInstructions(instructions);
fprintf(stderr, "----code_generator in %ldns\n", diff);
start = getTimeInNano();
bool ranSuccess = _runner(instructions);
diff = getTimeInNano() - start;
sum += diff;
fprintf(stderr, "----runner in %ldns\n", diff);
fprintf(stderr, "ran sucessfully = %s\n",
ranSuccess ? "true" : "false");
fprintf(stderr, "----sum %ldns\n", sum);
deleteInstructions(instructions);
} else {
fprintf(stderr, "----returned error");
}
deleteParsedTree(parsedTree);
} else {
fprintf(stderr, "----returned error");
}
deleteParsedNode(parsedNode);
} else {
fprintf(stderr, "----returned error");
}
} else {
fprintf(stderr, "----returned error");
}
deleteNodes(nodes);
return sum;
}
Clock process() {
Clock sumAll = 0;
for (size_t i = 0; i < sizeof(codes) / sizeof(char *); ++i) {
Clock start = getTimeInNano();
runWithPrint(codes[i]);
sumAll += getTimeInNano() - start;
}
return sumAll;
}
void runBenchmark() {
Clock sum = 0;
const int times = 100;
for (int i = 0; i < times; ++i) {
sum += process();
}
printf("\n\navg = %fns", ((float)sum) / times);
}
int main(int argc, char *argv[]) {
/*runBenchmark();*/
if (argc < 2) {
fprintf(stderr, "no file");
return 1;
}
char *code = read_whole_file(argv[1]);
if (code == NULL) {
return 1;
}
char *import = read_whole_file("stdlib/builtins.felan");
if (import == NULL) {
return 1;
}
char *lastCode = code;
code = a404m_malloc(strlen(code) + strlen(import) + 2);
strcpy(code, import);
strcat(code, "\n");
strcat(code, lastCode);
free(lastCode);
free(import);
bool ret = runner(code);
free(code);
if (ret) {
return 0;
} else {
return 1;
}
}
|