summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c
blob: 8e455313e541cb7e64bc996a8221200ef047a3c2 (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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../global/types.h"
#include "../global/util.h"

#define MAXARGS 8
#define MAXFUNCS 2048
#define MAXVARS 8192

int getBuiltIn(char *func, ast_node *node); // checks if a function is built in to zippy
void expressFunction(char *function, ast_node *node); // puts a string into the ast_node struct
ast_node *tokenize(char *input); // does the tokenization
void printAst(ast_node *root); // shows an ast and its sub nodes

int getBuiltIn(char *func, ast_node *node){ // returns NIL when the function doesn't exist
	if (strcmp(func, "defun") == 0){
		node->func->builtInFunc= DEFUN;
	}else if (strcmp(func, "let") == 0){
		node->func->builtInFunc = LET;
	}else if (strcmp(func, "set") == 0){
		node->func->builtInFunc = SET;
	}else if (strcmp(func, "if") == 0){
		node->func->builtInFunc = IF;
	}else if (strcmp(func, "elif") == 0){
		node->func->builtInFunc = ELIF;
	}else if (strcmp(func, "else") == 0){
		node->func->builtInFunc = ELSE;
	}else if (strcmp(func, "for") == 0){
		node->func->builtInFunc = FOR;
	}else if (strcmp(func, "while") == 0){
		node->func->builtInFunc = WHILE;
	}else if (strcmp(func, "symbol") == 0){
		node->func->builtInFunc = SYMBOL;
	}else if (strcmp(func, "+") == 0){
		node->func->builtInFunc = ADD;
	}else if (strcmp(func, "-") == 0){
		node->func->builtInFunc = SUB;
	}else if (strcmp(func, "*") == 0){
		node->func->builtInFunc = MUL;
	}else if (strcmp(func, "/") == 0){
		node->func->builtInFunc = DIV;
	}else if (strcmp(func, "=") == 0){
		node->func->builtInFunc = EQ;
	}else if (strcmp(func, "!=") == 0){
		node->func->builtInFunc = NEQ;
	}else if (strcmp(func, ">") == 0){
		node->func->builtInFunc = GT;
	}else if (strcmp(func, "<") == 0){
		node->func->builtInFunc = LT;
	}else if (strcmp(func, ">=") == 0){
		node->func->builtInFunc = GTEQ;
	}else if (strcmp(func, "<=") == 0){
		node->func->builtInFunc = LTEQ;
	}else if (strcmp(func, "cast") == 0){
		node->func->builtInFunc = CAST;
	}else if (strcmp(func, "typeof") == 0){
		node->func->builtInFunc = TYPEOF;
	}else if (strcmp(func, "exit") == 0){
		node->func->builtInFunc = EXIT;
	}else if (strcmp(func, "return") == 0){
		node->func->builtInFunc = RETURN;
	}else if (strcmp(func, "write") == 0){
		node->func->builtInFunc = WRITE;
	}else {
		node->func->builtInFunc = NIL;
		return -1;
	}
	return 0;
}

void expressFunction(char *function, ast_node *node){
	node->func = CheckedMalloc(sizeof(functionToken));
	if ((getBuiltIn(function, node)) == NIL) // non user defined function
		node->func->name = function;
}

ast_node *tokenize(char *input){
	ast_node *node, *child;

	char *exp, *function, **args;
	size_t i = 0, argCount = -1;
	int depth = 0;

	node = CheckedMalloc(sizeof(ast_node));
	node->args = CheckedMalloc(sizeof(ast_node) * MAXARGS);
	node->literalArgs = CheckedMalloc(sizeof(void *) * MAXARGS);

	if (input[i] == '('){
		depth = 1;
		i++;
		exp = CheckedMalloc(strlen(input));
		while (depth != 0){
			if (input[i] == ' ') argCount++;
			if (input[i] == '('){
				child = tokenize(&input[i]);
				node->args[argCount] = child;
				depth++;
			} else if (input[i] == ')'){
				depth--;
			}
			exp[i - 1] = input[i];
			if (input[i] == '\0'){
				fprintf(stderr, "error brace not closed\n");
				exit(1);
			}
			i++;
		}
		exp[i-2] = '\0';
		exp = CheckedRealloc(exp, strlen(exp) + 1);
	}else if (input[i] == '"'){
		i++;
		while (input[i] != '"') i++;
	}


	i = 0;
	function = CheckedMalloc(strlen(exp));
	while (exp[i] != ' '){
		function[i] = exp[i];
		i++;	
	}

	function[i] = '\0';
	function = CheckedRealloc(function, i);

	expressFunction(function, node);

	char *tok, *saveptr, *expptr = exp;
	
	exp = strstr(exp, " ");
	tok = strtok_r(exp, " ", &saveptr);

	argCount = 0;
	depth = 0;
	do {
		if (node->args[argCount] != NULL){
			argCount++;
		}
		if (tok[0] != '(' && tok[strlen(tok)-1] != ')' && depth == 0){
			if (node->args[argCount] == NULL){
				node->literalArgs[argCount] = giveType(tok);
			}
			argCount++;
		}
		
		if (tok[0] == '(') depth++;
		if (tok[strlen(tok)-1] == ')') depth--;
		tok = strtok_r(NULL, " ", &saveptr);
	} while (tok != NULL);

	free(expptr);

	return node;
}

void printAst(ast_node *root){
	printf("-----------\n");
	if (root->func->builtInFunc == -1) printf("function: %s\n", root->func->name);
	else printf("function (built in): %d\n", root->func->builtInFunc);
	for (int i = 0; i < MAXARGS + 1; i++){
		if (root->args[i] != NULL) printAst(root->args[i]);
		else {
			if (root->literalArgs[i] != NULL) printf("%s\n", root->literalArgs[i]);
		}
	}
	printf("-----------\n");
}