forked from HuoLanguage/huo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.c
133 lines (130 loc) · 5.57 KB
/
execute.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include "structures.h"
#include "core_functions.h"
#include "base_util.h"
#include "process_defs.h"
#include "constants.h"
#include "execute.h"
#include "execution_functions/for_each.h"
#include "execution_functions/for_loop.h"
#include "execution_functions/reduce.h"
#include "execution_functions/read_file.h"
#include "execution_functions/map_array.h"
#include "execution_functions/let_binding.h"
#include "execution_functions/reduce_ast.h"
#include "execution_functions/if_block.h"
#include "execution_functions/switch.h"
#include "execution_functions/parallel_execution.h"
#include "apply_core_function.h"
struct Value execute (struct Tree * ast, struct Tree_map * defined, struct Map * let_map){
struct Value result;
// first check for special kinds of execution
if(ast->type == 'k' && string_matches(&ast->content.data.str, &if_const)){
return if_block(ast, defined, let_map);
}
else if(ast->type == 'k' && string_matches(&let_const, &ast->content.data.str)){
store_let_binding(ast, defined, let_map);
result.type = 'u';
}
else if(ast->type == 'k' && string_matches(&each_const, &ast->content.data.str)){
for_each(ast, defined, let_map);
result.type = 'u';
}
else if(ast->type == 'k' && string_matches(&map_const, &ast->content.data.str)){
return map_array(ast, defined, let_map);
}
else if(ast->type == 'k' && string_matches(&reduce_const, &ast->content.data.str)){
return reduce_array(ast, defined, let_map);
}
else if(ast->type == 'k' && string_matches(&set_const, &ast->content.data.str)){
struct Value index = execute(ast->children[0], defined, let_map);
struct Value item = execute(ast->children[1], defined, let_map);
struct Value array = execute(ast->children[2], defined, let_map);
result = array_set(index, item, array);
return result;
}
else if(ast->type == 'k' && string_matches(&for_const, &ast->content.data.str)){
for_loop(ast, defined, let_map);
result.type = 'u'; //return undefined
}
else if(ast->type == 'k' && string_matches(&do_const, &ast->content.data.str)){
for(int i = 0; i < ast->size; i++){
if(i == ast->size-1){
result = execute(ast->children[i], defined, let_map);
} else {
execute(ast->children[i], defined, let_map);
}
}
}
else if(ast->type == 'k' && string_matches(&read_const, &ast->content.data.str)){
return read_file(ast->children[0]->content.data.str);
}
else if(ast->type == 'k' && string_matches(&substring_const, &ast->content.data.str)){
struct Value string = execute(ast->children[2], defined, let_map);
struct Value start = execute(ast->children[0], defined, let_map);
struct Value end = execute(ast->children[1], defined, let_map);
if(string.type != 's'){
ERROR("Non-string value passed into substring: %c.", string.type);
result.type = 'u';
return result;
} else {
return substring(start.data.ln, end.data.ln, string);
}
}
else if(ast->type == 'k' && string_matches(&switch_const, &ast->content.data.str)){
return switch_case(ast, defined, let_map);
}
else if(ast->type == 'k' && string_matches(¶llel_const, &ast->content.data.str)){
parallel_execution(ast, defined, let_map);
result.type = 'u';
} else {
// no special execution types found, check for more basic conditions
int idx;
if(!ast->size){
// ast with no children is either a value or a variable
if(ast->type == 'k'){
for(int i = 0; i < let_map->size; i++){
if(string_matches(&let_map->members[i]->key->data.str, &ast->content.data.str)){
return *let_map->members[i]->val;
}
}
ERROR("Undefined variable: %s", ast->content.data.str.body);
} else {
return ast->content;
}
}
else if(ast->type == 'k' && (idx = is_defined_func(defined, ast->content.data.str)) > -1){
return execute_defined_func(ast, defined, let_map, idx);
}
else if(ast->size == 1){
struct Value a = execute(ast->children[0], defined, let_map);
if(ast->type == 'k'){
if(string_matches(&ast->content.data.str, &print_const)){
print(a);
printf("\n");
result.type = 'u';
}
else if(string_matches(&ast->content.data.str, &length_const)){
return length(a);
}
else if(string_matches(&ast->content.data.str, &return_const)){
return execute(ast->children[0], defined, let_map);
}
}
}
else if(ast->size == 2) {
struct Value a = execute(ast->children[0], defined, let_map);
struct Value b = execute(ast->children[1], defined, let_map);
result = apply_core_function(ast, a, b);
} else {
result = reduce_ast(ast, defined, let_map);
}
}
return result;
}
struct Value execute_defined_func(struct Tree * ast, struct Tree_map * defined, struct Map * let_map, int idx){
struct Map * arguments = make_args_map(ast, defined, let_map, idx);
struct Tree * populated_ast = populate_args(arguments, duplicate_tree(get_defined_body(defined->trees[idx])));
return execute(populated_ast, defined, let_map);
}