working towards function overloading, the emitted functions and function calls are now decorated and we're starting to get the prereqs for function lookup

This commit is contained in:
Nathan Braswell
2016-01-13 21:09:28 -05:00
parent cb5b072b58
commit fe6818edfc
5 changed files with 48 additions and 7 deletions

View File

@@ -943,6 +943,14 @@ fun get_ast_scope(node: *ast_node): *map<string,vector<*ast_node>> {
ast_node::value() return &node->value.scope ast_node::value() return &node->value.scope
} }
} }
fun get_ast_type(node: *ast_node): *type {
match (*node) {
ast_node::identifier() return node->identifier.type
ast_node::function() return node->function.type
ast_node::function_call() return get_ast_type(node->function_call.func)
ast_node::value() return node->value.value_type
}
}
fun ast_to_dot(root: *ast_node): string { fun ast_to_dot(root: *ast_node): string {
var ret = string("digraph Kaken {\n") var ret = string("digraph Kaken {\n")

View File

@@ -269,10 +269,11 @@ obj ast_transformation (Object) {
return ast_return_statement_ptr(transform(node->children[0], scope)) return ast_return_statement_ptr(transform(node->children[0], scope))
} }
fun transform_function_call(node: *tree<symbol>, scope: *ast_node): *ast_node { fun transform_function_call(node: *tree<symbol>, scope: *ast_node): *ast_node {
// don't bother a full transform for parameter, just get the boolean expression from it // don't bother with a full transform for parameters with their own function, just get the boolean expression and transform it
var parameters = get_nodes("parameter", node).map(fun(child: *tree<symbol>): *ast_node return transform(get_node("boolean_expression", child), scope);) var parameters = get_nodes("parameter", node).map(fun(child: *tree<symbol>): *ast_node return transform(get_node("boolean_expression", child), scope);)
var parameter_types = parameters.map(fun(param: *ast_node): *type return get_ast_type(param);)
/*return ast_function_call_ptr(function_lookup(concat_symbol_tree(get_node("unarad", node)), scope), parameters)*/ /*return ast_function_call_ptr(function_lookup(concat_symbol_tree(get_node("unarad", node)), scope), parameters)*/
var f = ast_function_call_ptr(function_lookup(concat_symbol_tree(get_node("unarad", node)), scope), parameters) var f = ast_function_call_ptr(function_lookup(concat_symbol_tree(get_node("unarad", node)), scope, parameter_types), parameters)
print("function call function ") print("function call function ")
println(f->function_call.func) println(f->function_call.func)
print("function call parameters ") print("function call parameters ")
@@ -287,7 +288,7 @@ obj ast_transformation (Object) {
} }
return results[0] return results[0]
} }
fun function_lookup(name: string, scope: *ast_node): *ast_node { fun function_lookup(name: string, scope: *ast_node, param_types: vector<*type>): *ast_node {
var results = scope_lookup(name, scope) var results = scope_lookup(name, scope)
if (!results.size) { if (!results.size) {
println(string("identifier lookup failed for ") + name) println(string("identifier lookup failed for ") + name)

View File

@@ -53,16 +53,17 @@ obj c_generator (Object) {
// check for and add to parameters if a closure // check for and add to parameters if a closure
var parameter_types = string() var parameter_types = string()
var parameters = string() var parameters = string()
var decorated_name = generate_function(child)
// also add in name decoration // also add in name decoration
backing.parameters.for_each(fun(parameter: *ast_node) { backing.parameters.for_each(fun(parameter: *ast_node) {
if (parameter_types != "") { parameter_types += ", "; parameters += ", ";} if (parameter_types != "") { parameter_types += ", "; parameters += ", ";}
parameter_types += type_to_c(parameter->identifier.type) parameter_types += type_to_c(parameter->identifier.type)
parameters += type_to_c(parameter->identifier.type) + " " + parameter->identifier.name parameters += type_to_c(parameter->identifier.type) + " " + parameter->identifier.name
}) })
function_prototypes += type_to_c(backing.type->return_type) + " " + backing.name + "(" + parameter_types + ");\n" function_prototypes += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameter_types + ");\n"
// add parameters to destructor thingy (for returns)? Or should that be a different pass? // add parameters to destructor thingy (for returns)? Or should that be a different pass?
function_definitions += type_to_c(backing.type->return_type) + " " + backing.name + "(" + parameters + ") {\n" + generate_statement(backing.body_statement) function_definitions += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameters + ") {\n" + generate_statement(backing.body_statement)
// emit parameter destructors? // emit parameter destructors?
function_definitions += "}\n" function_definitions += "}\n"
} }
@@ -91,7 +92,10 @@ obj c_generator (Object) {
} }
// this generates the function as a value, not the actuall function // this generates the function as a value, not the actuall function
fun generate_function(node: *ast_node): string { fun generate_function(node: *ast_node): string {
return node->function.name var str = node->function.name
node->function.parameters.for_each(fun(param: *ast_node) str += string("_") + type_decoration(param->identifier.type);)
return str
} }
fun generate_function_call(node: *ast_node): string { fun generate_function_call(node: *ast_node): string {
var call_string = string() var call_string = string()
@@ -118,6 +122,28 @@ obj c_generator (Object) {
} }
return string("/* COULD NOT GENERATE */") return string("/* COULD NOT GENERATE */")
} }
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
}
fun type_to_c(type: *type): string { fun type_to_c(type: *type): string {
var indirection = string() var indirection = string()
for (var i = 0; i < type->indirection; i++;) indirection += "*" for (var i = 0; i < type->indirection; i++;) indirection += "*"

View File

@@ -4,6 +4,11 @@ fun simple_print(to_print: *char) {
printf("%s", to_print); printf("%s", to_print);
""" """
} }
fun simple_print(to_print: int) {
__if_comp__ __C__ simple_passthrough(to_print::) """
printf("%d", to_print);
"""
}
var a = 1 var a = 1
var b = 2 var b = 2

View File

@@ -12,7 +12,8 @@ fun some_other_function(in: bool): float {
return 0.0 return 0.0
} }
fun main(): int { fun main(): int {
simple_print("Hello World!") simple_print("Hello World!\n")
simple_print(1337)
return 0 return 0
} }