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

#include "vars.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 LET:
			newVar(node->literalArgs[0]->vdef, node->literalArgs[1]);
			break;
		
		case EXIT:
			exit((int)node->literalArgs[0]->i64->data);
			break;

		default:
			fprintf(stderr, "command not implemented");
			exit(1);
			break;
	}
}