diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index 338c969..47e6cdc 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -403,12 +403,14 @@ std::pair CGenerator::generateTranslationUnit(std::str for (auto child : decChildren) { if (child->getName() != "function" && child->getDataRef()->valueType->typeDefinition != declaration) { std::string option_name = child->getDataRef()->symbol.getName(); + std::string prefixed_option_name = prefixIfNeeded(scopePrefix(child),option_name); functionDefinitions += " " + elsePrefix + " if (this->flag == " + declarationData.symbol.getName() + "__" + option_name + ") {\n"; elsePrefix = "else"; NodeTree* method = nullptr; if ((method = getMethod(child->getDataRef()->valueType, "destruct", std::vector()))) { functionDefinitions += " " + generateMethodIfExists(child->getDataRef()->valueType, "destruct", - "&this->" + option_name, std::vector()) + ";\n"; + "&this->" + prefixed_option_name, std::vector()) + ";\n"; + //"&this->" + option_name, std::vector()) + ";\n"; } functionDefinitions += " }\n"; } @@ -628,13 +630,15 @@ CCodeTriple CGenerator::generate(NodeTree* from, NodeTree* enc // was accidntally adding in prefix when it shouldn't, though maybe it should when both option and ADT name are identical, deal with this later //std::string option = generate(case_children[0], enclosingObject, false, enclosingFunction).oneString(); std::string option = case_children[0]->getDataRef()->symbol.getName(); + std::string prefixed_option = prefixIfNeeded(scopePrefix(case_children[0]),option); std::string parentName = case_children[0]->getDataRef()->scope["~enclosing_scope"][0]->getDataRef()->symbol.getName(); output += "/* case " + option + " if " + thingToMatch.value + " */\n"; output += tabs() + "if ((" + thingToMatch.value + ").flag == " + parentName + "__" + option + ") {\n"; tabLevel++; if (case_children.size() > 2) { output += tabs() + ValueTypeToCType(case_children[1]->getData().valueType, generate(case_children[1], enclosingObject, false, enclosingFunction).oneString()) - + " = (" + thingToMatch.value + ")." + option + ";\n"; + + " = (" + thingToMatch.value + ")." + prefixed_option + ";\n"; + //+ " = (" + thingToMatch.value + ")." + option + ";\n"; output += generate(case_children[2], enclosingObject, false, enclosingFunction).oneString(); } else { output += generate(case_children[1], enclosingObject, false, enclosingFunction).oneString(); diff --git a/stdlib/ast_nodes.krak b/stdlib/ast_nodes.krak index 8ecf21b..c10126f 100644 --- a/stdlib/ast_nodes.krak +++ b/stdlib/ast_nodes.krak @@ -189,12 +189,15 @@ fun is_type_def(node: *ast_node): bool { obj type_def (Object) { var scope: map> var name: string + var self_type: *type fun construct(nameIn: string): *type_def { scope.construct() name.copy_construct(&nameIn) + self_type = null() return this } fun copy_construct(old: *type_def) { + self_type = old->self_type scope.copy_construct(&old->scope) name.copy_construct(&old->name) } @@ -207,7 +210,7 @@ obj type_def (Object) { copy_construct(&other) } fun operator==(other: ref type_def): bool { - return name == other.name + return name == other.name && self_type == other.self_type } } fun ast_adt_def_ptr(name: string): *ast_node { diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index be4a9a0..3681baa 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -42,6 +42,7 @@ obj ast_transformation (Object) { if (child->data.name == "type_def") { var name = concat_symbol_tree(get_node("identifier", child)) var type_def_node = ast_type_def_ptr(name) + type_def_node->type_def.self_type = type_ptr(type_def_node) translation_unit->translation_unit.children.add(type_def_node) ast_to_syntax.set(type_def_node, child) add_to_scope("~enclosing_scope", translation_unit, type_def_node) @@ -168,6 +169,14 @@ obj ast_transformation (Object) { return type_ptr(base_type::function(), indirection) 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 + } + } + println("No objects in lookup, returning none") return type_ptr(base_type::none(), indirection) } } diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index 84479cf..c164554 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -24,7 +24,7 @@ obj c_generator (Object) { fun generate_c(name_ast_map: map,*ast_node>>): pair { var linker_string:string = "" var prequal: string = "#include \n#include \n#include \n" - var plain_typedefs: string = "" + var plain_typedefs: string = "\n/**Plain Typedefs**/\n" var top_level_c_passthrough: string = "" var variable_extern_declarations: string = "" var structs: string = "\n/**Type Structs**/\n" @@ -75,7 +75,8 @@ obj c_generator (Object) { }) }) type_poset.get_sorted().for_each(fun(vert: *ast_node) { - structs += "/* a type */\n" + plain_typedefs += string("typedef struct ") + vert->type_def.name + "_dummy " + vert->type_def.name + ";\n" + structs += string("struct ") + vert->type_def.name + "_dummy {};\n" }) 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) @@ -185,6 +186,9 @@ obj c_generator (Object) { base_type::integer() return indirection + string("int") base_type::floating() return indirection + string("float") base_type::double_precision() return indirection + string("double") + base_type::object() { + return type->type_def->type_def.name + } base_type::function() { var temp = indirection + string("function_") type->parameter_types.for_each(fun(parameter_type: *type) temp += type_decoration(parameter_type) + "_";) @@ -206,6 +210,9 @@ obj c_generator (Object) { base_type::integer() return string("int") + indirection base_type::floating() return string("float") + indirection base_type::double_precision() return string("double") + indirection + base_type::object() { + return type->type_def->type_def.name + } base_type::function() { var temp = indirection + string("function: (") type->parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + " ";) diff --git a/stdlib/type.krak b/stdlib/type.krak index 016ccf8..72d4dd3 100644 --- a/stdlib/type.krak +++ b/stdlib/type.krak @@ -1,11 +1,13 @@ import mem:* import string:* import vector:* +import ast_nodes:* // hmm, like the ast_node, this is another candadate for being fully an ADT // one issue is that there are properties shared between most of the options (indirection, say) adt base_type { none, + object, template, template_type, void_return, @@ -20,6 +22,9 @@ adt base_type { fun type_ptr(): *type { return new()->construct() } +fun type_ptr(definition: *ast_node): *type { + return new()->construct(definition) +} fun type_ptr(base: base_type): *type return type_ptr(base, 0); fun type_ptr(base: base_type, indirection: int): *type { return new()->construct(base, indirection) @@ -34,11 +39,13 @@ obj type (Object) { var parameter_types: vector<*type> var return_type: *type var indirection: int + var type_def: *ast_node fun construct(): *type { base.copy_construct(&base_type::none()) parameter_types.construct() indirection = 0 return_type = null() + type_def = null() return this } fun construct(base_in: base_type, indirection_in: int): *type { @@ -46,6 +53,15 @@ obj type (Object) { parameter_types.construct() indirection = indirection_in return_type = null() + type_def = null() + return this + } + fun construct(type_def_in: *ast_node): *type { + base.copy_construct(&base_type::object()) + parameter_types.construct() + indirection = 0 + return_type = null() + type_def = type_def_in return this } fun construct(parameter_types_in: vector<*type>, return_type_in: *type, indirection_in: int): *type { @@ -53,6 +69,7 @@ obj type (Object) { parameter_types.copy_construct(¶meter_types_in) return_type = return_type_in indirection = indirection_in + type_def = null() return this } fun copy_construct(old: *type) { @@ -60,6 +77,7 @@ obj type (Object) { parameter_types.copy_construct(&old->parameter_types) return_type = old->return_type indirection = old->indirection + type_def = old->type_def } fun operator=(other: ref type) { destruct() @@ -73,13 +91,14 @@ obj type (Object) { fun operator==(other: ref type):bool { if ( (return_type && other.return_type && *return_type != *other.return_type) || (return_type && !other.return_type) || (!return_type && other.return_type) ) return false - return base == other.base && parameter_types == other.parameter_types && indirection == other.indirection + return base == other.base && parameter_types == other.parameter_types && indirection == other.indirection && type_def == other.type_def } fun to_string(): string { var indirection_str = string() for (var i = 0; i < indirection; i++;) indirection_str += "*" match (base) { base_type::none() return indirection_str + string("none") + base_type::object() return indirection_str + string("obj:") + type_def->type_def.name base_type::template() return indirection_str + string("template") base_type::template_type() return indirection_str + string("template_type") base_type::void_return() return indirection_str + string("void_return")