summaryrefslogtreecommitdiff
path: root/parse.c
blob: 94623a59abb18ac5b89f2e47b5deb14e21a1276a (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "types.h"

parsetypes prevtype = NILPARSETYPE;

ast *genast(lexobj lexes[4096], int *lexcount) {
	static ast a;
	switch (prevtype) {
		case NILPARSETYPE:
		case FUNCEND:
			a.function = lexes[*lexcount + 1].data; // function name
			a.litteralchildren[0] = lexes[*lexcount].data; // function ret type
			*lexcount += 2;
			a.childcount++;
			while (lexes[*lexcount].t != CLOSEBRACE) {
				a.litteralchildren[a.childcount] = lexes[*lexcount].data;
				*lexcount += 1;
				a.childcount++;
			}
			*lexcount += 1; // move the lexptr to the start of the functions code
			prevtype = FUNCTIONDEF;
			return &a;
		case FUNCTIONDEF:
			a.function = lexes[*lexcount].data;
			*lexcount += 1;
			if (lexes[*lexcount].t == INTLIT) {
				a.litteralchildren[0] = lexes[*lexcount].data;
				*lexcount += 1;
				return &a;
			}
			else {
				a.children[0] = genast(lexes, lexcount);
			}
	}
	return &a;
}