partial rollback, might still not work

This commit is contained in:
Nathan Braswell
2016-06-16 09:53:15 -07:00
parent 4de7dd1210
commit 3f89cb1e37
7 changed files with 68 additions and 20 deletions

View File

@@ -9,6 +9,7 @@ import serialize:*
import interpreter:* import interpreter:*
import os:* import os:*
import ast_transformation:* import ast_transformation:*
/*import adt_lower:**/
import defer_lower:* import defer_lower:*
import c_line_control:* import c_line_control:*
import c_generator:* import c_generator:*
@@ -99,6 +100,8 @@ fun main(argc: int, argv: **char):int {
var importer.construct(parsers, ast_pass, vector(string(), base_dir + "/stdlib/")): importer var importer.construct(parsers, ast_pass, vector(string(), base_dir + "/stdlib/")): importer
importer.import(kraken_file_name) importer.import(kraken_file_name)
// Passes // Passes
/*printlnerr("Lowering ADTs")*/
/*adt_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)*/
printlnerr("Lowering Defer") printlnerr("Lowering Defer")
defer_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax) defer_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
if (interpret_instead) { if (interpret_instead) {

View File

@@ -83,8 +83,9 @@ opt_parameter_list = parameter_list | ;
parameter_list = parameter_list WS "," WS parameter | parameter ; parameter_list = parameter_list WS "," WS parameter | parameter ;
parameter = boolean_expression ; parameter = boolean_expression ;
obj_nonterm = "obj" | "uni" ; def_nonterm = "def" ;
type_def = obj_nonterm WS identifier WS template_dec WS "{" WS declaration_block WS "}" | obj_nonterm WS identifier WS "{" WS declaration_block WS "}" | obj_nonterm WS identifier WS template_dec WS traits WS "{" WS declaration_block WS "}" | obj_nonterm WS identifier WS traits WS "{" WS declaration_block WS "}" ; obj_nonterm = "obj" ;
type_def = def_nonterm WS identifier WS type | obj_nonterm WS identifier WS template_dec WS "{" WS declaration_block WS "}" | obj_nonterm WS identifier WS "{" WS declaration_block WS "}" | obj_nonterm WS identifier WS template_dec WS traits WS "{" WS declaration_block WS "}" | obj_nonterm WS identifier WS traits WS "{" WS declaration_block WS "}" ;
declaration_block = declaration_statement line_end WS declaration_block | function WS declaration_block | declaration_statement line_end | function | ; declaration_block = declaration_statement line_end WS declaration_block | function WS declaration_block | declaration_statement line_end | function | ;
traits = "\(" WS trait_list WS "\)" ; traits = "\(" WS trait_list WS "\)" ;

33
stdlib/adt_lower.krak Normal file
View File

@@ -0,0 +1,33 @@
import symbol:*
import tree:*
import vector:*
import map:*
import util:*
import string:*
import mem:*
import io:*
import ast_nodes:*
import ast_transformation:*
import pass_common:*
fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
match(*node) {
ast_node::adt_def(backing) {
println(backing.name + ": entered!")
}
}
}
var helper_after = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
match(*node) {
ast_node::adt_def(backing) {
println(backing.name + ": exited!")
}
}
}
run_on_tree(helper_before, helper_after, syntax_ast_pair.second)
})
}

View File

