forked from HuoLanguage/huo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_array.c
51 lines (49 loc) · 2.03 KB
/
build_array.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
#include <stdlib.h>
#include <string.h>
#include "structures.h"
#include "constants.h"
void build_array(struct Value_array * array, struct Tokens * tokens){
while(tokens->tokens[tokens->counter].type != 'e'){
tokens->counter++;
if(tokens->tokens[tokens->counter].type == 's'){
struct Value * val = malloc(sizeof(struct Value));
val->type = 's';
val->data.str = string_copy_stack(&tokens->tokens[tokens->counter].data);
array->values[array->size] = val;
array->size++;
}
else if(tokens->tokens[tokens->counter].type == 'n'){
struct Value * val = malloc(sizeof(struct Value));
if(string_contains(dot_const, &tokens->tokens[tokens->counter].data)){
float content = atof(tokens->tokens[tokens->counter].data.body);
val->type='f';
val->data.fl=content;
} else {
long content = atol(tokens->tokens[tokens->counter].data.body);
val->type='l';
val->data.ln=content;
}
array->values[array->size] = val;
array->size++;
}
else if(tokens->tokens[tokens->counter].type == 'k'){
struct Value * val = malloc(sizeof(struct Value));
val->type = 'k';
val->data.str = string_copy_stack(&tokens->tokens[tokens->counter].data);
array->values[array->size] = val;
array->size++;
}
else if(tokens->tokens[tokens->counter].type == 'b'){
struct Value * val = malloc(sizeof(struct Value));
val->type = 'a';
val->data.array = malloc(sizeof(struct Value_array));
val->data.array->size = 0;
build_array(val->data.array, tokens);
array->values[array->size] = val;
array->size++;
// to advance beyond the inner closing bracket, otherwise it
// will cause the outer array to stop building
tokens->counter++;
}
}
}