2016-01-04 00:38:59 -05:00
|
|
|
import io:*
|
|
|
|
|
import mem:*
|
|
|
|
|
import map:*
|
2017-01-22 16:36:04 -05:00
|
|
|
import hash_map:*
|
2016-01-25 13:48:27 -05:00
|
|
|
import stack:*
|
2018-05-22 19:43:54 -04:00
|
|
|
import str:*
|
2016-01-04 00:38:59 -05:00
|
|
|
import util:*
|
|
|
|
|
import tree:*
|
|
|
|
|
import symbol:*
|
|
|
|
|
import ast_nodes:*
|
2016-07-03 01:55:32 -07:00
|
|
|
// for error with syntax tree
|
2016-07-03 15:32:45 -07:00
|
|
|
import pass_common:*
|
2016-01-20 13:50:40 -05:00
|
|
|
import poset:*
|
2016-01-04 00:38:59 -05:00
|
|
|
|
|
|
|
|
obj c_generator (Object) {
|
2016-01-31 19:29:08 -05:00
|
|
|
var id_counter: int
|
2018-05-22 19:43:54 -04:00
|
|
|
var ast_name_map: hash_map<*ast_node, str>
|
|
|
|
|
var used_names: hash_set<str>
|
|
|
|
|
var function_type_map: map<type, str>
|
|
|
|
|
var replacement_map: map<str, str>
|
2016-02-24 15:25:58 -05:00
|
|
|
var longest_replacement: int
|
2018-05-22 19:43:54 -04:00
|
|
|
var function_typedef_string: str
|
|
|
|
|
var linker_string: str
|
2016-01-04 00:38:59 -05:00
|
|
|
fun construct(): *c_generator {
|
2016-01-31 19:29:08 -05:00
|
|
|
id_counter = 0
|
2017-01-24 22:11:33 -05:00
|
|
|
ast_name_map.construct()
|
2017-10-28 15:28:34 -04:00
|
|
|
used_names.construct()
|
2018-01-02 23:22:46 -05:00
|
|
|
// to avoid using c keywords
|
2018-05-22 19:43:54 -04:00
|
|
|
used_names.add(str("extern"))
|
|
|
|
|
used_names.add(str("register"))
|
2016-02-24 01:54:20 -05:00
|
|
|
function_type_map.construct()
|
2016-02-22 16:18:55 -05:00
|
|
|
function_typedef_string.construct()
|
2016-03-28 17:12:53 -04:00
|
|
|
linker_string.construct()
|
2016-02-24 15:25:58 -05:00
|
|
|
|
2017-10-22 21:41:58 -04:00
|
|
|
replacement_map.construct()
|
2018-05-22 19:43:54 -04:00
|
|
|
replacement_map[str("+")] = str("plus")
|
|
|
|
|
replacement_map[str("-")] = str("minus")
|
|
|
|
|
replacement_map[str("*")] = str("star")
|
|
|
|
|
replacement_map[str("/")] = str("div")
|
|
|
|
|
replacement_map[str("%")] = str("mod")
|
|
|
|
|
replacement_map[str("^")] = str("carat")
|
|
|
|
|
replacement_map[str("&")] = str("amprsd")
|
|
|
|
|
replacement_map[str("|")] = str("pipe")
|
|
|
|
|
replacement_map[str("~")] = str("tilde")
|
|
|
|
|
replacement_map[str("!")] = str("exlmtnpt")
|
|
|
|
|
replacement_map[str(",")] = str("comma")
|
|
|
|
|
replacement_map[str("=")] = str("eq")
|
|
|
|
|
replacement_map[str("++")] = str("dbplus")
|
|
|
|
|
replacement_map[str("--")] = str("dbminus")
|
|
|
|
|
replacement_map[str("<<")] = str("dbleft")
|
|
|
|
|
replacement_map[str(">>")] = str("dbright")
|
|
|
|
|
replacement_map[str("::")] = str("scopeop")
|
|
|
|
|
replacement_map[str(":")] = str("colon")
|
|
|
|
|
replacement_map[str("==")] = str("dbq")
|
|
|
|
|
replacement_map[str("!=")] = str("notequals")
|
|
|
|
|
replacement_map[str("&&")] = str("doubleamprsnd")
|
|
|
|
|
replacement_map[str("||")] = str("doublepipe")
|
|
|
|
|
replacement_map[str("+=")] = str("plusequals")
|
|
|
|
|
replacement_map[str("-=")] = str("minusequals")
|
|
|
|
|
replacement_map[str("/=")] = str("divequals")
|
|
|
|
|
replacement_map[str("%=")] = str("modequals")
|
|
|
|
|
replacement_map[str("^=")] = str("caratequals")
|
|
|
|
|
replacement_map[str("&=")] = str("amprsdequals")
|
|
|
|
|
replacement_map[str("|=")] = str("pipeequals")
|
|
|
|
|
replacement_map[str("*=")] = str("starequals")
|
|
|
|
|
replacement_map[str("<<=")] = str("doublerightequals")
|
|
|
|
|
replacement_map[str("<")] = str("lt")
|
|
|
|
|
replacement_map[str(">")] = str("gt")
|
|
|
|
|
replacement_map[str(">>=")] = str("doubleleftequals")
|
|
|
|
|
replacement_map[str("(")] = str("openparen")
|
|
|
|
|
replacement_map[str(")")] = str("closeparen")
|
|
|
|
|
replacement_map[str("[")] = str("obk")
|
|
|
|
|
replacement_map[str("]")] = str("cbk")
|
|
|
|
|
replacement_map[str(" ")] = str("_")
|
|
|
|
|
replacement_map[str(".")] = str("dot")
|
|
|
|
|
replacement_map[str("->")] = str("arrow")
|
2016-02-24 15:25:58 -05:00
|
|
|
|
2017-10-22 21:41:58 -04:00
|
|
|
longest_replacement = 0
|
2018-05-22 19:43:54 -04:00
|
|
|
replacement_map.for_each(fun(key: str, value: str) {
|
2017-10-22 21:41:58 -04:00
|
|
|
if (key.length() > longest_replacement)
|
|
|
|
|
longest_replacement = key.length()
|
|
|
|
|
})
|
|
|
|
|
|
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
|
2017-01-24 22:11:33 -05:00
|
|
|
ast_name_map.copy_construct(&old->ast_name_map)
|
2017-10-28 15:28:34 -04:00
|
|
|
used_names.copy_construct(&old->used_names)
|
2016-02-24 01:54:20 -05:00
|
|
|
function_type_map.copy_construct(&old->function_type_map)
|
|
|
|
|
function_typedef_string.copy_construct(&old->function_typedef_string)
|
2016-02-24 15:25:58 -05:00
|
|
|
replacement_map.copy_construct(&old->replacement_map)
|
|
|
|
|
longest_replacement = old->longest_replacement
|
2016-03-28 17:12:53 -04:00
|
|
|
linker_string.copy_construct(&old->linker_string)
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
|
|
|
|
fun operator=(other: ref c_generator) {
|
|
|
|
|
destruct()
|
|
|
|
|
copy_construct(&other)
|
|
|
|
|
}
|
|
|
|
|
fun destruct() {
|
2017-01-24 22:11:33 -05:00
|
|
|
ast_name_map.destruct()
|
2017-10-28 15:28:34 -04:00
|
|
|
used_names.destruct()
|
2016-02-24 01:54:20 -05:00
|
|
|
function_type_map.destruct()
|
|
|
|
|
function_typedef_string.destruct()
|
2016-02-24 15:25:58 -05:00
|
|
|
replacement_map.destruct()
|
2016-03-28 17:12:53 -04:00
|
|
|
linker_string.destruct()
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun get_id(): str return to_string(id_counter++);
|
|
|
|
|
fun generate_function_prototype_and_header(child: *ast_node):pair<str,str> {
|
2016-03-19 21:45:07 -04:00
|
|
|
var backing = child->function
|
2018-05-22 19:43:54 -04:00
|
|
|
var parameter_types = str()
|
|
|
|
|
var parameters = str()
|
|
|
|
|
var decorated_name = str()
|
2017-12-27 16:49:08 -05:00
|
|
|
|
2016-04-29 01:14:26 -04:00
|
|
|
if (backing.is_extern)
|
|
|
|
|
decorated_name = backing.name
|
|
|
|
|
else
|
2017-10-23 00:06:25 -04:00
|
|
|
decorated_name = generate_function(child)
|
2017-12-27 16:49:08 -05:00
|
|
|
|
2016-03-19 21:45:07 -04:00
|
|
|
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) + " " + get_name(parameter)
|
|
|
|
|
})
|
2016-05-19 23:17:32 -07:00
|
|
|
if (backing.is_variadic) {
|
|
|
|
|
parameter_types += ", ..."
|
|
|
|
|
parameters += ", ..."
|
|
|
|
|
}
|
2017-10-29 17:53:30 -04:00
|
|
|
return make_pair(type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameter_types + ");\n",
|
|
|
|
|
type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameters + ")")
|
2016-03-19 21:45:07 -04:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_c(name_ast_map: map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax_in: map<*ast_node, *tree<symbol>> ): pair<str,str> {
|
|
|
|
|
var prequal: str = "#include <stdbool.h>\n"
|
|
|
|
|
var plain_typedefs: str = "\n/**Plain Typedefs**/\n"
|
|
|
|
|
var top_level_c_passthrough: str = ""
|
|
|
|
|
var variable_extern_declarations: str = ""
|
|
|
|
|
var structs: str = "\n/**Type Structs**/\n"
|
2016-02-22 16:18:55 -05:00
|
|
|
function_typedef_string = "\n/**Typedefs**/\n"
|
2018-05-22 19:43:54 -04:00
|
|
|
var function_prototypes: str = "\n/**Function Prototypes**/\n"
|
|
|
|
|
var function_definitions: str = "\n/**Function Definitions**/\n"
|
|
|
|
|
var variable_declarations: str = "\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...)
|
2017-12-27 16:49:08 -05:00
|
|
|
var generate_function_definition = fun(child: *ast_node) {
|
2016-01-21 12:54:21 -05:00
|
|
|
var backing = child->function
|
2017-12-27 16:49:08 -05:00
|
|
|
var prototype_and_header = generate_function_prototype_and_header(child)
|
2016-03-19 21:45:07 -04:00
|
|
|
function_prototypes += prototype_and_header.first
|
2016-04-29 01:14:26 -04:00
|
|
|
if (!backing.is_extern)
|
|
|
|
|
function_definitions += prototype_and_header.second
|
2016-03-19 21:45:07 -04:00
|
|
|
if (backing.body_statement) {
|
2017-12-27 16:49:08 -05:00
|
|
|
function_definitions += " {\n" + generate(backing.body_statement)
|
2016-07-03 22:50:42 -07:00
|
|
|
function_definitions += ";\n}\n"
|
2016-03-19 21:45:07 -04:00
|
|
|
}
|
2016-01-21 12:54:21 -05:00
|
|
|
}
|
|
|
|
|
|
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
|
2018-05-22 19:43:54 -04:00
|
|
|
name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree<symbol>,*ast_node>) {
|
2016-01-04 00:38:59 -05:00
|
|
|
// iterate through children for each ast
|
2016-03-10 04:49:38 -05:00
|
|
|
// do lambdas seperatly, so we can reconstitute the enclosing object if it has one
|
|
|
|
|
tree_pair.second->translation_unit.lambdas.for_each(fun(child: *ast_node) {
|
2017-12-27 16:49:08 -05:00
|
|
|
generate_function_definition(child)
|
2016-03-10 04:49:38 -05:00
|
|
|
})
|
|
|
|
|
tree_pair.second->translation_unit.children.for_each(fun(child: *ast_node) {
|
2016-01-04 00:38:59 -05:00
|
|
|
match (*child) {
|
2017-10-22 21:41:58 -04:00
|
|
|
ast_node::if_comp(backing) error("if_comp not currently supported")
|
2017-10-23 01:08:25 -04:00
|
|
|
ast_node::simple_passthrough(backing) error("simple_passthrough removed")
|
2017-12-27 16:49:08 -05:00
|
|
|
ast_node::declaration_statement(backing) variable_declarations += generate_declaration_statement(child) + ";\n" // false - don't do defer
|
2016-04-30 16:52:56 -04:00
|
|
|
// shouldn't need to do anything with return, as the intrinsic should be something like link
|
|
|
|
|
ast_node::compiler_intrinsic(backing) generate_compiler_intrinsic(child)
|
2016-01-06 02:46:42 -05:00
|
|
|
ast_node::function(backing) {
|
|
|
|
|
// check for and add to parameters if a closure
|
2017-12-27 16:49:08 -05:00
|
|
|
generate_function_definition(child)
|
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) {
|
2017-12-27 16:49:08 -05:00
|
|
|
ast_node::function(backing) generate_function_definition(node)
|
2016-02-20 02:36:35 -05:00
|
|
|
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
|
|
|
}
|
2017-10-23 01:08:25 -04:00
|
|
|
ast_node::adt_def(backing) error("ADT remaining!")
|
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 = get_name(vert)
|
2018-05-22 19:43:54 -04:00
|
|
|
plain_typedefs += str("typedef ")
|
2017-10-22 21:41:58 -04:00
|
|
|
if (vert->type_def.is_union) {
|
2016-06-16 23:06:38 -07:00
|
|
|
plain_typedefs += "union "
|
|
|
|
|
structs += "union "
|
|
|
|
|
} else {
|
2016-06-15 22:26:03 -07:00
|
|
|
plain_typedefs += "struct "
|
|
|
|
|
structs += "struct "
|
2016-06-16 23:06:38 -07:00
|
|
|
}
|
2016-06-15 22:26:03 -07:00
|
|
|
plain_typedefs += base_name + "_dummy " + base_name + ";\n"
|
|
|
|
|
structs += base_name + "_dummy {\n"
|
2017-12-27 16:49:08 -05:00
|
|
|
vert->type_def.variables.for_each(fun(variable_declaration: *ast_node) structs += generate_declaration_statement(variable_declaration) + ";\n";)
|
2017-10-22 21:41:58 -04: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))
|
2017-12-27 16:49:08 -05:00
|
|
|
method->template.instantiated.for_each(fun(m: *ast_node) generate_function_definition(m);)
|
2017-10-22 21:41:58 -04:00
|
|
|
else
|
2017-12-27 16:49:08 -05:00
|
|
|
generate_function_definition(method);
|
2017-10-22 21:41:58 -04:00
|
|
|
})
|
2016-01-21 12:54:21 -05:00
|
|
|
structs += "};\n"
|
2016-01-20 13:50:40 -05:00
|
|
|
})
|
2016-01-04 00:38:59 -05:00
|
|
|
|
2018-01-02 23:22:46 -05:00
|
|
|
return make_pair(prequal+plain_typedefs+function_typedef_string+top_level_c_passthrough+variable_extern_declarations+structs+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)
|
2016-02-22 16:18:55 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_declaration_statement(node: *ast_node): str {
|
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
|
2017-10-23 00:06:25 -04:00
|
|
|
var to_ret = type_to_c(identifier->identifier.type) + " " + get_name(identifier)
|
2017-01-23 01:09:31 -05:00
|
|
|
if (identifier->identifier.is_extern)
|
2017-10-23 00:06:25 -04:00
|
|
|
to_ret = "extern " + to_ret
|
2016-01-31 19:29:08 -05:00
|
|
|
if (node->declaration_statement.expression) {
|
2017-10-23 01:08:25 -04:00
|
|
|
// in case of recursive closures, make sure variable is declared before assignment
|
2018-03-06 23:30:00 -05:00
|
|
|
/*to_ret += ";\n"*/
|
|
|
|
|
/*to_ret += get_name(identifier) + " = " + generate(node->declaration_statement.expression)*/
|
|
|
|
|
to_ret += " = " + generate(node->declaration_statement.expression)
|
2016-01-31 19:29:08 -05:00
|
|
|
}
|
2016-02-25 14:24:55 -05:00
|
|
|
if (node->declaration_statement.init_method_call) {
|
2017-11-03 00:39:58 -04:00
|
|
|
error("init_method_call remaining")
|
2016-02-25 14:24:55 -05:00
|
|
|
}
|
2016-01-17 01:10:09 -05:00
|
|
|
return to_ret
|
2016-01-15 19:10:52 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_assignment_statement(node: *ast_node): str {
|
2017-12-27 16:49:08 -05:00
|
|
|
return generate(node->assignment_statement.to) + " = " + generate(node->assignment_statement.from)
|
2016-01-16 22:14:59 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_if_statement(node: *ast_node): str {
|
2017-12-27 16:49:08 -05:00
|
|
|
var if_str = "if (" + generate(node->if_statement.condition) + ") {\n" + generate(node->if_statement.then_part) + "}"
|
2016-01-19 02:06:30 -05:00
|
|
|
if (node->if_statement.else_part)
|
2017-12-27 16:49:08 -05:00
|
|
|
if_str += " else {\n" + generate(node->if_statement.else_part) + "}"
|
2016-01-19 02:06:30 -05:00
|
|
|
return if_str + "\n"
|
|
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_while_loop(node: *ast_node): str {
|
2017-12-27 16:49:08 -05:00
|
|
|
return "while (" + generate(node->while_loop.condition) + ")\n" + generate(node->while_loop.statement)
|
2016-01-19 03:16:16 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_for_loop(node: *ast_node): str {
|
|
|
|
|
var init = str(";")
|
2016-06-25 16:02:53 -07:00
|
|
|
if (node->for_loop.init)
|
2017-12-27 16:49:08 -05:00
|
|
|
init = generate(node->for_loop.init)
|
2018-05-22 19:43:54 -04:00
|
|
|
var cond = str(";")
|
2016-06-25 16:02:53 -07:00
|
|
|
if (node->for_loop.condition)
|
2017-12-27 16:49:08 -05:00
|
|
|
cond = generate(node->for_loop.condition)
|
2017-10-23 01:08:25 -04:00
|
|
|
// gotta take off last semicolon
|
2018-05-22 19:43:54 -04:00
|
|
|
var update = str()
|
2017-01-23 23:00:26 -05:00
|
|
|
if (node->for_loop.update) {
|
2017-12-27 16:49:08 -05:00
|
|
|
update = generate(node->for_loop.update)
|
2017-01-23 23:00:26 -05:00
|
|
|
if (update.length() < 2)
|
|
|
|
|
error("update less than 2! Likely legal, but need easy compiler mod here")
|
|
|
|
|
update = update.slice(0,-2)
|
|
|
|
|
}
|
2017-12-27 16:49:08 -05:00
|
|
|
return "for (" + init + cond + "; " + update + ")\n" + generate(node->for_loop.body)
|
2016-01-19 11:47:09 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_identifier(node: *ast_node): str {
|
2017-06-06 01:33:18 -04:00
|
|
|
if (get_ast_type(node)->is_ref)
|
2017-01-20 01:11:06 -05:00
|
|
|
error("still existin ref in identifier")
|
2017-10-23 00:06:25 -04:00
|
|
|
return get_name(node)
|
2016-01-16 22:14:59 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_return_statement(node: *ast_node): str {
|
2017-12-27 16:49:08 -05:00
|
|
|
if (node->return_statement.return_value)
|
|
|
|
|
return "return " + generate(node->return_statement.return_value)
|
2018-05-22 19:43:54 -04:00
|
|
|
return str("return")
|
2016-01-25 13:48:27 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_branching_statement(node: *ast_node): str {
|
2016-01-24 17:31:41 -05:00
|
|
|
match(node->branching_statement.b_type) {
|
2018-05-22 19:43:54 -04:00
|
|
|
branching_type::break_stmt() return str("break")
|
|
|
|
|
branching_type::continue_stmt() return str("continue")
|
2016-01-24 17:31:41 -05:00
|
|
|
}
|
|
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_cast(node: *ast_node): str {
|
2017-12-27 16:49:08 -05:00
|
|
|
return "((" + type_to_c(node->cast.to_type) + ")(" + generate(node->cast.value) + "))"
|
2016-04-18 22:56:29 -04:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_value(node: *ast_node): str {
|
2016-02-15 23:12:56 -05:00
|
|
|
var value = node->value.string_value
|
2018-03-22 00:51:57 -04:00
|
|
|
if (node->value.value_type->base == base_type::character() && node->value.value_type->indirection == 0)
|
|
|
|
|
return "'" + value + "'"
|
2018-03-19 21:03:06 -04:00
|
|
|
if (node->value.value_type->base != base_type::character() || node->value.value_type->indirection != 1)
|
2017-10-29 17:53:30 -04:00
|
|
|
return value
|
|
|
|
|
|
2018-05-22 19:43:54 -04:00
|
|
|
var to_ret = str("\"") //"
|
2017-10-29 17:53:30 -04:00
|
|
|
value.for_each(fun(c: char) {
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
to_ret += "\\n"
|
2018-03-19 21:03:06 -04:00
|
|
|
else if (c == '\\')
|
|
|
|
|
to_ret += "\\\\"
|
|
|
|
|
else if (c == '"')
|
2017-10-29 17:53:30 -04:00
|
|
|
to_ret += "\\\""
|
2016-03-19 21:45:07 -04:00
|
|
|
else
|
2017-10-29 17:53:30 -04:00
|
|
|
to_ret += c
|
|
|
|
|
})
|
|
|
|
|
return to_ret + "\""
|
2016-02-15 23:12:56 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_code_block(node: *ast_node): str {
|
|
|
|
|
var to_ret = str("{\n")
|
2017-12-27 16:49:08 -05:00
|
|
|
node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child) + ";\n";)
|
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
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_function(node: *ast_node): str {
|
2017-10-23 00:06:25 -04:00
|
|
|
return get_name(node)
|
2016-01-11 23:41:09 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_function_call(node: *ast_node): str {
|
2017-12-27 16:49:08 -05:00
|
|
|
var func_name = generate(node->function_call.func)
|
2018-05-22 19:43:54 -04:00
|
|
|
var call_string = str()
|
2016-01-31 19:29:08 -05:00
|
|
|
var func_return_type = get_ast_type(node)
|
2016-01-24 01:49:14 -05:00
|
|
|
|
2016-01-19 11:47:09 -05:00
|
|
|
var parameters = node->function_call.parameters
|
2016-03-09 15:21:50 -05:00
|
|
|
if ( parameters.size == 2 && (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/"
|
|
|
|
|
|| func_name == "<" || func_name == ">" || func_name == "<=" || func_name == ">="
|
2016-04-19 18:39:01 -04:00
|
|
|
|| func_name == "==" || func_name == "!=" || func_name == "%" || func_name == "^"
|
2017-01-23 01:09:31 -05:00
|
|
|
|| func_name == "|" || func_name == "&" || func_name == ">>" || func_name == "<<"
|
2016-01-28 12:55:51 -05:00
|
|
|
))
|
2017-12-27 16:49:08 -05:00
|
|
|
return "(" + generate(parameters[0]) + func_name + generate(parameters[1]) + ")"
|
2017-10-23 01:08:25 -04:00
|
|
|
if ( parameters.size == 2 && (func_name == "||" || func_name == "&&"))
|
2016-06-25 23:56:07 -07:00
|
|
|
error("Remaining || or &&")
|
2016-01-23 20:39:06 -05:00
|
|
|
// don't propegate enclosing function down right of access
|
2016-03-08 16:04:59 -05:00
|
|
|
// XXX what about enclosing object? should it be the thing on the left?
|
2017-10-23 01:08:25 -04:00
|
|
|
if (func_name == "." || func_name == "->")
|
2017-12-27 16:49:08 -05:00
|
|
|
return "(" + generate(parameters[0]) + func_name + generate(parameters[1]) + ")"
|
2016-03-02 20:23:25 -05:00
|
|
|
if (func_name == "[]")
|
2017-12-27 16:49:08 -05:00
|
|
|
return "(" + generate(parameters[0]) + "[" + generate(parameters[1]) + "])"
|
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")
|
2017-12-27 16:49:08 -05:00
|
|
|
return "(" + generate(parameters[0]) + ")" + 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 == "&")
|
2017-12-27 16:49:08 -05:00
|
|
|
return "(" + func_name + generate(parameters[0]) + ")"
|
2016-01-30 23:59:21 -05:00
|
|
|
|
2016-03-01 14:54:58 -05:00
|
|
|
var func_type = get_ast_type(node->function_call.func)
|
2016-01-24 01:49:14 -05:00
|
|
|
// regular parameter generation
|
2016-03-01 14:54:58 -05:00
|
|
|
for (var i = 0; i < parameters.size; i++;) {
|
|
|
|
|
var param = parameters[i]
|
2016-05-19 23:17:32 -07:00
|
|
|
var in_function_param_type = null<type>()
|
|
|
|
|
// grab type from param itself if we're out of param types (because variadic function)
|
|
|
|
|
if (i < func_type->parameter_types.size)
|
|
|
|
|
in_function_param_type = func_type->parameter_types[i]
|
|
|
|
|
else
|
|
|
|
|
in_function_param_type = get_ast_type(param)->clone_without_ref()
|
2016-01-11 23:41:09 -05:00
|
|
|
if (call_string != "")
|
|
|
|
|
call_string += ", "
|
2016-03-19 21:45:07 -04:00
|
|
|
|
2017-12-27 16:49:08 -05:00
|
|
|
call_string += generate(param)
|
2016-03-01 14:54:58 -05:00
|
|
|
}
|
2017-10-23 00:06:25 -04:00
|
|
|
call_string = func_name + "(" + call_string + ")"
|
2016-03-10 16:07:12 -05:00
|
|
|
return call_string
|
2016-01-11 23:41:09 -05:00
|
|
|
}
|
2017-08-27 14:15:09 -04:00
|
|
|
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate_compiler_intrinsic(node: *ast_node): str {
|
2016-04-28 18:47:53 -04:00
|
|
|
if (node->compiler_intrinsic.intrinsic == "sizeof") {
|
|
|
|
|
if (node->compiler_intrinsic.parameters.size || node->compiler_intrinsic.type_parameters.size != 1)
|
|
|
|
|
error("wrong parameters to sizeof compiler intrinsic")
|
2017-10-23 00:06:25 -04:00
|
|
|
return "sizeof(" + type_to_c(node->compiler_intrinsic.type_parameters[0]) + ")"
|
2016-04-30 16:52:56 -04:00
|
|
|
} else if (node->compiler_intrinsic.intrinsic == "link") {
|
2016-07-03 22:50:42 -07:00
|
|
|
node->compiler_intrinsic.parameters.for_each(fun(value: *ast_node) {
|
2018-05-22 19:43:54 -04:00
|
|
|
linker_string += str("-l") + value->value.string_value + " "
|
2016-04-30 16:52:56 -04:00
|
|
|
})
|
2018-05-22 19:43:54 -04:00
|
|
|
return str()
|
2016-04-28 18:47:53 -04:00
|
|
|
}
|
|
|
|
|
error(node->compiler_intrinsic.intrinsic + ": unknown intrinsic")
|
2018-05-22 19:43:54 -04:00
|
|
|
return str("ERROR")
|
2016-04-28 18:47:53 -04:00
|
|
|
}
|
2016-01-07 02:52:22 -05:00
|
|
|
|
2018-05-22 19:43:54 -04:00
|
|
|
fun generate(node: *ast_node): str {
|
|
|
|
|
if (!node) return str("/*NULL*/")
|
2016-01-04 02:00:06 -05:00
|
|
|
match (*node) {
|
2017-12-27 16:49:08 -05:00
|
|
|
ast_node::declaration_statement(backing) return generate_declaration_statement(node)
|
|
|
|
|
ast_node::assignment_statement(backing) return generate_assignment_statement(node)
|
|
|
|
|
ast_node::if_statement(backing) return generate_if_statement(node)
|
|
|
|
|
ast_node::while_loop(backing) return generate_while_loop(node)
|
|
|
|
|
ast_node::for_loop(backing) return generate_for_loop(node)
|
|
|
|
|
ast_node::function(backing) return generate_function(node)
|
|
|
|
|
ast_node::function_call(backing) return generate_function_call(node)
|
|
|
|
|
ast_node::compiler_intrinsic(backing) return generate_compiler_intrinsic(node)
|
|
|
|
|
ast_node::code_block(backing) return generate_code_block(node)
|
|
|
|
|
ast_node::return_statement(backing) return generate_return_statement(node)
|
|
|
|
|
ast_node::branching_statement(backing) return generate_branching_statement(node)
|
|
|
|
|
ast_node::defer_statement(backing) error("unremoved defer")
|
|
|
|
|
ast_node::match_statement(backing) error("unremoved match")
|
|
|
|
|
ast_node::cast(backing) return generate_cast(node)
|
|
|
|
|
ast_node::value(backing) return generate_value(node)
|
|
|
|
|
ast_node::identifier(backing) return generate_identifier(node)
|
2016-01-04 02:00:06 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
error(str("COULD NOT GENERATE ") + get_ast_name(node))
|
|
|
|
|
return str("/* COULD NOT GENERATE */")
|
2016-01-04 02:00:06 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun type_to_c(type: *type): str {
|
|
|
|
|
var indirection = str()
|
2017-01-20 01:11:06 -05:00
|
|
|
if (type->is_ref) error("still ref in type_to_c") //indirection += "/*ref*/ *"
|
2016-01-10 18:26:31 -05:00
|
|
|
for (var i = 0; i < type->indirection; i++;) indirection += "*"
|
2016-01-06 02:46:42 -05:00
|
|
|
match (type->base) {
|
2018-05-22 19:43:54 -04:00
|
|
|
base_type::none() return str("none") + indirection
|
|
|
|
|
base_type::template() return str("template") + indirection
|
|
|
|
|
base_type::template_type() return str("template_type") + indirection
|
|
|
|
|
base_type::void_return() return str("void") + indirection
|
|
|
|
|
base_type::boolean() return str("bool") + indirection
|
|
|
|
|
base_type::character() return str("char") + indirection
|
|
|
|
|
base_type::ucharacter() return str("unsigned char") + indirection
|
|
|
|
|
base_type::short_int() return str("short") + indirection
|
|
|
|
|
base_type::ushort_int() return str("unsigned short") + indirection
|
|
|
|
|
base_type::integer() return str("int") + indirection
|
|
|
|
|
base_type::uinteger() return str("unsigned int") + indirection
|
|
|
|
|
base_type::long_int() return str("long") + indirection
|
|
|
|
|
base_type::ulong_int() return str("unsigned long") + indirection
|
|
|
|
|
base_type::floating() return str("float") + indirection
|
|
|
|
|
base_type::double_precision() return str("double") + indirection
|
2017-12-27 16:49:08 -05:00
|
|
|
base_type::object() return get_name(type->type_def) + indirection
|
2016-01-06 02:46:42 -05:00
|
|
|
base_type::function() {
|
2016-06-14 02:14:25 -07:00
|
|
|
type = type->clone_with_indirection(0,false)
|
|
|
|
|
if (!function_type_map.contains_key(*type)) {
|
2018-05-22 19:43:54 -04:00
|
|
|
var temp_name = str("function") + get_id()
|
|
|
|
|
var temp = str()
|
2017-10-29 17:53:30 -04:00
|
|
|
type->parameter_types.for_each(fun(parameter_type: *type) {
|
2018-05-22 19:43:54 -04:00
|
|
|
temp += str(", ") + type_to_c(parameter_type) + " "
|
2017-10-29 17:53:30 -04:00
|
|
|
temp_name += "_" + cify_name(type_to_c(parameter_type))
|
|
|
|
|
})
|
|
|
|
|
if (type->is_raw)
|
2018-05-22 19:43:54 -04:00
|
|
|
function_typedef_string += str("typedef ") + type_to_c(type->return_type) + " (*" + temp_name + ")(" + temp.slice(1,-1) + ");\n"
|
2017-10-29 17:53:30 -04:00
|
|
|
else
|
2017-08-15 19:53:17 -04:00
|
|
|
error(type->to_string() + " is not raw!")
|
2016-06-14 02:14:25 -07:00
|
|
|
// again, the indirection
|
|
|
|
|
function_type_map[*type] = temp_name
|
|
|
|
|
}
|
|
|
|
|
return function_type_map[*type] + indirection
|
2016-01-06 02:46:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
return str("impossible type") + indirection
|
2016-01-06 02:46:42 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun type_decoration(type: *type): str {
|
2018-06-12 23:29:39 -04:00
|
|
|
return cify_name(type->to_string())
|
2017-10-29 17:53:30 -04:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun get_name(node: *ast_node): str {
|
2017-01-24 22:11:33 -05:00
|
|
|
var maybe_it = ast_name_map.get_ptr_or_null(node);
|
|
|
|
|
if (maybe_it)
|
|
|
|
|
return *maybe_it
|
2018-05-22 19:43:54 -04:00
|
|
|
var result = str("impossible name")
|
2016-06-11 00:45:18 -07:00
|
|
|
var make_unique = true
|
2016-02-15 16:31:01 -05:00
|
|
|
match (*node) {
|
|
|
|
|
ast_node::type_def(backing) {
|
2018-05-22 19:43:54 -04:00
|
|
|
var upper = backing.scope[str("~enclosing_scope")][0]
|
2016-03-24 21:32:28 -04:00
|
|
|
result = cify_name(backing.name)
|
2017-01-24 22:11:33 -05:00
|
|
|
if (is_template(upper))
|
2018-05-22 19:43:54 -04:00
|
|
|
upper->template.instantiated_map.reverse_get(node).for_each(fun(t: ref type) result += str("_") + type_decoration(&t);)
|
2016-03-19 21:45:07 -04:00
|
|
|
}
|
2016-02-15 16:31:01 -05:00
|
|
|
ast_node::function(backing) {
|
2017-10-29 17:53:30 -04:00
|
|
|
// be careful, operators like . come through this
|
|
|
|
|
if (backing.name == "main" || backing.is_extern || !backing.body_statement) {
|
2016-06-11 00:45:18 -07:00
|
|
|
result = backing.name
|
|
|
|
|
make_unique = false
|
|
|
|
|
} else {
|
|
|
|
|
result = "fun_"
|
2018-05-22 19:43:54 -04:00
|
|
|
var upper = backing.scope.get_with_default(str("~enclosing_scope"), vec(null<ast_node>()))[0]
|
2017-01-24 22:11:33 -05:00
|
|
|
if (upper && is_type_def(upper))
|
|
|
|
|
result += get_name(upper) + "_"
|
2016-06-11 00:45:18 -07:00
|
|
|
result += cify_name(node->function.name)
|
2018-05-22 19:43:54 -04:00
|
|
|
node->function.parameters.for_each(fun(param: *ast_node) result += str("_") + type_decoration(param->identifier.type);)
|
2016-06-11 00:45:18 -07:00
|
|
|
}
|
2016-02-15 16:31:01 -05:00
|
|
|
}
|
2016-02-17 13:59:10 -05:00
|
|
|
ast_node::identifier(backing) {
|
2017-01-23 01:09:31 -05:00
|
|
|
if (backing.name == "this" || backing.is_extern)
|
2016-06-11 00:45:18 -07:00
|
|
|
make_unique = false
|
2016-02-17 13:59:10 -05:00
|
|
|
result = backing.name
|
|
|
|
|
}
|
2016-02-15 16:31:01 -05:00
|
|
|
}
|
2016-02-17 13:37:48 -05:00
|
|
|
if (result == "impossible name")
|
2016-02-29 19:18:22 -05:00
|
|
|
error("HUGE PROBLEMS")
|
2018-01-02 23:22:46 -05:00
|
|
|
if (make_unique && used_names.contains(result))
|
2017-01-24 22:11:33 -05:00
|
|
|
result += get_id()
|
|
|
|
|
ast_name_map.set(node, result)
|
2017-10-28 15:28:34 -04:00
|
|
|
used_names.add(result)
|
2016-02-17 13:37:48 -05:00
|
|
|
return result
|
2016-02-15 16:31:01 -05:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun cify_name(name: str): str {
|
|
|
|
|
var to_ret = str()
|
2016-02-24 15:25:58 -05:00
|
|
|
for (var i = 0; i < name.length(); i++;) {
|
|
|
|
|
var replaced = false
|
|
|
|
|
for (var j = longest_replacement; j > 0; j--;) {
|
|
|
|
|
if (i + j <= name.length() && replacement_map.contains_key(name.slice(i,i+j))) {
|
|
|
|
|
to_ret += replacement_map[name.slice(i,i+j)]
|
|
|
|
|
replaced = true
|
|
|
|
|
i += j-1;
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!replaced)
|
|
|
|
|
to_ret += name[i]
|
|
|
|
|
}
|
|
|
|
|
return to_ret
|
|
|
|
|
}
|
2016-01-04 00:38:59 -05:00
|
|
|
}
|