summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code/execution/builtin.c
blob: 12441cd6c2bd8b97a1e89312f86627f8468eb26e (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
#include <stdlib.h>
#include <stdio.h>
#include "../global/types.h"
#include "../global/util.h"

#define MAXARGS 8

void *doCall(ast_node *node){
	builtInFuncs id = node->func->builtInFunc;
	for (int i = 0; i < MAXARGS; i++){
		if (node->args[i] != NULL){
			node->literalArgs[i] = doCall(node->args[i]);
		}
	}

	literal *out = CheckedMalloc(sizeof(literal));
	switch (id){
		case ADD:
    			out->i64 = CheckedMalloc(sizeof(I64));
			out->i64->data = node->literalArgs[0]->i64->data + node->literalArgs[1]->i64->data;
			return out;
			break;
			
	       	case SUB:
    			out->i64 = CheckedMalloc(sizeof(I64));
			out->i64->data = node->literalArgs[0]->i64->data - node->literalArgs[1]->i64->data;
			return out;
			break;
		case DIV:
    			out->i64 = CheckedMalloc(sizeof(I64));
			out->i64->data = node->literalArgs[0]->i64->data / node->literalArgs[1]->i64->data;
			return out;
			break;
		case MUL:
    			out->i64 = CheckedMalloc(sizeof(I64));
			out->i64->data = node->literalArgs[0]->i64->data * node->literalArgs[1]->i64->data;
			return out;
			break;

		case WRITE:
			for (int i = 0; i < node->literalArgs[0]->arr->len; i++)
				fputc(node->literalArgs[0]->arr->arr[i].ch->data, stdout);
			break;
		
		case EXIT:
			int returnValue = (int)node->literalArgs[0]->i64->data;
			exit(returnValue);
			break;
	}
}