forked from HuoLanguage/huo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
119 lines (113 loc) · 3.8 KB
/
parser.c
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "structures.h"
#include "constants.h"
#include "core_functions.h"
#include "build_array.h"
void parse(struct Tree * root, struct Tokens *tokens){
while(tokens->counter < tokens->length){
struct Token token = tokens->tokens[tokens->counter];
if(token.type == 'o'){
struct Tree * tree = malloc(sizeof(struct Tree));
tree->type = 'f';
tree->size = 0;
tokens->counter++;
parse(tree, tokens);
root->children[root->size] = tree;
root->size++;
}
else if(is_a_function(token.type)){
root->type = token.type;
struct String content = string_copy_stack(&token.data);
struct Value val = {
.type='k',
.data={
.str=content
}
};
root->content = val;
}
else if(token.type == 'b'){ //open bracket
struct Tree * value = malloc(sizeof(struct Tree));
value->type = 'a'; // a for array
value->size = 0;
struct Value content = {
.type='a',
};
content.data.array = malloc(sizeof(struct Value_array));
content.data.array->size = 0;
build_array(content.data.array, tokens);
value->content = content;
root->children[root->size] = value;
root->size++;
}
else if(token.type == 'k'){
// if we are inside a def call this could be name or args
if(root->type != 'f'){
// != 'f' means we already set this root node as some kind of function
struct Tree * value = malloc(sizeof(struct Tree));
value->type = token.type;
value->size = 0;
struct String content = string_copy_stack(&token.data);
struct Value val = {
.type='k',
.data={
.str=content
}
};
value->content = val;
root->children[root->size] = value;
root->size++;
} else {
root->type = token.type;
struct String content = string_copy_stack(&token.data);
struct Value val = {
.type='k',
.data={
.str=content
}
};
root->content = val;
}
}
else if(token.type == 's'){
struct Tree * value = malloc(sizeof(struct Tree));
value->type = token.type;
value->size = 0;
struct String content = string_copy_stack(&token.data);
struct Value val = {
.type='s',
.data={
.str=content
}
};
value->content = val;
root->children[root->size] = value;
root->size++;
}
else if(token.type == 'n'){
struct Tree * value = malloc(sizeof(struct Tree));
value->type = token.type;
value->size = 0;
struct Value val;
if(string_contains(dot_const, &token.data)){
float content = atof(token.data.body);
val.type='f';
val.data.fl=content;
} else {
long content = atol(token.data.body);
val.type='l';
val.data.ln=content;
}
value->content = val;
root->children[root->size] = value;
root->size++;
}
else if(token.type == 'c'){
return;
}
tokens->counter++;
}
return;
}