diff --git a/k.krak b/k.krak index 39065fc..b952993 100644 --- a/k.krak +++ b/k.krak @@ -136,11 +136,11 @@ fun main(argc: int, argv: **char): int { set_ast_binding(binding, options[0]) } } - var handle_type: fun(*type, *tree): void = fun(t: *type, n: *tree) { - match(t->base) { + var handle_type: fun(*binding, *tree): void = fun(t: *binding, n: *tree) { + 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) { 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, *tree>,*/ 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) { + handle_type(tb, t) }) ast::_cast(b) handle_type(b, t) /*_value: pair*/ @@ -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): 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): *type { +fun parse_type(syntax: *tree): *binding { var is_ref = get_node("\"ref\"", syntax) != null>() var indr = 0 syntax = get_node("pre_reffed", syntax) @@ -407,39 +408,39 @@ fun parse_type(syntax: *tree): *type { if (ident != null>()) { var template_inst = get_node("template_inst", syntax) if (template_inst != null>()) { - 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 { - 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>()) { - var param_types = vec<*type>() - var return_type = type(base_type::_void(), 0, false) + var param_types = vec<*binding>() + 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, import_paths: ref vecdata.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() + var return_type = null>() var return_type_node = get_node("typed_return", syntax) if (return_type_node != null>()) 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): *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): *binding 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>()) { @@ -499,7 +500,7 @@ fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vec>()) 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>()) { @@ -523,7 +524,7 @@ fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vecchildren.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>() t = parse_type(type_syntax) @@ -571,9 +572,9 @@ fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vecchildren[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 { diff --git a/stdlib/ast.krak b/stdlib/ast.krak index 0960af1..9a6df8f 100644 --- a/stdlib/ast.krak +++ b/stdlib/ast.krak @@ -9,11 +9,11 @@ import mem:* adt ast { _translation_unit: str, _import: pair<*tree, set>, - _identifier: pair, + _identifier: pair>, _binding: pair>>, _type_def: str, _adt_def: str, - _function: triple, + _function: triple, bool>, _template: pair>, _declaration, _assignment, @@ -28,19 +28,19 @@ adt ast { _continue, _defer, _call, - _compiler_intrinsic: pair>, - _cast: *type, - _value: pair + _compiler_intrinsic: pair>>, + _cast: *binding, + _value: pair> } 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 { @@ -72,25 +72,25 @@ fun _type_def(p: str): *tree { fun _adt_def(p: str): *tree { return new>()->construct(ast::_adt_def(p)) } -fun _cast(p: *type): *tree { +fun _cast(p: *binding): *tree { return new>()->construct(ast::_cast(p)) } -fun _identifier(p1: str, p2: *type): *tree { +fun _identifier(p1: str, p2: *binding): *tree { return new>()->construct(ast::_identifier(make_pair(p1, p2))) } fun _binding(p1: str, p2: *binding>): *tree { return new>()->construct(ast::_binding(make_pair(p1, p2))) } -fun _function(p1: str, p2: *type, p3: bool): *tree { +fun _function(p1: str, p2: *binding, p3: bool): *tree { return new>()->construct(ast::_function(make_triple(p1, p2, p3))) } fun _template(p1: str, p2: set): *tree { return new>()->construct(ast::_template(make_pair(p1, p2))) } -fun _compiler_intrinsic(p1: str, p2: vec<*type>): *tree { +fun _compiler_intrinsic(p1: str, p2: vec<*binding>): *tree { return new>()->construct(ast::_compiler_intrinsic(make_pair(p1, p2))) } -fun _value(p1: str, p2: *type): *tree { +fun _value(p1: str, p2: *binding): *tree { return new>()->construct(ast::_value(make_pair(p1, p2))) } fun _declaration(): *tree { @@ -147,25 +147,25 @@ fun _type_def(p: str, c: ref vec<*tree>): *tree { fun _adt_def(p: str, c: ref vec<*tree>): *tree { return new>()->construct(ast::_adt_def(p), c) } -fun _cast(p: *type, c: ref vec<*tree>): *tree { +fun _cast(p: *binding, c: ref vec<*tree>): *tree { return new>()->construct(ast::_cast(p), c) } -fun _identifier(p1: str, p2: *type, c: ref vec<*tree>): *tree { +fun _identifier(p1: str, p2: *binding, c: ref vec<*tree>): *tree { return new>()->construct(ast::_identifier(make_pair(p1, p2)), c) } fun _binding(p1: str, p2: *binding>, c: ref vec<*tree>): *tree { return new>()->construct(ast::_binding(make_pair(p1, p2)), c) } -fun _function(p1: str, p2: *type, p3: bool, c: ref vec<*tree>): *tree { +fun _function(p1: str, p2: *binding, p3: bool, c: ref vec<*tree>): *tree { return new>()->construct(ast::_function(make_triple(p1, p2, p3)), c) } fun _template(p1: str, p2: set, c: ref vec<*tree>): *tree { return new>()->construct(ast::_template(make_pair(p1, p2)), c) } -fun _compiler_intrinsic(p1: str, p2: vec<*type>, c: ref vec<*tree>): *tree { +fun _compiler_intrinsic(p1: str, p2: vec<*binding>, c: ref vec<*tree>): *tree { return new>()->construct(ast::_compiler_intrinsic(make_pair(p1, p2)), c) } -fun _value(p1: str, p2: *type, c: ref vec<*tree>): *tree { +fun _value(p1: str, p2: *binding, c: ref vec<*tree>): *tree { return new>()->construct(ast::_value(make_pair(p1, p2)), c) } fun _declaration(c: ref vec<*tree>): *tree { @@ -241,7 +241,10 @@ fun is_top_level_item(i: *tree): bool { return i->parent != null> var bindings: *vec<*void> fun binding(): *binding { - var to_ret = new>()->construct() + return binding(null()) +} +fun binding(it: *T): *binding { + var to_ret = new>()->construct(it) if (bindings == null>()) bindings = new>()->construct() bindings->add( (to_ret) cast *void ) @@ -254,6 +257,10 @@ obj binding (Object) { bound_to = null() return this } + fun construct(it: *T): *binding { + bound_to = it + return this + } fun copy_construct(old: *binding): void { bound_to = old->bound_to } @@ -274,6 +281,10 @@ obj binding (Object) { if ( ((bindings->get(i)) cast *binding)->bound_to == from) ((bindings->get(i)) cast *binding)->bound_to = to } + fun to_string(): str { + return "binding(" + to_string(bound_to) + ")" + /*return "binding(" + deref_to_string(bound_to) + ")"*/ + } } diff --git a/stdlib/type2.krak b/stdlib/type2.krak index 50501b7..9f4a2e7 100644 --- a/stdlib/type2.krak +++ b/stdlib/type2.krak @@ -10,7 +10,7 @@ adt base_type { _void, _obj: *tree, // triple, is_variadic, is raw> - _fun: triple, *type>, bool, bool>, + _fun: triple>, *binding>, 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): 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"