2016-01-04 00:38:59 -05:00
|
|
|
import io:*
|
|
|
|
|
import mem:*
|
|
|
|
|
import map:*
|
2016-01-25 13:48:27 -05:00
|
|
|
import stack:*
|
2016-01-04 00:38:59 -05:00
|
|
|
import string:*
|
|
|
|
|
import util:*
|
|
|
|
|
import tree:*
|
|
|
|
|
import symbol:*
|
|
|
|
|
import ast_nodes:*
|
2016-01-20 13:50:40 -05:00
|
|
|
import poset:*
|
2016-01-29 22:46:09 -05:00
|
|
|
// we import ast_transformation for its make_method_call function
|
|
|
|
|
import ast_transformation:*
|
2016-01-04 00:38:59 -05:00
|
|
|
|
2016-01-27 18:56:44 -05:00
|
|
|
fun code_triple(): code_triple return code_triple(string(), string(), string());
|
|
|
|
|
fun code_triple(only: *char): code_triple return code_triple(string(), string(only), string());
|
|
|
|
|
fun code_triple(only: string): code_triple return code_triple(string(), only, string());
|
|
|
|
|
fun code_triple(first: string, second: string, third: string): code_triple {
|
|
|
|
|
var to_ret.construct(first, second, third): code_triple
|
|
|
|
|
return to_ret
|
|
|
|
|
}
|
|
|
|
|
obj code_triple (Object) {
|
|
|
|
|
var pre: string
|
|
|
|
|
var value: string
|
|
|
|
|
var post: string
|
|
|
|
|
fun construct(first: string, second: string, third: string): *code_triple {
|
|
|
|
|
pre.copy_construct(&first)
|
|
|
|
|
value.copy_construct(&second)
|
|
|
|
|
post.copy_construct(&third)
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
fun copy_construct(old: *code_triple) {
|
|
|
|
|
pre.copy_construct(&old->pre)
|
|
|
|
|
value.copy_construct(&old->value)
|
|
|
|
|
post.copy_construct(&old->post)
|
|
|
|
|
}
|
|
|
|
|
fun operator=(other: code_triple) {
|
|
|
|
|
destruct()
|
|
|
|
|
copy_construct(&other)
|
|
|
|
|
}
|
|
|
|
|
fun destruct() {
|
|
|
|
|
pre.destruct()
|
|
|
|
|
value.destruct()
|
|
|
|
|
post.destruct()
|
|
|
|
|
}
|
|
|
|
|
fun operator==(other: code_triple): bool return pre == other.pre && value == other.value && post == other.value;
|
|
|
|
|
fun operator==(other: string): bool return (pre + value + post) == other;
|
|
|
|
|
fun operator==(other: *char): bool return (pre + value + post) == other;
|
|
|
|
|
fun operator!=(other: code_triple): bool return pre != other.pre || value != other.value || post != other.value;
|
|
|
|
|
fun operator!=(other: string): bool return (pre + value + post) != other;
|
|
|
|
|
fun operator!=(other: *char): bool return (pre + value + post) != other;
|
|
|
|
|
fun operator+(other: code_triple): code_triple {
|
|
|
|
|
return code_triple(pre+other.pre, value+other.value, other.post+post);
|
|
|
|
|
}
|
|
|
|
|
fun operator+(other: string): code_triple {
|
|
|
|
|
return code_triple(pre, value+other, post);
|
|
|
|
|
}
|
|
|
|
|
fun operator+(other: *char): code_triple {
|
|
|
|
|
return code_triple(pre, value + other, post);
|
|
|
|
|
}
|
|
|
|
|
fun operator+=(other: string) {
|
|
|
|
|
value += other
|
|
|
|
|
}
|
|
|
|
|
fun operator+=(other: *char) {
|
|
|
|
|
value += other
|
|
|
|
|
}
|
|
|
|
|
fun operator+=(other: code_triple) {
|
|
|
|
|
pre += other.pre
|
|
|
|
|
value += other.value
|
|
|
|
|
post += other.post
|
|
|
|
|
}
|
|
|
|
|
fun one_string():string {
|
|
|
|
|
return pre+value+post
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-04 00:38:59 -05:00
|
|
|
|
|
|
|
|
obj c_generator (Object) {
|
2016-01-31 19:29:08 -05:00
|
|
|
var id_counter: int
|
2016-02-17 13:37:48 -05:00
|
|
|
var ast_name_map: map<*ast_node, string>
|
2016-02-22 16:18:55 -05:00
|
|
|
var closure_struct_map: map<set<*ast_node>, string>
|
2016-02-24 01:54:20 -05:00
|
|
|
var function_type_map: map<type, string>
|
2016-02-20 21:02:41 -05:00
|
|
|
var function_typedef_string: string
|
2016-02-22 16:18:55 -05:00
|
|
|
var closure_struct_definitions: string
|
2016-01-04 00:38:59 -05:00
|
|
|
fun construct(): *c_generator {
|
2016-01-31 19:29:08 -05:00
|
|
|
id_counter = 0
|
2016-02-17 13:37:48 -05:00
|
|
|
ast_name_map.construct()
|
2016-02-22 16:18:55 -05:00
|
|
|
closure_struct_map.construct()
|
2016-02-24 01:54:20 -05:00
|
|
|
function_type_map.construct()
|
2016-02-22 16:18:55 -05:00
|
|
|
function_typedef_string.construct()
|
|
|
|
|
closure_struct_definitions.construct()
|
2016-01-04 00:38:59 -05:00
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
fun copy_construct(old: *c_generator) {
|
2016-01-31 19:29:08 -05:00
|
|
|
id_counter = old->id_counter
|
2016-02-17 13:37:48 -05:00
|
|
|
ast_name_map.copy_construct(&old->ast_name_map)
|
2016-02-24 01:54:20 -05:00
|
|
|
closure_struct_map.copy_construct(&old->closure_struct_map)
|
|
|
|
|
function_type_map.copy_construct(&old->function_type_map)
|
|
|
|
|
function_typedef_string.copy_construct(&old->function_typedef_string)
|
|
|
|
|
closure_struct_definitions.copy_construct(&old->closure_struct_definitions)
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
|
|
|
|
fun operator=(other: ref c_generator) {
|
|
|
|
|
destruct()
|
|
|
|
|
copy_construct(&other)
|
|
|
|
|
}
|
|
|
|
|
fun destruct() {
|
2016-02-17 13:37:48 -05:00
|
|
|
ast_name_map.destruct()
|
2016-02-24 01:54:20 -05:00
|
|
|
closure_struct_map.destruct()
|
|
|
|
|
function_type_map.destruct()
|
|
|
|
|
function_typedef_string.destruct()
|
|
|
|
|
closure_struct_definitions.destruct()
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
2016-02-07 16:22:55 -05:00
|
|
|
fun get_id(): string return to_string(id_counter++);
|
2016-01-04 02:00:06 -05:00
|
|
|
fun generate_c(name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>): pair<string,string> {
|
2016-01-04 00:38:59 -05:00
|
|
|
var linker_string:string = ""
|
|
|
|
|
var prequal: string = "#include <stdbool.h>\n#include <stdlib.h>\n#include <stdio.h>\n"
|
2016-01-21 03:18:02 -05:00
|
|
|
var plain_typedefs: string = "\n/**Plain Typedefs**/\n"
|
2016-01-04 00:38:59 -05:00
|
|
|
var top_level_c_passthrough: string = ""
|
|
|
|
|
var variable_extern_declarations: string = ""
|
2016-01-20 13:50:40 -05:00
|
|
|
var structs: string = "\n/**Type Structs**/\n"
|
2016-02-22 16:18:55 -05:00
|
|
|
function_typedef_string = "\n/**Typedefs**/\n"
|
|
|
|
|
closure_struct_definitions = "\n/**Closure Struct Definitions**/\n"
|
2016-01-06 02:46:42 -05:00
|
|
|
var function_prototypes: string = "\n/**Function Prototypes**/\n"
|
|
|
|
|
var function_definitions: string = "\n/**Function Definitions**/\n"
|
|
|
|
|
var variable_declarations: string = "\n/**Variable Declarations**/\n"
|
2016-01-04 00:38:59 -05:00
|
|
|
|
2016-01-21 12:54:21 -05:00
|
|
|
// moved out from below so that it can be used for methods as well as regular functions (and eventually lambdas...)
|
2016-01-23 05:33:56 -05:00
|
|
|
var generate_function_definition = fun(child: *ast_node, enclosing_object: *ast_node) {
|
2016-01-21 12:54:21 -05:00
|
|
|
var backing = child->function
|
|
|
|
|
|
|
|
|
|
var parameter_types = string()
|
|
|
|
|
var parameters = string()
|
2016-01-23 05:33:56 -05:00
|
|
|
if (enclosing_object) {
|
|
|
|
|
parameter_types = type_to_c(enclosing_object->type_def.self_type) + "*"
|
|
|
|
|
parameters = type_to_c(enclosing_object->type_def.self_type) + "* this"
|
|
|
|
|
}
|
2016-02-22 16:18:55 -05:00
|
|
|
if (backing.closed_variables.size()) {
|
|
|
|
|
println("HAS CLOSED VARIABLES")
|
|
|
|
|
if (parameter_types != "") { parameter_types += ", "; parameters += ", ";}
|
|
|
|
|
var closed_type_name = get_closure_struct_type(backing.closed_variables)
|
|
|
|
|
parameter_types += closed_type_name + "*"
|
|
|
|
|
parameters += closed_type_name + "* closure_data"
|
|
|
|
|
}
|
2016-01-31 19:29:08 -05:00
|
|
|
|
|
|
|
|
// stack-stack thing // this could be a stack of strings too, maybe
|
|
|
|
|
// start out with one stack on the stack
|
|
|
|
|
var defer_stack = stack<pair<bool,stack<*ast_node>>>(make_pair(false, stack<*ast_node>()))
|
|
|
|
|
|
2016-02-24 01:54:20 -05:00
|
|
|
var decorated_name = generate_function(child, false).one_string()
|
2016-01-21 12:54:21 -05:00
|
|
|
backing.parameters.for_each(fun(parameter: *ast_node) {
|
|
|
|
|
if (parameter_types != "") { parameter_types += ", "; parameters += ", ";}
|
|
|
|
|
parameter_types += type_to_c(parameter->identifier.type)
|
2016-02-17 13:37:48 -05:00
|
|
|
parameters += type_to_c(parameter->identifier.type) + " " + get_name(parameter)
|
2016-01-31 19:29:08 -05:00
|
|
|
|
|
|
|
|
// add parameters to destructor thingy (for returns)? Or should that be a different pass?
|
|
|
|
|
var parameter_type = parameter->identifier.type
|
|
|
|
|
if (parameter_type->indirection == 0 && parameter_type->is_object() && has_method(parameter_type->type_def, "destruct", vector<*type>()))
|
|
|
|
|
defer_stack.top().second.push(ast_statement_ptr(make_method_call(parameter, "destruct", vector<*ast_node>())))
|
2016-01-21 12:54:21 -05:00
|
|
|
})
|
|
|
|
|
function_prototypes += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameter_types + ");\n"
|
2016-02-24 01:54:20 -05:00
|
|
|
function_definitions += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameters + ") {\n" + generate_statement(backing.body_statement, enclosing_object, child, &defer_stack).one_string()
|
2016-01-21 12:54:21 -05:00
|
|
|
// emit parameter destructors?
|
2016-02-24 01:54:20 -05:00
|
|
|
function_definitions += generate_from_defer_stack(&defer_stack, -1, enclosing_object, child).one_string()
|
2016-01-21 12:54:21 -05:00
|
|
|
function_definitions += "}\n"
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 13:50:40 -05:00
|
|
|
var type_poset = poset<*ast_node>()
|
2016-01-04 00:38:59 -05:00
|
|
|
// iterate through asts
|
|
|
|
|
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) {
|
|
|
|
|
// iterate through children for each ast
|
|
|
|
|
// assert translation_unit?
|
2016-02-21 17:22:16 -05:00
|
|
|
(tree_pair.second->translation_unit.children + tree_pair.second->translation_unit.lambdas).for_each(fun(child: *ast_node) {
|
2016-01-04 00:38:59 -05:00
|
|
|
match (*child) {
|
2016-01-04 02:00:06 -05:00
|
|
|
// should really check the genrator
|
|
|
|
|
ast_node::if_comp(backing) {
|
|
|
|
|
if (is_simple_passthrough(backing.statement->statement.child))
|
2016-02-20 02:36:35 -05:00
|
|
|
top_level_c_passthrough += generate_simple_passthrough(backing.statement->statement.child, true)
|
2016-01-04 02:00:06 -05:00
|
|
|
}
|
2016-02-20 02:36:35 -05:00
|
|
|
ast_node::simple_passthrough(backing) top_level_c_passthrough += generate_simple_passthrough(child, true)
|
2016-02-24 01:54:20 -05:00
|
|
|
ast_node::declaration_statement(backing) variable_declarations += generate_declaration_statement(child, null<ast_node>(), null<ast_node>(), null<stack<pair<bool,stack<*ast_node>>>>(), true).one_string() + ";\n"
|
2016-01-06 02:46:42 -05:00
|
|
|
ast_node::function(backing) {
|
|
|
|
|
// check for and add to parameters if a closure
|
2016-01-23 05:33:56 -05:00
|
|
|
generate_function_definition(child, null<ast_node>())
|
2016-01-06 02:46:42 -05:00
|
|
|
}
|
2016-02-13 16:56:37 -05:00
|
|
|
ast_node::template(backing) {
|
2016-02-20 02:36:35 -05:00
|
|
|
backing.instantiated.for_each(fun(node: *ast_node) {
|
|
|
|
|
match (*node) {
|
|
|
|
|
ast_node::function(backing) generate_function_definition(node, null<ast_node>())
|
|
|
|
|
ast_node::type_def(backing) {
|
|
|
|
|
type_poset.add_vertex(node)
|
|
|
|
|
backing.variables.for_each(fun(i: *ast_node) {
|
|
|
|
|
var var_type = get_ast_type(i->declaration_statement.identifier)
|
|
|
|
|
if (!var_type->indirection && var_type->type_def)
|
|
|
|
|
type_poset.add_relationship(node, var_type->type_def)
|
|
|
|
|
})
|
2016-02-13 16:56:37 -05:00
|
|
|
}
|
2016-02-20 02:36:35 -05:00
|
|
|
}
|
2016-02-01 05:35:08 -05:00
|
|
|
})
|
|
|
|
|
}
|
2016-01-20 13:50:40 -05:00
|
|
|
ast_node::type_def(backing) {
|
|
|
|
|
type_poset.add_vertex(child)
|
2016-02-07 16:22:55 -05:00
|
|
|
backing.variables.for_each(fun(i: *ast_node) {
|
|
|
|
|
var var_type = get_ast_type(i->declaration_statement.identifier)
|
|
|
|
|
if (!var_type->indirection && var_type->type_def)
|
|
|
|
|
type_poset.add_relationship(child, var_type->type_def)
|
|
|
|
|
})
|
2016-01-20 13:50:40 -05:00
|
|
|
}
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
2016-01-20 13:50:40 -05:00
|
|
|
type_poset.get_sorted().for_each(fun(vert: *ast_node) {
|
2016-02-15 16:31:01 -05:00
|
|
|
/*var base_name = vert->type_def.name*/
|
|
|
|
|
var base_name = get_name(vert)
|
|
|
|
|
plain_typedefs += string("typedef struct ") + base_name + "_dummy " + base_name + ";\n"
|
|
|
|
|
structs += string("struct ") + base_name + "_dummy {\n"
|
2016-02-24 01:54:20 -05:00
|
|
|
vert->type_def.variables.for_each(fun(variable_declaration: *ast_node) structs += generate_declaration_statement(variable_declaration, null<ast_node>(), null<ast_node>(), null<stack<pair<bool,stack<*ast_node>>>>(), true).one_string() + ";\n";)
|
2016-01-21 12:54:21 -05:00
|
|
|
structs += "};\n"
|
2016-02-20 02:36:35 -05:00
|
|
|
// generate the methods (note some of these may be templates)
|
|
|
|
|
vert->type_def.methods.for_each(fun(method: *ast_node) {
|
|
|
|
|
if (is_template(method))
|
|
|
|
|
method->template.instantiated.for_each(fun(m: *ast_node) generate_function_definition(m, vert);)
|
|
|
|
|
else
|
|
|
|
|
generate_function_definition(method, vert);
|
|
|
|
|
})
|
2016-01-20 13:50:40 -05:00
|
|
|
})
|
2016-01-04 00:38:59 -05:00
|
|
|
|
2016-02-22 16:18:55 -05:00
|
|
|
return make_pair(prequal+plain_typedefs+top_level_c_passthrough+variable_extern_declarations+structs+closure_struct_definitions+function_typedef_string+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)
|
|
|
|
|
}
|
|
|
|
|
fun get_closure_struct_type(closed_variables: set<*ast_node>): string {
|
|
|
|
|
if (!closure_struct_map.contains_key(closed_variables)) {
|
2016-02-24 01:54:20 -05:00
|
|
|
var closure_name = string("closure_data_type") + get_id()
|
|
|
|
|
closure_struct_definitions += "typedef struct {\n"
|
|
|
|
|
closed_variables.for_each(fun(i: *ast_node) closure_struct_definitions += type_to_c(i->identifier.type->clone_with_increased_indirection()) + " " + i->identifier.name + ";\n";)
|
|
|
|
|
closure_struct_definitions += string("} ") + closure_name + ";\n"
|
|
|
|
|
closure_struct_map[closed_variables] = closure_name
|
2016-02-22 16:18:55 -05:00
|
|
|
}
|
|
|
|
|
return closure_struct_map[closed_variables]
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_if_comp(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
2016-01-12 00:21:01 -05:00
|
|
|
if (node->if_comp.wanted_generator == "__C__")
|
2016-02-24 01:54:20 -05:00
|
|
|
return generate(node->if_comp.statement, enclosing_object, enclosing_func, defer_stack)
|
2016-01-27 18:56:44 -05:00
|
|
|
return code_triple()
|
2016-01-12 00:21:01 -05:00
|
|
|
}
|
2016-02-20 02:36:35 -05:00
|
|
|
fun generate_simple_passthrough(node: *ast_node, is_top_level: bool): string {
|
2016-01-04 02:00:06 -05:00
|
|
|
// deal with all the passthrough params
|
2016-02-17 13:37:48 -05:00
|
|
|
var result = string()
|
|
|
|
|
var pre = string()
|
|
|
|
|
var post = string()
|
|
|
|
|
node->simple_passthrough.in_params.for_each(fun(i: pair<*ast_node, string>) {
|
|
|
|
|
var wanted_name = i.second
|
2016-02-24 01:54:20 -05:00
|
|
|
var current_name = generate_identifier(i.first, null<ast_node>(), null<ast_node>()).one_string()
|
2016-02-17 13:37:48 -05:00
|
|
|
if (wanted_name != current_name)
|
|
|
|
|
result += type_to_c(i.first->identifier.type) + " " + wanted_name + " = " + current_name + ";\n"
|
|
|
|
|
})
|
|
|
|
|
result += node->simple_passthrough.passthrough_str
|
|
|
|
|
node->simple_passthrough.out_params.for_each(fun(i: pair<*ast_node, string>) {
|
|
|
|
|
var temp_name = string("out_temp") + get_id()
|
|
|
|
|
pre += type_to_c(i.first->identifier.type) + " " + temp_name + ";\n"
|
|
|
|
|
result += temp_name + " = " + i.second + ";\n"
|
2016-02-24 01:54:20 -05:00
|
|
|
post += generate_identifier(i.first, null<ast_node>(), null<ast_node>()).one_string() + " = " + temp_name + ";\n"
|
2016-02-17 13:37:48 -05:00
|
|
|
})
|
2016-02-20 02:36:35 -05:00
|
|
|
if (is_top_level)
|
|
|
|
|
return pre + result + post
|
2016-02-17 13:37:48 -05:00
|
|
|
return pre + "{" + result + "}" + post
|
2016-01-04 02:00:06 -05:00
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_statement(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple return generate(node->statement.child, enclosing_object, enclosing_func, defer_stack) + ";\n";
|
|
|
|
|
fun generate_declaration_statement(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>, add_to_defer: bool): code_triple {
|
2016-01-29 22:46:09 -05:00
|
|
|
// add destruct to defer_stack
|
2016-01-15 19:10:52 -05:00
|
|
|
var identifier = node->declaration_statement.identifier
|
2016-01-29 22:46:09 -05:00
|
|
|
var ident_type = identifier->identifier.type
|
2016-02-17 13:37:48 -05:00
|
|
|
var to_ret = code_triple() + type_to_c(identifier->identifier.type) + " " + get_name(identifier)
|
2016-01-31 19:29:08 -05:00
|
|
|
if (node->declaration_statement.expression) {
|
|
|
|
|
if (ident_type->is_object() && has_method(ident_type->type_def, "copy_construct", vector(get_ast_type(node->declaration_statement.expression)->clone_with_increased_indirection()))) {
|
|
|
|
|
to_ret += ";\n";
|
2016-02-24 01:54:20 -05:00
|
|
|
to_ret += generate(ast_statement_ptr(make_method_call(identifier, "copy_construct", vector(make_operator_call("&", vector(node->declaration_statement.expression))))), enclosing_object, enclosing_func, defer_stack)
|
2016-01-31 19:29:08 -05:00
|
|
|
} else {
|
2016-02-24 01:54:20 -05:00
|
|
|
to_ret += code_triple(" = ") + generate(node->declaration_statement.expression, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
|
2016-01-31 19:29:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
if (node->declaration_statement.init_method_call) to_ret += code_triple(";\n") + generate(node->declaration_statement.init_method_call, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
|
2016-01-30 22:04:37 -05:00
|
|
|
if (add_to_defer && ident_type->is_object() && has_method(ident_type->type_def, "destruct", vector<*type>()))
|
2016-01-29 22:46:09 -05:00
|
|
|
defer_stack->top().second.push(ast_statement_ptr(make_method_call(identifier, "destruct", vector<*ast_node>())))
|
2016-01-17 01:10:09 -05:00
|
|
|
return to_ret
|
2016-01-15 19:10:52 -05:00
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_assignment_statement(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node): code_triple {
|
|
|
|
|
return generate(node->assignment_statement.to, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + " = " + generate(node->assignment_statement.from, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
|
2016-01-16 22:14:59 -05:00
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_if_statement(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
|
|
|
|
var if_str = code_triple("if (") + generate(node->if_statement.condition, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + ") {\n" + generate(node->if_statement.then_part, enclosing_object, enclosing_func, defer_stack) + "}"
|
2016-01-19 02:06:30 -05:00
|
|
|
if (node->if_statement.else_part)
|
2016-02-24 01:54:20 -05:00
|
|
|
if_str += code_triple(" else {\n") + generate(node->if_statement.else_part, enclosing_object, enclosing_func, defer_stack) + "}"
|
2016-01-19 02:06:30 -05:00
|
|
|
return if_str + "\n"
|
|
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_while_loop(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
2016-01-25 13:48:27 -05:00
|
|
|
// stick another stack on
|
2016-01-25 22:48:41 -05:00
|
|
|
defer_stack->push(make_pair(true, stack<*ast_node>()))
|
2016-02-24 01:54:20 -05:00
|
|
|
var to_ret = code_triple("while (") + generate(node->while_loop.condition, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()).one_string() + ") {\n" + generate(node->while_loop.statement, enclosing_object, enclosing_func, defer_stack).one_string()
|
|
|
|
|
to_ret += generate_from_defer_stack(defer_stack, 1, enclosing_object, enclosing_func)
|
2016-01-25 13:48:27 -05:00
|
|
|
defer_stack->pop()
|
|
|
|
|
to_ret += "}\n"
|
|
|
|
|
return to_ret
|
2016-01-19 03:16:16 -05:00
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_for_loop(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
2016-01-25 13:48:27 -05:00
|
|
|
// stick another stack on
|
2016-01-25 22:48:41 -05:00
|
|
|
defer_stack->push(make_pair(true, stack<*ast_node>()))
|
2016-01-19 11:47:09 -05:00
|
|
|
// gotta take off last semicolon
|
2016-02-24 01:54:20 -05:00
|
|
|
var to_ret = code_triple("for (") + generate(node->for_loop.init, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()).one_string() + " " + generate(node->for_loop.condition, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()).one_string() + "; " +
|
|
|
|
|
generate(node->for_loop.update, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()).one_string().slice(0,-3) +
|
|
|
|
|
") {\n" + generate(node->for_loop.body, enclosing_object, enclosing_func, defer_stack).one_string()
|
|
|
|
|
to_ret += generate_from_defer_stack(defer_stack, 1, enclosing_object, enclosing_func).one_string()
|
2016-01-25 13:48:27 -05:00
|
|
|
defer_stack->pop()
|
|
|
|
|
to_ret += "}\n"
|
|
|
|
|
return to_ret
|
2016-01-19 11:47:09 -05:00
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_identifier(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node): code_triple {
|
|
|
|
|
if (enclosing_func && enclosing_func->function.closed_variables.contains(node))
|
|
|
|
|
return code_triple(string("*(closure_data->") + get_name(node) + ")")
|
2016-02-09 02:59:38 -05:00
|
|
|
if (enclosing_object && get_ast_scope(enclosing_object)->contains_key(node->identifier.name) && get_ast_scope(enclosing_object)->get(node->identifier.name).contains(node))
|
2016-02-17 13:37:48 -05:00
|
|
|
return code_triple("(this->") + get_name(node) + ")"
|
|
|
|
|
return code_triple(get_name(node))
|
2016-01-16 22:14:59 -05:00
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_return_statement(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
2016-01-30 22:04:37 -05:00
|
|
|
var return_value = node->return_statement.return_value
|
2016-02-07 16:22:55 -05:00
|
|
|
if (!return_value)
|
|
|
|
|
return code_triple("return")
|
2016-01-30 22:04:37 -05:00
|
|
|
var return_value_type = get_ast_type(return_value)
|
|
|
|
|
var to_ret = code_triple()
|
|
|
|
|
// if we're returning an object, copy_construct a new one to return
|
|
|
|
|
if (return_value_type->is_object() && return_value_type->indirection == 0 && has_method(return_value_type->type_def, "copy_construct", vector(return_value_type->clone_with_indirection(1)))) {
|
2016-02-22 16:18:55 -05:00
|
|
|
var temp_ident = ast_identifier_ptr(string("temporary_return")+get_id(), return_value_type, null<ast_node>())
|
2016-01-30 22:04:37 -05:00
|
|
|
var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>())
|
|
|
|
|
// have to pass false to the declaration generator, so can't do it through generate_statement
|
2016-02-24 01:54:20 -05:00
|
|
|
to_ret.pre = generate_declaration_statement(declaration, enclosing_object, enclosing_func, defer_stack, false).one_string() + ";\n"
|
|
|
|
|
to_ret.pre += generate_statement(ast_statement_ptr(make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(return_value))))), enclosing_object, enclosing_func, defer_stack).one_string()
|
2016-01-30 22:04:37 -05:00
|
|
|
// make this new identifier the new return value
|
|
|
|
|
return_value = temp_ident
|
|
|
|
|
}
|
2016-01-27 18:56:44 -05:00
|
|
|
// generate all in stack by passing -1
|
2016-02-24 01:54:20 -05:00
|
|
|
to_ret += generate_from_defer_stack(defer_stack, -1, enclosing_object, enclosing_func)
|
|
|
|
|
to_ret += code_triple("return ") + generate(return_value, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
|
2016-01-25 13:48:27 -05:00
|
|
|
return to_ret
|
|
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_branching_statement(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
2016-01-27 18:56:44 -05:00
|
|
|
// -2 means generate up through loop
|
2016-02-24 01:54:20 -05:00
|
|
|
var to_ret = generate_from_defer_stack(defer_stack, -2, enclosing_object, enclosing_func)
|
2016-01-24 17:31:41 -05:00
|
|
|
match(node->branching_statement.b_type) {
|
2016-01-25 13:48:27 -05:00
|
|
|
branching_type::break_stmt() return to_ret + string("break")
|
|
|
|
|
branching_type::continue_stmt() return to_ret + string("continue")
|
2016-01-24 17:31:41 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_from_defer_stack(defer_stack: *stack<pair<bool,stack<*ast_node>>>, num: int, enclosing_object: *ast_node, enclosing_func: *ast_node): code_triple {
|
2016-01-27 18:56:44 -05:00
|
|
|
var to_ret = code_triple()
|
2016-01-25 13:48:27 -05:00
|
|
|
if (num == -1)
|
|
|
|
|
num = defer_stack->size()
|
2016-01-25 22:48:41 -05:00
|
|
|
if (num == -2) {
|
|
|
|
|
num = 1
|
|
|
|
|
for (var i = 0; i < defer_stack->size(); i++;)
|
|
|
|
|
if (defer_stack->from_top(i).first)
|
|
|
|
|
break
|
|
|
|
|
else
|
|
|
|
|
num++
|
|
|
|
|
}
|
2016-01-25 13:48:27 -05:00
|
|
|
for (var i = 0; i < num; i++;)
|
2016-02-24 01:54:20 -05:00
|
|
|
defer_stack->from_top(i).second.for_each(fun(node: *ast_node) to_ret += generate(node, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>());)
|
2016-01-25 13:48:27 -05:00
|
|
|
return to_ret
|
|
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_defer_statement(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
2016-01-25 22:48:41 -05:00
|
|
|
defer_stack->top().second.push(node->defer_statement.statement)
|
2016-01-27 18:56:44 -05:00
|
|
|
return code_triple("/*defer wanna know what*/")
|
2016-01-25 13:48:27 -05:00
|
|
|
}
|
2016-02-15 23:12:56 -05:00
|
|
|
fun generate_value(node: *ast_node): code_triple {
|
|
|
|
|
var value = node->value.string_value
|
|
|
|
|
if (value[0] != '"')
|
|
|
|
|
return code_triple(value);
|
|
|
|
|
var to_ret = string("\"")
|
|
|
|
|
if (value.slice(0,3) == "\"\"\"")
|
|
|
|
|
value = value.slice(3,-4)
|
|
|
|
|
else
|
|
|
|
|
value = value.slice(1,-2)
|
|
|
|
|
value.for_each(fun(c: char) {
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
to_ret += "\\n"
|
|
|
|
|
else if (c == '"')
|
|
|
|
|
to_ret += "\\\""
|
|
|
|
|
else
|
|
|
|
|
to_ret += c
|
|
|
|
|
})
|
|
|
|
|
return code_triple(to_ret + "\"")
|
|
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_code_block(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
2016-01-27 18:56:44 -05:00
|
|
|
var to_ret = code_triple("{\n")
|
2016-01-25 13:48:27 -05:00
|
|
|
// stick another stack on
|
2016-01-25 22:48:41 -05:00
|
|
|
defer_stack->push(make_pair(false, stack<*ast_node>()))
|
2016-02-24 01:54:20 -05:00
|
|
|
node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child, enclosing_object, enclosing_func, defer_stack).one_string();)
|
|
|
|
|
to_ret += generate_from_defer_stack(defer_stack, 1, enclosing_object, enclosing_func)
|
2016-01-25 13:48:27 -05:00
|
|
|
defer_stack->pop()
|
2016-01-08 00:33:05 -05:00
|
|
|
return to_ret + "}"
|
2016-01-07 02:52:22 -05:00
|
|
|
}
|
2016-01-20 13:50:40 -05:00
|
|
|
// this generates the function as a value, not the actual function
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_function(node: *ast_node, as_value: bool): code_triple {
|
|
|
|
|
if (as_value) {
|
|
|
|
|
var closed_vars = node->function.closed_variables
|
|
|
|
|
if (closed_vars.size() == 0)
|
|
|
|
|
return code_triple(string("((") + type_to_c(node->function.type) + "){NULL," + get_name(node) + "})")
|
|
|
|
|
var temp_closure_struct = string("closure_struct_temp") + get_id()
|
|
|
|
|
var to_ret = code_triple()
|
|
|
|
|
var closure_type_str = get_closure_struct_type(closed_vars)
|
|
|
|
|
to_ret.pre += closure_type_str + " " + temp_closure_struct + " = (" + closure_type_str + "){"
|
|
|
|
|
closed_vars.for_each(fun(i: *ast_node) to_ret.pre += string(".") + get_name(i) + "=&" + get_name(i) + ",";)
|
|
|
|
|
to_ret.pre += "};\n"
|
|
|
|
|
return to_ret + string("((") + type_to_c(node->function.type) + "){(void*)&" + temp_closure_struct + ",(void*)" + get_name(node) + "})"
|
|
|
|
|
}
|
2016-02-15 16:31:01 -05:00
|
|
|
return code_triple(get_name(node))
|
2016-01-11 23:41:09 -05:00
|
|
|
}
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate_function_call(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node): code_triple {
|
2016-01-23 05:33:56 -05:00
|
|
|
var func_name = string()
|
2016-01-27 18:56:44 -05:00
|
|
|
var call_string = code_triple()
|
2016-01-31 19:29:08 -05:00
|
|
|
var func_return_type = get_ast_type(node)
|
2016-01-24 01:49:14 -05:00
|
|
|
|
|
|
|
|
// handle the obj.method() style of method call
|
|
|
|
|
var dot_style_method_call = is_function_call(node->function_call.func) &&
|
2016-01-22 23:59:59 -05:00
|
|
|
is_function(node->function_call.func->function_call.func) &&
|
2016-01-23 05:33:56 -05:00
|
|
|
(node->function_call.func->function_call.func->function.name == "->" || node->function_call.func->function_call.func->function.name == ".") &&
|
|
|
|
|
is_function(node->function_call.func->function_call.parameters[1]) &&
|
2016-02-20 02:36:35 -05:00
|
|
|
(is_type_def(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(string("~enclosing_scope"))[0]) ||
|
|
|
|
|
// or if it's a templated method (yes, this has gotten uuuuugly)
|
|
|
|
|
is_type_def(get_ast_scope(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(string("~enclosing_scope"))[0])->get(string("~enclosing_scope"))[0]))
|
2016-02-24 01:54:20 -05:00
|
|
|
// should get uglier when we have to figure out if it's just an inside lambda
|
2016-01-24 01:49:14 -05:00
|
|
|
|
|
|
|
|
if (dot_style_method_call) {
|
2016-02-24 01:54:20 -05:00
|
|
|
func_name = generate_function(node->function_call.func->function_call.parameters[1], false).one_string()
|
2016-01-24 01:02:56 -05:00
|
|
|
// don't add & if it was ->
|
|
|
|
|
if (node->function_call.func->function_call.func->function.name == ".")
|
2016-01-27 18:56:44 -05:00
|
|
|
call_string += "&"
|
2016-02-24 01:54:20 -05:00
|
|
|
call_string += generate_function(node->function_call.func->function_call.parameters[0], false)
|
|
|
|
|
} else {
|
|
|
|
|
// regular style function name or lambda
|
|
|
|
|
func_name = generate_function(node->function_call.func, false).one_string()
|
|
|
|
|
if (!is_function(node->function_call.func)) {
|
|
|
|
|
// not function, so we must be an identifier or function call return or something
|
|
|
|
|
call_string += func_name + ".data"
|
|
|
|
|
func_name = func_name + ".func"
|
|
|
|
|
}
|
2016-01-22 23:59:59 -05:00
|
|
|
}
|
2016-01-24 01:49:14 -05:00
|
|
|
// handle method call from inside method of same object
|
2016-02-16 13:43:47 -05:00
|
|
|
if (!dot_style_method_call && enclosing_object && enclosing_object->type_def.methods.contains(node->function_call.func))
|
2016-01-27 18:56:44 -05:00
|
|
|
call_string += "this"
|
2016-01-24 01:49:14 -05:00
|
|
|
|
2016-01-19 11:47:09 -05:00
|
|
|
var parameters = node->function_call.parameters
|
2016-01-28 12:55:51 -05:00
|
|
|
if ( parameters.size == 2 && (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/" || func_name == "||"
|
2016-01-19 11:47:09 -05:00
|
|
|
|| func_name == "&&" || func_name == "<" || func_name == ">" || func_name == "<=" || func_name == ">="
|
2016-02-07 16:22:55 -05:00
|
|
|
|| func_name == "==" || func_name == "!=" || func_name == "%"
|
2016-01-28 12:55:51 -05:00
|
|
|
))
|
2016-02-24 01:54:20 -05:00
|
|
|
return code_triple("(") + generate(parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + func_name + generate(parameters[1], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + string(")")
|
2016-01-23 20:39:06 -05:00
|
|
|
// don't propegate enclosing function down right of access
|
|
|
|
|
if (func_name == "." || func_name == "->")
|
2016-02-24 01:54:20 -05:00
|
|
|
return code_triple("(") + generate(parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + func_name + generate(parameters[1], null<ast_node>(), null<ast_node>(), null<stack<pair<bool,stack<*ast_node>>>>()) + string(")")
|
2016-02-07 16:22:55 -05:00
|
|
|
if (func_name == "[")
|
2016-02-24 01:54:20 -05:00
|
|
|
return code_triple("(") + generate(parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + "[" + generate(parameters[1], null<ast_node>(), null<ast_node>(), null<stack<pair<bool,stack<*ast_node>>>>()) + string("])")
|
2016-01-19 03:16:16 -05:00
|
|
|
// the post ones need to be post-ed specifically, and take the p off
|
|
|
|
|
if (func_name == "++p" || func_name == "--p")
|
2016-02-24 01:54:20 -05:00
|
|
|
return code_triple("(") + generate(parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + ")" + func_name.slice(0,-2)
|
2016-01-24 01:49:14 -05:00
|
|
|
|
2016-01-30 23:59:21 -05:00
|
|
|
// So we don't end up copy_constructing etc, we just handle the unary operators right here
|
|
|
|
|
if (func_name == "*" || func_name == "&")
|
2016-02-24 01:54:20 -05:00
|
|
|
return code_triple("(") + func_name + generate(parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + ")"
|
2016-01-30 23:59:21 -05:00
|
|
|
|
2016-01-24 01:49:14 -05:00
|
|
|
// regular parameter generation
|
2016-01-18 18:04:34 -05:00
|
|
|
parameters.for_each(fun(param: *ast_node) {
|
2016-01-11 23:41:09 -05:00
|
|
|
if (call_string != "")
|
|
|
|
|
call_string += ", "
|
2016-01-30 23:59:21 -05:00
|
|
|
|
|
|
|
|
var param_type = get_ast_type(param)
|
|
|
|
|
if (param_type->is_object() && param_type->indirection == 0 && has_method(param_type->type_def, "copy_construct", vector(param_type->clone_with_indirection(1)))) {
|
2016-02-22 16:18:55 -05:00
|
|
|
var temp_ident = ast_identifier_ptr(string("temporary_param")+get_id(), param_type, null<ast_node>())
|
2016-01-30 23:59:21 -05:00
|
|
|
var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>())
|
|
|
|
|
// have to pass false to the declaration generator, so can't do it through generate_statement
|
2016-02-24 01:54:20 -05:00
|
|
|
call_string.pre += generate_declaration_statement(declaration, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>(), false).one_string() + ";\n"
|
|
|
|
|
call_string.pre += generate_statement(ast_statement_ptr(make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(param))))), enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()).one_string()
|
|
|
|
|
call_string += generate(temp_ident, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
|
2016-01-31 00:10:54 -05:00
|
|
|
if (has_method(param_type->type_def, "destruct", vector<*type>())) {
|
2016-02-24 01:54:20 -05:00
|
|
|
call_string.post += generate_statement(ast_statement_ptr(make_method_call(temp_ident, "destruct", vector<*ast_node>())), enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()).one_string()
|
2016-01-31 00:10:54 -05:00
|
|
|
}
|
2016-01-30 23:59:21 -05:00
|
|
|
} else {
|
2016-02-24 01:54:20 -05:00
|
|
|
call_string += generate(param, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
|
2016-01-30 23:59:21 -05:00
|
|
|
}
|
2016-01-11 23:41:09 -05:00
|
|
|
})
|
2016-01-31 19:29:08 -05:00
|
|
|
if (func_return_type->is_object() && func_return_type->indirection == 0 && has_method(func_return_type->type_def, "destruct", vector<*type>())) {
|
2016-02-07 16:22:55 -05:00
|
|
|
// kind of ugly combo here of
|
2016-02-22 16:18:55 -05:00
|
|
|
var temp_ident = ast_identifier_ptr(string("temporary_return")+get_id(), func_return_type, null<ast_node>())
|
2016-01-31 19:29:08 -05:00
|
|
|
var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>())
|
|
|
|
|
// have to pass false to the declaration generator, so can't do it through generate_statement
|
2016-02-24 01:54:20 -05:00
|
|
|
call_string.pre += generate_declaration_statement(declaration, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>(), false).one_string() + ";\n"
|
|
|
|
|
call_string.pre += generate_identifier(temp_ident, enclosing_object, enclosing_func).one_string() + " = " + func_name + "(" + call_string.value + ");\n"
|
|
|
|
|
call_string.value = generate_identifier(temp_ident, enclosing_object, enclosing_func).one_string()
|
|
|
|
|
call_string.post += generate_statement(ast_statement_ptr(make_method_call(temp_ident, "destruct", vector<*ast_node>())), enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()).one_string()
|
2016-01-31 19:29:08 -05:00
|
|
|
return call_string
|
|
|
|
|
}
|
2016-01-27 18:56:44 -05:00
|
|
|
return code_triple() + func_name + "(" + call_string + ")"
|
2016-01-11 23:41:09 -05:00
|
|
|
}
|
2016-01-07 02:52:22 -05:00
|
|
|
|
2016-01-04 02:00:06 -05:00
|
|
|
// for now, anyway
|
2016-02-24 01:54:20 -05:00
|
|
|
fun generate(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
2016-01-27 18:56:44 -05:00
|
|
|
if (!node) return code_triple("/*NULL*/")
|
2016-01-04 02:00:06 -05:00
|
|
|
match (*node) {
|
2016-02-24 01:54:20 -05:00
|
|
|
ast_node::if_comp(backing) return generate_if_comp(node, enclosing_object, enclosing_func, defer_stack)
|
2016-02-20 02:36:35 -05:00
|
|
|
ast_node::simple_passthrough(backing) return code_triple() + generate_simple_passthrough(node, false)
|
2016-02-24 01:54:20 -05:00
|
|
|
ast_node::statement(backing) return generate_statement(node, enclosing_object, enclosing_func, defer_stack)
|
|
|
|
|
ast_node::declaration_statement(backing) return generate_declaration_statement(node, enclosing_object, enclosing_func, defer_stack, true)
|
|
|
|
|
ast_node::assignment_statement(backing) return generate_assignment_statement(node, enclosing_object, enclosing_func)
|
|
|
|
|
ast_node::if_statement(backing) return generate_if_statement(node, enclosing_object, enclosing_func, defer_stack)
|
|
|
|
|
ast_node::while_loop(backing) return generate_while_loop(node, enclosing_object, enclosing_func, defer_stack)
|
|
|
|
|
ast_node::for_loop(backing) return generate_for_loop(node, enclosing_object, enclosing_func, defer_stack)
|
|
|
|
|
ast_node::function(backing) return generate_function(node,true)
|
|
|
|
|
ast_node::function_call(backing) return generate_function_call(node, enclosing_object, enclosing_func)
|
|
|
|
|
ast_node::code_block(backing) return generate_code_block(node, enclosing_object, enclosing_func, defer_stack)
|
|
|
|
|
ast_node::return_statement(backing) return generate_return_statement(node, enclosing_object, enclosing_func, defer_stack)
|
|
|
|
|
ast_node::branching_statement(backing) return generate_branching_statement(node, enclosing_object, enclosing_func, defer_stack)
|
|
|
|
|
ast_node::defer_statement(backing) return generate_defer_statement(node, enclosing_object, enclosing_func, defer_stack)
|
2016-01-08 00:33:05 -05:00
|
|
|
ast_node::value(backing) return generate_value(node)
|
2016-02-24 01:54:20 -05:00
|
|
|
ast_node::identifier(backing) return generate_identifier(node, enclosing_object, enclosing_func)
|
2016-01-04 02:00:06 -05:00
|
|
|
}
|
2016-01-27 18:56:44 -05:00
|
|
|
return code_triple("/* COULD NOT GENERATE */")
|
2016-01-04 02:00:06 -05:00
|
|
|
}
|
2016-01-13 21:09:28 -05:00
|
|
|
fun type_decoration(type: *type): string {
|
|
|
|
|
var indirection = string()
|
|
|
|
|
for (var i = 0; i < type->indirection; i++;) indirection += "p"
|
|
|
|
|
if (type->indirection) indirection += "_"
|
|
|
|
|
match (type->base) {
|
|
|
|
|
base_type::none() return indirection + string("none")
|
|
|
|
|
base_type::template() return indirection + string("template")
|
|
|
|
|
base_type::template_type() return indirection + string("template_type")
|
|
|
|
|
base_type::void_return() return indirection + string("void")
|
|
|
|
|
base_type::boolean() return indirection + string("bool")
|
|
|
|
|
base_type::character() return indirection + string("char")
|
|
|
|
|
base_type::integer() return indirection + string("int")
|
|
|
|
|
base_type::floating() return indirection + string("float")
|
|
|
|
|
base_type::double_precision() return indirection + string("double")
|
2016-01-21 03:18:02 -05:00
|
|
|
base_type::object() {
|
|
|
|
|
return type->type_def->type_def.name
|
|
|
|
|
}
|
2016-01-13 21:09:28 -05:00
|
|
|
base_type::function() {
|
|
|
|
|
var temp = indirection + string("function_")
|
|
|
|
|
type->parameter_types.for_each(fun(parameter_type: *type) temp += type_decoration(parameter_type) + "_";)
|
|
|
|
|
return indirection + temp + "_" + type_decoration(type->return_type)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return string("impossible type") + indirection
|
|
|
|
|
}
|
2016-01-06 02:46:42 -05:00
|
|
|
fun type_to_c(type: *type): string {
|
2016-01-10 18:26:31 -05:00
|
|
|
var indirection = string()
|
|
|
|
|
for (var i = 0; i < type->indirection; i++;) indirection += "*"
|
2016-01-06 02:46:42 -05:00
|
|
|
match (type->base) {
|
2016-01-10 18:26:31 -05:00
|
|
|
base_type::none() return string("none") + indirection
|
|
|
|
|
base_type::template() return string("template") + indirection
|
|
|
|
|
base_type::template_type() return string("template_type") + indirection
|
|
|
|
|
base_type::void_return() return string("void") + indirection
|
|
|
|
|
base_type::boolean() return string("bool") + indirection
|
|
|
|
|
base_type::character() return string("char") + indirection
|
|
|
|
|
base_type::integer() return string("int") + indirection
|
|
|
|
|
base_type::floating() return string("float") + indirection
|
|
|
|
|
base_type::double_precision() return string("double") + indirection
|
2016-01-21 03:18:02 -05:00
|
|
|
base_type::object() {
|
2016-02-15 16:31:01 -05:00
|
|
|
return get_name(type->type_def) + indirection
|
2016-01-21 03:18:02 -05:00
|
|
|
}
|
2016-01-06 02:46:42 -05:00
|
|
|
base_type::function() {
|
2016-02-24 01:54:20 -05:00
|
|
|
// maybe disregard indirection in the future?
|
|
|
|
|
if (function_type_map.contains_key(*type))
|
|
|
|
|
return function_type_map[*type]
|
|
|
|
|
var temp_name = string("function_struct") + get_id()
|
|
|
|
|
var temp = string("func)(void*")
|
|
|
|
|
type->parameter_types.for_each(fun(parameter_type: *type) temp += string(", ") + type_to_c(parameter_type) + " ";)
|
|
|
|
|
function_typedef_string += string("typedef struct {\nvoid* data;\n") + type_to_c(type->return_type) + " (*" + temp + ");\n} " + temp_name + ";"
|
|
|
|
|
// again, the indirection
|
|
|
|
|
function_type_map[*type] = temp_name+indirection
|
2016-02-20 21:02:41 -05:00
|
|
|
return temp_name + indirection
|
2016-01-06 02:46:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-10 18:26:31 -05:00
|
|
|
return string("impossible type") + indirection
|
2016-01-06 02:46:42 -05:00
|
|
|
}
|
2016-02-15 16:31:01 -05:00
|
|
|
fun get_name(node: *ast_node): string {
|
2016-02-17 13:37:48 -05:00
|
|
|
if (ast_name_map.contains_key(node))
|
|
|
|
|
return ast_name_map[node]
|
|
|
|
|
var result = string("impossible name")
|
2016-02-15 16:31:01 -05:00
|
|
|
match (*node) {
|
|
|
|
|
ast_node::type_def(backing) {
|
|
|
|
|
var upper = backing.scope[string("~enclosing_scope")][0]
|
2016-02-17 13:37:48 -05:00
|
|
|
result = backing.name
|
2016-02-15 16:31:01 -05:00
|
|
|
if (is_template(upper))
|
|
|
|
|
upper->template.instantiated_map.reverse_get(node).for_each(fun(t: ref type) result += string("_") + type_decoration(&t);)
|
|
|
|
|
}
|
|
|
|
|
ast_node::function(backing) {
|
|
|
|
|
// be careful, operators like . come through this
|
2016-02-17 13:37:48 -05:00
|
|
|
if (!backing.body_statement)
|
|
|
|
|
return backing.name
|
|
|
|
|
result = ""
|
2016-02-15 16:31:01 -05:00
|
|
|
var upper = backing.scope.get_with_default(string("~enclosing_scope"), vector(null<ast_node>()))[0]
|
|
|
|
|
if (upper && is_type_def(upper))
|
2016-02-17 13:37:48 -05:00
|
|
|
result += get_name(upper) + "_"
|
|
|
|
|
result += node->function.name
|
|
|
|
|
node->function.parameters.for_each(fun(param: *ast_node) result += string("_") + type_decoration(param->identifier.type);)
|
2016-02-15 16:31:01 -05:00
|
|
|
}
|
2016-02-17 13:59:10 -05:00
|
|
|
ast_node::identifier(backing) {
|
|
|
|
|
if (backing.name == "this")
|
|
|
|
|
return backing.name
|
|
|
|
|
result = backing.name
|
|
|
|
|
}
|
2016-02-15 16:31:01 -05:00
|
|
|
}
|
2016-02-17 13:37:48 -05:00
|
|
|
if (result == "impossible name")
|
|
|
|
|
println("HUGE PROBLEMS")
|
|
|
|
|
if (ast_name_map.contains_value(result))
|
|
|
|
|
result += get_id()
|
|
|
|
|
ast_name_map.set(node, result)
|
|
|
|
|
return result
|
2016-02-15 16:31:01 -05:00
|
|
|
}
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|