2016-01-04 00:38:59 -05:00
|
|
|
import io:*
|
|
|
|
|
import mem:*
|
|
|
|
|
import map:*
|
|
|
|
|
import string:*
|
|
|
|
|
import util:*
|
|
|
|
|
import tree:*
|
|
|
|
|
import symbol:*
|
|
|
|
|
import ast_nodes:*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
obj c_generator (Object) {
|
|
|
|
|
fun construct(): *c_generator {
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
fun copy_construct(old: *c_generator) {
|
|
|
|
|
}
|
|
|
|
|
fun operator=(other: ref c_generator) {
|
|
|
|
|
destruct()
|
|
|
|
|
copy_construct(&other)
|
|
|
|
|
}
|
|
|
|
|
fun destruct() {
|
|
|
|
|
}
|
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"
|
|
|
|
|
var plain_typedefs: string = ""
|
|
|
|
|
var top_level_c_passthrough: string = ""
|
|
|
|
|
var variable_extern_declarations: string = ""
|
|
|
|
|
var structs: string = ""
|
|
|
|
|
var function_typedef_string_pre: string = ""
|
|
|
|
|
var function_typedef_string: string = ""
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
// poset generation into structs string
|
|
|
|
|
// 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?
|
|
|
|
|
tree_pair.second->translation_unit.children.for_each(fun(child: *ast_node) {
|
|
|
|
|
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))
|
|
|
|
|
top_level_c_passthrough += generate_simple_passthrough(backing.statement->statement.child)
|
|
|
|
|
}
|
|
|
|
|
ast_node::simple_passthrough(backing) top_level_c_passthrough += generate_simple_passthrough(child)
|
2016-01-15 19:10:52 -05:00
|
|
|
ast_node::declaration_statement(backing) variable_declarations += generate_declaration_statement(child) + ";\n"
|
2016-01-06 02:46:42 -05:00
|
|
|
ast_node::function(backing) {
|
|
|
|
|
// make sure not a template
|
|
|
|
|
// or a passthrough
|
|
|
|
|
// check for and add to parameters if a closure
|
|
|
|
|
var parameter_types = string()
|
|
|
|
|
var parameters = string()
|
2016-01-13 21:09:28 -05:00
|
|
|
var decorated_name = generate_function(child)
|
2016-01-06 02:46:42 -05:00
|
|
|
// also add in name decoration
|
|
|
|
|
backing.parameters.for_each(fun(parameter: *ast_node) {
|
|
|
|
|
if (parameter_types != "") { parameter_types += ", "; parameters += ", ";}
|
|
|
|
|
parameter_types += type_to_c(parameter->identifier.type)
|
|
|
|
|
parameters += type_to_c(parameter->identifier.type) + " " + parameter->identifier.name
|
|
|
|
|
})
|
2016-01-13 21:09:28 -05:00
|
|
|
function_prototypes += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameter_types + ");\n"
|
2016-01-06 02:46:42 -05:00
|
|
|
|
|
|
|
|
// add parameters to destructor thingy (for returns)? Or should that be a different pass?
|
2016-01-13 21:09:28 -05:00
|
|
|
function_definitions += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameters + ") {\n" + generate_statement(backing.body_statement)
|
2016-01-06 02:46:42 -05:00
|
|
|
// emit parameter destructors?
|
|
|
|
|
function_definitions += "}\n"
|
|
|
|
|
}
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2016-01-06 02:46:42 -05:00
|
|
|
return make_pair(prequal+plain_typedefs+top_level_c_passthrough+variable_extern_declarations+structs+function_typedef_string_pre+function_typedef_string+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
2016-01-12 00:21:01 -05:00
|
|
|
fun generate_if_comp(node: *ast_node): string {
|
|
|
|
|
if (node->if_comp.wanted_generator == "__C__")
|
|
|
|
|
return generate(node->if_comp.statement)
|
|
|
|
|
return string("")
|
|
|
|
|
}
|
2016-01-04 02:00:06 -05:00
|
|
|
fun generate_simple_passthrough(node: *ast_node): string {
|
|
|
|
|
// deal with all the passthrough params
|
|
|
|
|
return node->simple_passthrough.passthrough_str
|
|
|
|
|
}
|
2016-01-07 02:52:22 -05:00
|
|
|
fun generate_statement(node: *ast_node): string return generate(node->statement.child) + ";\n";
|
2016-01-15 19:10:52 -05:00
|
|
|
fun generate_declaration_statement(node: *ast_node): string {
|
|
|
|
|
var identifier = node->declaration_statement.identifier
|
2016-01-17 01:10:09 -05:00
|
|
|
var to_ret = type_to_c(identifier->identifier.type) + " " + identifier->identifier.name
|
|
|
|
|
if (node->declaration_statement.expression) to_ret += string(" = ") + generate(node->declaration_statement.expression)
|
|
|
|
|
return to_ret
|
2016-01-15 19:10:52 -05:00
|
|
|
}
|
2016-01-16 22:14:59 -05:00
|
|
|
fun generate_assignment_statement(node: *ast_node): string {
|
|
|
|
|
return generate(node->assignment_statement.to) + " = " + generate(node->assignment_statement.from)
|
|
|
|
|
}
|
|
|
|
|
fun generate_identifier(node: *ast_node): string {
|
|
|
|
|
return node->identifier.name
|
|
|
|
|
}
|
2016-01-08 00:33:05 -05:00
|
|
|
fun generate_return_statement(node: *ast_node): string return string("return ") + generate(node->return_statement.return_value);
|
|
|
|
|
fun generate_value(node: *ast_node): string return node->value.string_value;
|
2016-01-07 02:52:22 -05:00
|
|
|
fun generate_code_block(node: *ast_node): string {
|
|
|
|
|
var to_ret = string("{\n")
|
|
|
|
|
node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child);)
|
2016-01-08 00:33:05 -05:00
|
|
|
return to_ret + "}"
|
2016-01-07 02:52:22 -05:00
|
|
|
}
|
2016-01-11 23:41:09 -05:00
|
|
|
// this generates the function as a value, not the actuall function
|
|
|
|
|
fun generate_function(node: *ast_node): string {
|
2016-01-13 21:09:28 -05:00
|
|
|
var str = node->function.name
|
|
|
|
|
node->function.parameters.for_each(fun(param: *ast_node) str += string("_") + type_decoration(param->identifier.type);)
|
|
|
|
|
return str
|
|
|
|
|
|
2016-01-11 23:41:09 -05:00
|
|
|
}
|
|
|
|
|
fun generate_function_call(node: *ast_node): string {
|
|
|
|
|
var call_string = string()
|
|
|
|
|
node->function_call.parameters.for_each(fun(param: *ast_node) {
|
|
|
|
|
if (call_string != "")
|
|
|
|
|
call_string += ", "
|
|
|
|
|
call_string += generate(param)
|
|
|
|
|
})
|
|
|
|
|
return generate(node->function_call.func) + "(" + call_string + ")"
|
|
|
|
|
}
|
2016-01-07 02:52:22 -05:00
|
|
|
|
2016-01-04 02:00:06 -05:00
|
|
|
// for now, anyway
|
|
|
|
|
fun generate(node: *ast_node): string {
|
2016-01-07 02:52:22 -05:00
|
|
|
if (!node) return string("/*NULL*/")
|
2016-01-04 02:00:06 -05:00
|
|
|
match (*node) {
|
2016-01-12 00:21:01 -05:00
|
|
|
ast_node::if_comp(backing) return generate_if_comp(node)
|
2016-01-04 02:00:06 -05:00
|
|
|
ast_node::simple_passthrough(backing) return generate_simple_passthrough(node)
|
2016-01-07 02:52:22 -05:00
|
|
|
ast_node::statement(backing) return generate_statement(node)
|
2016-01-15 19:10:52 -05:00
|
|
|
ast_node::declaration_statement(backing) return generate_declaration_statement(node)
|
2016-01-16 22:14:59 -05:00
|
|
|
ast_node::assignment_statement(backing) return generate_assignment_statement(node)
|
2016-01-11 23:41:09 -05:00
|
|
|
ast_node::function(backing) return generate_function(node)
|
|
|
|
|
ast_node::function_call(backing) return generate_function_call(node)
|
2016-01-07 02:52:22 -05:00
|
|
|
ast_node::code_block(backing) return generate_code_block(node)
|
2016-01-08 00:33:05 -05:00
|
|
|
ast_node::return_statement(backing) return generate_return_statement(node)
|
|
|
|
|
ast_node::value(backing) return generate_value(node)
|
2016-01-16 22:14:59 -05:00
|
|
|
ast_node::identifier(backing) return generate_identifier(node)
|
2016-01-04 02:00:06 -05:00
|
|
|
}
|
2016-01-07 02:52:22 -05:00
|
|
|
return string("/* 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")
|
|
|
|
|
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-06 02:46:42 -05:00
|
|
|
base_type::function() {
|
2016-01-10 18:26:31 -05:00
|
|
|
var temp = indirection + string("function: (")
|
2016-01-06 02:46:42 -05:00
|
|
|
type->parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + " ";)
|
|
|
|
|
return temp + ")" + type->return_type->to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-10 18:26:31 -05:00
|
|
|
return string("impossible type") + indirection
|
2016-01-06 02:46:42 -05:00
|
|
|
}
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|