Cephelapod-compiled Kalypso will compile Kalypso, but the result segfaults

This commit is contained in:
Nathan Braswell
2016-03-26 04:39:26 -04:00
parent 7d749e7ca5
commit 1cc8fd294e
3 changed files with 79 additions and 30 deletions

View File

@@ -342,19 +342,22 @@ obj ast_transformation (Object) {
} else { } else {
print("Not using cached template - was looking for:\n\t\t") print("Not using cached template - was looking for:\n\t\t")
var typeStr = string() var typeStr = string()
real_types_deref.for_each(fun(t: type) typeStr += t.to_string() + ", ";) real_types_deref.for_each(fun(t: type) typeStr += t.to_string(false) + " ";)
print(typeStr) print(typeStr)
println("\ninstead, only had:") println("\ninstead, only had:")
results[i]->template.instantiated_map.for_each(fun(key: vector<type>, value: *ast_node) { results[i]->template.instantiated_map.for_each(fun(key: vector<type>, value: *ast_node) {
print("\t\t") print("\t\t")
var hasTypStr = string() var hasTypStr = string()
key.for_each(fun(t: type) hasTypStr += t.to_string() + ", ";) key.for_each(fun(t: type) hasTypStr += t.to_string(false) + " ";)
print(hasTypStr) print(hasTypStr)
if (typeStr == hasTypStr) if (typeStr == hasTypStr)
error("they're equal but really shouldnt be") error("they're equal but really shouldnt be")
println() println()
}) })
println("donr") println("donr")
if (real_types.any_true(fun(t: *type): bool return t->is_none() || t ->is_template_type();)) {
error("Instantiating types for templated object are not all real types!")
}
inst_type = first_pass_type_def(results[i]->template.syntax_node, results[i], true) inst_type = first_pass_type_def(results[i]->template.syntax_node, results[i], true)
// no change up it's name so we can see that it's instantiated when printed out and keep track of it // no change up it's name so we can see that it's instantiated when printed out and keep track of it
inst_type->type_def.name += "<" + typeStr + ">" inst_type->type_def.name += "<" + typeStr + ">"
@@ -787,10 +790,12 @@ obj ast_transformation (Object) {
if (is_template(enclosing) && is_type_def(enclosing->template.scope[string("~enclosing_scope")][0])) if (is_template(enclosing) && is_type_def(enclosing->template.scope[string("~enclosing_scope")][0]))
return set(make_this(enclosing->template.scope[string("~enclosing_scope")][0])) return set(make_this(enclosing->template.scope[string("~enclosing_scope")][0]))
} }
// if this is a lambda, we need to close over what it closes over // if this is a lambda, we need to check all of the things it closes over
// we don't need an if - if it's empty and not a lambda, it's empty // we don't need an if - if it's empty and not a lambda, it's empty
// and we don't close over actual functions // and we don't close over actual functions
return backing.closed_variables var to_ret = set<*ast_node>()
backing.closed_variables.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
} }
ast_node::return_statement(backing) { ast_node::return_statement(backing) {
println("found an return_statement") println("found an return_statement")
@@ -800,8 +805,16 @@ obj ast_transformation (Object) {
println("found an if statement") println("found an if statement")
return find_closed_variables(func, backing.condition) + find_closed_variables(func, backing.then_part) + find_closed_variables(func, backing.else_part) return find_closed_variables(func, backing.condition) + find_closed_variables(func, backing.then_part) + find_closed_variables(func, backing.else_part)
} }
// match_statement: match_statement, ast_node::match_statement(backing) {
// case_statement: case_statement, println("found an match_statement")
var to_ret = set<*ast_node>()
backing.cases.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::case_statement(backing) {
println("found a case_statement")
return find_closed_variables(func, backing.statement)
}
ast_node::while_loop(backing) { ast_node::while_loop(backing) {
println("found an while loop") println("found an while loop")
return find_closed_variables(func, backing.condition) + find_closed_variables(func, backing.statement) return find_closed_variables(func, backing.condition) + find_closed_variables(func, backing.statement)
@@ -925,8 +938,18 @@ obj ast_transformation (Object) {
var possible_overload = null<ast_node>() var possible_overload = null<ast_node>()
if ((parameter_types[0]->is_adt() || parameter_types[0]->is_object()) && parameter_types[0]->indirection == 0) { if ((parameter_types[0]->is_adt() || parameter_types[0]->is_object()) && parameter_types[0]->indirection == 0) {
possible_overload = function_lookup(string("operator")+func_name, parameter_types.first()->type_def, parameter_types.slice(1,-1)) possible_overload = function_lookup(string("operator")+func_name, parameter_types.first()->type_def, parameter_types.slice(1,-1))
if (!possible_overload) if (!possible_overload) {
possible_overload = find_or_instantiate_template_function(string("operator")+func_name, null<tree<symbol>>(), parameter_types.first()->type_def, parameter_types.slice(1,-1), template_replacements, map<string, *type>()) var inherited_replacements = map<string, *type>()
var parent = get_ast_scope(parameter_types.first()->type_def)->get(string("~enclosing_scope"))[0]
if (is_template(parent)) {
println("TEMPLATE type PARENT IS TEMPLATE")
for (var i = 0; i < parent->template.template_types.size; i++;)
inherited_replacements[parent->template.template_types[i]] = parent->template.instantiated_map.reverse_get(parameter_types.first()->type_def)[i].clone()
} else {
println("TEMPLATE type PARENT IS NOT TEMPLATE")
}
possible_overload = find_or_instantiate_template_function(string("operator")+func_name, null<tree<symbol>>(), parameter_types.first()->type_def, parameter_types.slice(1,-1), template_replacements, inherited_replacements)
}
if (possible_overload) if (possible_overload)
return make_method_call(parameters.first(), possible_overload, parameters.slice(1,-1)) return make_method_call(parameters.first(), possible_overload, parameters.slice(1,-1))
} }
@@ -1012,6 +1035,10 @@ obj ast_transformation (Object) {
println("Did not satisfy traits!") println("Did not satisfy traits!")
continue continue
} }
if (real_types.any_true(fun(t: *type): bool return t->is_none() || t ->is_template_type();)) {
println("Instantiating types not all real types!")
continue
}
if (results[i]->template.instantiated_map.contains_key(real_types_deref)) { if (results[i]->template.instantiated_map.contains_key(real_types_deref)) {
println("USING CACHED TEMPLATE FUNCITON") println("USING CACHED TEMPLATE FUNCITON")
@@ -1180,15 +1207,15 @@ fun identifier_lookup(name: string, scope: *ast_node): *ast_node {
return results[0] return results[0]
} }
fun scope_lookup(name: string, scope: *ast_node): vector<*ast_node> { fun scope_lookup(name: string, scope: *ast_node): vector<*ast_node> {
println("*****Doing a name lookup for*****") // println("*****Doing a name lookup for*****")
println(name) // println(name)
var results = vector(scope) var results = vector(scope)
name.split("::").for_each(fun(i: string) { name.split("::").for_each(fun(i: string) {
println(string("based on split, looking up: ") + i) // println(string("based on split, looking up: ") + i)
var next_results = vector<*ast_node>() var next_results = vector<*ast_node>()
results.for_each(fun(s: *ast_node) { results.for_each(fun(s: *ast_node) {
print("looking in scope: ") // print("looking in scope: ")
println(s) // println(s)
scope_lookup_helper(i, s, set<*ast_node>()).for_each(fun (result: *ast_node) { scope_lookup_helper(i, s, set<*ast_node>()).for_each(fun (result: *ast_node) {
if (!next_results.contains(result)) if (!next_results.contains(result))
next_results.add(result) next_results.add(result)
@@ -1200,16 +1227,16 @@ fun scope_lookup(name: string, scope: *ast_node): vector<*ast_node> {
} }
fun scope_lookup_helper(name: string, scope: *ast_node, visited: set<*ast_node>): vector<*ast_node> { fun scope_lookup_helper(name: string, scope: *ast_node, visited: set<*ast_node>): vector<*ast_node> {
// need to do properly scopded lookups // need to do properly scopded lookups
print("scope is: ") // print("scope is: ")
get_ast_scope(scope)->for_each(fun(key: string, value: vector<*ast_node>) print(key + " ");) // get_ast_scope(scope)->for_each(fun(key: string, value: vector<*ast_node>) print(key + " ");)
println() // println()
var results = vector<*ast_node>() var results = vector<*ast_node>()
// prevent re-checking the same one... // prevent re-checking the same one...
if (visited.contains(scope)) if (visited.contains(scope))
return results return results
visited.add(scope) visited.add(scope)
if (get_ast_scope(scope)->contains_key(name)) { if (get_ast_scope(scope)->contains_key(name)) {
println(name + " is in scope, adding to results") // println(name + " is in scope, adding to results")
results += get_ast_scope(scope)->get(name) results += get_ast_scope(scope)->get(name)
} }
if (get_ast_scope(scope)->contains_key(string("~enclosing_scope"))) if (get_ast_scope(scope)->contains_key(string("~enclosing_scope")))
@@ -1218,15 +1245,15 @@ fun scope_lookup_helper(name: string, scope: *ast_node, visited: set<*ast_node>)
scope->translation_unit.children.for_each(fun(child: *ast_node) { scope->translation_unit.children.for_each(fun(child: *ast_node) {
if (is_import(child)) { if (is_import(child)) {
if (child->import.imported.contains(name)) { if (child->import.imported.contains(name)) {
println(name + " is indeed imported") // println(name + " is indeed imported")
results += scope_lookup_helper(name, child->import.translation_unit, visited) results += scope_lookup_helper(name, child->import.translation_unit, visited)
} else if (child->import.starred) { } else if (child->import.starred) {
println("import has an import *, checking along it") // println("import has an import *, checking along it")
results += scope_lookup_helper(name, child->import.translation_unit, visited) results += scope_lookup_helper(name, child->import.translation_unit, visited)
} else { } else {
println(name + " is not imported (this time)") // println(name + " is not imported (this time)")
print("import imports") // print("import imports")
child->import.imported.for_each(fun(it: string) print(it + " ");) // child->import.imported.for_each(fun(it: string) print(it + " ");)
} }
} }
}) })

View File

@@ -131,7 +131,7 @@ obj c_generator (Object) {
replacement_map[string(")")] = string("closeparen") replacement_map[string(")")] = string("closeparen")
replacement_map[string("[")] = string("obk") replacement_map[string("[")] = string("obk")
replacement_map[string("]")] = string("cbk") replacement_map[string("]")] = string("cbk")
replacement_map[string(" ")] = string("space") replacement_map[string(" ")] = string("_")
replacement_map[string(".")] = string("dot") replacement_map[string(".")] = string("dot")
replacement_map[string("->")] = string("arrow") replacement_map[string("->")] = string("arrow")
@@ -264,6 +264,10 @@ obj c_generator (Object) {
equals_res.post += generate_from_defer_stack(&defer_stack, -1, enclosing_object, child).one_string() equals_res.post += generate_from_defer_stack(&defer_stack, -1, enclosing_object, child).one_string()
defer_stack.pop() defer_stack.pop()
function_definitions += equals_res.one_string() + "return result;\n" function_definitions += equals_res.one_string() + "return result;\n"
} else if (option_type->is_object()) {
// if we are an object but don't define an operator== function (or it is templated)
// always return false.
function_definitions += "return false;\n"
} else { } else {
var option_name = generate_identifier(option, null<ast_node>(), null<ast_node>()).one_string() var option_name = generate_identifier(option, null<ast_node>(), null<ast_node>()).one_string()
var param_name = generate_identifier(param, null<ast_node>(), null<ast_node>()).one_string() var param_name = generate_identifier(param, null<ast_node>(), null<ast_node>()).one_string()
@@ -644,14 +648,15 @@ obj c_generator (Object) {
to_ret = value; to_ret = value;
} else { } else {
to_ret = string("\"") to_ret = string("\"")
if (value.slice(0,3) == "\"\"\"") var triple_quoted = value.slice(0,3) == "\"\"\""
if (triple_quoted)
value = value.slice(3,-4) value = value.slice(3,-4)
else else
value = value.slice(1,-2) value = value.slice(1,-2)
value.for_each(fun(c: char) { value.for_each(fun(c: char) {
if (c == '\n') if (c == '\n')
to_ret += "\\n" to_ret += "\\n"
else if (c == '"') else if (c == '"' && triple_quoted)
to_ret += "\\\"" to_ret += "\\\""
else else
to_ret += c to_ret += c

View File

@@ -138,10 +138,15 @@ obj type (Object) {
return base == other.base && deref_equality(return_type, other.return_type) && return base == other.base && deref_equality(return_type, other.return_type) &&
indirection == other.indirection && deref_equality(type_def, other.type_def) && traits == other.traits indirection == other.indirection && deref_equality(type_def, other.type_def) && traits == other.traits
} }
fun to_string(): string { fun to_string(): string return to_string(true);
var trait_string = string(":[") fun to_string(include_traits: bool): string {
var trait_string = string()
if (include_traits) {
trait_string = string(":[")
traits.for_each(fun(t: string) trait_string += t;) traits.for_each(fun(t: string) trait_string += t;)
trait_string += "] " trait_string += "] "
}
var indr_string = string("") var indr_string = string("")
if (is_ref) if (is_ref)
indr_string += " ref " indr_string += " ref "
@@ -221,5 +226,17 @@ obj type (Object) {
} }
return false return false
} }
fun is_none(): bool {
match (base) {
base_type::none() return true
}
return false
}
fun is_template_type(): bool {
match (base) {
base_type::template_type() return true
}
return false
}
} }