References working, pass test_references, 46 tests passing

This commit is contained in:
Nathan Braswell
2016-03-01 14:54:58 -05:00
parent 2fb8dab08d
commit 84cbcc3820
3 changed files with 78 additions and 38 deletions

View File

@@ -233,6 +233,7 @@ obj ast_transformation (Object) {
fun transform_type(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *type {
// check for references and step down
// always get to pre-reffed level
var is_ref = get_node("\"ref\"", node) != null<tree<symbol>>()
var real_node = get_node("pre_reffed", node)
// check for indirection and step down
var indirection = 0
@@ -302,43 +303,43 @@ obj ast_transformation (Object) {
error("FREAK OUT AUTOMATON")
return null<type>()
}
return fitting_types.max(fun(a: pair<*ast_node, int>, b: pair<*ast_node, int>): bool return a.second < b.second;).first->type_def.self_type->clone_with_indirection(indirection)
return fitting_types.max(fun(a: pair<*ast_node, int>, b: pair<*ast_node, int>): bool return a.second < b.second;).first->type_def.self_type->clone_with_indirection(indirection, is_ref)
}
var type_syntax_str = concat_symbol_tree(real_node)
println(type_syntax_str + " *************************")
if (template_replacements.contains_key(type_syntax_str)) {
print("Is in template_replacements, returning: ")
var to_ret = template_replacements[type_syntax_str]->clone_with_increased_indirection(indirection)
var to_ret = template_replacements[type_syntax_str]->clone_with_increased_indirection(indirection, is_ref)
println(to_ret->to_string())
return to_ret
}
// should take into account references...
if (type_syntax_str == "void")
return type_ptr(base_type::void_return(), indirection)
return type_ptr(base_type::void_return(), indirection, is_ref)
else if (type_syntax_str == "bool")
return type_ptr(base_type::boolean(), indirection)
return type_ptr(base_type::boolean(), indirection, is_ref)
else if (type_syntax_str == "int")
return type_ptr(base_type::integer(), indirection)
return type_ptr(base_type::integer(), indirection, is_ref)
else if (type_syntax_str == "float")
return type_ptr(base_type::floating(), indirection)
return type_ptr(base_type::floating(), indirection, is_ref)
else if (type_syntax_str == "double")
return type_ptr(base_type::double_precision(), indirection)
return type_ptr(base_type::double_precision(), indirection, is_ref)
else if (type_syntax_str == "char")
return type_ptr(base_type::character(), indirection)
return type_ptr(base_type::character(), indirection, is_ref)
else if (get_node("function_type", real_node)) {
var types = get_nodes("type", get_node("function_type", real_node)).map(fun(node: *tree<symbol>): *type transform_type(node, scope, template_replacements);)
return type_ptr(types.slice(0,-2), types.last(), indirection)
return type_ptr(types.slice(0,-2), types.last(), indirection, is_ref)
} else {
// do lookup for objects, ADTs, templates, etc
var possibilities = scope_lookup(type_syntax_str, scope)
print("There are "); print(possibilities.size); println(" possibilites for this object type lookup")
for (var i = 0; i < possibilities.size; i++;) {
match(*possibilities[i]) {
ast_node::type_def(backing) return backing.self_type->clone_with_indirection(indirection)
ast_node::type_def(backing) return backing.self_type->clone_with_indirection(indirection, is_ref)
}
}
println("No objects in lookup, returning none")
return type_ptr(base_type::none(), indirection)
return type_ptr(base_type::none(), indirection, is_ref)
}
}
fun transform(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node return transform(node, scope, search_type::none(), template_replacements)
@@ -492,7 +493,7 @@ obj ast_transformation (Object) {
if (expression_syntax_node) {
expression = transform(expression_syntax_node, scope, template_replacements)
if (!type_syntax_node)
identifier->identifier.type = get_ast_type(expression)
identifier->identifier.type = get_ast_type(expression)->clone_without_ref()
}
if (!identifier->identifier.type) error("declaration statement with no type or expression from which to inference type")
var declaration = ast_declaration_statement_ptr(identifier, expression)
@@ -921,7 +922,8 @@ fun function_satisfies_params(node: *ast_node, param_types: vector<*type>): bool
return false
}
for (var j = 0; j < param_types.size; j++;) {
if (*func_param_types[j] != *param_types[j]) {
// don't care about references
if (!func_param_types[j]->equality(param_types[j], false)) {
println(string("types don't match ") + func_param_types[j]->to_string() + " with needed " + param_types[j]->to_string())
return false
}

View File

@@ -203,7 +203,7 @@ obj c_generator (Object) {
// 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>()))
if (!parameter_type->is_ref && 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>())))
})
function_prototypes += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameter_types + ");\n"
@@ -375,19 +375,23 @@ obj c_generator (Object) {
return to_ret
}
fun generate_identifier(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node): code_triple {
var pre = code_triple()
if (get_ast_type(node)->is_ref)
pre += "*"
if (enclosing_func && enclosing_func->function.closed_variables.contains(node))
return code_triple(string("(*(closure_data->") + get_name(node) + "))")
return pre + code_triple(string("(*(closure_data->") + get_name(node) + "))")
if (enclosing_object && get_ast_scope(enclosing_object)->contains_key(node->identifier.name) && get_ast_scope(enclosing_object)->get(node->identifier.name).contains(node))
return code_triple("(this->") + get_name(node) + ")"
return code_triple(get_name(node))
return pre + code_triple("(this->") + get_name(node) + ")"
return pre + code_triple(get_name(node))
}
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 {
var return_value = node->return_statement.return_value
var function_return_type = get_ast_type(enclosing_func)->return_type
var to_ret = code_triple()
if (return_value) {
var return_value_type = get_ast_type(return_value)
// 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)))) {
if (return_value_type->is_object() && !function_return_type->is_ref && return_value_type->indirection == 0 && has_method(return_value_type->type_def, "copy_construct", vector(return_value_type->clone_with_indirection(1)))) {
var temp_ident = ast_identifier_ptr(string("temporary_return")+get_id(), return_value_type, null<ast_node>())
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
@@ -400,6 +404,8 @@ obj c_generator (Object) {
// generate all in stack by passing -1
to_ret.pre += generate_from_defer_stack(defer_stack, -1, enclosing_object, enclosing_func).one_string()
to_ret += code_triple("return")
if (function_return_type->is_ref)
to_ret += code_triple(" &")
if (return_value)
to_ret += code_triple(" ") + generate(return_value, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
@@ -497,7 +503,8 @@ obj c_generator (Object) {
// don't add & if it was ->
if (node->function_call.func->function_call.func->function.name == ".")
call_string += "&"
call_string += generate_identifier(node->function_call.func->function_call.parameters[0], enclosing_object, enclosing_func)
// having a null defer stack should be ok, as the only things we should get through here are identifiers and function calls
call_string += generate(node->function_call.func->function_call.parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
} else if (is_identifier(node->function_call.func) || is_function(node->function_call.func)) {
// we handle the case when it's not this later, i.e. it's a lambda returned from another function or something
func_name = generate_function(node->function_call.func, false).one_string()
@@ -525,13 +532,21 @@ obj c_generator (Object) {
if (func_name == "*" || func_name == "&")
return code_triple("(") + func_name + generate(parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>()) + ")"
// for checking if we pass in a ref
var func_type = get_ast_type(node->function_call.func)
// regular parameter generation
parameters.for_each(fun(param: *ast_node) {
// parameters.for_each(fun(param: *ast_node) {
for (var i = 0; i < parameters.size; i++;) {
var param = parameters[i]
var in_function_param_type = func_type->parameter_types[i]
if (call_string != "")
call_string += ", "
if (in_function_param_type->is_ref)
call_string += "&"
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)))) {
if (param_type->is_object() && !in_function_param_type->is_ref && param_type->indirection == 0 && has_method(param_type->type_def, "copy_construct", vector(param_type->clone_with_indirection(1)))) {
var temp_ident = ast_identifier_ptr(string("temporary_param")+get_id(), param_type, null<ast_node>())
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
@@ -541,9 +556,9 @@ obj c_generator (Object) {
} else {
call_string += generate(param, enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>())
}
})
}
var pre_call = string()
if (func_return_type->is_object() && func_return_type->indirection == 0 && has_method(func_return_type->type_def, "destruct", vector<*type>())) {
if (func_return_type->is_object() && !func_return_type->is_ref && func_return_type->indirection == 0 && has_method(func_return_type->type_def, "destruct", vector<*type>())) {
// kind of ugly combo here of
var temp_ident = ast_identifier_ptr(string("temporary_return")+get_id(), func_return_type, null<ast_node>())
var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>())
@@ -552,6 +567,9 @@ obj c_generator (Object) {
pre_call = 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()
}
var ref_pre = code_triple()
if (func_return_type->is_ref)
ref_pre += "*"
if (!is_function(node->function_call.func)) {
// not function, so we must be an identifier or function call return or something
if (!dot_style_method_call) {
@@ -579,7 +597,7 @@ obj c_generator (Object) {
call_string.pre += string("else ") + pre_call + " = ((" + type_to_c(func_type) + "_without_data) " + func_name+".func)(" + call_string.value + ");\n"
call_string.value = pre_call
}
return call_string
return ref_pre + call_string
}
}
if (pre_call != "") {
@@ -588,7 +606,7 @@ obj c_generator (Object) {
} else {
call_string.value = func_name + "(" + call_string.value + ")"
}
return call_string
return ref_pre + call_string
}
// for now, anyway
@@ -616,6 +634,7 @@ obj c_generator (Object) {
}
fun type_decoration(type: *type): string {
var indirection = string()
if (type->is_ref) indirection += "ref_"
for (var i = 0; i < type->indirection; i++;) indirection += "p"
if (type->indirection) indirection += "_"
match (type->base) {
@@ -641,6 +660,7 @@ obj c_generator (Object) {
}
fun type_to_c(type: *type): string {
var indirection = string()
if (type->is_ref) indirection += "/*ref*/ *"
for (var i = 0; i < type->indirection; i++;) indirection += "*"
match (type->base) {
base_type::none() return string("none") + indirection

View File

@@ -28,13 +28,15 @@ fun type_ptr(definition: *ast_node): *type return type_ptr(definition, set<strin
fun type_ptr(definition: *ast_node, traits: set<string>): *type {
return new<type>()->construct(definition, traits)
}
fun type_ptr(base: base_type): *type return type_ptr(base, 0);
fun type_ptr(base: base_type, indirection: int): *type {
return new<type>()->construct(base, indirection)
fun type_ptr(base: base_type): *type return type_ptr(base, 0, false);
fun type_ptr(base: base_type, indirection: int): *type return type_ptr(base, indirection, false)
fun type_ptr(base: base_type, indirection: int, is_ref: bool): *type {
return new<type>()->construct(base, indirection, is_ref)
}
fun type_ptr(parameters: vector<*type>, return_type: *type): *type return type_ptr(parameters, return_type, 0);
fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int): *type {
return new<type>()->construct(parameters, return_type, indirection)
fun type_ptr(parameters: vector<*type>, return_type: *type): *type return type_ptr(parameters, return_type, 0, false);
fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int): *type return type_ptr(parameters, return_type, indirection, false)
fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int, is_ref: bool): *type {
return new<type>()->construct(parameters, return_type, indirection, is_ref)
}
fun type_ptr(traits: set<string>): *type {
@@ -48,6 +50,7 @@ obj type (Object) {
var indirection: int
var type_def: *ast_node
var traits: set<string>
var is_ref: bool
fun construct(): *type {
base.copy_construct(&base_type::none())
parameter_types.construct()
@@ -55,6 +58,7 @@ obj type (Object) {
return_type = null<type>()
type_def = null<ast_node>()
traits.construct()
is_ref = false
return this
}
fun construct(traits_in: set<string>): *type {
@@ -64,15 +68,17 @@ obj type (Object) {
return_type = null<type>()
type_def = null<ast_node>()
traits.copy_construct(&traits_in)
is_ref = false
return this
}
fun construct(base_in: base_type, indirection_in: int): *type {
fun construct(base_in: base_type, indirection_in: int, is_ref_in: bool): *type {
base.copy_construct(&base_in)
parameter_types.construct()
indirection = indirection_in
return_type = null<type>()
type_def = null<ast_node>()
traits.construct()
is_ref = is_ref_in
return this
}
fun construct(type_def_in: *ast_node, traits_in: set<string>): *type {
@@ -82,15 +88,17 @@ obj type (Object) {
return_type = null<type>()
type_def = type_def_in
traits.copy_construct(&traits_in)
is_ref = false
return this
}
fun construct(parameter_types_in: vector<*type>, return_type_in: *type, indirection_in: int): *type {
fun construct(parameter_types_in: vector<*type>, return_type_in: *type, indirection_in: int, is_ref_in: bool): *type {
base.copy_construct(&base_type::function())
parameter_types.copy_construct(&parameter_types_in)
return_type = return_type_in
indirection = indirection_in
type_def = null<ast_node>()
traits.construct()
is_ref = is_ref_in
return this
}
fun copy_construct(old: *type) {
@@ -100,6 +108,7 @@ obj type (Object) {
indirection = old->indirection
type_def = old->type_def
traits.copy_construct(&old->traits)
is_ref = old->is_ref
}
fun operator=(other: ref type) {
destruct()
@@ -110,13 +119,17 @@ obj type (Object) {
parameter_types.destruct()
traits.destruct()
}
fun operator!=(other: ref type):bool return !(*this == other);
fun operator==(other: ref type):bool {
fun operator!=(other: ref type):bool return !equality(other, true);
fun operator==(other: ref type):bool return equality(other, true);
fun equality(other: *type, care_about_ref: bool):bool return equality(*other, care_about_ref);
fun equality(other: ref type, care_about_ref: bool):bool {
if (parameter_types.size != other.parameter_types.size)
return false
for (var i = 0; i < parameter_types.size; i++;)
if (!deref_equality(parameter_types[i], other.parameter_types[i]))
return false
if (care_about_ref && (is_ref != other.is_ref))
return false
return base == other.base && deref_equality(return_type, other.return_type) &&
indirection == other.indirection && deref_equality(type_def, other.type_def) && traits == other.traits
}
@@ -124,6 +137,8 @@ obj type (Object) {
var all_string = string("traits:[")
traits.for_each(fun(t: string) all_string += t;)
all_string += "] "
if (is_ref)
all_string += " ref "
for (var i = 0; i < indirection; i++;) all_string += "*"
match (base) {
base_type::none() return all_string + string("none")
@@ -153,14 +168,17 @@ obj type (Object) {
}
return 0
}
fun clone(): *type return clone_with_indirection(indirection);
fun clone(): *type return clone_with_indirection(indirection, is_ref);
fun clone_without_ref(): *type return clone_with_indirection(indirection, false);
fun clone_with_increased_indirection(): *type return clone_with_indirection(indirection+1);
fun clone_with_increased_indirection(more: int): *type return clone_with_indirection(indirection+more);
fun clone_with_increased_indirection(more: int, is_ref_in: bool): *type return clone_with_indirection(indirection+more, is_ref_in);
fun clone_with_decreased_indirection(): *type return clone_with_indirection(indirection-1);
fun clone_with_indirection(ind: int): *type {
fun clone_with_indirection(ind: int): *type return clone_with_indirection(ind, false)
fun clone_with_indirection(ind: int, is_ref_in: bool): *type {
var to_ret = new<type>()
to_ret->copy_construct(this)
to_ret->indirection = ind
to_ret->is_ref = is_ref_in
return to_ret
}
fun is_object(): bool {