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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "parse.h"
#include "hashtable.h"
sym_t* init_sym(prov_t province, litType_t litType, void* name) {
sym_t* symbol = malloc(sizeof(sym_t));
symbol->province = province;
symbol->litType = litType;
symbol->name = name;
symbol->argCount = 0;
return symbol;
}
sym_t* init_sym_key(char* value) {
char* vp = malloc((strlen(value) + 1) * sizeof(char));
strcpy(vp, value);
return init_sym(KEY_REF, NO_TYPE, vp);
}
sym_t* init_sym_int(int value) {
int* vp = malloc(sizeof(int));
*vp = value;
return init_sym(LITERAL, INT, vp);
}
sym_t* init_sym_str(char* value) {
char* vp = malloc((strlen(value) + 1) * sizeof(char));
strcpy(vp, value);
return init_sym(LITERAL, STRING, vp);
}
void destroy_sym(sym_t* symbol) {
free(symbol->name);
if (symbol->argCount > 0) {
free(symbol->args);
}
free(symbol);
}
void destroy_sym_tree(sym_t* origin) {
if (origin->province == KEY_REF) {
/* doesn't follow user-defined refs */
if (is_internal((char*)origin->name)) {
for (int i = 0; i < origin->argCount; i++) {
destroy_sym_tree(origin->args[i]);
}
destroy_sym(origin);
}
}
else if (origin->province == LITERAL) {
destroy_sym(origin);
}
}
int symtoi(sym_t* symbol) {
return *((int*)symbol->name);
}
float symtof(sym_t* symbol) {
return *((float*)symbol->name);
}
char* symtos(sym_t* symbol) {
return (char*)symbol->name;
}
void symbol_add_arg(sym_t* symbol, sym_t* arg) {
symbol->argCount += 1;
if (symbol->argCount == 1) {
symbol->args = malloc(sizeof(sym_t*));
}
else {
symbol->args = realloc(symbol->args, symbol->argCount * (sizeof(sym_t*)));
}
(symbol->args)[symbol->argCount - 1] = arg;
}
void symset_add_symbol(symset_t* symbolSet, sym_t* symbol) {
symbolSet->count += 1;
if (symbolSet->count == 1) {
symbolSet->symbols = malloc(sizeof(sym_t*));
}
else {
symbolSet->symbols = realloc(symbolSet->symbols, symbolSet->count * (sizeof(sym_t*)));
}
(symbolSet->symbols)[symbolSet->count - 1] = symbol;
}
symset_t* parse_symbols(char** str) {
symset_t* symbolSet = malloc(sizeof(symset_t));
symbolSet->count = 0;
while (1) {
/* clear whitespace */
while (!isalnum(**str) && **str != '(' && **str != ')' && **str != '\0') {
(*str)++;
}
if (**str == ')') {
(*str)++;
return symbolSet;
}
else if (**str == '\0') {
return symbolSet;
}
char* symbolName = calloc(30, sizeof(char));
int i = 0;
while (1) {
symbolName[i] = **str;
if (**str == ',' || **str == ';') {
(*str)++;
symbolName[i] = '\0';
sym_t* symbol;
/* TODO: float */
/* string */
/* TODO: more frees in here */
if (isalpha(symbolName[0])) {
//TODO: realloc this
symbol = init_sym_str(symbolName);
}
/* int */
else {
int v = atoi(symbolName);
symbol = init_sym_int(v);
}
symset_add_symbol(symbolSet, symbol);
break;
}
else if (**str == '(') {
(*str)++;
symbolName[i] = '\0';
sym_t* symbol;
if (i > 1) {
symbol = init_sym_key(symbolName);
}
else {
symbol = init_sym_key("root");
}
symset_t* symbols = parse_symbols(str);
for (int s = 0; s < symbols->count; s++) {
symbol_add_arg(symbol, symbols->symbols[s]);
}
symset_add_symbol(symbolSet, symbol);
break;
}
i++;
(*str)++;
}
free(symbolName);
}
}
sym_t* parse_symbol_final(char* str) {
char** strp = malloc(sizeof(char**));
*strp = str;
symset_t* finalSymset = parse_symbols(strp);
free(strp);
sym_t* root = init_sym_key("root");
for (int s = 0; s < finalSymset->count; s++) {
symbol_add_arg(root, finalSymset->symbols[s]);
}
return root;
}
/* TODO: improve this system */
int is_internal(char* name) {
unsigned int hash = get_hash(name);
if (hash == get_hash("root") || hash == get_hash("store")
|| hash == get_hash("eq") || hash == get_hash("if")
|| hash == get_hash("not") || hash == get_hash("sum")
|| hash == get_hash("print")) {
return 1;
}
return 0;
}
void write_symbol_visual(sym_t* symbol, int offset) {
for (int o = 0; o < offset; o++) {
printf(" ");
}
if (symbol->province == KEY_REF) {
printf("%s (%s)\n", (char*)(symbol->name), "KEY");
}
else if (symbol->province == LITERAL) {
if (symbol->litType == INT) {
printf("%d (%s)\n", *(int*)(symbol->name), "LITERAL");
}
else if (symbol->litType == FLOAT) {
printf("%f (%s)\n", *(float*)(symbol->name), "LITERAL");
}
else if (symbol->litType == STRING) {
printf("%s (%s)\n", (char*)(symbol->name), "LITERAL");
}
}
else {
printf("symbol parse error\n");
}
for (int i = 0; i < symbol->argCount; i++) {
write_symbol_visual(symbol->args[i], offset + 4);
}
}
|