more work on the ast_transformation
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import tree:*
|
import tree:*
|
||||||
import vector:*
|
import vector:*
|
||||||
|
import set:*
|
||||||
import stack:*
|
import stack:*
|
||||||
import map:*
|
import map:*
|
||||||
import util:*
|
import util:*
|
||||||
@@ -91,22 +92,22 @@ fun ast_import_ptr(name: string): *ast_node {
|
|||||||
}
|
}
|
||||||
obj import (Object) {
|
obj import (Object) {
|
||||||
var scope: map<string, vector<*ast_node>>
|
var scope: map<string, vector<*ast_node>>
|
||||||
var children: vector<*ast_node>
|
var imported: set<string>
|
||||||
var name: string
|
var name: string
|
||||||
fun construct(nameIn: string): *import {
|
fun construct(nameIn: string): *import {
|
||||||
scope.construct()
|
scope.construct()
|
||||||
children.construct()
|
imported.construct()
|
||||||
name.copy_construct(&nameIn)
|
name.copy_construct(&nameIn)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
fun copy_construct(old: *import) {
|
fun copy_construct(old: *import) {
|
||||||
scope.copy_construct(&old->scope)
|
scope.copy_construct(&old->scope)
|
||||||
children.copy_construct(&old->children)
|
imported.copy_construct(&old->imported)
|
||||||
name.copy_construct(&old->name)
|
name.copy_construct(&old->name)
|
||||||
}
|
}
|
||||||
fun destruct() {
|
fun destruct() {
|
||||||
scope.destruct()
|
scope.destruct()
|
||||||
children.destruct()
|
imported.destruct()
|
||||||
name.destruct()
|
name.destruct()
|
||||||
}
|
}
|
||||||
fun operator=(other: ref import) {
|
fun operator=(other: ref import) {
|
||||||
@@ -114,25 +115,32 @@ obj import (Object) {
|
|||||||
copy_construct(&other)
|
copy_construct(&other)
|
||||||
}
|
}
|
||||||
fun operator==(other: ref import): bool {
|
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 {
|
fun ast_identifier_ptr(name: *char): *ast_node {
|
||||||
var to_ret.construct(): identifier
|
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>()
|
var ptr = new<ast_node>()
|
||||||
ptr->copy_construct(&ast_node::identifier(to_ret))
|
ptr->copy_construct(&ast_node::identifier(to_ret))
|
||||||
return ptr
|
return ptr
|
||||||
}
|
}
|
||||||
obj identifier (Object) {
|
obj identifier (Object) {
|
||||||
|
var name: string
|
||||||
var scope: map<string, vector<*ast_node>>
|
var scope: map<string, vector<*ast_node>>
|
||||||
fun construct(): *identifier {
|
fun construct(nameIn: string): *identifier {
|
||||||
|
name.copy_construct(&nameIn)
|
||||||
scope.construct()
|
scope.construct()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
fun copy_construct(old: *identifier) {
|
fun copy_construct(old: *identifier) {
|
||||||
|
name.copy_construct(&old->name)
|
||||||
scope.copy_construct(&old->scope)
|
scope.copy_construct(&old->scope)
|
||||||
}
|
}
|
||||||
fun destruct() {
|
fun destruct() {
|
||||||
|
name.destruct()
|
||||||
scope.destruct()
|
scope.destruct()
|
||||||
}
|
}
|
||||||
fun operator=(other: ref identifier) {
|
fun operator=(other: ref identifier) {
|
||||||
@@ -140,7 +148,7 @@ obj identifier (Object) {
|
|||||||
copy_construct(&other)
|
copy_construct(&other)
|
||||||
}
|
}
|
||||||
fun operator==(other: ref identifier): bool {
|
fun operator==(other: ref identifier): bool {
|
||||||
return true
|
return name == other.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun ast_type_def_ptr(name: string): *ast_node {
|
fun ast_type_def_ptr(name: string): *ast_node {
|
||||||
@@ -237,22 +245,26 @@ fun ast_code_block_ptr(): *ast_node {
|
|||||||
}
|
}
|
||||||
obj code_block (Object) {
|
obj code_block (Object) {
|
||||||
var scope: map<string, vector<*ast_node>>
|
var scope: map<string, vector<*ast_node>>
|
||||||
|
var children: vector<*ast_node>
|
||||||
fun construct(): *code_block {
|
fun construct(): *code_block {
|
||||||
scope.construct()
|
scope.construct()
|
||||||
|
children.construct()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
fun copy_construct(old: *code_block) {
|
fun copy_construct(old: *code_block) {
|
||||||
scope.copy_construct(&old->scope)
|
scope.copy_construct(&old->scope)
|
||||||
|
children.copy_construct(&old->children)
|
||||||
}
|
}
|
||||||
fun destruct() {
|
fun destruct() {
|
||||||
scope.destruct()
|
scope.destruct()
|
||||||
|
children.destruct()
|
||||||
}
|
}
|
||||||
fun operator=(other: ref code_block) {
|
fun operator=(other: ref code_block) {
|
||||||
destruct()
|
destruct()
|
||||||
copy_construct(&other)
|
copy_construct(&other)
|
||||||
}
|
}
|
||||||
fun operator==(other: ref code_block): bool {
|
fun operator==(other: ref code_block): bool {
|
||||||
return true
|
return children == other.children && scope == other.scope
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun ast_statement_ptr(): *ast_node {
|
fun ast_statement_ptr(): *ast_node {
|
||||||
@@ -749,12 +761,12 @@ obj value (Object) {
|
|||||||
fun get_ast_children(node: *ast_node): vector<*ast_node> {
|
fun get_ast_children(node: *ast_node): vector<*ast_node> {
|
||||||
match (*node) {
|
match (*node) {
|
||||||
ast_node::translation_unit(backing) return backing.children
|
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::identifier(backing) return vector<*ast_node>()
|
||||||
ast_node::type_def(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::adt_def(backing) return vector<*ast_node>()
|
||||||
ast_node::function(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::statement(backing) return vector<*ast_node>()
|
||||||
ast_node::if_statement(backing) return vector<*ast_node>()
|
ast_node::if_statement(backing) return vector<*ast_node>()
|
||||||
ast_node::match_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 {
|
fun get_ast_name(node: *ast_node): string {
|
||||||
match (*node) {
|
match (*node) {
|
||||||
ast_node::translation_unit(backing) return string("translation_unit: ") + backing.name
|
ast_node::translation_unit(backing) return string("translation_unit: ") + backing.name
|
||||||
ast_node::import(backing) return string("import: ") + backing.name
|
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")
|
ast_node::identifier(backing) return string("identifier: ") + backing.name
|
||||||
ast_node::type_def(backing) return string("type_def: ") + 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::adt_def(backing) return string("adt_def: ") + backing.name
|
||||||
ast_node::function(backing) return string("function")
|
ast_node::function(backing) return string("function")
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ obj ast_transformation (Object) {
|
|||||||
translation_unit->translation_unit.children.add(type_def_node)
|
translation_unit->translation_unit.children.add(type_def_node)
|
||||||
add_to_scope("~enclosing_scope", translation_unit, 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(name, type_def_node, translation_unit)
|
||||||
// add to scope, the reverse
|
|
||||||
// set up type - self-referential, traits, template, etc
|
// set up type - self-referential, traits, template, etc
|
||||||
} else if (child->data.name == "adt_def") {
|
} else if (child->data.name == "adt_def") {
|
||||||
var name = concat_symbol_tree(get_node("identifier", child))
|
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)
|
translation_unit->translation_unit.children.add(adt_def_node)
|
||||||
add_to_scope("~enclosing_scope", translation_unit, 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(name, adt_def_node, translation_unit)
|
||||||
// add to scope, the reverse
|
|
||||||
} else if (child->data.name == "if_comp") {
|
} 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)
|
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
|
// this one already has all its types defined
|
||||||
parse_tree->children.for_each(fun(child: *tree<symbol>) {
|
parse_tree->children.for_each(fun(child: *tree<symbol>) {
|
||||||
if (child->data.name == "import") {
|
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)
|
var import_node = ast_import_ptr(name)
|
||||||
translation_unit->translation_unit.children.add(import_node)
|
translation_unit->translation_unit.children.add(import_node)
|
||||||
add_to_scope("~enclosing_scope", translation_unit, import_node)
|
add_to_scope("~enclosing_scope", translation_unit, import_node)
|
||||||
var outside_translation_unit = importer->import(name + ".krak")
|
var outside_translation_unit = importer->import(name + ".krak")
|
||||||
add_to_scope(name, outside_translation_unit, translation_unit)
|
add_to_scope(name, outside_translation_unit, translation_unit)
|
||||||
// call out to importer
|
import_node->import.imported = from_vector(import_identifier_children.slice(1,-1).map(fun(ident: *tree<symbol>):string return concat_symbol_tree(ident);))
|
||||||
// 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
|
||||||
}
|
}
|
||||||
|
/*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 {
|
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 results[0]
|
||||||
return null<tree<symbol>>()
|
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>> {
|
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;)
|
return parent->children.filter(fun(node: *tree<symbol>):bool return node->data.name == lookup;)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,14 +14,17 @@ import parser:*
|
|||||||
obj importer (Object) {
|
obj importer (Object) {
|
||||||
var parse: parser
|
var parse: parser
|
||||||
var ast_pass: ast_transformation
|
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 {
|
fun construct(parseIn: ref parser, ast_passIn: ref ast_transformation): *importer {
|
||||||
parse.copy_construct(&parseIn)
|
parse.copy_construct(&parseIn)
|
||||||
ast_pass.copy_construct(&ast_passIn)
|
ast_pass.copy_construct(&ast_passIn)
|
||||||
|
name_ast_map.construct()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
fun copy_construct(old: *importer) {
|
fun copy_construct(old: *importer) {
|
||||||
parse.copy_construct(&old->parse)
|
parse.copy_construct(&old->parse)
|
||||||
ast_pass.copy_construct(&old->ast_pass)
|
ast_pass.copy_construct(&old->ast_pass)
|
||||||
|
name_ast_map.copy_construct(&old->name_ast_map)
|
||||||
}
|
}
|
||||||
fun operator=(old: ref importer) {
|
fun operator=(old: ref importer) {
|
||||||
destruct()
|
destruct()
|
||||||
@@ -30,8 +33,12 @@ obj importer (Object) {
|
|||||||
fun destruct() {
|
fun destruct() {
|
||||||
parse.destruct()
|
parse.destruct()
|
||||||
ast_pass.destruct()
|
ast_pass.destruct()
|
||||||
|
name_ast_map.destruct()
|
||||||
}
|
}
|
||||||
fun import(file_name: string): *ast_node {
|
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)
|
print("pre-parse: "); println(file_name)
|
||||||
var parse_tree = parse.parse_input(read_file(file_name), file_name)
|
var parse_tree = parse.parse_input(read_file(file_name), file_name)
|
||||||
print("post-parse: "); println(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))
|
write_file(file_name + ".ast.dot", ast_to_dot(ast))
|
||||||
return 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>) {
|
fun trim(parse_tree: *tree<symbol>) {
|
||||||
remove_node(symbol("$NULL$", false), parse_tree)
|
remove_node(symbol("$NULL$", false), parse_tree)
|
||||||
remove_node(symbol("WS", 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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,11 @@ obj map<T,U> (Object, Serializable) {
|
|||||||
pos = values.unserialize(it, pos)
|
pos = values.unserialize(it, pos)
|
||||||
return 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) {
|
fun operator[]=(key: T, value: U) {
|
||||||
set(key,value)
|
set(key,value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,5 +91,8 @@ obj set<T> (Object, Serializable) {
|
|||||||
fun any_true(func: fun(T):bool):bool {
|
fun any_true(func: fun(T):bool):bool {
|
||||||
return data.any_true(func)
|
return data.any_true(func)
|
||||||
}
|
}
|
||||||
|
fun reduce<U>(func: fun(T,U): U, initial: U): U {
|
||||||
|
return data.reduce(func, initial)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -258,5 +258,10 @@ obj vector<T> (Object, Serializable) {
|
|||||||
maxIdx = i
|
maxIdx = i
|
||||||
return data[maxIdx]
|
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
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
var a = 1
|
var a = 1
|
||||||
|
var b = 2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import to_import
|
import to_import: a,b
|
||||||
|
|
||||||
fun main(): int {
|
fun main(): int {
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user