Move types into binding also

This commit is contained in:
Nathan Braswell
2018-09-22 14:54:52 -04:00
parent 38133b8bc3
commit 0505a0e7d6
3 changed files with 74 additions and 62 deletions

81
k.krak
View File

@@ -136,11 +136,11 @@ fun main(argc: int, argv: **char): int {
set_ast_binding(binding, options[0])
}
}
var handle_type: fun(*type, *tree<ast>): void = fun(t: *type, n: *tree<ast>) {
match(t->base) {
var handle_type: fun(*binding<type>, *tree<ast>): void = fun(t: *binding<type>, n: *tree<ast>) {
match(t->bound_to->base) {
base_type::_obj(b) try_binding(b, n, true)
base_type::_fun(b) {
b.first.first.for_each(fun(it: *type) {
b.first.first.for_each(fun(it: *binding<type>) {
handle_type(it, n)
})
handle_type(b.first.second, n)
@@ -153,8 +153,8 @@ fun main(argc: int, argv: **char): int {
ast::_identifier(b) handle_type(b.second, t)
/*_binding: triple<str, vec<*type>, *tree<ast>>,*/
ast::_function(b) handle_type(b.second, t)
ast::_compiler_intrinsic(b) b.second.for_each(fun(ty: *type) {
handle_type(ty, t)
ast::_compiler_intrinsic(b) b.second.for_each(fun(tb: *binding<type>) {
handle_type(tb, t)
})
ast::_cast(b) handle_type(b, t)
/*_value: pair<str, *type>*/
@@ -181,7 +181,8 @@ fun main(argc: int, argv: **char): int {
return
}
var to_c_type = fun(t: *type): str {
var to_c_type = fun(tb: *binding<type>): str {
var t = tb->bound_to
var ind = str("*") * t->indirection
if (t->is_ref)
error("type is ref in to_c_type")
@@ -238,7 +239,7 @@ fun main(argc: int, argv: **char): int {
ast::_adt_def(b) { error("no adt_def should remain at C emit"); }
ast::_function(b) {
var fun_name = b.first
var fun_type = b.second
var fun_type = b.second->bound_to
var is_ext = b.third
var return_type = fun_type->base._fun.first.second
var parameter_types = fun_type->base._fun.first.first
@@ -342,21 +343,21 @@ fun main(argc: int, argv: **char): int {
// starting generation of the entire program
var real_main = _function(
str("main"),
type(base_type::_fun(make_triple(make_pair(vec(
type(base_type::_int(), 0, false),
type(base_type::_char(), 2, false)
binding(type(base_type::_fun(make_triple(make_pair(vec(
binding(type(base_type::_int(), 0, false)),
binding(type(base_type::_char(), 2, false))
),
type(base_type::_int(), 0, false)
), false, false)), 0, false),
binding(type(base_type::_int(), 0, false))
), false, false)), 0, false)),
true, vec(
_identifier(str("argc"), type(base_type::_int(), 0, false)),
_identifier(str("argv"), type(base_type::_char(), 2, false)),
_identifier(str("argc"), binding(type(base_type::_int(), 0, false))),
_identifier(str("argv"), binding(type(base_type::_char(), 2, false))),
_return(vec(_call(vec(make_ast_binding("fmain"), make_ast_binding("argc"), make_ast_binding("argv")))))
)
)
var top_unit = _translation_unit(str(), vec(
_import(make_ast_binding(kraken_file_name), set(str("*")), vec(
_identifier(kraken_file_name, type(base_type::_void(), 0, false))
_identifier(kraken_file_name, binding(type(base_type::_void(), 0, false)))
)),
real_main
))
@@ -391,7 +392,7 @@ fun main(argc: int, argv: **char): int {
return 0
}
fun parse_type(syntax: *tree<symbol>): *type {
fun parse_type(syntax: *tree<symbol>): *binding<type> {
var is_ref = get_node("\"ref\"", syntax) != null<tree<symbol>>()
var indr = 0
syntax = get_node("pre_reffed", syntax)
@@ -407,39 +408,39 @@ fun parse_type(syntax: *tree<symbol>): *type {
if (ident != null<tree<symbol>>()) {
var template_inst = get_node("template_inst", syntax)
if (template_inst != null<tree<symbol>>()) {
return type(base_type::_obj(make_ast_binding(concat(ident) + "<somin>")), indr, is_ref)
return binding(type(base_type::_obj(make_ast_binding(concat(ident) + "<somin>")), indr, is_ref))
} else {
return type(base_type::_obj(make_ast_binding(concat(ident))), indr, is_ref)
return binding(type(base_type::_obj(make_ast_binding(concat(ident))), indr, is_ref))
}
} else if (func != null<tree<symbol>>()) {
var param_types = vec<*type>()
var return_type = type(base_type::_void(), 0, false)
var param_types = vec<*binding<type>>()
var return_type = binding(type(base_type::_void(), 0, false))
var variadic = false
var raw = false
return type(base_type::_fun(make_triple(make_pair(param_types, return_type),
variadic, raw)), indr, is_ref)
return binding(type(base_type::_fun(make_triple(make_pair(param_types, return_type),
variadic, raw)), indr, is_ref))
} else if (first_child_name == "\"void\"") {
return type(base_type::_void(), indr, is_ref)
return binding(type(base_type::_void(), indr, is_ref))
} else if (first_child_name == "\"char\"") {
return type(base_type::_char(), indr, is_ref)
return binding(type(base_type::_char(), indr, is_ref))
} else if (first_child_name == "\"uchar\"") {
return type(base_type::_uchar(), indr, is_ref)
return binding(type(base_type::_uchar(), indr, is_ref))
} else if (first_child_name == "\"short\"") {
return type(base_type::_short(), indr, is_ref)
return binding(type(base_type::_short(), indr, is_ref))
} else if (first_child_name == "\"ushort\"") {
return type(base_type::_ushort(), indr, is_ref)
return binding(type(base_type::_ushort(), indr, is_ref))
} else if (first_child_name == "\"int\"") {
return type(base_type::_int(), indr, is_ref)
return binding(type(base_type::_int(), indr, is_ref))
} else if (first_child_name == "\"uint\"") {
return type(base_type::_uint(), indr, is_ref)
return binding(type(base_type::_uint(), indr, is_ref))
} else if (first_child_name == "\"long\"") {
return type(base_type::_long(), indr, is_ref)
return binding(type(base_type::_long(), indr, is_ref))
} else if (first_child_name == "\"ulong\"") {
return type(base_type::_ulong(), indr, is_ref)
return binding(type(base_type::_ulong(), indr, is_ref))
} else if (first_child_name == "\"float\"") {
return type(base_type::_float(), indr, is_ref)
return binding(type(base_type::_float(), indr, is_ref))
} else if (first_child_name == "\"double\"") {
return type(base_type::_double(), indr, is_ref)
return binding(type(base_type::_double(), indr, is_ref))
}
error(syntax, "could not parse type " + first_child_name)
}
@@ -467,13 +468,13 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
} else if (syntax->data.name == "function") {
var parameters = get_nodes("typed_parameter", syntax).map(syntax_to_ast_helper)
var body = syntax_to_ast_helper(get_node("statement", syntax))
var return_type = null<type>()
var return_type = null<binding<type>>()
var return_type_node = get_node("typed_return", syntax)
if (return_type_node != null<tree<symbol>>())
return_type = parse_type(get_node("type", return_type_node))
else
return_type = type(base_type::_void(), 0, false)
var function_type = type(base_type::_fun(make_triple(make_pair(parameters.map(fun(i: *tree<ast>): *type return i->data._identifier.second;), return_type), false, false)), 0, false)
return_type = binding(type(base_type::_void(), 0, false))
var function_type = binding(type(base_type::_fun(make_triple(make_pair(parameters.map(fun(i: *tree<ast>): *binding<type> return i->data._identifier.second;), return_type), false, false)), 0, false))
var n = _function(concat(get_node("func_identifier", syntax)), function_type, false, parameters + body)
var template = get_node("template_dec", syntax)
if (template == null<tree<symbol>>()) {
@@ -499,7 +500,7 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
if (option_type != null<tree<symbol>>())
return _identifier(concat(get_node("identifier", s)), parse_type(option_type))
else
return _identifier(concat(get_node("identifier", s)), type(base_type::_void(), 0, false))
return _identifier(concat(get_node("identifier", s)), binding(type(base_type::_void(), 0, false)))
}))
var template = get_node("template_dec", syntax)
if (template == null<tree<symbol>>()) {
@@ -523,7 +524,7 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
return _case(s->children.map(syntax_to_ast_helper))
}))
} else if (syntax->data.name == "declaration_statement") {
var t = type(base_type::_unknown(), 0, false)
var t = binding(type(base_type::_unknown(), 0, false))
var type_syntax = get_node("type", syntax)
if type_syntax != null<tree<symbol>>()
t = parse_type(type_syntax)
@@ -571,9 +572,9 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
syntax_to_ast_helper(syntax->children[2])))
}
} else if (syntax->data.name == "number")
return _value(concat(syntax), type(base_type::_int(), 0, false))
return _value(concat(syntax), binding(type(base_type::_int(), 0, false)))
else if (syntax->data.name == "bool")
return _value(concat(syntax), type(base_type::_bool(), 0, false))
return _value(concat(syntax), binding(type(base_type::_bool(), 0, false)))
else if (syntax->data.name == "scoped_identifier" || syntax->data.name == "identifier")
return make_ast_binding(concat(syntax))
else {

View File

@@ -9,11 +9,11 @@ import mem:*
adt ast {
_translation_unit: str,
_import: pair<*tree<ast>, set<str>>,
_identifier: pair<str, *type>,
_identifier: pair<str, *binding<type>>,
_binding: pair<str, *binding<tree<ast>>>,
_type_def: str,
_adt_def: str,
_function: triple<str, *type, bool>,
_function: triple<str, *binding<type>, bool>,
_template: pair<str, set<str>>,
_declaration,
_assignment,
@@ -28,19 +28,19 @@ adt ast {
_continue,
_defer,
_call,
_compiler_intrinsic: pair<str, vec<*type>>,
_cast: *type,
_value: pair<str, *type>
_compiler_intrinsic: pair<str, vec<*binding<type>>>,
_cast: *binding<type>,
_value: pair<str, *binding<type>>
}
fun to_string(a: ref ast): str {
match(a) {
ast::_translation_unit(b) return str("_translation_unit(") + b + ")"
ast::_import(b) return str("_import(") + to_string(b.first->data) + ")[" + str(",").join(b.second.data) + "]"
ast::_identifier(b) return str("_identifier(") + b.first + ": " + deref_to_string(b.second) + ")"
ast::_identifier(b) return str("_identifier(") + b.first + ": " + deref_to_string(b.second->bound_to) + ")"
ast::_binding(b) return str("_binding(") + b.first + "->" + to_string(b.second->bound_to) + ")"
ast::_type_def(b) return str("_type_def(") + b + ")"
ast::_adt_def(b) return str("_adt_def(") + b + ")"
ast::_function(b) return str("_function(") + b.first + ": " + deref_to_string(b.second) + ", ext?:" + to_string(b.third) + ")"
ast::_function(b) return str("_function(") + b.first + ": " + deref_to_string(b.second->bound_to) + ", ext?:" + to_string(b.third) + ")"
ast::_template(b) return str("_template(") + b.first + "[" + str(",").join(b.second.data) + "])"
ast::_declaration() return str("_declaration")
ast::_assignment() return str("_assignment")
@@ -57,7 +57,7 @@ fun to_string(a: ref ast): str {
ast::_call() return str("_call")
ast::_compiler_intrinsic(b) return str("_compiler_intrinsic(") + b.first + ")"
ast::_cast(b) return str("_cast")
ast::_value(b) return str("_value(") + b.first + ": " + deref_to_string(b.second) + ")"
ast::_value(b) return str("_value(") + b.first + ": " + deref_to_string(b.second->bound_to) + ")"
}
}
fun _translation_unit(p: str): *tree<ast> {
@@ -72,25 +72,25 @@ fun _type_def(p: str): *tree<ast> {
fun _adt_def(p: str): *tree<ast> {
return new<tree<ast>>()->construct(ast::_adt_def(p))
}
fun _cast(p: *type): *tree<ast> {
fun _cast(p: *binding<type>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_cast(p))
}
fun _identifier(p1: str, p2: *type): *tree<ast> {
fun _identifier(p1: str, p2: *binding<type>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_identifier(make_pair(p1, p2)))
}
fun _binding(p1: str, p2: *binding<tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_binding(make_pair(p1, p2)))
}
fun _function(p1: str, p2: *type, p3: bool): *tree<ast> {
fun _function(p1: str, p2: *binding<type>, p3: bool): *tree<ast> {
return new<tree<ast>>()->construct(ast::_function(make_triple(p1, p2, p3)))
}
fun _template(p1: str, p2: set<str>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_template(make_pair(p1, p2)))
}
fun _compiler_intrinsic(p1: str, p2: vec<*type>): *tree<ast> {
fun _compiler_intrinsic(p1: str, p2: vec<*binding<type>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_compiler_intrinsic(make_pair(p1, p2)))
}
fun _value(p1: str, p2: *type): *tree<ast> {
fun _value(p1: str, p2: *binding<type>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_value(make_pair(p1, p2)))
}
fun _declaration(): *tree<ast> {
@@ -147,25 +147,25 @@ fun _type_def(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
fun _adt_def(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_adt_def(p), c)
}
fun _cast(p: *type, c: ref vec<*tree<ast>>): *tree<ast> {
fun _cast(p: *binding<type>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_cast(p), c)
}
fun _identifier(p1: str, p2: *type, c: ref vec<*tree<ast>>): *tree<ast> {
fun _identifier(p1: str, p2: *binding<type>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_identifier(make_pair(p1, p2)), c)
}
fun _binding(p1: str, p2: *binding<tree<ast>>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_binding(make_pair(p1, p2)), c)
}
fun _function(p1: str, p2: *type, p3: bool, c: ref vec<*tree<ast>>): *tree<ast> {
fun _function(p1: str, p2: *binding<type>, p3: bool, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_function(make_triple(p1, p2, p3)), c)
}
fun _template(p1: str, p2: set<str>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_template(make_pair(p1, p2)), c)
}
fun _compiler_intrinsic(p1: str, p2: vec<*type>, c: ref vec<*tree<ast>>): *tree<ast> {
fun _compiler_intrinsic(p1: str, p2: vec<*binding<type>>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_compiler_intrinsic(make_pair(p1, p2)), c)
}
fun _value(p1: str, p2: *type, c: ref vec<*tree<ast>>): *tree<ast> {
fun _value(p1: str, p2: *binding<type>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_value(make_pair(p1, p2)), c)
}
fun _declaration(c: ref vec<*tree<ast>>): *tree<ast> {
@@ -241,7 +241,10 @@ fun is_top_level_item(i: *tree<ast>): bool { return i->parent != null<tree<ast>>
var bindings: *vec<*void>
fun binding<T>(): *binding<T> {
var to_ret = new<binding<T>>()->construct()
return binding(null<T>())
}
fun binding<T>(it: *T): *binding<T> {
var to_ret = new<binding<T>>()->construct(it)
if (bindings == null<vec<*void>>())
bindings = new<vec<*void>>()->construct()
bindings->add( (to_ret) cast *void )
@@ -254,6 +257,10 @@ obj binding<T> (Object) {
bound_to = null<T>()
return this
}
fun construct(it: *T): *binding<T> {
bound_to = it
return this
}
fun copy_construct(old: *binding<T>): void {
bound_to = old->bound_to
}
@@ -274,6 +281,10 @@ obj binding<T> (Object) {
if ( ((bindings->get(i)) cast *binding<T>)->bound_to == from)
((bindings->get(i)) cast *binding<T>)->bound_to = to
}
fun to_string(): str {
return "binding(" + to_string(bound_to) + ")"
/*return "binding(" + deref_to_string(bound_to) + ")"*/
}
}

View File

@@ -10,7 +10,7 @@ adt base_type {
_void,
_obj: *tree<ast>,
// triple<pair<param_types, return_type>, is_variadic, is raw>
_fun: triple<pair<vec<*type>, *type>, bool, bool>,
_fun: triple<pair<vec<*binding<type>>, *binding<type>>, bool, bool>,
_template_placeholder,
_bool,
_char,
@@ -81,10 +81,10 @@ obj type (Object) {
to_ret += "_run("
else
to_ret += "_fun("
to_ret += str(", ").join(b.first.first.map(fun(pt: *type): str return pt->to_string();))
to_ret += str(", ").join(b.first.first.map(fun(pt: *binding<type>): str return pt->bound_to->to_string();))
if (b.third)
to_ret += " ..."
return to_ret + "): " + b.first.second->to_string()
return to_ret + "): " + b.first.second->bound_to->to_string()
}
base_type::_template_placeholder() return indr_string + "_template_placeholder"
base_type::_bool() return indr_string + "_bool"