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

#include "astg.h"

void getBrackets(char *in, int bpos, char *out){ // gets the content of the brackets that open at bpos
	if (in[0] != '(' && in[0] != '[')
		out = NULL;

	char *input = malloc(strlen(in) + 1);
	char *Pinput = input;
	memcpy(input, in, strlen(in) + 1);

	int i = 0;
	while (i != bpos){
		input++;
		i++;
	}
	i = 0;
	int depth = 0;
	while (input[0] != '\0'){
		out[i] = input[0];
		if (input[0] == '(' || input[0] == '[')
			depth++;
		if (input[0] == ')' || input[0] == ']')
			depth--;
		if (depth == 0){
			i++;
			break;
		}

		input++;
		i++;
	}

	out[i] = '\0';
	free(Pinput);
}

int getContents(char *brackets){
	int i = 0;
	char *num = malloc(strlen(brackets));
	while (brackets[0] != '\0'){
		if (brackets[0] != '[' && brackets[0] != ']'){
			num[i] = brackets[0];
			i++;
		}
		brackets++;
	}
	num[i] = '\0';
	int out = atoi(num);
	free(num);
	return out;
}

ast_node *genAst(char *expression){
	ast_node *out = malloc(sizeof(ast_node));
	
	/* take the expression
	 * get the first operation from it
	 * get the first number from after the expression
	 * if that number is another expression{
	 * 	grab everything inside its braket and then call this function on it
	 * }
	 * get the second number from the expression
	 * if that number is another expression{
	 * 	grab everything inside its braket and then call this function on it
	 * }
	 * execute the output
	 */

	int i = 0, j = 0;
	int currentTokenVal = -1;

	char *currentToken = malloc(strlen(expression)+1);
	while (expression[i] != '\0'){
		if (expression[i] == '(' || expression[i] == '['){
			getBrackets(expression, i, currentToken);
			currentTokenVal++;
			
			int depth = 0;

			if (i != 0){
				while (expression[i] != '\0'){
					if (expression[i] == '(' || expression[i] == '[')
						depth++;
					if (expression[i] == ')' || expression[i] == ']')
						depth--;
					if (depth == 0){
						break;
					}
					i++;
				}
			}
			
			if (currentTokenVal == 0){
				switch (currentToken[1]) {
					case '+':
						out->operation= ADD;
						break;
					case '-':
						out->operation = SUB;
						break;
					case '*':
						out->operation = MUL;
						break;
					case '/':
						out->operation = DIV;
						break;
				}
			}

			if (currentTokenVal == 1){
				if (currentToken[0] == '(')
					out->left = genAst(currentToken);
				else {
					out->realLeft = getContents(currentToken);
					out->left = NULL;
				}
			} else if (currentTokenVal == 2){ 
				if (currentToken[0] == '(')
					out->right= genAst(currentToken);
				else {
					out->realRight = getContents(currentToken);
					out->right = NULL;
				}
			}
		} 
	i++;
	}
	free(currentToken);	

	return out;
}


int main(int argc, char **argv){
	if (argc < 2)
		exit(1);

	ast_node *head = genAst(argv[1]);

	printf("%d\n", exec(head));

	freeAst(head);
}