From f753d4f421eb111f89582512e31e41cdd07e6362 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sun, 6 Dec 2015 18:44:04 -0500 Subject: [PATCH] little more work --- stdlib/ast_node.krak | 56 ++++++++++++++++++++++------------ stdlib/ast_transformation.krak | 29 ++++++++++++++++-- tests/to_parse.krak | 5 +++ 3 files changed, 68 insertions(+), 22 deletions(-) diff --git a/stdlib/ast_node.krak b/stdlib/ast_node.krak index 8c7a649..c5ee6c0 100644 --- a/stdlib/ast_node.krak +++ b/stdlib/ast_node.krak @@ -54,56 +54,64 @@ fun ast_node_ptr(node: ast_node): *ast_node { to_ret->copy_construct(&node) return to_ret } -fun ast_translation_unit_ptr(): *ast_node { - var obj_var.construct(): translation_unit +fun ast_translation_unit_ptr(name: string): *ast_node { + var obj_var.construct(name): translation_unit var ptr = new() ptr->copy_construct(&ast_node::translation_unit(obj_var)) return ptr } obj translation_unit (Object) { var children: vector<*ast_node> - fun construct(): *translation_unit { + var name: string + fun construct(nameIn: string): *translation_unit { children.construct() + name.copy_construct(&nameIn) return this } fun copy_construct(old: *translation_unit) { children.copy_construct(&old->children) + name.copy_construct(&old->name) } fun destruct() { children.destruct() + name.destruct() } fun operator=(other: ref translation_unit) { destruct() copy_construct(&other) } fun operator==(other: ref translation_unit): bool { - return children == other.children + return children == other.children && name == other.name } } -fun ast_import_ptr(): *ast_node { - var to_ret.construct(): import +fun ast_import_ptr(name: string): *ast_node { + var to_ret.construct(name): import var ptr = new() ptr->copy_construct(&ast_node::import(to_ret)) return ptr } obj import (Object) { var children: vector<*ast_node> - fun construct(): *import { + var name: string + fun construct(nameIn: string): *import { children.construct() + name.copy_construct(&nameIn) return this } fun copy_construct(old: *import) { children.copy_construct(&old->children) + name.copy_construct(&old->name) } fun destruct() { children.destruct() + name.destruct() } fun operator=(other: ref import) { destruct() copy_construct(&other) } fun operator==(other: ref import): bool { - return children == other.children + return children == other.children && name == other.name } } fun ast_identifier_ptr(): *ast_node { @@ -128,48 +136,56 @@ obj identifier (Object) { return true } } -fun ast_type_def_ptr(): *ast_node { - var to_ret.construct(): type_def +fun ast_type_def_ptr(name: string): *ast_node { + var to_ret.construct(name): type_def var ptr = new() ptr->copy_construct(&ast_node::type_def(to_ret)) return ptr } obj type_def (Object) { - fun construct(): *type_def { + var name: string + fun construct(nameIn: string): *type_def { + name.copy_construct(&nameIn) return this } fun copy_construct(old: *type_def) { + name.copy_construct(&old->name) } fun destruct() { + name.destruct() } fun operator=(other: ref type_def) { destruct() copy_construct(&other) } fun operator==(other: ref type_def): bool { - return true + return name == other.name } } -fun ast_adt_def_ptr(): *ast_node { - var to_ret.construct(): adt_def +fun ast_adt_def_ptr(name: string): *ast_node { + var to_ret.construct(name): adt_def var ptr = new() ptr->copy_construct(&ast_node::adt_def(to_ret)) return ptr } obj adt_def (Object) { - fun construct(): *adt_def { + var name: string + fun construct(nameIn: string): *adt_def { + name.copy_construct(&nameIn) return this } fun copy_construct(old: *adt_def) { + name.copy_construct(&old->name) } fun destruct() { + name.destruct() } fun operator=(other: ref adt_def) { destruct() copy_construct(&other) } fun operator==(other: ref adt_def): bool { - return true + return name == other.name } } fun ast_function_ptr(): *ast_node { @@ -782,11 +798,11 @@ fun get_ast_children(node: *ast_node): vector<*ast_node> { } fun get_ast_name(node: *ast_node): string { match (*node) { - ast_node::translation_unit(backing) return string("translation_unit") - ast_node::import(backing) return string("import") + ast_node::translation_unit(backing) return string("translation_unit: ") + backing.name + ast_node::import(backing) return string("import: ") + backing.name ast_node::identifier(backing) return string("identifier") - ast_node::type_def(backing) return string("type_def") - ast_node::adt_def(backing) return string("adt_def") + ast_node::type_def(backing) return string("type_def: ") + backing.name + ast_node::adt_def(backing) return string("adt_def: ") + backing.name ast_node::function(backing) return string("function") ast_node::code_block(backing) return string("code_block") ast_node::typed_parameter(backing) return string("typed_parameter") diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index 56e2e3a..0e05470 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -31,13 +31,38 @@ obj ast_transformation (Object) { } // first pass defines all type_defs (objects and aliases), ADTs, and top-level if/passthroughs fun first_pass(file_name: string, parse_tree: *tree, importer: *importer): *ast_node { - var translation_unit = ast_translation_unit_ptr() + var translation_unit = ast_translation_unit_ptr(file_name) importer->register(file_name, parse_tree, translation_unit) parse_tree->children.for_each(fun(child: *tree) { if (child->data.name == "type_def") { var name = concat_symbol_tree(get_node("identifier", child)) - var type_def_node = ast_type_def_ptr() + var type_def_node = ast_type_def_ptr(name) translation_unit->translation_unit.children.add(type_def_node) + // add to scope, the reverse + // set up type - self-referential, traits, template, etc + } else if (child->data.name == "adt_def") { + var name = concat_symbol_tree(get_node("identifier", child)) + var adt_def_node = ast_adt_def_ptr(name) + translation_unit->translation_unit.children.add(adt_def_node) + // add to scope, the reverse + } else if (child->data.name == "if_comp") { + var if_comp_node = ast_if_comp_ptr() + translation_unit->translation_unit.children.add(if_comp_node) + // add parent to scope? + // do children... + } + }) + + // now do all inports (done second so that if it imports this translation_unit, + // this one already has all its types defined + parse_tree->children.for_each(fun(child: *tree) { + if (child->data.name == "import") { + var name = concat_symbol_tree(get_node("identifier", child)) + var import_node = ast_import_ptr(name) + translation_unit->translation_unit.children.add(import_node) + // call out to importer + // go through what is imported - *, or individual names + // for names undefined right now (i.e. not of a type), leave an empty vector? } }) return translation_unit diff --git a/tests/to_parse.krak b/tests/to_parse.krak index d9e695e..48b9573 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -1,6 +1,11 @@ def not_int int +adt maybe { + no_int, + an_int: int +} + fun main(): int { return 0 }