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

typedef struct ast_node ast_node;

typedef enum op {
	ADD = 0,
	SUB = 1,
	MUL = 2,
	DIV = 3,
} op;

typedef struct ast_node {
	op operation;
	int realLeft;
	int realRight;
	ast_node *right;
	ast_node *left;
} ast_node;

int exec(ast_node *exp){
	if (exp->left != NULL)
		exp->realLeft = exec(exp->left);
	if (exp->right != NULL)
		exp->realRight = exec(exp->right);

	if (exp->operation == ADD)
		return exp->realLeft+ exp->realRight;
	if (exp->operation == SUB)
		return exp->realLeft - exp->realRight;
	if (exp->operation == MUL)
		return exp->realLeft * exp->realRight;
	if (exp->operation == DIV)
		return exp->realLeft/ exp->realRight;
	return 0;
}

void freeAst(ast_node *head){
	if (head->left != NULL)
		freeAst(head->left);
	if (head->right != NULL)
		freeAst(head->left);
	free(head);
}