@@ -190,10 +190,12 @@ obj identifier (Object) {
return name == other.name && type == other.type && enclosing_scope == other.enclosing_scope return name == other.name && type == other.type && enclosing_scope == other.enclosing_scope
} }
} }
fun ast_type_def_ptr(name: ref string): *ast_node fun ast_type_def_ptr(name: ref string): *ast_node {
return ast_type_def_ptr(name, false) return ast_type_def_ptr(name, false)
}
fun ast_type_def_ptr(name: ref string, is_union: bool): *ast_node { fun ast_type_def_ptr(name: ref string, is_union: bool): *ast_node {
var to_ret.construct(name, is_union): type_def /*var to_ret.construct(name, is_union): 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
@@ -211,10 +213,11 @@ obj type_def (Object) {
var self_type: *type var self_type: *type
var variables: vector<*ast_node> var variables: vector<*ast_node>
var methods: vector<*ast_node> var methods: vector<*ast_node>
fun construct(nameIn: ref string, is_unionIn: bool): *type_def { /*fun construct(nameIn: ref string, is_unionIn: bool): *type_def {*/
fun construct(nameIn: ref string): *type_def {
scope.construct() scope.construct()
name.copy_construct(&nameIn) name.copy_construct(&nameIn)
is_union = is_unionIn /*is_union = is_unionIn*/
self_type = null<type>() self_type = null<type>()
variables.construct() variables.construct()
methods.construct() methods.construct()
@@ -224,7 +227,7 @@ obj type_def (Object) {
self_type = old->self_type self_type = old->self_type
scope.copy_construct(&old->scope) scope.copy_construct(&old->scope)
name.copy_construct(&old->name) name.copy_construct(&old->name)
is_union = old->is_union /*is_union = old->is_union*/
variables.copy_construct(&old->variables) variables.copy_construct(&old->variables)
methods.copy_construct(&old->methods) methods.copy_construct(&old->methods)
} }
@@ -239,7 +242,8 @@ obj type_def (Object) {
copy_construct(&other) copy_construct(&other)
} }
fun operator==(other: ref type_def): bool { fun operator==(other: ref type_def): bool {
return name == other.name && is_union == other.is_union && self_type == other.self_type && variables == other.variables && methods == other.methods /*return name == other.name && is_union == other.is_union && self_type == other.self_type && variables == other.variables && methods == other.methods*/
return name == other.name && self_type == other.self_type && variables == other.variables && methods == other.methods
} }
} }
fun ast_adt_def_ptr(name: string): *ast_node { fun ast_adt_def_ptr(name: string): *ast_node {
@@ -1133,7 +1137,12 @@ fun get_ast_name(node: *ast_node): string {
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 + "; [" + backing.imported.reduce(fun(name: string, acc: string): string return acc + " " + name;, string()) + " ]" 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 + ": " + backing.type->to_string() ast_node::identifier(backing) return string("identifier: ") + backing.name + ": " + backing.type->to_string()
ast_node::type_def(backing) return string("type_def: ") + backing.name + " union?:" + backing.is_union ast_node::type_def(backing) {
/*if (backing.is_union)*/
/*return string("type_def union: ") + backing.name*/
/*else*/
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) { ast_node::function(backing) {
if (backing.is_extern) if (backing.is_extern)

View File

@@ -104,7 +104,9 @@ obj ast_transformation (Object) {
return template return template
} else { } else {
// pass in whether or not this is a union // pass in whether or not this is a union
var type_def_node = ast_type_def_ptr(name, concat_symbol_tree(get_node("obj_nonterm", child)) == "uni") /*var type_def_node = ast_type_def_ptr(name, concat_symbol_tree(get_node("obj_nonterm", child)) == "uni")*/
/*var type_def_node = ast_type_def_ptr(name, false)*/
var type_def_node = ast_type_def_ptr(name)
type_def_node->type_def.self_type = type_ptr(type_def_node, transform_traits(get_node("traits", child))) type_def_node->type_def.self_type = type_ptr(type_def_node, transform_traits(get_node("traits", child)))
ast_to_syntax.set(type_def_node, child) ast_to_syntax.set(type_def_node, child)
add_to_scope("~enclosing_scope", scope, type_def_node) add_to_scope("~enclosing_scope", scope, type_def_node)

View File

@@ -446,13 +446,13 @@ obj c_generator (Object) {
/*var base_name = vert->type_def.name*/ /*var base_name = vert->type_def.name*/
var base_name = get_name(vert) var base_name = get_name(vert)
plain_typedefs += string("typedef ") plain_typedefs += string("typedef ")
if (vert->type_def.is_union) { /*if (is_type_def(vert) && vert->type_def.is_union) {*/
plain_typedefs += "union " /*plain_typedefs += "union "*/
structs += "union " /*structs += "union "*/
} else { /*} else {*/
plain_typedefs += "struct " plain_typedefs += "struct "
structs += "struct " structs += "struct "
} /*}*/
plain_typedefs += base_name + "_dummy " + base_name + ";\n" plain_typedefs += base_name + "_dummy " + base_name + ";\n"
structs += base_name + "_dummy {\n" structs += base_name + "_dummy {\n"
if (is_type_def(vert)) { if (is_type_def(vert)) {

View File

@@ -4,9 +4,9 @@ import mem
import serialize import serialize
import io import io
fun to_string(in: bool): string /*fun to_string(in: bool): string*/
if (in) return string("true") /*if (in) return string("true")*/
else return string("false") /*else return string("false")*/
fun to_string(in: uchar): string fun to_string(in: uchar): string
return to_string_num(in) return to_string_num(in)
fun to_string(in: short): string fun to_string(in: short): string
@@ -171,7 +171,7 @@ obj string (Object, Serializable) {
} }
fun operator+(integer: int): string return *this + to_string(integer); fun operator+(integer: int): string return *this + to_string(integer);
fun operator+(b: bool): string return *this + to_string(b); /*fun operator+(b: bool): string return *this + to_string(b);*/
fun operator+(str: *char): string { fun operator+(str: *char): string {
var newStr.construct(str):string var newStr.construct(str):string
@@ -185,7 +185,7 @@ obj string (Object, Serializable) {
} }
fun operator+=(integer: int) *this += to_string(integer); fun operator+=(integer: int) *this += to_string(integer);
fun operator+=(b: bool) *this += to_string(b); /*fun operator+=(b: bool) *this += to_string(b);*/
fun operator+=(character: char): void { fun operator+=(character: char): void {
data += character data += character