2016-07-03 22:50:42 -07:00
|
|
|
import symbol:*
|
|
|
|
|
import tree:*
|
2018-05-22 19:43:54 -04:00
|
|
|
import vec:*
|
2016-07-03 22:50:42 -07:00
|
|
|
import map:*
|
|
|
|
|
import util:*
|
2018-05-22 19:43:54 -04:00
|
|
|
import str:*
|
2016-07-03 22:50:42 -07:00
|
|
|
import mem:*
|
|
|
|
|
import io:*
|
|
|
|
|
import ast_nodes:*
|
|
|
|
|
import ast_transformation:*
|
|
|
|
|
import interpreter:*
|
2017-01-22 10:13:06 -05:00
|
|
|
import hash_set:*
|
2016-07-03 22:50:42 -07:00
|
|
|
|
|
|
|
|
import pass_common:*
|
|
|
|
|
|
2018-05-22 19:43:54 -04:00
|
|
|
fun ctce_lower(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
|
2017-01-22 10:13:06 -05:00
|
|
|
var visited = hash_set<*ast_node>()
|
2016-07-06 00:16:39 -07:00
|
|
|
var globals = setup_globals(*name_ast_map)
|
2018-05-22 19:43:54 -04:00
|
|
|
var ctce_passes = vec<*ast_node>()
|
|
|
|
|
name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
|
2016-07-03 22:50:42 -07:00
|
|
|
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
|
|
|
|
|
match(*node) {
|
|
|
|
|
ast_node::compiler_intrinsic(backing) {
|
|
|
|
|
if (backing.intrinsic == "ctce") {
|
2016-07-06 00:16:39 -07:00
|
|
|
var result = evaluate_with_globals(backing.parameters[0], &globals)
|
2016-07-03 22:50:42 -07:00
|
|
|
*node = *unwrap_value(result)
|
2016-07-09 15:08:57 -07:00
|
|
|
} else if (backing.intrinsic == "ctce_pass") {
|
|
|
|
|
ctce_passes.add(backing.parameters[0])
|
|
|
|
|
remove(node, parent_chain)
|
2016-07-03 22:50:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-12 23:52:12 -04:00
|
|
|
run_on_tree(helper_before, empty_pass_second_half(), syntax_ast_pair.second, &visited)
|
2016-07-03 22:50:42 -07:00
|
|
|
})
|
2016-07-09 15:08:57 -07:00
|
|
|
ctce_passes.for_each(fun(func: *ast_node) {
|
|
|
|
|
// don't want to pick up the ast_node::value
|
2018-05-22 19:43:54 -04:00
|
|
|
var params = vec<interpreter::value>()
|
2016-07-09 15:08:57 -07:00
|
|
|
// easier to pick up types from the function itself
|
2018-05-22 19:43:54 -04:00
|
|
|
if (!is_function(func)) error(str("trying to CTCE pass with non function") + get_ast_name(func))
|
2016-07-09 15:33:15 -07:00
|
|
|
params.add(interpreter::value::pointer(make_pair((name_ast_map) cast *void, func->function.type->parameter_types[0])))
|
|
|
|
|
params.add(interpreter::value::pointer(make_pair((ast_to_syntax) cast *void, func->function.type->parameter_types[1])))
|
|
|
|
|
call_function(func, params, &globals)
|
2016-07-09 15:08:57 -07:00
|
|
|
})
|
2016-07-03 22:50:42 -07:00
|
|
|
}
|
|
|
|
|
|