little more work

This commit is contained in:
Nathan Braswell
2015-12-06 18:44:04 -05:00
parent 803b415220
commit f753d4f421
3 changed files with 68 additions and 22 deletions

View File

@@ -54,56 +54,64 @@ fun ast_node_ptr(node: ast_node): *ast_node {
to_ret->copy_construct(&node) to_ret->copy_construct(&node)
return to_ret return to_ret
} }
fun ast_translation_unit_ptr(): *ast_node { fun ast_translation_unit_ptr(name: string): *ast_node {
var obj_var.construct(): translation_unit var obj_var.construct(name): translation_unit
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::translation_unit(obj_var)) ptr->copy_construct(&ast_node::translation_unit(obj_var))
return ptr return ptr
} }
obj translation_unit (Object) { obj translation_unit (Object) {
var children: vector<*ast_node> var children: vector<*ast_node>
fun construct(): *translation_unit { var name: string
fun construct(nameIn: string): *translation_unit {
children.construct() children.construct()
name.copy_construct(&nameIn)
return this return this
} }
fun copy_construct(old: *translation_unit) { fun copy_construct(old: *translation_unit) {
children.copy_construct(&old->children) children.copy_construct(&old->children)
name.copy_construct(&old->name)
} }
fun destruct() { fun destruct() {
children.destruct() children.destruct()
name.destruct()
} }
fun operator=(other: ref translation_unit) { fun operator=(other: ref translation_unit) {
destruct() destruct()
copy_construct(&other) copy_construct(&other)
} }
fun operator==(other: ref translation_unit): bool { fun operator==(other: ref translation_unit): bool {
return children == other.children return children == other.children && name == other.name
} }
} }
fun ast_import_ptr(): *ast_node { fun ast_import_ptr(name: string): *ast_node {
var to_ret.construct(): import var to_ret.construct(name): import
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::import(to_ret)) ptr->copy_construct(&ast_node::import(to_ret))
return ptr return ptr
} }
obj import (Object) { obj import (Object) {
var children: vector<*ast_node> var children: vector<*ast_node>
fun construct(): *import { var name: string
fun construct(nameIn: string): *import {
children.construct() children.construct()
name.copy_construct(&nameIn)
return this return this
} }
fun copy_construct(old: *import) { fun copy_construct(old: *import) {
children.copy_construct(&old->children) children.copy_construct(&old->children)
name.copy_construct(&old->name)
} }
fun destruct() { fun destruct() {
children.destruct() children.destruct()
name.destruct()
} }
fun operator=(other: ref import) { fun operator=(other: ref import) {
destruct() destruct()
copy_construct(&other) copy_construct(&other)
} }
fun operator==(other: ref import): bool { fun operator==(other: ref import): bool {
return children == other.children return children == other.children && name == other.name
} }
} }
fun ast_identifier_ptr(): *ast_node { fun ast_identifier_ptr(): *ast_node {
@@ -128,48 +136,56 @@ obj identifier (Object) {
return true return true
} }
} }
fun ast_type_def_ptr(): *ast_node { fun ast_type_def_ptr(name: string): *ast_node {
var to_ret.construct(): type_def var to_ret.construct(name): type_def
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::type_def(to_ret)) ptr->copy_construct(&ast_node::type_def(to_ret))
return ptr return ptr
} }
obj type_def (Object) { obj type_def (Object) {
fun construct(): *type_def { var name: string
fun construct(nameIn: string): *type_def {
name.copy_construct(&nameIn)
return this return this
} }
fun copy_construct(old: *type_def) { fun copy_construct(old: *type_def) {
name.copy_construct(&old->name)
} }
fun destruct() { fun destruct() {
name.destruct()
} }
fun operator=(other: ref type_def) { fun operator=(other: ref type_def) {
destruct() destruct()
copy_construct(&other) copy_construct(&other)
} }
fun operator==(other: ref type_def): bool { fun operator==(other: ref type_def): bool {
return true return name == other.name
} }
} }
fun ast_adt_def_ptr(): *ast_node { fun ast_adt_def_ptr(name: string): *ast_node {
var to_ret.construct(): adt_def var to_ret.construct(name): adt_def
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::adt_def(to_ret)) ptr->copy_construct(&ast_node::adt_def(to_ret))
return ptr return ptr
} }
obj adt_def (Object) { obj adt_def (Object) {
fun construct(): *adt_def { var name: string
fun construct(nameIn: string): *adt_def {
name.copy_construct(&nameIn)
return this return this
} }
fun copy_construct(old: *adt_def) { fun copy_construct(old: *adt_def) {
name.copy_construct(&old->name)
} }
fun destruct() { fun destruct() {
name.destruct()
} }
fun operator=(other: ref adt_def) { fun operator=(other: ref adt_def) {
destruct() destruct()
copy_construct(&other) copy_construct(&other)
} }
fun operator==(other: ref adt_def): bool { fun operator==(other: ref adt_def): bool {
return true return name == other.name
} }
} }
fun ast_function_ptr(): *ast_node { 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 { fun get_ast_name(node: *ast_node): string {
match (*node) { match (*node) {
ast_node::translation_unit(backing) return string("translation_unit") ast_node::translation_unit(backing) return string("translation_unit: ") + backing.name
ast_node::import(backing) return string("import") ast_node::import(backing) return string("import: ") + backing.name
ast_node::identifier(backing) return string("identifier") ast_node::identifier(backing) return string("identifier")
ast_node::type_def(backing) return string("type_def") ast_node::type_def(backing) return string("type_def: ") + backing.name
ast_node::adt_def(backing) return string("adt_def") ast_node::adt_def(backing) return string("adt_def: ") + backing.name
ast_node::function(backing) return string("function") ast_node::function(backing) return string("function")
ast_node::code_block(backing) return string("code_block") ast_node::code_block(backing) return string("code_block")
ast_node::typed_parameter(backing) return string("typed_parameter") ast_node::typed_parameter(backing) return string("typed_parameter")

View File

@@ -31,13 +31,38 @@ obj ast_transformation (Object) {
} }
// first pass defines all type_defs (objects and aliases), ADTs, and top-level if/passthroughs // first pass defines all type_defs (objects and aliases), ADTs, and top-level if/passthroughs
fun first_pass(file_name: string, parse_tree: *tree<symbol>, importer: *importer): *ast_node { fun first_pass(file_name: string, parse_tree: *tree<symbol>, 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) importer->register(file_name, parse_tree, translation_unit)
parse_tree->children.for_each(fun(child: *tree<symbol>) { parse_tree->children.for_each(fun(child: *tree<symbol>) {
if (child->data.name == "type_def") { if (child->data.name == "type_def") {
var name = concat_symbol_tree(get_node("identifier", child)) 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) 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<symbol>) {
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 return translation_unit

View File

@@ -1,6 +1,11 @@
def not_int int def not_int int
adt maybe {
no_int,
an_int: int
}
fun main(): int { fun main(): int {
return 0 return 0
} }