diff --git a/k.krak b/k.krak index 2ceded8..a425a39 100644 --- a/k.krak +++ b/k.krak @@ -83,14 +83,14 @@ fun main(argc: int, argv: **char): int { if (positional_args.size > 1) executable_name = positional_args[1] - var pass_poset = poset>() + var pass_poset = poset, str>>() var name_ast_map = map>() - var passes = map() + var passes = map): bool>() // resolves a single import - passes[str("import_resolver")] = fun(import_binding: *ast): bool { + passes[str("import_resolver")] = fun(import_binding: *tree): bool { var file_path = binding_str(import_binding) - println("Running import resolver for" + file_path) + println("Running import resolver for " + file_path) if (!name_ast_map.contains_key(file_path)) { printerr(file_path + ", ") var parse_tree = parse.parse_input(read_file(file_path), file_path) @@ -103,7 +103,7 @@ fun main(argc: int, argv: **char): int { } // ensures that all imports reachable from this one are resolved - passes[str("import_checker")] = fun(import_binding: *ast): bool { + passes[str("import_checker")] = fun(import_binding: *tree): bool { var all_resolved = true var file_path = binding_str(import_binding) println("Running import checker for " + file_path) @@ -113,9 +113,9 @@ fun main(argc: int, argv: **char): int { if (!bound(b.first)) { all_resolved = false pass_poset.add_relationship(make_pair(import_binding, str("import_checker")), make_pair(b.first, str("import_resolver"))) - println(to_string(*b.first) + " is not bound!") + println(to_string(b.first->data) + " is not bound!") } else { - println(to_string(*b.first) + " is bound!") + println(to_string(b.first->data) + " is bound!") } } } @@ -129,7 +129,7 @@ fun main(argc: int, argv: **char): int { while (pass_poset.size() != 0) { var file_pass = pass_poset.top() - printlnerr("doing pass " + file_pass.second + " on " + to_string(*file_pass.first)) + printlnerr("doing pass " + file_pass.second + " on " + to_string(file_pass.first->data)) var done = passes[file_pass.second](file_pass.first) if (done) pass_poset.remove(file_pass) @@ -158,47 +158,97 @@ fun main(argc: int, argv: **char): int { return 0 } -var bindings: *vec<*ast> -fun make_binding(s: str): *ast { - var binding = new()->copy_construct(&ast::_binding(make_triple(s, vec<*type>(), null>()))) - if (bindings == null>()) - bindings = new>()->construct() +var bindings: *vec<*tree> +fun make_binding(s: str): *tree { + var binding = _binding(s, vec<*type>(), null>()) + if (bindings == null>>()) + bindings = new>>()->construct() bindings->add(binding) return binding } fun set_bindings(binding: *tree, to: *tree) { - set_bindings(&binding->data, to) -} -fun set_bindings(binding: *ast, to: *tree) { - match(*binding) { + match(binding->data) { ast::_binding(b) { - var from = binding->_binding.third + var from = b.third // don't set null, that will set all unbound ones if (from == null>()) { - binding->_binding.third = to + b.third = to return } for (var i = 0; i < bindings->size; i++;) - if (bindings->get(i)->_binding.third == from) - bindings->get(i)->_binding.third = to + if (bindings->get(i)->data._binding.third == from) + bindings->get(i)->data._binding.third = to return } } error("trying to set bindings on not a binding") } -fun bound(binding: *ast): bool { - match(*binding) { +fun bound(binding: *tree): bool { + match(binding->data) { ast::_binding(b) return b.third != null>() } error("Trying to check bound for not a binding") } -fun binding_str(binding: *ast): str { - match(*binding) { +fun binding_str(binding: *tree): str { + match(binding->data) { ast::_binding(b) return b.first } error("Trying to get name for not a binding") } +fun parse_type(syntax: *tree): *type { + var is_ref = get_node("\"ref\"", syntax) != null>() + var indr = 0 + syntax = get_node("pre_reffed", syntax) + var next = get_node("pre_reffed", syntax) + while(next != null>()) { + indr++ + syntax = next + next = get_node("pre_reffed", syntax) + } + var ident = get_node("scoped_identifier", syntax) + var func = get_node("function_type", syntax) + var first_child_name = syntax->children[0]->data.name + if (ident != null>()) { + var template_inst = get_node("template_inst", syntax) + if (template_inst != null>()) { + return type(base_type::_obj(make_binding(concat(ident) + "")), indr, is_ref) + } else { + return type(base_type::_obj(make_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 variadic = false + var raw = false + return 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) + } else if (first_child_name == "\"char\"") { + return type(base_type::_char(), indr, is_ref) + } else if (first_child_name == "\"uchar\"") { + return type(base_type::_uchar(), indr, is_ref) + } else if (first_child_name == "\"short\"") { + return type(base_type::_short(), indr, is_ref) + } else if (first_child_name == "\"ushort\"") { + return type(base_type::_ushort(), indr, is_ref) + } else if (first_child_name == "\"int\"") { + return type(base_type::_int(), indr, is_ref) + } else if (first_child_name == "\"uint\"") { + return type(base_type::_uint(), indr, is_ref) + } else if (first_child_name == "\"long\"") { + return type(base_type::_long(), indr, is_ref) + } else if (first_child_name == "\"ulong\"") { + return type(base_type::_ulong(), indr, is_ref) + } else if (first_child_name == "\"float\"") { + return type(base_type::_float(), indr, is_ref) + } else if (first_child_name == "\"double\"") { + return type(base_type::_double(), indr, is_ref) + } + error(syntax, "could not parse type " + first_child_name) +} + fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vec): *tree { var resolve_import_file = fun(file_name: str): str { var file_path = str() @@ -219,12 +269,25 @@ fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vecchildren[1]) + ".krak")), from_vector(syntax->children.slice(2,-1).filter(fun(s:*tree):bool { return s->data.name == "identifier" || s->data.data == "*" }).map(concat)), vec(syntax_to_ast_helper(syntax->children[1]))) - } else if (syntax->data.name == "function") - return _function(concat(get_node("func_identifier", syntax)), null(), - (get_nodes("typed_parameter", syntax) + - get_nodes("statement", syntax)).map(syntax_to_ast_helper)) - else if (syntax->data.name == "typed_parameter") - return _identifier(concat(get_node("identifier", syntax)), null()) + } 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() + 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) + var n = _function(concat(get_node("func_identifier", syntax)), function_type, parameters + body) + var template = get_node("template_dec", syntax) + if (template == null>()) { + return n + } else { + return _template(n->data._function.first, from_vector(get_nodes("template_param", template).map(concat)), vec(n)) + } + } else if (syntax->data.name == "typed_parameter") + return _identifier(concat(get_node("identifier", syntax)), parse_type(get_node("type", syntax))) else if (syntax->data.name == "type_def") { var n = _type_def(concat(get_node("identifier", syntax)), get_nodes("declaration_statement", syntax).map(syntax_to_ast_helper)) @@ -237,7 +300,11 @@ fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vecdata.name == "adt_def") { var n = _adt_def(concat(get_node("identifier", syntax)), get_nodes("adt_option", syntax).map(fun(s: *tree): *tree { - return _identifier(concat(get_node("identifier", s)), null()) + var option_type = get_node("type", s) + if (option_type != null>()) + 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)) })) var template = get_node("template_dec", syntax) if (template == null>()) { @@ -259,11 +326,11 @@ 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 children = vec(_identifier(concat(get_node("identifier", syntax)), null())) + var children = vec(_identifier(concat(get_node("identifier", syntax)), parse_type(get_node("type", syntax)))) children += get_nodes("boolean_expression", syntax).map(syntax_to_ast_helper) return _declaration(children) } else if (syntax->data.name == "assignment_statement") - return _assignment(vec(_binding(concat(syntax->children[1]), null>()), + return _assignment(vec(make_binding(concat(syntax->children[1])), syntax_to_ast_helper(syntax->children[0]), syntax_to_ast_helper(syntax->children[2]))) else if (syntax->data.name == "function_call") @@ -289,23 +356,23 @@ fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vec>()) { if (syntax->children[0]->data.name != "scoped_identifier") error(syntax, "Unexpected template instantiation (not on an identifier)") - return _binding(concat(syntax->children[0]) + "", vec<*type>(), null>()) + return make_binding(concat(syntax->children[0]) + "") } else if (syntax->children[0]->data.terminal) { - return _call(vec(_binding(concat(syntax->children[0]), null>()), + return _call(vec(make_binding(concat(syntax->children[0])), syntax_to_ast_helper(syntax->children[1]))) } else { - return _call(vec(_binding(concat(syntax->children[1]), null>()), + return _call(vec(make_binding(concat(syntax->children[1])), syntax_to_ast_helper(syntax->children[0]))) } } else { - return _call(vec(_binding(concat(syntax->children[1]), null>()), + return _call(vec(make_binding(concat(syntax->children[1])), syntax_to_ast_helper(syntax->children[0]), syntax_to_ast_helper(syntax->children[2]))) } } else if (syntax->data.name == "number") - return _value(concat(syntax), null()) + return _value(concat(syntax), type(base_type::_int(), 0, false)) else if (syntax->data.name == "scoped_identifier" || syntax->data.name == "identifier") - return _binding(concat(syntax), null>()) + return make_binding(concat(syntax)) else return null>() } diff --git a/stdlib/ast.krak b/stdlib/ast.krak index ce4ba65..d887574 100644 --- a/stdlib/ast.krak +++ b/stdlib/ast.krak @@ -8,7 +8,7 @@ import mem:* adt ast { _translation_unit: str, - _import: pair<*ast, set>, + _import: pair<*tree, set>, _identifier: pair, _binding: triple, *tree>, _type_def: str, @@ -35,12 +35,12 @@ adt ast { 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) + ")[" + str(",").join(b.second.data) + "]" - ast::_identifier(b) return str("_identifier(") + b.first + ")" + 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::_binding(b) return str("_binding(") + b.first + "->" + to_string(b.third) + ")" 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 + ")" + ast::_function(b) return str("_function(") + b.first + ": " + deref_to_string(b.second) + ")" ast::_template(b) return str("_template(") + b.first + "[" + str(",").join(b.second.data) + "])" ast::_declaration() return str("_declaration") ast::_assignment() return str("_assignment") @@ -57,13 +57,13 @@ 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 + ")" + ast::_value(b) return str("_value(") + b.first + ": " + deref_to_string(b.second) + ")" } } fun _translation_unit(p: str): *tree { return new>()->construct(ast::_translation_unit(p)) } -fun _import(p1: *ast, p2: set): *tree { +fun _import(p1: *tree, p2: set): *tree { return new>()->construct(ast::_import(make_pair(p1,p2))) } fun _type_def(p: str): *tree { @@ -78,9 +78,6 @@ fun _cast(p: *type): *tree { fun _identifier(p1: str, p2: *type): *tree { return new>()->construct(ast::_identifier(make_pair(p1, p2))) } -fun _binding(p1: str, p3: *tree): *tree { - return new>()->construct(ast::_binding(make_triple(p1, vec<*type>(), p3))) -} fun _binding(p1: str, p2: vec<*type>, p3: *tree): *tree { return new>()->construct(ast::_binding(make_triple(p1, p2, p3))) } @@ -141,7 +138,7 @@ fun _call(): *tree { fun _translation_unit(p: str, c: ref vec<*tree>): *tree { return new>()->construct(ast::_translation_unit(p), c) } -fun _import(p1: *ast, p2: set, c: ref vec<*tree>): *tree { +fun _import(p1: *tree, p2: set, c: ref vec<*tree>): *tree { return new>()->construct(ast::_import(make_pair(p1,p2)), c) } fun _type_def(p: str, c: ref vec<*tree>): *tree { @@ -156,9 +153,6 @@ fun _cast(p: *type, c: ref vec<*tree>): *tree { fun _identifier(p1: str, p2: *type, c: ref vec<*tree>): *tree { return new>()->construct(ast::_identifier(make_pair(p1, p2)), c) } -fun _binding(p1: str, p3: *tree, c: ref vec<*tree>): *tree { - return new>()->construct(ast::_binding(make_triple(p1, vec<*type>(), p3)), c) -} fun _binding(p1: str, p2: vec<*type>, p3: *tree, c: ref vec<*tree>): *tree { return new>()->construct(ast::_binding(make_triple(p1, p2, p3)), c) } diff --git a/stdlib/str.krak b/stdlib/str.krak index f43b450..20278d4 100644 --- a/stdlib/str.krak +++ b/stdlib/str.krak @@ -37,6 +37,12 @@ fun to_string(in: ulong): str fun to_string(in: *T): str return str("ptr:<") + to_string_num((in) cast ulong) + ">" +fun deref_to_string(in: *T): str + if (in == mem::null()) + return str("null") + else + return in->to_string() + fun string_to_num(it: str): T { var is_negative = false if (it[0] == '-') { diff --git a/stdlib/type2.krak b/stdlib/type2.krak index d2c91b0..50501b7 100644 --- a/stdlib/type2.krak +++ b/stdlib/type2.krak @@ -2,14 +2,15 @@ import mem:* import str:* import vec:* import util:* +import tree:* import ast:* adt base_type { _unknown, _void, - _object: *ast, + _obj: *tree, // triple, is_variadic, is raw> - _function: triple, *type>, bool, bool>, + _fun: triple, *type>, bool, bool>, _template_placeholder, _bool, _char, @@ -23,7 +24,9 @@ adt base_type { _float, _double } - +fun type(b: base_type, ind: int, ref: bool): *type { + return new()->construct(b, ind, ref) +} obj type (Object) { var base: base_type var indirection: int @@ -68,12 +71,20 @@ obj type (Object) { match (base) { base_type::_unknown() return indr_string + "_unknown" base_type::_void() return indr_string + "_void" - base_type::_object(b) { - return indr_string + "_object" + base_type::_obj(b) { + return indr_string + "_obj(" + to_string(b->data) + ")" } - base_type::_function(b) { + base_type::_fun(b) { // triple, is_variadic, is raw> - return indr_string + "_function()" + var to_ret = indr_string + if (b.second) + to_ret += "_run(" + else + to_ret += "_fun(" + to_ret += str(", ").join(b.first.first.map(fun(pt: *type): str return pt->to_string();)) + if (b.third) + to_ret += " ..." + return to_ret + "): " + b.first.second->to_string() } base_type::_template_placeholder() return indr_string + "_template_placeholder" base_type::_bool() return indr_string + "_bool" @@ -86,7 +97,7 @@ obj type (Object) { base_type::_long() return indr_string + "_long" base_type::_ulong() return indr_string + "_ulong" base_type::_float() return indr_string + "_float" - base_type::_double() return indr_string + "_double" + base_type::_double() return indr_string + "_double" } return str("impossible type, indirection:") + indirection } @@ -102,15 +113,15 @@ obj type (Object) { } return false } - fun is_object(): bool { + fun is_obj(): bool { match (base) { - base_type::_object() return true + base_type::_obj() return true } return false } - fun is_function(): bool { + fun is_fun(): bool { match (base) { - base_type::_function() return true + base_type::_fun() return true } return false }