more work on the ast_transformation

This commit is contained in:
Nathan Braswell
2015-12-28 03:34:40 -05:00
parent 22b334a2ae
commit fd6383124c
8 changed files with 106 additions and 54 deletions

View File

@@ -1,5 +1,6 @@
import tree:*
import vector:*
import set:*
import stack:*
import map:*
import util:*
@@ -91,22 +92,22 @@ fun ast_import_ptr(name: string): *ast_node {
}
obj import (Object) {
var scope: map<string, vector<*ast_node>>
var children: vector<*ast_node>
var imported: set<string>
var name: string
fun construct(nameIn: string): *import {
scope.construct()
children.construct()
imported.construct()
name.copy_construct(&nameIn)
return this
}
fun copy_construct(old: *import) {
scope.copy_construct(&old->scope)
children.copy_construct(&old->children)
imported.copy_construct(&old->imported)
name.copy_construct(&old->name)
}
fun destruct() {
scope.destruct()
children.destruct()
imported.destruct()
name.destruct()
}
fun operator=(other: ref import) {
@@ -114,25 +115,32 @@ obj import (Object) {
copy_construct(&other)
}
fun operator==(other: ref import): bool {
return children == other.children && name == other.name
return imported == other.imported && name == other.name
}
}
fun ast_identifier_ptr(): *ast_node {
var to_ret.construct(): identifier
fun ast_identifier_ptr(name: *char): *ast_node {
return ast_identifier_ptr(string(name))
}
fun ast_identifier_ptr(name: string): *ast_node {
var to_ret.construct(name): identifier
var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::identifier(to_ret))
return ptr
}
obj identifier (Object) {
var name: string
var scope: map<string, vector<*ast_node>>
fun construct(): *identifier {
fun construct(nameIn: string): *identifier {
name.copy_construct(&nameIn)
scope.construct()
return this
}
fun copy_construct(old: *identifier) {
name.copy_construct(&old->name)
scope.copy_construct(&old->scope)
}
fun destruct() {
name.destruct()
scope.destruct()
}
fun operator=(other: ref identifier) {
@@ -140,7 +148,7 @@ obj identifier (Object) {
copy_construct(&other)
}
fun operator==(other: ref identifier): bool {
return true
return name == other.name
}
}
fun ast_type_def_ptr(name: string): *ast_node {
@@ -237,22 +245,26 @@ fun ast_code_block_ptr(): *ast_node {
}
obj code_block (Object) {
var scope: map<string, vector<*ast_node>>
var children: vector<*ast_node>
fun construct(): *code_block {
scope.construct()
children.construct()
return this
}
fun copy_construct(old: *code_block) {
scope.copy_construct(&old->scope)
children.copy_construct(&old->children)
}
fun destruct() {
scope.destruct()
children.destruct()
}
fun operator=(other: ref code_block) {
destruct()
copy_construct(&other)
}
fun operator==(other: ref code_block): bool {
return true
return children == other.children && scope == other.scope
}
}
fun ast_statement_ptr(): *ast_node {
@@ -749,12 +761,12 @@ obj value (Object) {
fun get_ast_children(node: *ast_node): vector<*ast_node> {
match (*node) {
ast_node::translation_unit(backing) return backing.children
ast_node::import(backing) return backing.children
ast_node::import(backing) return vector<*ast_node>()
ast_node::identifier(backing) return vector<*ast_node>()
ast_node::type_def(backing) return vector<*ast_node>()
ast_node::adt_def(backing) return vector<*ast_node>()
ast_node::function(backing) return vector<*ast_node>()
ast_node::code_block(backing) return vector<*ast_node>()
ast_node::code_block(backing) return backing.children
ast_node::statement(backing) return vector<*ast_node>()
ast_node::if_statement(backing) return vector<*ast_node>()
ast_node::match_statement(backing) return vector<*ast_node>()
@@ -779,8 +791,8 @@ 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: ") + backing.name
ast_node::import(backing) return string("import: ") + backing.name
ast_node::identifier(backing) return string("identifier")
ast_node::import(backing) return string("import: ") + backing.name + "; [" + backing.imported.reduce(fun(name: string, acc: string): string return acc + " " + name;, string()) + " ]"
ast_node::identifier(backing) return string("identifier: ") + backing.name
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")

View File

@@ -40,7 +40,6 @@ obj ast_transformation (Object) {
translation_unit->translation_unit.children.add(type_def_node)
add_to_scope("~enclosing_scope", translation_unit, type_def_node)
add_to_scope(name, type_def_node, translation_unit)
// 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))
@@ -48,13 +47,9 @@ obj ast_transformation (Object) {
translation_unit->translation_unit.children.add(adt_def_node)
add_to_scope("~enclosing_scope", translation_unit, adt_def_node)
add_to_scope(name, adt_def_node, translation_unit)
// add to scope, the reverse
} else if (child->data.name == "if_comp") {
var if_comp_node = ast_if_comp_ptr()
var if_comp_node = transform(child, translation_unit)
translation_unit->translation_unit.children.add(if_comp_node)
add_to_scope("~enclosing_scope", translation_unit, if_comp_node)
// add parent to scope?
// do children...
}
})
@@ -62,19 +57,39 @@ obj ast_transformation (Object) {
// 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_identifier_children = get_nodes("identifier", child)
var name = concat_symbol_tree(import_identifier_children[0])
var import_node = ast_import_ptr(name)
translation_unit->translation_unit.children.add(import_node)
add_to_scope("~enclosing_scope", translation_unit, import_node)
var outside_translation_unit = importer->import(name + ".krak")
add_to_scope(name, outside_translation_unit, translation_unit)
// 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?
import_node->import.imported = from_vector(import_identifier_children.slice(1,-1).map(fun(ident: *tree<symbol>):string return concat_symbol_tree(ident);))
}
})
return translation_unit
}
/*NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope, std::vector<Type> types, bool limitToFunction, std::map<std::string, Type*> templateTypeReplacements) {*/
fun transform(node: *tree<symbol>, scope: *ast_node): *ast_node {
var name = node->data.name
if (name == "identifier" || name == "scoped_identifier") {
} else if (name == "code_block") {
var new_block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", scope, new_block)
new_block->code_block.children = transform_all(node->children, new_block)
return new_block
} else if (name == "code_block") {
var new_if_comp = ast_if_comp_ptr()
add_to_scope("~enclosing_scope", scope, new_if_comp)
new_if_comp->code_block.children = transform_all(node->children, new_if_comp)
return new_if_comp
}
print("FAILED TO TRANSFORM: "); println(concat_symbol_tree(node))
return null<ast_node>()
}
fun transform_all(nodes: vector<*tree<symbol>>, scope: *ast_node): vector<*ast_node> {
return nodes.map(fun(node: *tree<symbol>): *ast_node return transform(node, scope);)
}
}
fun concat_symbol_tree(node: *tree<symbol>): string {
@@ -95,6 +110,9 @@ fun get_node(lookup: string, parent: *tree<symbol>): *tree<symbol> {
return results[0]
return null<tree<symbol>>()
}
fun get_nodes(lookup: *char, parent: *tree<symbol>): vector<*tree<symbol>> {
return get_nodes(string(lookup), parent)
}
fun get_nodes(lookup: string, parent: *tree<symbol>): vector<*tree<symbol>> {
return parent->children.filter(fun(node: *tree<symbol>):bool return node->data.name == lookup;)
}

View File

@@ -14,14 +14,17 @@ import parser:*
obj importer (Object) {
var parse: parser
var ast_pass: ast_transformation
var name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>
fun construct(parseIn: ref parser, ast_passIn: ref ast_transformation): *importer {
parse.copy_construct(&parseIn)
ast_pass.copy_construct(&ast_passIn)
name_ast_map.construct()
return this
}
fun copy_construct(old: *importer) {
parse.copy_construct(&old->parse)
ast_pass.copy_construct(&old->ast_pass)
name_ast_map.copy_construct(&old->name_ast_map)
}
fun operator=(old: ref importer) {
destruct()
@@ -30,8 +33,12 @@ obj importer (Object) {
fun destruct() {
parse.destruct()
ast_pass.destruct()
name_ast_map.destruct()
}
fun import(file_name: string): *ast_node {
if (name_ast_map.contains_key(file_name))
return name_ast_map[file_name].second
print("pre-parse: "); println(file_name)
var parse_tree = parse.parse_input(read_file(file_name), file_name)
print("post-parse: "); println(file_name)
@@ -46,6 +53,11 @@ obj importer (Object) {
write_file(file_name + ".ast.dot", ast_to_dot(ast))
return ast
}
fun register(file_name: string, parse_tree: *tree<symbol>, translation_unit: *ast_node) {
name_ast_map.set(file_name, make_pair(parse_tree, translation_unit))
print("Registered parse_tree+translation_unit for ")
println(file_name)
}
fun trim(parse_tree: *tree<symbol>) {
remove_node(symbol("$NULL$", false), parse_tree)
remove_node(symbol("WS", false), parse_tree)
@@ -127,9 +139,5 @@ obj importer (Object) {
}
}
}
fun register(file_name: string, parse_tree: *tree<symbol>, translation_unit: *ast_node) {
print("Registered parse_tree+translation_unit for ")
println(file_name)
}
}

View File

@@ -45,6 +45,11 @@ obj map<T,U> (Object, Serializable) {
pos = values.unserialize(it, pos)
return pos
}
// the old unnecessary template to prevent generation
// if not used trick (in this case, changing out U with V)
fun operator==<V>(other: ref map<T,V>): bool {
return keys == other.keys && values == other.values
}
fun operator[]=(key: T, value: U) {
set(key,value)
}

View File

@@ -91,5 +91,8 @@ obj set<T> (Object, Serializable) {
fun any_true(func: fun(T):bool):bool {
return data.any_true(func)
}
fun reduce<U>(func: fun(T,U): U, initial: U): U {
return data.reduce(func, initial)
}
}

View File

@@ -258,5 +258,10 @@ obj vector<T> (Object, Serializable) {
maxIdx = i
return data[maxIdx]
}
fun reduce<U>(func: fun(T,U): U, initial: U): U {
for (var i = 0; i < size; i++;)
initial = func(data[i], initial)
return initial
}
};

View File

@@ -1,5 +1,6 @@
var a = 1
var b = 2

View File

@@ -1,4 +1,4 @@
import to_import
import to_import: a,b
fun main(): int {
return 0