diff --git a/kraken.krak b/kraken.krak index 8d54d33..fbc486f 100644 --- a/kraken.krak +++ b/kraken.krak @@ -2,7 +2,7 @@ import io:* import grammer:* import lexer:* import parser:* -import string:* +import str:* import util:* import symbol:* import tree:* @@ -22,16 +22,16 @@ import address_of_ensure_variable_lower:* import c_line_control:* import node_counter:* import c_generator:* -import vector:* +import vec:* import set:* fun main(argc: int, argv: **char):int { var curr_time = get_time() // delay construction until we either load it or copy construct it var gram: grammer - var base_dir = string("/").join(string(argv[0]).split('/').slice(0,-2)) + var base_dir = str("/").join(str(argv[0]).split('/').slice(0,-2)) var file_name = base_dir + "/krakenGrammer.kgm" - var compiled_name = file_name + string(".comp_new") + var compiled_name = file_name + str(".comp_new") var compiled_version = 1 var file_contents = read_file(file_name) var loaded_and_valid = false @@ -40,13 +40,13 @@ fun main(argc: int, argv: **char):int { if (argc <= 1) { println("No input file!\n Call with one argument (the input file), or two arguments (input file and output name)\n Falling into REPL...") compiled_name += ".expr" - file_contents = string("RealGoal = boolean_expression ;\n") + file_contents + file_contents = str("RealGoal = boolean_expression ;\n") + file_contents doing_repl = true - } else if (string(argv[1]) == "-v" || string(argv[1]) == "--version") { + } else if (str(argv[1]) == "-v" || str(argv[1]) == "--version") { /*var version_c_string = #ctce(fun(): *char {*/ - /*var version_string = string("Self-hosted Kraken compiler \"Kalypso\" - revision ") + from_system_command(string("git rev-list HEAD | wc -l"), 100) +*/ - /*", commit: " + from_system_command(string("git rev-parse HEAD"), 100) +*/ - /*", compile date: " + from_system_command(string("date"), 100) */ + /*var version_string = str("Self-hosted Kraken compiler \"Kalypso\" - revision ") + from_system_command(str("git rev-list HEAD | wc -l"), 100) +*/ + /*", commit: " + from_system_command(str("git rev-parse HEAD"), 100) +*/ + /*", compile date: " + from_system_command(str("date"), 100) */ /*return version_string.toCharArray()*/ /*}())*/ /*println(version_c_string)*/ @@ -54,13 +54,13 @@ fun main(argc: int, argv: **char):int { } var input_file_offset = 1 var interpret_instead = false - var opt_str = string("-O2") + var opt_str = str("-O2") var line_ctrl = false var compile_c = true - var positional_args = vector() - var flags = set() + var positional_args = vec() + var flags = set() for (var i = 1; i < argc; i++;) { - var arg_str = string(argv[i]) + var arg_str = str(argv[i]) if (arg_str == "-i") { interpret_instead = true } else if (arg_str.length() > 2 && arg_str.slice(0,2) == "-O") { @@ -75,8 +75,8 @@ fun main(argc: int, argv: **char):int { positional_args.add(arg_str) } } - /*positional_args.for_each(fun(i:string) println("positional_arg: " + i);)*/ - flags.for_each(fun(i:string) println("flag: " + i);) + /*positional_args.for_each(fun(i:str) println("positional_arg: " + i);)*/ + flags.for_each(fun(i:str) println("flag: " + i);) if (file_exists(compiled_name)) { var pos = 0 @@ -84,8 +84,8 @@ fun main(argc: int, argv: **char):int { var saved_version = 0 unpack(saved_version, pos) = unserialize(binary, pos) if (saved_version == compiled_version) { - var cached_contents = string() - unpack(cached_contents, pos) = unserialize(binary, pos) + var cached_contents = str() + unpack(cached_contents, pos) = unserialize(binary, pos) if (cached_contents == file_contents) { loaded_and_valid = true pos = gram.unserialize(binary, pos) @@ -118,33 +118,33 @@ fun main(argc: int, argv: **char):int { /*var parse7.construct(&gram): parser*/ /*var parse8.construct(&gram): parser*/ var ast_pass.construct(): ast_transformation - var parsers = vector(parse1) - /*var parsers = vector(parse1,parse2,parse3,parse4)*/ - /*var parsers = vector(parse1,parse2,parse3,parse4,parse5,parse6)*/ - /*var parsers = vector(parse1,parse2,parse3,parse4,parse5,parse6,parse7,parse8)*/ + var parsers = vec(parse1) + /*var parsers = vec(parse1,parse2,parse3,parse4)*/ + /*var parsers = vec(parse1,parse2,parse3,parse4,parse5,parse6)*/ + /*var parsers = vec(parse1,parse2,parse3,parse4,parse5,parse6,parse7,parse8)*/ // This is our REPL loop - var scope = ast_translation_unit_ptr(string("stdin")) + var scope = ast_translation_unit_ptr(str("stdin")) if (doing_repl) { /*var globals = setup_globals(importer.name_ast_map)*/ while (doing_repl) { - var line = get_line(string("> "), 100) + var line = get_line(str("> "), 100) if (line == "end") return 0 - var parse = parse1.parse_input(line, string("stdin")) + var parse = parse1.parse_input(line, str("stdin")) trim(parse) - var ast_expression = ast_pass.transform_expression(parse, scope, map()) + var ast_expression = ast_pass.transform_expression(parse, scope, map()) print_value(evaluate_constant_expression(ast_expression)) /*print_value(evaluate_with_globals(ast_expression, &globals))*/ } } var kraken_file_name = positional_args[0] - var executable_name = string(".").join(kraken_file_name.split('.').slice(0,-2)) + var executable_name = str(".").join(kraken_file_name.split('.').slice(0,-2)) if (positional_args.size > 1) executable_name = positional_args[1] curr_time = split(curr_time, "Finish setup") - var name_ast_map = import(kraken_file_name, parsers, ast_pass, vector(string(), base_dir + "/stdlib/")) + var name_ast_map = import(kraken_file_name, parsers, ast_pass, vec(str(), base_dir + "/stdlib/")) curr_time = split(curr_time, "Import") // Passes /*printlnerr("Counting Nodes")*/ diff --git a/stdlib/address_of_ensure_variable_lower.krak b/stdlib/address_of_ensure_variable_lower.krak index 693dfe3..63e514c 100644 --- a/stdlib/address_of_ensure_variable_lower.krak +++ b/stdlib/address_of_ensure_variable_lower.krak @@ -2,15 +2,15 @@ import symbol:* import tree:* import map:* import util:* -import string:* +import str:* import io:* import ast_nodes:* import ast_transformation:* import pass_common:* -fun address_of_ensure_variable_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { +fun address_of_ensure_variable_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var visited = hash_set<*ast_node>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { match(*node) { ast_node::function_call(backing) { diff --git a/stdlib/adt_lower.krak b/stdlib/adt_lower.krak index dc320a1..a8a5dfc 100644 --- a/stdlib/adt_lower.krak +++ b/stdlib/adt_lower.krak @@ -1,9 +1,9 @@ import symbol:* import tree:* -import vector:* +import vec:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* @@ -13,16 +13,16 @@ import hash_set:* import pass_common:* -fun adt_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { - var type_def_option_map = map<*ast_node, vector<*ast_node>>() +fun adt_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { + var type_def_option_map = map<*ast_node, vec<*ast_node>>() var visited1 = hash_set<*ast_node>() var visited2 = hash_set<*ast_node>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { match(*node) { ast_node::adt_def(backing) { /*println(backing.name + ": transforming!")*/ - type_def_option_map[node] = vector<*ast_node>() + type_def_option_map[node] = vec<*ast_node>() var replacement = ast_type_def_ptr(backing.name, false); // we're going to be replacing adt_def in the same ptr, so this works replacement->type_def.self_type = node->adt_def.self_type @@ -35,27 +35,27 @@ fun adt_lower(name_ast_map: *map,*ast_node>>, ast_to_ }) var option_union_type = type_ptr(option_union) option_union->type_def.self_type = option_union_type - var option_union_ident = ast_identifier_ptr(string("data"), option_union_type, replacement) + var option_union_ident = ast_identifier_ptr(str("data"), option_union_type, replacement) replacement->type_def.variables.add(ast_declaration_statement_ptr(option_union_ident, null())) add_to_scope("data", option_union_ident, replacement) - var flag = ast_identifier_ptr(string("flag"), type_ptr(base_type::integer()), replacement) + var flag = ast_identifier_ptr(str("flag"), type_ptr(base_type::integer()), replacement) replacement->type_def.variables.add(ast_declaration_statement_ptr(flag, null())) add_to_scope("flag", flag, replacement) add_before_in(option_union, node, parent_chain) - var enclosing_scope = node->adt_def.scope[string("~enclosing_scope")][0] + var enclosing_scope = node->adt_def.scope[str("~enclosing_scope")][0] var idx = 0 node->adt_def.option_funcs.for_each(fun(func: *ast_node) { var adt_type = replacement->type_def.self_type var block = ast_code_block_ptr() add_to_scope("~enclosing_scope", func, block) func->function.body_statement = block - var to_ret = ast_identifier_ptr(string("to_ret"), adt_type, block) + var to_ret = ast_identifier_ptr(str("to_ret"), adt_type, block) block->code_block.children.add(ast_declaration_statement_ptr(to_ret, null())) var value = ast_value_ptr(to_string(idx), type_ptr(base_type::integer())) - block->code_block.children.add(ast_assignment_statement_ptr(make_operator_call(".", vector(to_ret, flag)), value)) + block->code_block.children.add(ast_assignment_statement_ptr(make_operator_call(".", vec(to_ret, flag)), value)) var opt = type_def_option_map[node][idx] - var lvalue = make_operator_call(".", vector(make_operator_call(".", vector(to_ret, option_union_ident)), opt)) + var lvalue = make_operator_call(".", vec(make_operator_call(".", vec(to_ret, option_union_ident)), opt)) if (func->function.parameters.size) { // do copy_construct if it should block->code_block.children.add(assign_or_copy_construct_statement(lvalue, func->function.parameters[0])) @@ -73,55 +73,55 @@ fun adt_lower(name_ast_map: *map,*ast_node>>, ast_to_ var func_this = func->function.this_param if (func->function.name == "operator==") { var other = func->function.parameters[0] - var if_stmt = ast_if_statement_ptr(make_operator_call("!=", vector(make_operator_call("->", vector(func_this, flag)), make_operator_call(".", vector(other, flag))))) - if_stmt->if_statement.then_part = ast_return_statement_ptr(ast_value_ptr(string("false"), type_ptr(base_type::boolean()))) + var if_stmt = ast_if_statement_ptr(make_operator_call("!=", vec(make_operator_call("->", vec(func_this, flag)), make_operator_call(".", vec(other, flag))))) + if_stmt->if_statement.then_part = ast_return_statement_ptr(ast_value_ptr(str("false"), type_ptr(base_type::boolean()))) block->code_block.children.add(if_stmt) for (var i = 0; i < type_def_option_map[node].size; i++;) { if (get_ast_type(type_def_option_map[node][i])->is_empty_adt_option()) continue - var if_stmt_inner = ast_if_statement_ptr(make_operator_call("==", vector(make_operator_call("->", vector(func_this, flag)), ast_value_ptr(to_string(i), type_ptr(base_type::integer()))))) + var if_stmt_inner = ast_if_statement_ptr(make_operator_call("==", vec(make_operator_call("->", vec(func_this, flag)), ast_value_ptr(to_string(i), type_ptr(base_type::integer()))))) var option = type_def_option_map[node][i] - var our_option = make_operator_call(".", vector(make_operator_call("->", vector(func_this, option_union_ident)), option)) - var their_option = make_operator_call(".", vector(make_operator_call(".", vector(other, option_union_ident)), option)) + var our_option = make_operator_call(".", vec(make_operator_call("->", vec(func_this, option_union_ident)), option)) + var their_option = make_operator_call(".", vec(make_operator_call(".", vec(other, option_union_ident)), option)) if_stmt_inner->if_statement.then_part = ast_return_statement_ptr(possible_object_equality(our_option, their_option)) block->code_block.children.add(if_stmt_inner) } - block->code_block.children.add(ast_return_statement_ptr(ast_value_ptr(string("true"), type_ptr(base_type::boolean())))) + block->code_block.children.add(ast_return_statement_ptr(ast_value_ptr(str("true"), type_ptr(base_type::boolean())))) } else if (func->function.name == "operator!=") { var other = func->function.parameters[0] - block->code_block.children.add(ast_return_statement_ptr(make_operator_call("!", vector(make_method_call(func_this, "operator==", vector(other)))))) + block->code_block.children.add(ast_return_statement_ptr(make_operator_call("!", vec(make_method_call(func_this, "operator==", vec(other)))))) } else if (func->function.name == "construct") { - var value = ast_value_ptr(string("-1"), type_ptr(base_type::integer())) - block->code_block.children.add(ast_assignment_statement_ptr(make_operator_call("->", vector(func_this, flag)), value)) + var value = ast_value_ptr(str("-1"), type_ptr(base_type::integer())) + block->code_block.children.add(ast_assignment_statement_ptr(make_operator_call("->", vec(func_this, flag)), value)) block->code_block.children.add(ast_return_statement_ptr(func_this)) } else if (func->function.name == "copy_construct") { var other = func->function.parameters[0] - block->code_block.children.add(ast_assignment_statement_ptr(make_operator_call("->", vector(func_this, flag)), make_operator_call("->", vector(other, flag)))) + block->code_block.children.add(ast_assignment_statement_ptr(make_operator_call("->", vec(func_this, flag)), make_operator_call("->", vec(other, flag)))) for (var i = 0; i < type_def_option_map[node].size; i++;) { if (get_ast_type(type_def_option_map[node][i])->is_empty_adt_option()) continue - var if_stmt_inner = ast_if_statement_ptr(make_operator_call("==", vector(make_operator_call("->", vector(func_this, flag)), ast_value_ptr(to_string(i), type_ptr(base_type::integer()))))) + var if_stmt_inner = ast_if_statement_ptr(make_operator_call("==", vec(make_operator_call("->", vec(func_this, flag)), ast_value_ptr(to_string(i), type_ptr(base_type::integer()))))) var option = type_def_option_map[node][i] - var our_option = make_operator_call(".", vector(make_operator_call("->", vector(func_this, option_union_ident)), option)) - var their_option = make_operator_call(".", vector(make_operator_call("->", vector(other, option_union_ident)), option)) + var our_option = make_operator_call(".", vec(make_operator_call("->", vec(func_this, option_union_ident)), option)) + var their_option = make_operator_call(".", vec(make_operator_call("->", vec(other, option_union_ident)), option)) if_stmt_inner->if_statement.then_part = assign_or_copy_construct_statement(our_option, their_option) block->code_block.children.add(if_stmt_inner) } } else if (func->function.name == "operator=") { var other = func->function.parameters[0] - block->code_block.children.add(make_method_call(func_this, "destruct", vector<*ast_node>())) - block->code_block.children.add(make_method_call(func_this, "copy_construct", vector(make_operator_call("&", vector(other))))) + block->code_block.children.add(make_method_call(func_this, "destruct", vec<*ast_node>())) + block->code_block.children.add(make_method_call(func_this, "copy_construct", vec(make_operator_call("&", vec(other))))) } else if (func->function.name == "destruct") { for (var i = 0; i < type_def_option_map[node].size; i++;) { var option = type_def_option_map[node][i] var option_type = get_ast_type(option) if (option_type->is_empty_adt_option()) continue - if (option_type->indirection == 0 && option_type->is_object() && has_method(option_type->type_def, "destruct", vector<*type>())) { - var if_stmt_inner = ast_if_statement_ptr(make_operator_call("==", vector(make_operator_call("->", vector(func_this, flag)), ast_value_ptr(to_string(i), type_ptr(base_type::integer()))))) - var our_option = make_operator_call(".", vector(make_operator_call("->", vector(func_this, option_union_ident)), option)) - if_stmt_inner->if_statement.then_part = make_method_call(our_option, "destruct", vector<*ast_node>()) + if (option_type->indirection == 0 && option_type->is_object() && has_method(option_type->type_def, "destruct", vec<*type>())) { + var if_stmt_inner = ast_if_statement_ptr(make_operator_call("==", vec(make_operator_call("->", vec(func_this, flag)), ast_value_ptr(to_string(i), type_ptr(base_type::integer()))))) + var our_option = make_operator_call(".", vec(make_operator_call("->", vec(func_this, option_union_ident)), option)) + if_stmt_inner->if_statement.then_part = make_method_call(our_option, "destruct", vec<*ast_node>()) block->code_block.children.add(if_stmt_inner) } } @@ -138,16 +138,16 @@ fun adt_lower(name_ast_map: *map,*ast_node>>, ast_to_ } run_on_tree(helper_before, empty_pass_second_half(), syntax_ast_pair.second, &visited1) }) - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var second_helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { match(*node) { ast_node::match_statement(backing) { var block = ast_code_block_ptr() add_to_scope("~enclosing_scope", parent_chain->item_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i) || is_function(i);), block) var value = backing.value - var holder = ast_identifier_ptr(string("holder"), get_ast_type(value)->clone_with_increased_indirection(), block) + var holder = ast_identifier_ptr(str("holder"), get_ast_type(value)->clone_with_increased_indirection(), block) block->code_block.children.add(ast_declaration_statement_ptr(holder, null())) - block->code_block.children.add(ast_assignment_statement_ptr(holder, make_operator_call("&", vector(value)))) + block->code_block.children.add(ast_assignment_statement_ptr(holder, make_operator_call("&", vec(value)))) backing.cases.for_each(fun(case_stmt: *ast_node) { var option = case_stmt->case_statement.option var flag = get_from_scope(get_ast_type(value)->type_def, "flag") @@ -158,14 +158,14 @@ fun adt_lower(name_ast_map: *map,*ast_node>>, ast_to_ for (var i = 0; i < type_def_option_map[get_ast_type(value)->type_def].size; i++;) if (type_def_option_map[get_ast_type(value)->type_def][i] == option) option_num = i; - var condition = make_operator_call("==", vector(make_operator_call("->", vector(holder, flag)), ast_value_ptr(to_string(option_num), type_ptr(base_type::integer())))) + var condition = make_operator_call("==", vec(make_operator_call("->", vec(holder, flag)), ast_value_ptr(to_string(option_num), type_ptr(base_type::integer())))) var if_stmt = ast_if_statement_ptr(condition) var inner_block = ast_code_block_ptr() add_to_scope("~enclosing_scope", block, inner_block) var unpack_ident = case_stmt->case_statement.unpack_ident if (unpack_ident) { - var get_option = make_operator_call(".", vector(make_operator_call("->", vector(holder, data)), option)) - get_option = make_operator_call("&", vector(get_option)) + var get_option = make_operator_call(".", vec(make_operator_call("->", vec(holder, data)), option)) + get_option = make_operator_call("&", vec(get_option)) unpack_ident->identifier.type = unpack_ident->identifier.type->clone_with_ref() inner_block->code_block.children.add(ast_declaration_statement_ptr(unpack_ident, get_option)) } @@ -184,8 +184,8 @@ fun adt_lower(name_ast_map: *map,*ast_node>>, ast_to_ return; /*println(backing.parameters[1]->identifier.name + " getting, adt . or -> call!")*/ var object = node->function_call.parameters[0] - node->function_call.parameters[0] = make_operator_call(backing.func->function.name, vector(object, get_from_scope(left_type->type_def, "data"))) - node->function_call.func = get_builtin_function(".", vector(get_ast_type(get_from_scope(left_type->type_def, "data")), get_ast_type(backing.parameters[1]))) + node->function_call.parameters[0] = make_operator_call(backing.func->function.name, vec(object, get_from_scope(left_type->type_def, "data"))) + node->function_call.func = get_builtin_function(".", vec(get_ast_type(get_from_scope(left_type->type_def, "data")), get_ast_type(backing.parameters[1]))) } } } diff --git a/stdlib/ast_nodes.krak b/stdlib/ast_nodes.krak index 329d15c..c29a669 100644 --- a/stdlib/ast_nodes.krak +++ b/stdlib/ast_nodes.krak @@ -1,12 +1,12 @@ import tree:* import symbol:* -import vector:* -import vector_literals:* +import vec:* +import vec_literals:* import set:* import stack:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import type:* @@ -42,7 +42,7 @@ fun ast_node_ptr(node: ast_node): *ast_node { to_ret->copy_construct(&node) return to_ret } -fun ast_translation_unit_ptr(name: string): *ast_node { +fun ast_translation_unit_ptr(name: str): *ast_node { var obj_var.construct(name): translation_unit var ptr = new() ptr->copy_construct(&ast_node::translation_unit(obj_var)) @@ -55,11 +55,11 @@ fun is_translation_unit(node: *ast_node): bool { return false } obj translation_unit (Object) { - var scope: map> - var children: vector<*ast_node> - var lambdas: vector<*ast_node> - var name: string - fun construct(nameIn: string): *translation_unit { + var scope: map> + var children: vec<*ast_node> + var lambdas: vec<*ast_node> + var name: str + fun construct(nameIn: str): *translation_unit { scope.construct() children.construct() lambdas.construct() @@ -86,7 +86,7 @@ obj translation_unit (Object) { return children == other.children && name == other.name && lambdas == other.lambdas } } -fun ast_import_ptr(name: string, containing_tu: *ast_node): *ast_node { +fun ast_import_ptr(name: str, containing_tu: *ast_node): *ast_node { var to_ret.construct(name, containing_tu): import var ptr = new() ptr->copy_construct(&ast_node::import(to_ret)) @@ -99,13 +99,13 @@ fun is_import(node: *ast_node): bool { return false } obj import (Object) { - var scope: map> - var imported: set + var scope: map> + var imported: set var containing_translation_unit: *ast_node var translation_unit: *ast_node - var name: string + var name: str var starred: bool - fun construct(nameIn: string, containing_tu: *ast_node): *import { + fun construct(nameIn: str, containing_tu: *ast_node): *import { scope.construct() imported.construct() name.copy_construct(&nameIn) @@ -136,12 +136,12 @@ obj import (Object) { } } fun ast_identifier_ptr(name: *char, type: *type, enclosing_scope: *ast_node): *ast_node { - return ast_identifier_ptr(string(name), type, enclosing_scope) + return ast_identifier_ptr(str(name), type, enclosing_scope) } -fun ast_identifier_ptr(name: string, type: *type, enclosing_scope: *ast_node): *ast_node { +fun ast_identifier_ptr(name: str, type: *type, enclosing_scope: *ast_node): *ast_node { return ast_identifier_ptr(name, type, enclosing_scope, false) } -fun ast_identifier_ptr(name: string, type: *type, enclosing_scope: *ast_node, is_extern: bool): *ast_node { +fun ast_identifier_ptr(name: str, type: *type, enclosing_scope: *ast_node, is_extern: bool): *ast_node { var to_ret.construct(name, type, enclosing_scope, is_extern): identifier var ptr = new() ptr->copy_construct(&ast_node::identifier(to_ret)) @@ -154,12 +154,12 @@ fun is_identifier(node: *ast_node): bool { return false } obj identifier (Object) { - var name: string - var scope: map> + var name: str + var scope: map> var type: *type var enclosing_scope: *ast_node var is_extern: bool - fun construct(name_in: string, type_in: *type, enclosing_scope: *ast_node, is_extern_in: bool): *identifier { + fun construct(name_in: str, type_in: *type, enclosing_scope: *ast_node, is_extern_in: bool): *identifier { name.copy_construct(&name_in) scope.construct() type = type_in @@ -186,11 +186,11 @@ obj identifier (Object) { return name == other.name && type == other.type && enclosing_scope == other.enclosing_scope && is_extern == other.is_extern } } -/*fun ast_type_def_ptr(name: string): *ast_node {*/ -fun ast_type_def_ptr(name: ref string): *ast_node { +/*fun ast_type_def_ptr(name: str): *ast_node {*/ +fun ast_type_def_ptr(name: ref str): *ast_node { 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 str, is_union: bool): *ast_node { var to_ret.construct(name, is_union): type_def /*var to_ret.construct(name): type_def*/ var ptr = new() @@ -204,15 +204,15 @@ fun is_type_def(node: *ast_node): bool { return false } obj type_def (Object) { - var scope: map> - var name: string + var scope: map> + var name: str var self_type: *type - var variables: vector<*ast_node> - var methods: vector<*ast_node> + var variables: vec<*ast_node> + var methods: vec<*ast_node> var is_union: bool - fun construct(nameIn: ref string, is_unionIn: bool): *type_def { - /*fun construct(nameIn: ref string): *type_def {*/ - /*fun construct(nameIn: string): *type_def {*/ + fun construct(nameIn: ref str, is_unionIn: bool): *type_def { + /*fun construct(nameIn: ref str): *type_def {*/ + /*fun construct(nameIn: str): *type_def {*/ scope.construct() name.copy_construct(&nameIn) is_union = is_unionIn @@ -244,7 +244,7 @@ obj type_def (Object) { /*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: str): *ast_node { var to_ret.construct(name): adt_def var ptr = new() ptr->copy_construct(&ast_node::adt_def(to_ret)) @@ -257,13 +257,13 @@ fun is_adt_def(node: *ast_node): bool { return false } obj adt_def (Object) { - var scope: map> - var name: string + var scope: map> + var name: str var self_type: *type - var options: vector<*ast_node> - var option_funcs: vector<*ast_node> - var regular_funcs: vector<*ast_node> - fun construct(nameIn: string): *adt_def { + var options: vec<*ast_node> + var option_funcs: vec<*ast_node> + var regular_funcs: vec<*ast_node> + fun construct(nameIn: str): *adt_def { scope.construct() name.copy_construct(&nameIn) self_type = null() @@ -295,9 +295,9 @@ obj adt_def (Object) { return name == other.name && self_type == other.self_type && options == other.options && option_funcs == other.option_funcs && regular_funcs == other.regular_funcs } } -fun ast_function_ptr(name: string, type: *type, parameters: vector<*ast_node>, is_extern: bool): *ast_node +fun ast_function_ptr(name: str, type: *type, parameters: vec<*ast_node>, is_extern: bool): *ast_node return ast_function_ptr(name, type, parameters, null(), is_extern, false) -fun ast_function_ptr(name: string, type: *type, parameters: vector<*ast_node>, this_param: *ast_node, is_extern: bool, is_variadic: bool): *ast_node { +fun ast_function_ptr(name: str, type: *type, parameters: vec<*ast_node>, this_param: *ast_node, is_extern: bool, is_variadic: bool): *ast_node { var to_ret.construct(name, type, parameters, this_param, is_extern, is_variadic): function var ptr = new() ptr->copy_construct(&ast_node::function(to_ret)) @@ -310,16 +310,16 @@ fun is_function(node: *ast_node): bool { return false } obj function (Object) { - var name: string + var name: str var type: *type - var parameters: vector<*ast_node> + var parameters: vec<*ast_node> var this_param: *ast_node var closed_variables: set<*ast_node> var body_statement: *ast_node - var scope: map> + var scope: map> var is_extern: bool var is_variadic: bool - fun construct(name_in: string, type_in: *type, parameters_in: vector<*ast_node>, this_param_in: *ast_node, is_extern_in: bool, is_variadic_in: bool): *function { + fun construct(name_in: str, type_in: *type, parameters_in: vec<*ast_node>, this_param_in: *ast_node, is_extern_in: bool, is_variadic_in: bool): *function { name.copy_construct(&name_in) parameters.copy_construct(¶meters_in) this_param = this_param_in @@ -356,7 +356,7 @@ obj function (Object) { return name == name && type == other.type && parameters == other.parameters && this_param == other.this_param && body_statement == other.body_statement && closed_variables == other.closed_variables && is_extern == other.is_extern && is_variadic == other.is_variadic } } -fun ast_template_ptr(name: string, syntax_node: *tree, template_types: vector, template_type_replacements: map, is_function: bool): *ast_node { +fun ast_template_ptr(name: str, syntax_node: *tree, template_types: vec, template_type_replacements: map, is_function: bool): *ast_node { var to_ret.construct(name, syntax_node, template_types, template_type_replacements, is_function): template var ptr = new() ptr->copy_construct(&ast_node::template(to_ret)) @@ -369,15 +369,15 @@ fun is_template(node: *ast_node): bool { return false } obj template (Object) { - var name: string + var name: str var syntax_node: *tree - var instantiated: vector<*ast_node> - var template_types: vector - var template_type_replacements: map - var instantiated_map: map, *ast_node> - var scope: map> + var instantiated: vec<*ast_node> + var template_types: vec + var template_type_replacements: map + var instantiated_map: map, *ast_node> + var scope: map> var is_function: bool - fun construct(name_in: string, syntax_node_in: *tree, template_types_in: vector, template_type_replacements_in: map, is_function: bool): *template { + fun construct(name_in: str, syntax_node_in: *tree, template_types_in: vec, template_type_replacements_in: map, is_function: bool): *template { name.copy_construct(&name_in) syntax_node = syntax_node_in instantiated.construct() @@ -434,8 +434,8 @@ fun is_code_block(node: *ast_node): bool { return false } obj code_block (Object) { - var scope: map> - var children: vector<*ast_node> + var scope: map> + var children: vec<*ast_node> fun construct(): *code_block { scope.construct() children.construct() @@ -474,7 +474,7 @@ obj if_statement (Object) { // these are not a part of the constructor because they have to be trnasformed with this as its scope var then_part: *ast_node var else_part: *ast_node - var scope: map> + var scope: map> fun construct(condition_in: *ast_node): *if_statement { condition = condition_in then_part = null() @@ -512,9 +512,9 @@ fun is_match_statement(node: *ast_node): bool { return false } obj match_statement (Object) { - var scope: map> + var scope: map> var value: *ast_node - var cases: vector<*ast_node> + var cases: vec<*ast_node> fun construct(value_in: *ast_node): *match_statement { scope.construct() value = value_in @@ -551,7 +551,7 @@ fun is_case_statement(node: *ast_node): bool { return false } obj case_statement (Object) { - var scope: map> + var scope: map> var option: *ast_node var unpack_ident: *ast_node var statement: *ast_node @@ -594,7 +594,7 @@ fun is_while_loop(node: *ast_node): bool { obj while_loop (Object) { var condition: *ast_node var statement: *ast_node - var scope: map> + var scope: map> fun construct(condition_in: *ast_node): *while_loop { condition = condition_in statement = null() @@ -634,7 +634,7 @@ obj for_loop (Object) { var condition: *ast_node var update: *ast_node var body: *ast_node - var scope: map> + var scope: map> fun construct(): *for_loop { scope.construct() init = null() @@ -675,7 +675,7 @@ fun is_return_statement(node: *ast_node): bool { } obj return_statement (Object) { var return_value: *ast_node - var scope: map> + var scope: map> fun construct(return_value_in: *ast_node): *return_statement { return_value = return_value_in scope.construct() @@ -847,7 +847,7 @@ fun is_if_comp(node: *ast_node): bool { return false } obj if_comp (Object) { - var wanted_generator: string + var wanted_generator: str var statement: *ast_node fun construct(): *if_comp { wanted_generator.construct() @@ -881,11 +881,11 @@ fun is_simple_passthrough(node: *ast_node): bool { return false } obj simple_passthrough (Object) { - var scope: map> - var passthrough_str: string - var in_params: vector> - var out_params: vector> - var linker_str: string + var scope: map> + var passthrough_str: str + var in_params: vec> + var out_params: vec> + var linker_str: str fun construct(): *simple_passthrough { scope.construct() passthrough_str.construct() @@ -917,7 +917,7 @@ obj simple_passthrough (Object) { out_params == other.out_params && linker_str == other.linker_str } } -fun ast_function_call_ptr(func: *ast_node, parameters: vector<*ast_node>): *ast_node { +fun ast_function_call_ptr(func: *ast_node, parameters: vec<*ast_node>): *ast_node { var to_ret.construct(func, parameters): function_call var ptr = new() ptr->copy_construct(&ast_node::function_call(to_ret)) @@ -931,8 +931,8 @@ fun is_function_call(node: *ast_node): bool { } obj function_call (Object) { var func: *ast_node - var parameters: vector<*ast_node> - fun construct(func_in: *ast_node, parameters_in: vector<*ast_node>): *function_call { + var parameters: vec<*ast_node> + fun construct(func_in: *ast_node, parameters_in: vec<*ast_node>): *function_call { func = func_in parameters.copy_construct(¶meters_in) return this @@ -952,7 +952,7 @@ obj function_call (Object) { return func == func && parameters == other.parameters } } -fun ast_compiler_intrinsic_ptr(intrinsic: string, parameters: vector<*ast_node>, type_parameters: vector<*type>, return_type: *type): *ast_node { +fun ast_compiler_intrinsic_ptr(intrinsic: str, parameters: vec<*ast_node>, type_parameters: vec<*type>, return_type: *type): *ast_node { var to_ret.construct(intrinsic, parameters, type_parameters, return_type): compiler_intrinsic var ptr = new() ptr->copy_construct(&ast_node::compiler_intrinsic(to_ret)) @@ -965,11 +965,11 @@ fun is_compiler_intrinsic(node: *ast_node): bool { return false } obj compiler_intrinsic (Object) { - var intrinsic: string - var parameters: vector<*ast_node> - var type_parameters: vector<*type> + var intrinsic: str + var parameters: vec<*ast_node> + var type_parameters: vec<*type> var return_type: *type - fun construct(intrinsic_in: string, parameters_in: vector<*ast_node>, type_parameters_in: vector<*type>, return_type_in: *type): *compiler_intrinsic { + fun construct(intrinsic_in: str, parameters_in: vec<*ast_node>, type_parameters_in: vec<*type>, return_type_in: *type): *compiler_intrinsic { intrinsic.copy_construct(&intrinsic_in) parameters.copy_construct(¶meters_in) type_parameters.copy_construct(&type_parameters_in) @@ -1030,7 +1030,7 @@ obj cast (Object) { return value == other.value && to_type == other.to_type } } -fun ast_value_ptr(string_value: string, value_type: *type): *ast_node { +fun ast_value_ptr(string_value: str, value_type: *type): *ast_node { var to_ret.construct(string_value, value_type): value var ptr = new() ptr->copy_construct(&ast_node::value(to_ret)) @@ -1043,10 +1043,10 @@ fun is_value(node: *ast_node): bool { return false } obj value (Object) { - var string_value: string + var string_value: str var value_type: *type - var scope: map> - fun construct(string_value_in: string, value_type_in: *type): *value { + var scope: map> + fun construct(string_value_in: str, value_type_in: *type): *value { scope.construct() string_value.copy_construct(&string_value_in) value_type = value_type_in @@ -1070,76 +1070,76 @@ obj value (Object) { } } -fun get_ast_children(node: *ast_node): vector<*ast_node> { +fun get_ast_children(node: *ast_node): vec<*ast_node> { match (*node) { // Don't get lambdas, let them come up naturally in passes (so can get enclosing function and stuff) /*ast_node::translation_unit(backing) return backing.children + backing.lambdas*/ ast_node::translation_unit(backing) return backing.children - ast_node::import(backing) return vector<*ast_node>() - ast_node::identifier(backing) return vector<*ast_node>() + ast_node::import(backing) return vec<*ast_node>() + ast_node::identifier(backing) return vec<*ast_node>() ast_node::type_def(backing) return backing.variables + backing.methods ast_node::adt_def(backing) return backing.options + backing.option_funcs ast_node::function(backing) return backing.parameters + backing.body_statement ast_node::template(backing) return backing.instantiated ast_node::code_block(backing) return backing.children - ast_node::if_statement(backing) return vector(backing.condition, backing.then_part, backing.else_part) - ast_node::match_statement(backing) return vector(backing.value) + backing.cases - ast_node::case_statement(backing) return vector(backing.option, backing.unpack_ident, backing.statement) - ast_node::while_loop(backing) return vector(backing.condition, backing.statement) - ast_node::for_loop(backing) return vector(backing.init, backing.condition, backing.update, backing.body) - ast_node::return_statement(backing) return vector(backing.return_value) - ast_node::branching_statement(backing) return vector<*ast_node>() - ast_node::defer_statement(backing) return vector(backing.statement) - ast_node::assignment_statement(backing) return vector(backing.to, backing.from) - ast_node::declaration_statement(backing) return vector(backing.identifier, backing.expression, backing.init_method_call) - ast_node::if_comp(backing) return vector<*ast_node>(backing.statement) - ast_node::simple_passthrough(backing) return vector<*ast_node>() - ast_node::function_call(backing) return vector(backing.func) + backing.parameters + ast_node::if_statement(backing) return vec(backing.condition, backing.then_part, backing.else_part) + ast_node::match_statement(backing) return vec(backing.value) + backing.cases + ast_node::case_statement(backing) return vec(backing.option, backing.unpack_ident, backing.statement) + ast_node::while_loop(backing) return vec(backing.condition, backing.statement) + ast_node::for_loop(backing) return vec(backing.init, backing.condition, backing.update, backing.body) + ast_node::return_statement(backing) return vec(backing.return_value) + ast_node::branching_statement(backing) return vec<*ast_node>() + ast_node::defer_statement(backing) return vec(backing.statement) + ast_node::assignment_statement(backing) return vec(backing.to, backing.from) + ast_node::declaration_statement(backing) return vec(backing.identifier, backing.expression, backing.init_method_call) + ast_node::if_comp(backing) return vec<*ast_node>(backing.statement) + ast_node::simple_passthrough(backing) return vec<*ast_node>() + ast_node::function_call(backing) return vec(backing.func) + backing.parameters ast_node::compiler_intrinsic(backing) return backing.parameters - ast_node::cast(backing) return vector<*ast_node>(backing.value) - ast_node::value(backing) return vector<*ast_node>() + ast_node::cast(backing) return vec<*ast_node>(backing.value) + ast_node::value(backing) return vec<*ast_node>() } } -fun get_ast_name(node: *ast_node): string { +fun get_ast_name(node: *ast_node): str { match (*node) { - 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::identifier(backing) return string("identifier: ") + backing.name + ": " + backing.type->to_string() + ast_node::translation_unit(backing) return str("translation_unit: ") + backing.name + ast_node::import(backing) return str("import: ") + backing.name + "; [" + backing.imported.reduce(fun(name: str, acc: str): str return acc + " " + name;, str()) + " ]" + ast_node::identifier(backing) return str("identifier: ") + backing.name + ": " + backing.type->to_string() ast_node::type_def(backing) { /*if (backing.is_union)*/ - /*return string("type_def union: ") + backing.name*/ + /*return str("type_def union: ") + backing.name*/ /*else*/ - return string("type_def: ") + backing.name + return str("type_def: ") + backing.name } - ast_node::adt_def(backing) return string("adt_def: ") + backing.name + ast_node::adt_def(backing) return str("adt_def: ") + backing.name ast_node::function(backing) { if (backing.is_extern) - return string("extern function: ") + backing.name + ": " + backing.type->to_string() + return str("extern function: ") + backing.name + ": " + backing.type->to_string() else - return string("function: ") + backing.name + ": " + backing.type->to_string() + return str("function: ") + backing.name + ": " + backing.type->to_string() } - ast_node::template(backing) return string("template: ") + backing.name - ast_node::code_block(backing) return string("code_block") - ast_node::if_statement(backing) return string("if_statement") - ast_node::match_statement(backing) return string("match_statement") - ast_node::case_statement(backing) return string("case_statement") - ast_node::while_loop(backing) return string("while_loop") - ast_node::for_loop(backing) return string("for_loop") - ast_node::return_statement(backing) return string("return_statement") - ast_node::branching_statement(backing) return string("branching_statement") - ast_node::defer_statement(backing) return string("defer_statement") - ast_node::assignment_statement(backing) return string("assignment_statement") - ast_node::declaration_statement(backing) return string("declaration_statement") - ast_node::if_comp(backing) return string("if_comp: ") + backing.wanted_generator - ast_node::simple_passthrough(backing) return string("simple_passthrough: , string:") + backing.passthrough_str - ast_node::function_call(backing) return string("function_call:") + get_ast_name(backing.func) + "(" + backing.parameters.size + ")" - ast_node::compiler_intrinsic(backing) return string("compiler_intrinsic:") + backing.intrinsic + "(" + backing.parameters.size + "," + backing.type_parameters.size + "):" + backing.return_type->to_string() - ast_node::cast(backing) return string("cast: ") + get_ast_name(backing.value) + ": " + backing.to_type->to_string() - ast_node::value(backing) return string("value: ") + backing.string_value + ": " + backing.value_type->to_string() + ast_node::template(backing) return str("template: ") + backing.name + ast_node::code_block(backing) return str("code_block") + ast_node::if_statement(backing) return str("if_statement") + ast_node::match_statement(backing) return str("match_statement") + ast_node::case_statement(backing) return str("case_statement") + ast_node::while_loop(backing) return str("while_loop") + ast_node::for_loop(backing) return str("for_loop") + ast_node::return_statement(backing) return str("return_statement") + ast_node::branching_statement(backing) return str("branching_statement") + ast_node::defer_statement(backing) return str("defer_statement") + ast_node::assignment_statement(backing) return str("assignment_statement") + ast_node::declaration_statement(backing) return str("declaration_statement") + ast_node::if_comp(backing) return str("if_comp: ") + backing.wanted_generator + ast_node::simple_passthrough(backing) return str("simple_passthrough: , str:") + backing.passthrough_str + ast_node::function_call(backing) return str("function_call:") + get_ast_name(backing.func) + "(" + backing.parameters.size + ")" + ast_node::compiler_intrinsic(backing) return str("compiler_intrinsic:") + backing.intrinsic + "(" + backing.parameters.size + "," + backing.type_parameters.size + "):" + backing.return_type->to_string() + ast_node::cast(backing) return str("cast: ") + get_ast_name(backing.value) + ": " + backing.to_type->to_string() + ast_node::value(backing) return str("value: ") + backing.string_value + ": " + backing.value_type->to_string() } - return string("impossible adt type") + return str("impossible adt type") } -fun get_ast_scope(node: *ast_node): *map> { +fun get_ast_scope(node: *ast_node): *map> { match (*node) { ast_node::translation_unit() return &node->translation_unit.scope ast_node::import() return &node->import.scope @@ -1158,7 +1158,7 @@ fun get_ast_scope(node: *ast_node): *map> { ast_node::simple_passthrough() return &node->simple_passthrough.scope ast_node::value() return &node->value.scope } - return null>>() + return null>>() } fun get_ast_type(node: *ast_node): *type { match (*node) { @@ -1172,14 +1172,14 @@ fun get_ast_type(node: *ast_node): *type { return null() } -fun ast_to_dot(root: *ast_node): string { - var ret = string("digraph Kaken {\n") +fun ast_to_dot(root: *ast_node): str { + var ret = str("digraph Kaken {\n") var counter = 0 - var node_name_map = map<*ast_node, string>() - var get_name = fun(node: *ast_node): string { + var node_name_map = map<*ast_node, str>() + var get_name = fun(node: *ast_node): str { if (node_name_map.contains_key(node)) return node_name_map[node] - var escaped = string("") + var escaped = str("") get_ast_name(node).data.for_each(fun(c: char) { if (c != '"') escaped += c @@ -1196,7 +1196,7 @@ fun ast_to_dot(root: *ast_node): string { get_ast_children(node).for_each(fun(child: *ast_node) { if (!child || done_set.contains(child)) return; // where on earth does the null come from - ret += string("\"") + get_name(node) + "\" -> \"" + get_name(child) + "\"\n"; + ret += str("\"") + get_name(node) + "\" -> \"" + get_name(child) + "\"\n"; helper(child) }) } diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index c5330f6..c859de5 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -1,11 +1,11 @@ import symbol:* import tree:* -import vector:* +import vec:* import queue:* import stack:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import os:* @@ -16,7 +16,7 @@ import pass_common:* adt search_type { none, - function: vector<*type> + function: vec<*type> } obj ast_transformation (Object) { @@ -44,7 +44,7 @@ obj ast_transformation (Object) { fourth_pass_worklist.destruct() } // first pass defines all type_defs (objects and aliases), ADTs, and top-level if-comps/passthroughs - fun first_pass(file_name: string, parse_tree: *tree): pair<*ast_node, vector<*ast_node>> { + fun first_pass(file_name: str, parse_tree: *tree): pair<*ast_node, vec<*ast_node>> { var translation_unit = ast_translation_unit_ptr(file_name) parse_tree->children.for_each(fun(child: *tree) { if (child->data.name == "type_def") { @@ -52,7 +52,7 @@ obj ast_transformation (Object) { } else if (child->data.name == "adt_def") { var name = concat_symbol_tree(get_node("identifier", child)) var adt_def_node = ast_adt_def_ptr(name) - adt_def_node->adt_def.self_type = type_ptr(adt_def_node, set(string("Object"))) + adt_def_node->adt_def.self_type = type_ptr(adt_def_node, set(str("Object"))) translation_unit->translation_unit.children.add(adt_def_node) ast_to_syntax.set(adt_def_node, child) add_to_scope("~enclosing_scope", translation_unit, adt_def_node) @@ -65,9 +65,9 @@ obj ast_transformation (Object) { }) // now do all imports - // re return a vector of them so importer can fix them (and our translation unit scope) + // re return a vec of them so importer can fix them (and our translation unit scope) // up with actual pointers to the other ASTs - var imports = vector<*ast_node>() + var imports = vec<*ast_node>() parse_tree->children.for_each(fun(child: *tree) { if (child->data.name == "import") { var import_identifier_children = get_nodes("identifier", child) @@ -77,24 +77,24 @@ obj ast_transformation (Object) { translation_unit->translation_unit.children.add(import_node) ast_to_syntax.set(import_node, child) add_to_scope("~enclosing_scope", translation_unit, import_node) - import_node->import.imported = from_vector(import_identifier_children.slice(1,-1).map(fun(ident: *tree):string return concat_symbol_tree(ident);)) + import_node->import.imported = from_vector(import_identifier_children.slice(1,-1).map(fun(ident: *tree):str return concat_symbol_tree(ident);)) if (get_node("\"\\*\"", child)) import_node->import.starred = true } }) return make_pair(translation_unit, imports) } - fun transform_traits(traits_node: *tree): set { + fun transform_traits(traits_node: *tree): set { if (!traits_node) - return set() - return from_vector(get_nodes("scoped_identifier", traits_node).map(fun(s: *tree): string return concat_symbol_tree(s);)) + return set() + return from_vector(get_nodes("scoped_identifier", traits_node).map(fun(s: *tree): str return concat_symbol_tree(s);)) } fun first_pass_type_def(child: *tree, scope: *ast_node, instantiate_template: bool): *ast_node { var name = concat_symbol_tree(get_node("identifier", child)) var template_dec = get_node("template_dec", child) if (template_dec && !instantiate_template) { - var template_types = vector() - var template_type_replacements = map() + var template_types = vec() + var template_type_replacements = map() // XXX add traits get_nodes("template_param", template_dec).for_each(fun(template_param: *tree) { template_types.add(concat_symbol_tree(get_node("identifier", template_param))) @@ -122,23 +122,23 @@ obj ast_transformation (Object) { parse_tree->children.for_each(fun(child: *tree) { if (child->data.name == "function") { // also handles templated function - var function_node = second_pass_function(child, translation_unit, map(), true) + var function_node = second_pass_function(child, translation_unit, map(), true) translation_unit->translation_unit.children.add(function_node) ast_to_syntax.set(function_node, child) } else if (child->data.name == "declaration_statement") { // second pass declaration can actually just call a normal transform (but maybe should be it's own method to do so because typedef has to do it too?)... - translation_unit->translation_unit.children.add(transform_declaration_statement(child, translation_unit, map())) + translation_unit->translation_unit.children.add(transform_declaration_statement(child, translation_unit, map())) } }) // work on the ones already started translation_unit->translation_unit.children.for_each(fun(node: *ast_node) { match(*node) { - ast_node::type_def(backing) second_pass_type_def(ast_to_syntax[node], node, translation_unit, map()) - ast_node::adt_def(backing) second_pass_adt_def(ast_to_syntax[node], node, translation_unit, map()) + ast_node::type_def(backing) second_pass_type_def(ast_to_syntax[node], node, translation_unit, map()) + ast_node::adt_def(backing) second_pass_adt_def(ast_to_syntax[node], node, translation_unit, map()) } }) } - fun second_pass_type_def(type_def_syntax: *tree, node: *ast_node, scope: *ast_node, template_replacements: map) { + fun second_pass_type_def(type_def_syntax: *tree, node: *ast_node, scope: *ast_node, template_replacements: map) { type_def_syntax->children.for_each(fun(child: *tree) { if (child->data.name == "declaration_statement") { var declaration_node = transform_declaration_statement(child, node, template_replacements) @@ -152,7 +152,7 @@ obj ast_transformation (Object) { } }) } - fun second_pass_adt_def(adt_def_syntax: *tree, node: *ast_node, scope: *ast_node, template_replacements: map) { + fun second_pass_adt_def(adt_def_syntax: *tree, node: *ast_node, scope: *ast_node, template_replacements: map) { get_nodes("adt_option", adt_def_syntax).for_each(fun(adt_option: *tree) { var ident_type: *type var type_syntax = get_node("type", adt_option) @@ -171,9 +171,9 @@ obj ast_transformation (Object) { var function_node = null() if (type_syntax) { var identifier_param = ast_identifier_ptr(option_name, ident_type, node) - function_node = ast_function_ptr(option_name, type_ptr(vector(get_ast_type(identifier_param)), node->adt_def.self_type, 0, false, false, true), vector(identifier_param), false) + function_node = ast_function_ptr(option_name, type_ptr(vec(get_ast_type(identifier_param)), node->adt_def.self_type, 0, false, false, true), vec(identifier_param), false) } else { - function_node = ast_function_ptr(option_name, type_ptr(vector<*type>(), node->adt_def.self_type, 0, false, false, true), vector<*ast_node>(), false) + function_node = ast_function_ptr(option_name, type_ptr(vec<*type>(), node->adt_def.self_type, 0, false, false, true), vec<*ast_node>(), false) } add_to_scope(option_name, function_node, node) add_to_scope("~enclosing_scope", node, function_node) @@ -183,44 +183,44 @@ obj ast_transformation (Object) { // note they don't even have real parameters (but the type has them correctly) or bodies // I'm not sure this is the correct enclosing scope, but I'm not sure how to do it with the function either - var equals_param = ast_identifier_ptr(string("in"), node->adt_def.self_type->clone_with_indirection(0,true), node) - var nequals_param = ast_identifier_ptr(string("in"), node->adt_def.self_type->clone_with_indirection(0,true), node) - var copy_construct_param = ast_identifier_ptr(string("in"), node->adt_def.self_type->clone_with_indirection(1,false), node) - var assign_param = ast_identifier_ptr(string("in"), node->adt_def.self_type->clone_with_indirection(0,true), node) - vector( - make_pair("operator==", ast_function_ptr(string("operator=="), - type_ptr(vector(equals_param->identifier.type), type_ptr(base_type::boolean()), 0, false, false, true), - vector(equals_param), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), - make_pair("operator!=", ast_function_ptr(string("operator!="), - type_ptr(vector(nequals_param->identifier.type), type_ptr(base_type::boolean()), 0, false, false, true), - vector(nequals_param), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), - make_pair("construct", ast_function_ptr(string("construct"), - type_ptr(vector<*type>(), node->adt_def.self_type->clone_with_increased_indirection(), 0, false, false, true), - vector<*ast_node>(), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), - make_pair("copy_construct", ast_function_ptr(string("copy_construct"), - type_ptr(vector(copy_construct_param->identifier.type), type_ptr(base_type::void_return()), 0, false, false, true), - vector(copy_construct_param), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), - make_pair("operator=", ast_function_ptr(string("operator="), - type_ptr(vector(assign_param->identifier.type), type_ptr(base_type::void_return()), 0, false, false, true), - vector(assign_param), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), - make_pair("destruct", ast_function_ptr(string("destruct"), - type_ptr(vector<*type>(), type_ptr(base_type::void_return()), 0, false, false, true), - vector<*ast_node>(), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)) + var equals_param = ast_identifier_ptr(str("in"), node->adt_def.self_type->clone_with_indirection(0,true), node) + var nequals_param = ast_identifier_ptr(str("in"), node->adt_def.self_type->clone_with_indirection(0,true), node) + var copy_construct_param = ast_identifier_ptr(str("in"), node->adt_def.self_type->clone_with_indirection(1,false), node) + var assign_param = ast_identifier_ptr(str("in"), node->adt_def.self_type->clone_with_indirection(0,true), node) + vec( + make_pair("operator==", ast_function_ptr(str("operator=="), + type_ptr(vec(equals_param->identifier.type), type_ptr(base_type::boolean()), 0, false, false, true), + vec(equals_param), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), + make_pair("operator!=", ast_function_ptr(str("operator!="), + type_ptr(vec(nequals_param->identifier.type), type_ptr(base_type::boolean()), 0, false, false, true), + vec(nequals_param), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), + make_pair("construct", ast_function_ptr(str("construct"), + type_ptr(vec<*type>(), node->adt_def.self_type->clone_with_increased_indirection(), 0, false, false, true), + vec<*ast_node>(), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), + make_pair("copy_construct", ast_function_ptr(str("copy_construct"), + type_ptr(vec(copy_construct_param->identifier.type), type_ptr(base_type::void_return()), 0, false, false, true), + vec(copy_construct_param), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), + make_pair("operator=", ast_function_ptr(str("operator="), + type_ptr(vec(assign_param->identifier.type), type_ptr(base_type::void_return()), 0, false, false, true), + vec(assign_param), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)), + make_pair("destruct", ast_function_ptr(str("destruct"), + type_ptr(vec<*type>(), type_ptr(base_type::void_return()), 0, false, false, true), + vec<*ast_node>(), ast_identifier_ptr("this", node->adt_def.self_type->clone_with_indirection(1), node), false, false)) ).for_each(fun(func_pair: pair<*char, *ast_node>) { node->adt_def.regular_funcs.add(func_pair.second) - add_to_scope(string(func_pair.first), func_pair.second, node) + add_to_scope(str(func_pair.first), func_pair.second, node) add_to_scope("~enclosing_scope", node, func_pair.second) }) } - fun second_pass_function(node: *tree, scope: *ast_node, template_replacements: map, do_raw_template: bool): *ast_node { + fun second_pass_function(node: *tree, scope: *ast_node, template_replacements: map, do_raw_template: bool): *ast_node { var func_identifier_node = get_node("func_identifier", node) - var function_name = string("__compiler_lambda__") + var function_name = str("__compiler_lambda__") if (func_identifier_node) function_name = concat_symbol_tree(func_identifier_node) var template_dec = get_node("template_dec", node) if (do_raw_template && template_dec) { - var template_types = vector() - var template_type_replacements = map() + var template_types = vec() + var template_type_replacements = map() get_nodes("template_param", template_dec).for_each(fun(template_param: *tree) { template_types.add(concat_symbol_tree(get_node("identifier", template_param))) template_type_replacements.set(template_types.last(), type_ptr(transform_traits(get_node("traits", template_param)))) @@ -240,7 +240,7 @@ obj ast_transformation (Object) { if (return_type->is_none()) error(node, "return type none") // transform parameters - var parameters = vector<*ast_node>() + var parameters = vec<*ast_node>() get_nodes("typed_parameter", node).for_each(fun(child: *tree) { // note the temporary null() which gets replaced below, as the dependency is circular var param_type = transform_type(get_node("type", child), scope, template_replacements) @@ -254,7 +254,7 @@ obj ast_transformation (Object) { if (is_type_def(scope)) { this_param = ast_identifier_ptr("this", scope->type_def.self_type->clone_with_indirection(1), scope) } else if (is_template(scope)) { - var parent_scope = get_ast_scope(scope)->get(string("~enclosing_scope"))[0] + var parent_scope = get_ast_scope(scope)->get(str("~enclosing_scope"))[0] if (is_type_def(parent_scope)) { this_param = ast_identifier_ptr("this", parent_scope->type_def.self_type->clone_with_indirection(1), parent_scope) } @@ -280,19 +280,19 @@ obj ast_transformation (Object) { // also same body problem as below backing.methods.for_each(fun(method: *ast_node) { if (!is_template(method)) - method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, map()) + method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, map()) }) } ast_node::function(backing) { // make sure not a template if (!backing.is_extern) - backing.body_statement = transform_statement(get_node("statement", ast_to_syntax[node]), node, map()) + backing.body_statement = transform_statement(get_node("statement", ast_to_syntax[node]), node, map()) } } }) parse_tree->children.for_each(fun(child: *tree) { if (child->data.name == "compiler_intrinsic") { - translation_unit->translation_unit.children.add(transform_compiler_intrinsic(child, translation_unit, map())) + translation_unit->translation_unit.children.add(transform_compiler_intrinsic(child, translation_unit, map())) } }) } @@ -304,17 +304,17 @@ obj ast_transformation (Object) { // if this is a templated method, we've either instantiatedit if we need it or not if we didn't, so we don't do anything with it here if (is_template(method)) return - var template = partially_inst_type_def->type_def.scope[string("~enclosing_scope")][0] + var template = partially_inst_type_def->type_def.scope[str("~enclosing_scope")][0] var template_types = template->template.template_types var real_types = template->template.instantiated_map.reverse_get(partially_inst_type_def) - var replacements = map() + var replacements = map() for (var i = 0; i < template_types.size; i++;) replacements.set(template_types[i], real_types[i].clone()) method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, replacements) }) } } - fun transform_type(node: *tree, scope: *ast_node, template_replacements: map): *type { + fun transform_type(node: *tree, scope: *ast_node, template_replacements: map): *type { // check for references and step down // always get to pre-reffed level var is_ref = get_node("\"ref\"", node) != null>() @@ -332,7 +332,7 @@ obj ast_transformation (Object) { var real_types_deref = real_types.map(fun(t:*type):type return *t;) var results = scope_lookup(name, scope) - var fitting_types = vector>() + var fitting_types = vec>() for (var i = 0; i < results.size; i++;) { if (!is_template(results[i]) || results[i]->template.is_function) continue @@ -343,7 +343,7 @@ obj ast_transformation (Object) { var num_satisfied_traits = 0 var satisfied_traits = true - template_type_replacements.for_each(fun(key: string, value: *type) num_satisfied_traits += value->traits.size();) + template_type_replacements.for_each(fun(key: str, value: *type) num_satisfied_traits += value->traits.size();) for (var j = 0; j < template_types.size; j++;) { satisfied_traits = satisfied_traits && real_types[j]->traits.contains(template_type_replacements[template_types[j]]->traits) && (real_types[j]->indirection == 0 || template_type_replacements[template_types[j]]->traits.size() == 0) @@ -357,10 +357,10 @@ obj ast_transformation (Object) { if (results[i]->template.instantiated_map.contains_key(real_types_deref)) { inst_type = results[i]->template.instantiated_map[real_types_deref] } else { - var typeStr = string() + var typeStr = str() real_types_deref.for_each(fun(t: type) typeStr += t.to_string(false) + " ";) - results[i]->template.instantiated_map.for_each(fun(key: vector, value: *ast_node) { - var hasTypStr = string() + results[i]->template.instantiated_map.for_each(fun(key: vec, value: *ast_node) { + var hasTypStr = str() key.for_each(fun(t: type) hasTypStr += t.to_string(false) + " ";) if (typeStr == hasTypStr) error(node, "they're equal but really shouldnt be") @@ -434,8 +434,8 @@ obj ast_transformation (Object) { return type_ptr(base_type::none(), indirection, is_ref) } } - fun transform(node: *tree, scope: *ast_node, template_replacements: map): *ast_node return transform(node, scope, search_type::none(), template_replacements) - fun transform(node: *tree, scope: *ast_node, searching_for: search_type, template_replacements: map): *ast_node { + fun transform(node: *tree, scope: *ast_node, template_replacements: map): *ast_node return transform(node, scope, search_type::none(), template_replacements) + fun transform(node: *tree, scope: *ast_node, searching_for: search_type, template_replacements: map): *ast_node { var name = node->data.name if (name == "identifier" || name == "scoped_identifier") { return transform_identifier(node, scope, searching_for) @@ -488,7 +488,7 @@ obj ast_transformation (Object) { error(node, "FAILED TO TRANSFORM") return null() } - fun transform_all(nodes: vector<*tree>, scope: *ast_node, template_replacements: map): vector<*ast_node> { + fun transform_all(nodes: vec<*tree>, scope: *ast_node, template_replacements: map): vec<*ast_node> { return nodes.map(fun(node: *tree): *ast_node return transform(node, scope, template_replacements);) } fun transform_identifier(node: *tree, scope: *ast_node, searching_for: search_type): *ast_node { @@ -496,7 +496,7 @@ obj ast_transformation (Object) { var name = concat_symbol_tree(node) if (name == "this") { while (!is_function(scope) || scope->function.this_param == null()) - scope = get_ast_scope(scope)->get(string("~enclosing_scope"))[0] + scope = get_ast_scope(scope)->get(str("~enclosing_scope"))[0] if (!is_function(scope)) error(node, "Couldn't find this") return scope->function.this_param @@ -530,7 +530,7 @@ obj ast_transformation (Object) { if (value_str.length() > 3 && value_str[1] == '"' && value_str[2] == '"') { value_str = value_str.slice(3,-4) } else { - var new_str.construct(end-start): string + var new_str.construct(end-start): str var escaped = false for (var i = 1; i < value_str.length()-1; i++;) { if (escaped) { @@ -588,7 +588,7 @@ obj ast_transformation (Object) { } return ast_value_ptr(value_str, value_type) } - fun transform_code_block(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_code_block(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { 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, template_replacements) @@ -597,13 +597,13 @@ obj ast_transformation (Object) { fun transform_if_comp(node: *tree, scope: *ast_node): *ast_node { var new_if_comp = ast_if_comp_ptr() new_if_comp->if_comp.wanted_generator = concat_symbol_tree(get_node("identifier", node)) - new_if_comp->if_comp.statement = transform_statement(get_node("statement", node), scope, map()) + new_if_comp->if_comp.statement = transform_statement(get_node("statement", node), scope, map()) return new_if_comp } fun transform_simple_passthrough(node: *tree, scope: *ast_node): *ast_node { var new_passthrough = ast_simple_passthrough_ptr() new_passthrough->simple_passthrough.passthrough_str = concat_symbol_tree(get_node("triple_quoted_string", node)).slice(3,-4) - // setup passthrough params and string + // setup passthrough params and str var passthrough_params = get_node("passthrough_params", node) if (!passthrough_params) return new_passthrough @@ -630,10 +630,10 @@ obj ast_transformation (Object) { new_passthrough->simple_passthrough.linker_str = concat_symbol_tree(linker_str).slice(1,-2) return new_passthrough } - fun transform_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { return transform(node->children[0], scope, template_replacements); } - fun transform_declaration_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_declaration_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { // this might have an init position method call var identifiers = get_nodes("identifier", node) var name = concat_symbol_tree(identifiers[0]) @@ -663,7 +663,7 @@ obj ast_transformation (Object) { } return declaration } - fun transform_assignment_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_assignment_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { var to_assign = transform(get_node("boolean_expression", node), scope, template_replacements) // for []= overloading if (get_node("\"=\"", node)) { @@ -673,7 +673,7 @@ obj ast_transformation (Object) { if (get_node("\"]\"", inner_unarad)) { var assign_to = transform(get_node("unarad", inner_unarad), scope, template_replacements) var assign_idx = transform(get_node("expression", inner_unarad), scope, template_replacements) - var possible_bracket_assign = find_and_make_any_operator_overload_call(string("[]="), vector(assign_to, assign_idx, to_assign), scope, template_replacements) + var possible_bracket_assign = find_and_make_any_operator_overload_call(str("[]="), vec(assign_to, assign_idx, to_assign), scope, template_replacements) if (possible_bracket_assign) { return possible_bracket_assign } @@ -682,45 +682,45 @@ obj ast_transformation (Object) { } var assign_to = transform(get_node("factor", node), scope, template_replacements) if (get_node("\"=\"", node)) { - var possible_assign = find_and_make_any_operator_overload_call(string("="), vector(assign_to, to_assign), scope, template_replacements) + var possible_assign = find_and_make_any_operator_overload_call(str("="), vec(assign_to, to_assign), scope, template_replacements) if (possible_assign) { return possible_assign } } else if (get_node("\"\\+=\"", node)) { - var possible_assign = find_and_make_any_operator_overload_call(string("+="), vector(assign_to, to_assign), scope, template_replacements) + var possible_assign = find_and_make_any_operator_overload_call(str("+="), vec(assign_to, to_assign), scope, template_replacements) if (possible_assign) { return possible_assign } - to_assign = make_operator_call("+", vector(assign_to, to_assign)) + to_assign = make_operator_call("+", vec(assign_to, to_assign)) } else if (get_node("\"-=\"", node)) { - var possible_assign = find_and_make_any_operator_overload_call(string("-="), vector(assign_to, to_assign), scope, template_replacements) + var possible_assign = find_and_make_any_operator_overload_call(str("-="), vec(assign_to, to_assign), scope, template_replacements) if (possible_assign) { return possible_assign } - to_assign = make_operator_call("-", vector(assign_to, to_assign)) + to_assign = make_operator_call("-", vec(assign_to, to_assign)) } else if (get_node("\"\\*=\"", node)) { - var possible_assign = find_and_make_any_operator_overload_call(string("*="), vector(assign_to, to_assign), scope, template_replacements) + var possible_assign = find_and_make_any_operator_overload_call(str("*="), vec(assign_to, to_assign), scope, template_replacements) if (possible_assign) { return possible_assign } - to_assign = make_operator_call("*", vector(assign_to, to_assign)) + to_assign = make_operator_call("*", vec(assign_to, to_assign)) } else if (get_node("\"/=\"", node)){ - var possible_assign = find_and_make_any_operator_overload_call(string("/="), vector(assign_to, to_assign), scope, template_replacements) + var possible_assign = find_and_make_any_operator_overload_call(str("/="), vec(assign_to, to_assign), scope, template_replacements) if (possible_assign) { return possible_assign } - to_assign = make_operator_call("/", vector(assign_to, to_assign)) + to_assign = make_operator_call("/", vec(assign_to, to_assign)) } else if (get_node("\"^=\"", node)){ - var possible_assign = find_and_make_any_operator_overload_call(string("^="), vector(assign_to, to_assign), scope, template_replacements) + var possible_assign = find_and_make_any_operator_overload_call(str("^="), vec(assign_to, to_assign), scope, template_replacements) if (possible_assign) { return possible_assign } - to_assign = make_operator_call("^", vector(assign_to, to_assign)) + to_assign = make_operator_call("^", vec(assign_to, to_assign)) } var assignment = ast_assignment_statement_ptr(assign_to, to_assign) return assignment } - fun transform_if_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_if_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { if (get_node("AmbiguityInner", node)) error(node, "Ambigious two ifs with one else!") var if_statement = ast_if_statement_ptr(transform_expression(get_node("boolean_expression", node), scope, template_replacements)) @@ -733,13 +733,13 @@ obj ast_transformation (Object) { if_statement->if_statement.else_part = statements[1] return if_statement } - fun transform_while_loop(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_while_loop(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { var while_loop = ast_while_loop_ptr(transform_expression(get_node("boolean_expression", node), scope, template_replacements)) add_to_scope("~enclosing_scope", scope, while_loop) while_loop->while_loop.statement = transform(get_node("statement", node), while_loop, template_replacements) return while_loop } - fun transform_for_loop(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_for_loop(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { var for_loop = ast_for_loop_ptr() add_to_scope("~enclosing_scope", scope, for_loop) var statements = get_nodes("statement", node) @@ -749,7 +749,7 @@ obj ast_transformation (Object) { for_loop->for_loop.body = transform(statements[2], for_loop, template_replacements) return for_loop } - fun transform_return_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_return_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { var return_value = get_node("boolean_expression", node) var to_ret: *ast_node if (return_value) @@ -764,19 +764,19 @@ obj ast_transformation (Object) { return ast_branching_statement_ptr(branching_type::break_stmt()) return ast_branching_statement_ptr(branching_type::continue_stmt()) } - fun transform_defer_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_defer_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { return ast_defer_statement_ptr(transform(node->children[0], scope, template_replacements)) } - fun transform_match_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_match_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { var to_ret = ast_match_statement_ptr(transform(get_node("boolean_expression", node), scope, template_replacements)) get_nodes("case_statement", node).for_each(fun(syntax: *tree) to_ret->match_statement.cases.add(transform_case_statement(syntax, scope, template_replacements));) return to_ret } - fun transform_case_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_case_statement(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { var to_ret = ast_case_statement_ptr() var the_adts = scope_lookup(concat_symbol_tree(get_node("scoped_identifier", get_node("scoped_identifier", node))), scope).filter(fun(i: *ast_node): bool return is_adt_def(i);) if (the_adts.size != 1) - error(node, string("the number of adts found was not 1, it was ") + the_adts.size + " for " + concat_symbol_tree(get_node("scoped_identifier", node))) + error(node, str("the number of adts found was not 1, it was ") + the_adts.size + " for " + concat_symbol_tree(get_node("scoped_identifier", node))) var the_adt = the_adts[0] var the_option_name = concat_symbol_tree(get_node("identifier", get_node("scoped_identifier", node))) // ADD IN ERROR CHECKING HERE @@ -793,7 +793,7 @@ obj ast_transformation (Object) { to_ret->case_statement.statement = transform(get_node("statement", node), to_ret, template_replacements) return to_ret } - fun transform_function_call(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_function_call(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { // don't bother with a full transform for parameters with their own function, just get the boolean expression and transform it var parameters = get_nodes("parameter", node).map(fun(child: *tree): *ast_node return transform(get_node("boolean_expression", child), scope, template_replacements);) var parameter_types = parameters.map(fun(param: *ast_node): *type return get_ast_type(param);) @@ -810,7 +810,7 @@ obj ast_transformation (Object) { var f = ast_function_call_ptr(func, parameters) return f } - fun transform_compiler_intrinsic(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_compiler_intrinsic(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { var parameters = get_nodes("parameter", node).map(fun(child: *tree): *ast_node return transform(get_node("boolean_expression", child), scope, template_replacements);) var type_parameters = get_nodes("type", node).map(fun(child: *tree): *type return transform_type(child, scope, template_replacements);) var intrinsic_name = concat_symbol_tree(get_node("identifier", node)) @@ -821,31 +821,31 @@ obj ast_transformation (Object) { intrinsic_return_type = type_ptr(base_type::ulong_int()) return ast_compiler_intrinsic_ptr(intrinsic_name, parameters, type_parameters, intrinsic_return_type) } - fun transform_lambda(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { + fun transform_lambda(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { var function_node = second_pass_function(node, scope, template_replacements, false) function_node->function.body_statement = transform_statement(get_node("statement", node), function_node, template_replacements) - while (!is_translation_unit(scope)) scope = get_ast_scope(scope)->get(string("~enclosing_scope"))[0] + while (!is_translation_unit(scope)) scope = get_ast_scope(scope)->get(str("~enclosing_scope"))[0] scope->translation_unit.lambdas.add(function_node) return function_node } - fun transform_expression(node: *tree, scope: *ast_node, template_replacements: map): *ast_node + fun transform_expression(node: *tree, scope: *ast_node, template_replacements: map): *ast_node return transform_expression(node, scope, search_type::none(), template_replacements) - fun transform_expression(node: *tree, scope: *ast_node, searching_for: search_type, template_replacements: map): *ast_node { - var func_name = string() - var parameters = vector<*ast_node>() + fun transform_expression(node: *tree, scope: *ast_node, searching_for: search_type, template_replacements: map): *ast_node { + var func_name = str() + var parameters = vec<*ast_node>() if (node->children.size == 1) { var possible_value = transform(node->children[0], scope, searching_for, template_replacements) if (!possible_value) match (searching_for) { - search_type::function(type_vec) possible_value = find_or_instantiate_template_function(concat_symbol_tree(node->children[0]), null>(), scope, type_vec, template_replacements, map()); + search_type::function(type_vec) possible_value = find_or_instantiate_template_function(concat_symbol_tree(node->children[0]), null>(), scope, type_vec, template_replacements, map()); } if (!possible_value) error(node, concat_symbol_tree(node) + ": HAS NO POSSIBLE FUNCTION OR FUNCTION TEMPLATE SOLUTIONS\nlooking for: " + - concat_symbol_tree(node->children[0]) + "(" + searching_for.function.reduce(fun(n:*type, s:string):string { + concat_symbol_tree(node->children[0]) + "(" + searching_for.function.reduce(fun(n:*type, s:str):str { if (n) return s+","+n->to_string() else return s+",null" - }, string()) + ")") + }, str()) + ")") return possible_value } else if (node->children.size == 2) { var template_inst = get_node("template_inst", node) @@ -855,7 +855,7 @@ obj ast_transformation (Object) { match (searching_for) { // I guess this should never happen? search_type::none() error(template_inst, "TEMPLATE LOOKUP WITHOUT PERENS ()") - search_type::function(type_vec) result = find_or_instantiate_template_function(concat_symbol_tree(identifier), template_inst, scope, type_vec, template_replacements, map()) + search_type::function(type_vec) result = find_or_instantiate_template_function(concat_symbol_tree(identifier), template_inst, scope, type_vec, template_replacements, map()) } if (!result) error(node, "Could not find templated function " + concat_symbol_tree(identifier) + " even though had a template_inst") @@ -865,10 +865,10 @@ obj ast_transformation (Object) { if (check_if_post == "--" || check_if_post == "++") { // give the post-operators a special suffix so the c_generator knows to emit them post func_name = concat_symbol_tree(node->children[1]) + "p" - parameters = vector(transform(node->children[0], scope, template_replacements)) + parameters = vec(transform(node->children[0], scope, template_replacements)) } else { func_name = concat_symbol_tree(node->children[0]) - parameters = vector(transform(node->children[1], scope, template_replacements)) + parameters = vec(transform(node->children[1], scope, template_replacements)) } } else { func_name = concat_symbol_tree(node->children[1]) @@ -890,8 +890,8 @@ obj ast_transformation (Object) { if (!second_param) match (searching_for) { search_type::function(type_vec) { var template_inst = get_node("template_inst", node) - var inherited_replacements = map() - var parent = get_ast_scope(get_ast_type(first_param)->type_def)->get(string("~enclosing_scope"))[0] + var inherited_replacements = map() + var parent = get_ast_scope(get_ast_type(first_param)->type_def)->get(str("~enclosing_scope"))[0] if (is_template(parent)) for (var i = 0; i < parent->template.template_types.size; i++;) inherited_replacements[parent->template.template_types[i]] = parent->template.instantiated_map.reverse_get(get_ast_type(first_param)->type_def)[i].clone() @@ -900,19 +900,19 @@ obj ast_transformation (Object) { second_param = find_or_instantiate_template_function(method_name, template_inst, get_ast_type(first_param)->type_def, type_vec, template_replacements, inherited_replacements); if (!second_param) { error(node, "Could not find method " + method_name + " on the right side of (. or ->) " + concat_symbol_tree(node->children[0]) + - ", whole string: " + concat_symbol_tree(node) + ", left type: " + get_ast_type(first_param)->to_string()) + ", whole str: " + concat_symbol_tree(node) + ", left type: " + get_ast_type(first_param)->to_string()) } } } // this is the case where it's null but not a method call. Should add default to case above and move there if (!second_param) { error(node, "Could not find member " + concat_symbol_tree(node->children[2]) + " on the right side of (. or ->) " + concat_symbol_tree(node->children[0]) + - ", whole string: " + concat_symbol_tree(node) + ", left type: " + get_ast_type(first_param)->to_string()) + ", whole str: " + concat_symbol_tree(node) + ", left type: " + get_ast_type(first_param)->to_string()) } } else { second_param = transform(node->children[2], scope, template_replacements) } - parameters = vector(first_param, second_param) + parameters = vec(first_param, second_param) } parameters.for_each(fun(param: *ast_node) if (!is_legal_parameter_node_type(param)) error(node, "illegal node type used as parameter (perhaps name resolves to type or translation unit?)");) var parameter_types = parameters.map(fun(param: *ast_node): *type return get_ast_type(param);) @@ -922,42 +922,42 @@ obj ast_transformation (Object) { return possible_overload_call return ast_function_call_ptr(get_builtin_function(func_name, parameter_types, node), parameters) } - fun find_and_make_any_operator_overload_call(func_name: string, parameters: vector<*ast_node>, scope: *ast_node, template_replacements: map): *ast_node { + fun find_and_make_any_operator_overload_call(func_name: str, parameters: vec<*ast_node>, scope: *ast_node, template_replacements: map): *ast_node { var parameter_types = parameters.map(fun(param: *ast_node): *type return get_ast_type(param);) var possible_overload = null() if (parameter_types[0]->is_object() && parameter_types[0]->indirection == 0) { - possible_overload = function_lookup(string("operator")+func_name, parameter_types.first()->type_def, parameter_types.slice(1,-1)) + possible_overload = function_lookup(str("operator")+func_name, parameter_types.first()->type_def, parameter_types.slice(1,-1)) if (!possible_overload) { - var inherited_replacements = map() - var parent = get_ast_scope(parameter_types.first()->type_def)->get(string("~enclosing_scope"))[0] + var inherited_replacements = map() + var parent = get_ast_scope(parameter_types.first()->type_def)->get(str("~enclosing_scope"))[0] if (is_template(parent)) { for (var i = 0; i < parent->template.template_types.size; i++;) inherited_replacements[parent->template.template_types[i]] = parent->template.instantiated_map.reverse_get(parameter_types.first()->type_def)[i].clone() } - possible_overload = find_or_instantiate_template_function(string("operator")+func_name, null>(), parameter_types.first()->type_def, parameter_types.slice(1,-1), template_replacements, inherited_replacements) + possible_overload = find_or_instantiate_template_function(str("operator")+func_name, null>(), parameter_types.first()->type_def, parameter_types.slice(1,-1), template_replacements, inherited_replacements) } if (possible_overload) return make_method_call(parameters.first(), possible_overload, parameters.slice(1,-1)) } - possible_overload = function_lookup(string("operator")+func_name, scope, parameter_types) + possible_overload = function_lookup(str("operator")+func_name, scope, parameter_types) if (!possible_overload) - possible_overload = find_or_instantiate_template_function(string("operator")+func_name, null>(), scope, parameter_types, template_replacements, map()) + possible_overload = find_or_instantiate_template_function(str("operator")+func_name, null>(), scope, parameter_types, template_replacements, map()) if (possible_overload) return ast_function_call_ptr(possible_overload, parameters) return null() } - fun find_or_instantiate_template_function(name: string, template_inst: *tree, scope: *ast_node, param_types: vector<*type>, template_replacements: map, replacements_base: map): *ast_node { + fun find_or_instantiate_template_function(name: str, template_inst: *tree, scope: *ast_node, param_types: vec<*type>, template_replacements: map, replacements_base: map): *ast_node { // replacments base is for templated methods starting off with the replacements of their parent (possibly templated) object var results = scope_lookup(name, scope) - var real_types = vector<*type>() - var real_types_deref = vector() + var real_types = vec<*type>() + var real_types_deref = vec() var had_real_types = false if (template_inst) { real_types = get_nodes("type", template_inst).map(fun(t: *tree): *type return transform_type(t, scope, template_replacements);) real_types_deref = real_types.map(fun(t:*type):type return *t;) had_real_types = true } - var fitting_functions = vector>() + var fitting_functions = vec>() for (var i = 0; i < results.size; i++;) { if (is_template(results[i]) && results[i]->template.is_function) { var template_types = results[i]->template.template_types @@ -965,8 +965,8 @@ obj ast_transformation (Object) { if (!had_real_types) { var unify_template_type_replacements = template_type_replacements // reset the vars, cuz we might be iterating through multiple of them - real_types = vector<*type>() - real_types_deref = vector() + real_types = vec<*type>() + real_types_deref = vec() // Template Function Instance Inference time var typed_params = get_nodes("typed_parameter", results[i]->template.syntax_node).map(fun(t: *tree): *tree return get_node("type",t);) if (param_types.size != typed_params.size) @@ -982,7 +982,7 @@ obj ast_transformation (Object) { continue var num_satisfied_traits = 0 var satisfied_traits = true - template_type_replacements.for_each(fun(key: string, value: *type) num_satisfied_traits += value->traits.size();) + template_type_replacements.for_each(fun(key: str, value: *type) num_satisfied_traits += value->traits.size();) // check if already instantiated var inst_func = null() for (var j = 0; j < template_types.size; j++;) { @@ -990,7 +990,7 @@ obj ast_transformation (Object) { (real_types[j]->indirection == 0 || template_type_replacements[template_types[j]]->traits.size() == 0) template_type_replacements[template_types[j]] = real_types[j] } - replacements_base.for_each(fun(key: string, value: *type) { + replacements_base.for_each(fun(key: str, value: *type) { template_type_replacements[key] = value }) @@ -1021,7 +1021,7 @@ obj ast_transformation (Object) { return fitting_functions.max(fun(a: pair<*ast_node, int>, b: pair<*ast_node, int>): bool return a.second < b.second;).first } } -fun unify_type(template_type: *tree, param_type: *type, new_map: *map, template_replacements: map) { +fun unify_type(template_type: *tree, param_type: *type, new_map: *map, template_replacements: map) { // first get rid of the reference if we have it - we don't care for unification if (get_node("pre_reffed", template_type)) template_type = get_node("pre_reffed", template_type) @@ -1052,7 +1052,7 @@ fun unify_type(template_type: *tree, param_type: *type, new_map: *mapis_object()) { - var enclosing_template = param_type->type_def->type_def.scope[string("~enclosing_scope")][0] + var enclosing_template = param_type->type_def->type_def.scope[str("~enclosing_scope")][0] if (is_template(enclosing_template)) { var inst_params = enclosing_template->template.instantiated_map.reverse_get(param_type->type_def) var template_params = get_nodes("type", get_node("template_inst", template_type)) diff --git a/stdlib/bytecode_generator.krak b/stdlib/bytecode_generator.krak index b8e5885..69d0f26 100644 --- a/stdlib/bytecode_generator.krak +++ b/stdlib/bytecode_generator.krak @@ -4,7 +4,7 @@ import mem:* import map:* import hash_map:* import stack:* -import string:* +import str:* import util:* import tree:* import symbol:* @@ -53,7 +53,7 @@ fun type_size_and_alignment(t: *type): pair { base_type::floating() return make_pair(#sizeof, #sizeof) base_type::double_precision() return make_pair(#sizeof, #sizeof) } - error(string("Invalid type for type_size: ") + t->to_string()) + error(str("Invalid type for type_size: ") + t->to_string()) } fun offset_into_struct(struct_type: *type, ident: *ast_node): ulong { @@ -105,7 +105,7 @@ fun size_to_operand_size(size: ulong): operand_size { if (size == 2) return operand_size::b16() if (size == 4) return operand_size::b32() if (size == 8) return operand_size::b64() - error(string("invalid operand size ") + size) + error(str("invalid operand size ") + size) } adt byte_inst { nop, @@ -163,57 +163,57 @@ obj test { var offset: long } -fun to_string(s: operand_size): string { +fun to_string(s: operand_size): str { match (s) { - operand_size::b8() return string("8") - operand_size::b16() return string("16") - operand_size::b32() return string("32") - operand_size::b64() return string("64") + operand_size::b8() return str("8") + operand_size::b16() return str("16") + operand_size::b32() return str("32") + operand_size::b64() return str("64") } - return string("missed operand size") + return str("missed operand size") } -fun to_string(b: byte_inst): string { +fun to_string(b: byte_inst): str { match (b) { - byte_inst::nop() return string("nop") - byte_inst::imm(i) return string("r") + i.to_reg + " = imm " + i.val - byte_inst::add(a) return string("r") + a.to_reg + " = r" + a.a + " + r" + a.b - byte_inst::addi(a) return string("r") + a.to_reg + " = r" + a.a + " + " + a.bi - byte_inst::smul(a) return string("r") + a.to_reg + " = r" + a.a + " * r" + a.b - byte_inst::umul(a) return string("r") + a.to_reg + " = r" + a.a + " u* r"+ a.b - byte_inst::sdiv(a) return string("r") + a.to_reg + " = r" + a.a + " / r" + a.b - byte_inst::udiv(a) return string("r") + a.to_reg + " = r" + a.a + " u/ r"+ a.b - byte_inst::mod(a) return string("r") + a.to_reg + " = r" + a.a + " % r" + a.b - byte_inst::and(a) return string("r") + a.to_reg + " = r" + a.a + " & r" + a.b - byte_inst::shl(a) return string("r") + a.to_reg + " = r" + a.a + " u<< r" + a.b - byte_inst::shr(a) return string("r") + a.to_reg + " = r" + a.a + " u>> r" + a.b - byte_inst::sar(a) return string("r") + a.to_reg + " = r" + a.a + " s>> r" + a.b - byte_inst::or(a) return string("r") + a.to_reg + " = r" + a.a + " | r" + a.b - byte_inst::xor(a) return string("r") + a.to_reg + " = r" + a.a + " ^ r" + a.b - byte_inst::not(a) return string("r") + a.to_reg + " = ~r" + a.a - byte_inst::gz(a) return string("r") + a.to_reg + " = r" + a.a + " > 0" - byte_inst::lz(a) return string("r") + a.to_reg + " = r" + a.a + " < 0" - byte_inst::ez(a) return string("r") + a.to_reg + " = r" + a.a + " == 0" - byte_inst::ldr(l) return string("r") + l.reg + " = ldr" + to_string(l.size) + " r" + l.base_reg + " (" + l.offset + ")" + byte_inst::nop() return str("nop") + byte_inst::imm(i) return str("r") + i.to_reg + " = imm " + i.val + byte_inst::add(a) return str("r") + a.to_reg + " = r" + a.a + " + r" + a.b + byte_inst::addi(a) return str("r") + a.to_reg + " = r" + a.a + " + " + a.bi + byte_inst::smul(a) return str("r") + a.to_reg + " = r" + a.a + " * r" + a.b + byte_inst::umul(a) return str("r") + a.to_reg + " = r" + a.a + " u* r"+ a.b + byte_inst::sdiv(a) return str("r") + a.to_reg + " = r" + a.a + " / r" + a.b + byte_inst::udiv(a) return str("r") + a.to_reg + " = r" + a.a + " u/ r"+ a.b + byte_inst::mod(a) return str("r") + a.to_reg + " = r" + a.a + " % r" + a.b + byte_inst::and(a) return str("r") + a.to_reg + " = r" + a.a + " & r" + a.b + byte_inst::shl(a) return str("r") + a.to_reg + " = r" + a.a + " u<< r" + a.b + byte_inst::shr(a) return str("r") + a.to_reg + " = r" + a.a + " u>> r" + a.b + byte_inst::sar(a) return str("r") + a.to_reg + " = r" + a.a + " s>> r" + a.b + byte_inst::or(a) return str("r") + a.to_reg + " = r" + a.a + " | r" + a.b + byte_inst::xor(a) return str("r") + a.to_reg + " = r" + a.a + " ^ r" + a.b + byte_inst::not(a) return str("r") + a.to_reg + " = ~r" + a.a + byte_inst::gz(a) return str("r") + a.to_reg + " = r" + a.a + " > 0" + byte_inst::lz(a) return str("r") + a.to_reg + " = r" + a.a + " < 0" + byte_inst::ez(a) return str("r") + a.to_reg + " = r" + a.a + " == 0" + byte_inst::ldr(l) return str("r") + l.reg + " = ldr" + to_string(l.size) + " r" + l.base_reg + " (" + l.offset + ")" byte_inst::str(s) return "str" + to_string(s.size) + " (r" + s.base_reg + "(" + s.offset + ") <= r" + s.reg + ")" - byte_inst::jmp(j) return string("jmp(pc += ") + j + ")" - byte_inst::jz(j) return string("jmp(r") + j.reg + " == 0, pc += " + j.offset + ")" - byte_inst::call(c) return string("call pc = r") + c - byte_inst::ret() return string("ret") + byte_inst::jmp(j) return str("jmp(pc += ") + j + ")" + byte_inst::jz(j) return str("jmp(r") + j.reg + " == 0, pc += " + j.offset + ")" + byte_inst::call(c) return str("call pc = r") + c + byte_inst::ret() return str("ret") } - return string("Missed byte_inst case in to_string") + return str("Missed byte_inst case in to_string") } -fun bytecode_to_string(functions: ref vector, instructions: ref vector): string { - return string("\n").join(functions.map(fun(bb: ref bytecode_function): string return bb.to_string(instructions);)) +fun bytecode_to_string(functions: ref vec, instructions: ref vec): str { + return str("\n").join(functions.map(fun(bb: ref bytecode_function): str return bb.to_string(instructions);)) } -fun bytecode_function(name: ref string, start: int): bytecode_function { +fun bytecode_function(name: ref str, start: int): bytecode_function { var to_ret.construct(name, start): bytecode_function return to_ret } obj bytecode_function (Object) { - var name: string + var name: str var instruction_start: int var instruction_end: int var var_to_frame_offset: map<*ast_node, int> @@ -227,7 +227,7 @@ obj bytecode_function (Object) { frame_size = 0 return this } - fun construct(name_in: ref string, instruction_start_in: int): *bytecode_function { + fun construct(name_in: ref str, instruction_start_in: int): *bytecode_function { instruction_start = instruction_start_in instruction_end = 0 name.copy_construct(&name_in) @@ -250,7 +250,7 @@ obj bytecode_function (Object) { name.destruct() var_to_frame_offset.destruct() } - fun to_string(instructions: ref vector): string { + fun to_string(instructions: ref vec): str { var res = name + "(frame size " + frame_size + "):\n" res += "\t frame layout\n" res += "\t\tsaved RBP : RPB = 0\n" @@ -259,7 +259,7 @@ obj bytecode_function (Object) { }) res += "\n\t bytecode\n" for (var i = instruction_start; i < instruction_end; i++;) - res += string("\t\t") + i + string(": ") + to_string(instructions[i]) + "\n" + res += str("\t\t") + i + str(": ") + to_string(instructions[i]) + "\n" return res } } @@ -268,13 +268,13 @@ obj bytecode_generator (Object) { var reg_counter: int var reg_max: int var id_counter: int - var ast_name_map: hash_map<*ast_node, string> - var functions: vector + var ast_name_map: hash_map<*ast_node, str> + var functions: vec var node_function_idx: map<*ast_node, int> - var instructions: vector - var fixup_function_addresses: vector> - var fixup_break_addresses: stack> - var fixup_continue_addresses: stack> + var instructions: vec + var fixup_function_addresses: vec> + var fixup_break_addresses: stack> + var fixup_continue_addresses: stack> fun construct(): *bytecode_generator { id_counter = 0 ast_name_map.construct() @@ -314,7 +314,7 @@ obj bytecode_generator (Object) { fixup_break_addresses.destruct() fixup_continue_addresses.destruct() } - fun get_id(): string return to_string(id_counter++); + fun get_id(): str return to_string(id_counter++); fun get_reg(): int return reg_counter++; fun peek_reg(): int return reg_counter; fun reset_reg() reset_reg(3); @@ -324,11 +324,11 @@ obj bytecode_generator (Object) { } reg_counter = to } - /*fun generate_bytecode(name_ast_map: map,*ast_node>>): pair, vector> {*/ - fun generate_bytecode(name_ast_map: map,*ast_node>>) { + /*fun generate_bytecode(name_ast_map: map,*ast_node>>): pair, vec> {*/ + fun generate_bytecode(name_ast_map: map,*ast_node>>) { // iterate through asts - name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree,*ast_node>) { + name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree,*ast_node>) { // iterate through children for each ast tree_pair.second->translation_unit.lambdas.for_each(fun(child: *ast_node) { generate_function_definition(child) @@ -502,8 +502,8 @@ obj bytecode_generator (Object) { var jz_index = instructions.size emit_jz(cond_reg,0) reset_reg() - fixup_break_addresses.push(vector()) - fixup_continue_addresses.push(vector()) + fixup_break_addresses.push(vec()) + fixup_continue_addresses.push(vec()) generate(node->while_loop.statement) reset_reg() emit_jmp(top_index - instructions.size) @@ -529,8 +529,8 @@ obj bytecode_generator (Object) { var jz_index = instructions.size emit_jz(cond_reg,0) reset_reg() - fixup_break_addresses.push(vector()) - fixup_continue_addresses.push(vector()) + fixup_break_addresses.push(vec()) + fixup_continue_addresses.push(vec()) generate(node->for_loop.body) reset_reg() @@ -853,7 +853,7 @@ obj bytecode_generator (Object) { } error("Bad node") } - fun get_name(node: *ast_node): string { + fun get_name(node: *ast_node): str { var maybe_it = ast_name_map.get_ptr_or_null(node); if (maybe_it) return *maybe_it @@ -1073,7 +1073,7 @@ obj bytecode_generator (Object) { println("evaling main") println(bytecode_to_string(functions, instructions)) var main_entry = functions.find_first_satisfying(fun(block: bytecode_function): bool return block.name == "main";) - var registers.construct(reg_max): vector + var registers.construct(reg_max): vec registers.size = reg_max registers[1] = 0xdeadbeefcafebabe var stack_size = 8 * 1024 * 1024 @@ -1083,7 +1083,7 @@ obj bytecode_generator (Object) { stack[-i + -1] = 0 registers[0] = (stack-register_size) cast long // with the stack being zeroed out, this makes it a return address of 0 for (var i = main_entry.instruction_start; i < instructions.size; i++;) { - /*println(string("evaling: ") + i + ": " + to_string(instructions[i]))*/ + /*println(str("evaling: ") + i + ": " + to_string(instructions[i]))*/ match(instructions[i]) { byte_inst::nop() {} byte_inst::imm(i) registers[i.to_reg] = i.val @@ -1206,7 +1206,7 @@ obj bytecode_generator (Object) { *(registers[0] + #sizeof<*char> + #sizeof) cast **char, *(registers[0] + #sizeof<*char> + #sizeof + #sizeof<*char>) cast *double)) cast long else - error(string("bad extern call number") + func_start) + error(str("bad extern call number") + func_start) } else { /*registers[0] -= register_size*/ registers[0] = registers[0] - register_size @@ -1216,7 +1216,7 @@ obj bytecode_generator (Object) { /*println("call: " + functions.find_first_satisfying(fun(f: bytecode_function): bool return f.instruction_start == func_start;).name)*/ /*println("first part of memory is (after push)")*/ /*for (var i = 0; i < 8*8; i+=8;) {*/ - /*print(string("-") + i + string(": "))*/ + /*print(str("-") + i + str(": "))*/ /*for (var j = 0; j < 8; j++;) {*/ /*if (j == 4)*/ /*print(" ")*/ @@ -1235,7 +1235,7 @@ obj bytecode_generator (Object) { /*print("returning!")*/ /*println("first part of memory is")*/ /*for (var i = 0; i < 8*8; i+=8;) {*/ - /*print(string("-") + i + string(": "))*/ + /*print(str("-") + i + str(": "))*/ /*for (var j = 0; j < 8; j++;) {*/ /*if (j == 4)*/ /*print(" ")*/ @@ -1246,13 +1246,13 @@ obj bytecode_generator (Object) { /*}*/ /*println("Done")*/ if (pc == 0) { - /*println(string("got malloc is ") + *(got_malloc) cast *int)*/ + /*println(str("got malloc is ") + *(got_malloc) cast *int)*/ var value = registers[2] - println(string("returning from main, value is ") + value) + println(str("returning from main, value is ") + value) return value } else { i = pc - 1 - /*println(string("returning to ") + pc)*/ + /*println(str("returning to ") + pc)*/ } } } diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index 6245eac..6fcd888 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -3,7 +3,7 @@ import mem:* import map:* import hash_map:* import stack:* -import string:* +import str:* import util:* import tree:* import symbol:* @@ -14,69 +14,69 @@ import poset:* obj c_generator (Object) { var id_counter: int - var ast_name_map: hash_map<*ast_node, string> - var used_names: hash_set - var function_type_map: map - var replacement_map: map + var ast_name_map: hash_map<*ast_node, str> + var used_names: hash_set + var function_type_map: map + var replacement_map: map var longest_replacement: int - var function_typedef_string: string - var linker_string: string + var function_typedef_string: str + var linker_string: str fun construct(): *c_generator { id_counter = 0 ast_name_map.construct() used_names.construct() // to avoid using c keywords - used_names.add(string("extern")) - used_names.add(string("register")) + used_names.add(str("extern")) + used_names.add(str("register")) function_type_map.construct() function_typedef_string.construct() linker_string.construct() replacement_map.construct() - replacement_map[string("+")] = string("plus") - replacement_map[string("-")] = string("minus") - replacement_map[string("*")] = string("star") - replacement_map[string("/")] = string("div") - replacement_map[string("%")] = string("mod") - replacement_map[string("^")] = string("carat") - replacement_map[string("&")] = string("amprsd") - replacement_map[string("|")] = string("pipe") - replacement_map[string("~")] = string("tilde") - replacement_map[string("!")] = string("exlmtnpt") - replacement_map[string(",")] = string("comma") - replacement_map[string("=")] = string("eq") - replacement_map[string("++")] = string("dbplus") - replacement_map[string("--")] = string("dbminus") - replacement_map[string("<<")] = string("dbleft") - replacement_map[string(">>")] = string("dbright") - replacement_map[string("::")] = string("scopeop") - replacement_map[string(":")] = string("colon") - replacement_map[string("==")] = string("dbq") - replacement_map[string("!=")] = string("notequals") - replacement_map[string("&&")] = string("doubleamprsnd") - replacement_map[string("||")] = string("doublepipe") - replacement_map[string("+=")] = string("plusequals") - replacement_map[string("-=")] = string("minusequals") - replacement_map[string("/=")] = string("divequals") - replacement_map[string("%=")] = string("modequals") - replacement_map[string("^=")] = string("caratequals") - replacement_map[string("&=")] = string("amprsdequals") - replacement_map[string("|=")] = string("pipeequals") - replacement_map[string("*=")] = string("starequals") - replacement_map[string("<<=")] = string("doublerightequals") - replacement_map[string("<")] = string("lt") - replacement_map[string(">")] = string("gt") - replacement_map[string(">>=")] = string("doubleleftequals") - replacement_map[string("(")] = string("openparen") - replacement_map[string(")")] = string("closeparen") - replacement_map[string("[")] = string("obk") - replacement_map[string("]")] = string("cbk") - replacement_map[string(" ")] = string("_") - replacement_map[string(".")] = string("dot") - replacement_map[string("->")] = string("arrow") + replacement_map[str("+")] = str("plus") + replacement_map[str("-")] = str("minus") + replacement_map[str("*")] = str("star") + replacement_map[str("/")] = str("div") + replacement_map[str("%")] = str("mod") + replacement_map[str("^")] = str("carat") + replacement_map[str("&")] = str("amprsd") + replacement_map[str("|")] = str("pipe") + replacement_map[str("~")] = str("tilde") + replacement_map[str("!")] = str("exlmtnpt") + replacement_map[str(",")] = str("comma") + replacement_map[str("=")] = str("eq") + replacement_map[str("++")] = str("dbplus") + replacement_map[str("--")] = str("dbminus") + replacement_map[str("<<")] = str("dbleft") + replacement_map[str(">>")] = str("dbright") + replacement_map[str("::")] = str("scopeop") + replacement_map[str(":")] = str("colon") + replacement_map[str("==")] = str("dbq") + replacement_map[str("!=")] = str("notequals") + replacement_map[str("&&")] = str("doubleamprsnd") + replacement_map[str("||")] = str("doublepipe") + replacement_map[str("+=")] = str("plusequals") + replacement_map[str("-=")] = str("minusequals") + replacement_map[str("/=")] = str("divequals") + replacement_map[str("%=")] = str("modequals") + replacement_map[str("^=")] = str("caratequals") + replacement_map[str("&=")] = str("amprsdequals") + replacement_map[str("|=")] = str("pipeequals") + replacement_map[str("*=")] = str("starequals") + replacement_map[str("<<=")] = str("doublerightequals") + replacement_map[str("<")] = str("lt") + replacement_map[str(">")] = str("gt") + replacement_map[str(">>=")] = str("doubleleftequals") + replacement_map[str("(")] = str("openparen") + replacement_map[str(")")] = str("closeparen") + replacement_map[str("[")] = str("obk") + replacement_map[str("]")] = str("cbk") + replacement_map[str(" ")] = str("_") + replacement_map[str(".")] = str("dot") + replacement_map[str("->")] = str("arrow") longest_replacement = 0 - replacement_map.for_each(fun(key: string, value: string) { + replacement_map.for_each(fun(key: str, value: str) { if (key.length() > longest_replacement) longest_replacement = key.length() }) @@ -105,12 +105,12 @@ obj c_generator (Object) { replacement_map.destruct() linker_string.destruct() } - fun get_id(): string return to_string(id_counter++); - fun generate_function_prototype_and_header(child: *ast_node):pair { + fun get_id(): str return to_string(id_counter++); + fun generate_function_prototype_and_header(child: *ast_node):pair { var backing = child->function - var parameter_types = string() - var parameters = string() - var decorated_name = string() + var parameter_types = str() + var parameters = str() + var decorated_name = str() if (backing.is_extern) decorated_name = backing.name @@ -129,16 +129,16 @@ obj c_generator (Object) { return make_pair(type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameter_types + ");\n", type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameters + ")") } - fun generate_c(name_ast_map: map,*ast_node>>, ast_to_syntax_in: map<*ast_node, *tree> ): pair { - var prequal: string = "#include \n" - var plain_typedefs: string = "\n/**Plain Typedefs**/\n" - var top_level_c_passthrough: string = "" - var variable_extern_declarations: string = "" - var structs: string = "\n/**Type Structs**/\n" + fun generate_c(name_ast_map: map,*ast_node>>, ast_to_syntax_in: map<*ast_node, *tree> ): pair { + var prequal: str = "#include \n" + var plain_typedefs: str = "\n/**Plain Typedefs**/\n" + var top_level_c_passthrough: str = "" + var variable_extern_declarations: str = "" + var structs: str = "\n/**Type Structs**/\n" function_typedef_string = "\n/**Typedefs**/\n" - var function_prototypes: string = "\n/**Function Prototypes**/\n" - var function_definitions: string = "\n/**Function Definitions**/\n" - var variable_declarations: string = "\n/**Variable Declarations**/\n" + var function_prototypes: str = "\n/**Function Prototypes**/\n" + var function_definitions: str = "\n/**Function Definitions**/\n" + var variable_declarations: str = "\n/**Variable Declarations**/\n" // moved out from below so that it can be used for methods as well as regular functions (and eventually lambdas...) var generate_function_definition = fun(child: *ast_node) { @@ -155,7 +155,7 @@ obj c_generator (Object) { var type_poset = poset<*ast_node>() // iterate through asts - name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree,*ast_node>) { + name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree,*ast_node>) { // iterate through children for each ast // do lambdas seperatly, so we can reconstitute the enclosing object if it has one tree_pair.second->translation_unit.lambdas.for_each(fun(child: *ast_node) { @@ -201,7 +201,7 @@ obj c_generator (Object) { }) type_poset.get_sorted().for_each(fun(vert: *ast_node) { var base_name = get_name(vert) - plain_typedefs += string("typedef ") + plain_typedefs += str("typedef ") if (vert->type_def.is_union) { plain_typedefs += "union " structs += "union " @@ -224,7 +224,7 @@ obj c_generator (Object) { return make_pair(prequal+plain_typedefs+function_typedef_string+top_level_c_passthrough+variable_extern_declarations+structs+function_prototypes+variable_declarations+function_definitions + "\n", linker_string) } - fun generate_declaration_statement(node: *ast_node): string { + fun generate_declaration_statement(node: *ast_node): str { var identifier = node->declaration_statement.identifier var ident_type = identifier->identifier.type var to_ret = type_to_c(identifier->identifier.type) + " " + get_name(identifier) @@ -241,27 +241,27 @@ obj c_generator (Object) { } return to_ret } - fun generate_assignment_statement(node: *ast_node): string { + fun generate_assignment_statement(node: *ast_node): str { return generate(node->assignment_statement.to) + " = " + generate(node->assignment_statement.from) } - fun generate_if_statement(node: *ast_node): string { + fun generate_if_statement(node: *ast_node): str { var if_str = "if (" + generate(node->if_statement.condition) + ") {\n" + generate(node->if_statement.then_part) + "}" if (node->if_statement.else_part) if_str += " else {\n" + generate(node->if_statement.else_part) + "}" return if_str + "\n" } - fun generate_while_loop(node: *ast_node): string { + fun generate_while_loop(node: *ast_node): str { return "while (" + generate(node->while_loop.condition) + ")\n" + generate(node->while_loop.statement) } - fun generate_for_loop(node: *ast_node): string { - var init = string(";") + fun generate_for_loop(node: *ast_node): str { + var init = str(";") if (node->for_loop.init) init = generate(node->for_loop.init) - var cond = string(";") + var cond = str(";") if (node->for_loop.condition) cond = generate(node->for_loop.condition) // gotta take off last semicolon - var update = string() + var update = str() if (node->for_loop.update) { update = generate(node->for_loop.update) if (update.length() < 2) @@ -270,33 +270,33 @@ obj c_generator (Object) { } return "for (" + init + cond + "; " + update + ")\n" + generate(node->for_loop.body) } - fun generate_identifier(node: *ast_node): string { + fun generate_identifier(node: *ast_node): str { if (get_ast_type(node)->is_ref) error("still existin ref in identifier") return get_name(node) } - fun generate_return_statement(node: *ast_node): string { + fun generate_return_statement(node: *ast_node): str { if (node->return_statement.return_value) return "return " + generate(node->return_statement.return_value) - return string("return") + return str("return") } - fun generate_branching_statement(node: *ast_node): string { + fun generate_branching_statement(node: *ast_node): str { match(node->branching_statement.b_type) { - branching_type::break_stmt() return string("break") - branching_type::continue_stmt() return string("continue") + branching_type::break_stmt() return str("break") + branching_type::continue_stmt() return str("continue") } } - fun generate_cast(node: *ast_node): string { + fun generate_cast(node: *ast_node): str { return "((" + type_to_c(node->cast.to_type) + ")(" + generate(node->cast.value) + "))" } - fun generate_value(node: *ast_node): string { + fun generate_value(node: *ast_node): str { var value = node->value.string_value if (node->value.value_type->base == base_type::character() && node->value.value_type->indirection == 0) return "'" + value + "'" if (node->value.value_type->base != base_type::character() || node->value.value_type->indirection != 1) return value - var to_ret = string("\"") //" + var to_ret = str("\"") //" value.for_each(fun(c: char) { if (c == '\n') to_ret += "\\n" @@ -309,18 +309,18 @@ obj c_generator (Object) { }) return to_ret + "\"" } - fun generate_code_block(node: *ast_node): string { - var to_ret = string("{\n") + fun generate_code_block(node: *ast_node): str { + var to_ret = str("{\n") node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child) + ";\n";) return to_ret + "}" } // this generates the function as a value, not the actual function - fun generate_function(node: *ast_node): string { + fun generate_function(node: *ast_node): str { return get_name(node) } - fun generate_function_call(node: *ast_node): string { + fun generate_function_call(node: *ast_node): str { var func_name = generate(node->function_call.func) - var call_string = string() + var call_string = str() var func_return_type = get_ast_type(node) var parameters = node->function_call.parameters @@ -365,23 +365,23 @@ obj c_generator (Object) { return call_string } - fun generate_compiler_intrinsic(node: *ast_node): string { + fun generate_compiler_intrinsic(node: *ast_node): str { if (node->compiler_intrinsic.intrinsic == "sizeof") { if (node->compiler_intrinsic.parameters.size || node->compiler_intrinsic.type_parameters.size != 1) error("wrong parameters to sizeof compiler intrinsic") return "sizeof(" + type_to_c(node->compiler_intrinsic.type_parameters[0]) + ")" } else if (node->compiler_intrinsic.intrinsic == "link") { node->compiler_intrinsic.parameters.for_each(fun(value: *ast_node) { - linker_string += string("-l") + value->value.string_value + " " + linker_string += str("-l") + value->value.string_value + " " }) - return string() + return str() } error(node->compiler_intrinsic.intrinsic + ": unknown intrinsic") - return string("ERROR") + return str("ERROR") } - fun generate(node: *ast_node): string { - if (!node) return string("/*NULL*/") + fun generate(node: *ast_node): str { + if (!node) return str("/*NULL*/") match (*node) { ast_node::declaration_statement(backing) return generate_declaration_statement(node) ast_node::assignment_statement(backing) return generate_assignment_statement(node) @@ -400,41 +400,41 @@ obj c_generator (Object) { ast_node::value(backing) return generate_value(node) ast_node::identifier(backing) return generate_identifier(node) } - error(string("COULD NOT GENERATE ") + get_ast_name(node)) - return string("/* COULD NOT GENERATE */") + error(str("COULD NOT GENERATE ") + get_ast_name(node)) + return str("/* COULD NOT GENERATE */") } - fun type_to_c(type: *type): string { - var indirection = string() + fun type_to_c(type: *type): str { + var indirection = str() if (type->is_ref) error("still ref in type_to_c") //indirection += "/*ref*/ *" for (var i = 0; i < type->indirection; i++;) indirection += "*" match (type->base) { - base_type::none() return string("none") + indirection - base_type::template() return string("template") + indirection - base_type::template_type() return string("template_type") + indirection - base_type::void_return() return string("void") + indirection - base_type::boolean() return string("bool") + indirection - base_type::character() return string("char") + indirection - base_type::ucharacter() return string("unsigned char") + indirection - base_type::short_int() return string("short") + indirection - base_type::ushort_int() return string("unsigned short") + indirection - base_type::integer() return string("int") + indirection - base_type::uinteger() return string("unsigned int") + indirection - base_type::long_int() return string("long") + indirection - base_type::ulong_int() return string("unsigned long") + indirection - base_type::floating() return string("float") + indirection - base_type::double_precision() return string("double") + indirection + base_type::none() return str("none") + indirection + base_type::template() return str("template") + indirection + base_type::template_type() return str("template_type") + indirection + base_type::void_return() return str("void") + indirection + base_type::boolean() return str("bool") + indirection + base_type::character() return str("char") + indirection + base_type::ucharacter() return str("unsigned char") + indirection + base_type::short_int() return str("short") + indirection + base_type::ushort_int() return str("unsigned short") + indirection + base_type::integer() return str("int") + indirection + base_type::uinteger() return str("unsigned int") + indirection + base_type::long_int() return str("long") + indirection + base_type::ulong_int() return str("unsigned long") + indirection + base_type::floating() return str("float") + indirection + base_type::double_precision() return str("double") + indirection base_type::object() return get_name(type->type_def) + indirection base_type::function() { type = type->clone_with_indirection(0,false) if (!function_type_map.contains_key(*type)) { - var temp_name = string("function") + get_id() - var temp = string() + var temp_name = str("function") + get_id() + var temp = str() type->parameter_types.for_each(fun(parameter_type: *type) { - temp += string(", ") + type_to_c(parameter_type) + " " + temp += str(", ") + type_to_c(parameter_type) + " " temp_name += "_" + cify_name(type_to_c(parameter_type)) }) if (type->is_raw) - function_typedef_string += string("typedef ") + type_to_c(type->return_type) + " (*" + temp_name + ")(" + temp.slice(1,-1) + ");\n" + function_typedef_string += str("typedef ") + type_to_c(type->return_type) + " (*" + temp_name + ")(" + temp.slice(1,-1) + ");\n" else error(type->to_string() + " is not raw!") // again, the indirection @@ -443,23 +443,23 @@ obj c_generator (Object) { return function_type_map[*type] + indirection } } - return string("impossible type") + indirection + return str("impossible type") + indirection } - fun type_decoration(type: *type): string { + fun type_decoration(type: *type): str { return cify_name(type_to_c(type)) } - fun get_name(node: *ast_node): string { + fun get_name(node: *ast_node): str { var maybe_it = ast_name_map.get_ptr_or_null(node); if (maybe_it) return *maybe_it - var result = string("impossible name") + var result = str("impossible name") var make_unique = true match (*node) { ast_node::type_def(backing) { - var upper = backing.scope[string("~enclosing_scope")][0] + var upper = backing.scope[str("~enclosing_scope")][0] result = cify_name(backing.name) if (is_template(upper)) - upper->template.instantiated_map.reverse_get(node).for_each(fun(t: ref type) result += string("_") + type_decoration(&t);) + upper->template.instantiated_map.reverse_get(node).for_each(fun(t: ref type) result += str("_") + type_decoration(&t);) } ast_node::function(backing) { // be careful, operators like . come through this @@ -468,11 +468,11 @@ obj c_generator (Object) { make_unique = false } else { result = "fun_" - var upper = backing.scope.get_with_default(string("~enclosing_scope"), vector(null()))[0] + var upper = backing.scope.get_with_default(str("~enclosing_scope"), vec(null()))[0] if (upper && is_type_def(upper)) result += get_name(upper) + "_" result += cify_name(node->function.name) - node->function.parameters.for_each(fun(param: *ast_node) result += string("_") + type_decoration(param->identifier.type);) + node->function.parameters.for_each(fun(param: *ast_node) result += str("_") + type_decoration(param->identifier.type);) } } ast_node::identifier(backing) { @@ -489,8 +489,8 @@ obj c_generator (Object) { used_names.add(result) return result } - fun cify_name(name: string): string { - var to_ret = string() + fun cify_name(name: str): str { + var to_ret = str() for (var i = 0; i < name.length(); i++;) { var replaced = false for (var j = longest_replacement; j > 0; j--;) { diff --git a/stdlib/c_line_control.krak b/stdlib/c_line_control.krak index 02b8b7b..847a63a 100644 --- a/stdlib/c_line_control.krak +++ b/stdlib/c_line_control.krak @@ -1,9 +1,9 @@ import symbol:* import tree:* -import vector:* +import vec:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* @@ -11,19 +11,19 @@ import ast_transformation:* import pass_common:* -fun get_line(node: *tree, name: string): *ast_node { +fun get_line(node: *tree, name: str): *ast_node { var to_ret = ast_simple_passthrough_ptr() - to_ret->simple_passthrough.passthrough_str = string("\n#line ") + get_first_terminal(node)->data.position + " \"" + name + "\"\n" + to_ret->simple_passthrough.passthrough_str = str("\n#line ") + get_first_terminal(node)->data.position + " \"" + name + "\"\n" return to_ret } -fun c_line_control(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { +fun c_line_control(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var first = true - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { /*var helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {*/ /*match(*node) {*/ /*if (is_code_block(parent_chain->top()) && ast_to_syntax->contains_key(node)) {*/ - /*println(string("adding ") + get_ast_name(node) + " to " + get_ast_name(parent))*/ + /*println(str("adding ") + get_ast_name(node) + " to " + get_ast_name(parent))*/ /*add_before_in(get_line(ast_to_syntax->get(node), name), node, parent_chain->top())*/ /*}*/ /*}*/ diff --git a/stdlib/ctce_lower.krak b/stdlib/ctce_lower.krak index bfa4873..2ff4986 100644 --- a/stdlib/ctce_lower.krak +++ b/stdlib/ctce_lower.krak @@ -1,9 +1,9 @@ import symbol:* import tree:* -import vector:* +import vec:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* @@ -13,11 +13,11 @@ import hash_set:* import pass_common:* -fun ctce_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { +fun ctce_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var visited = hash_set<*ast_node>() var globals = setup_globals(*name_ast_map) - var ctce_passes = vector<*ast_node>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + var ctce_passes = vec<*ast_node>() + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { match(*node) { ast_node::compiler_intrinsic(backing) { @@ -35,9 +35,9 @@ fun ctce_lower(name_ast_map: *map,*ast_node>>, ast_to }) ctce_passes.for_each(fun(func: *ast_node) { // don't want to pick up the ast_node::value - var params = vector() + var params = vec() // easier to pick up types from the function itself - if (!is_function(func)) error(string("trying to CTCE pass with non function") + get_ast_name(func)) + if (!is_function(func)) error(str("trying to CTCE pass with non function") + get_ast_name(func)) params.add(interpreter::value::pointer(make_pair((name_ast_map) cast *void, func->function.type->parameter_types[0]))) params.add(interpreter::value::pointer(make_pair((ast_to_syntax) cast *void, func->function.type->parameter_types[1]))) call_function(func, params, &globals) diff --git a/stdlib/defer_lower.krak b/stdlib/defer_lower.krak index f79c346..095fd99 100644 --- a/stdlib/defer_lower.krak +++ b/stdlib/defer_lower.krak @@ -1,9 +1,9 @@ import symbol:* import tree:* -import vector:* +import vec:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* @@ -12,9 +12,9 @@ import hash_set:* import pass_common:* -fun defer_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { +fun defer_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var visited = hash_set<*ast_node>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var defer_triple_stack = stack>>() var loop_stack = stack(-1) var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { diff --git a/stdlib/function_value_lower.krak b/stdlib/function_value_lower.krak index 29e7fd7..03d18eb 100644 --- a/stdlib/function_value_lower.krak +++ b/stdlib/function_value_lower.krak @@ -1,10 +1,10 @@ import symbol:* import tree:* -import vector:* +import vec:* import map:* import util:* import type:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* @@ -83,16 +83,16 @@ fun find_closed_variables(func: *ast_node, node: *ast_node): set<*ast_node> { fun in_scope_chain(node: *ast_node, high_scope: *ast_node): bool { if (node == high_scope) return true - if (get_ast_scope(node)->contains_key(string("~enclosing_scope"))) - return in_scope_chain(get_ast_scope(node)->get(string("~enclosing_scope"))[0], high_scope) + if (get_ast_scope(node)->contains_key(str("~enclosing_scope"))) + return in_scope_chain(get_ast_scope(node)->get(str("~enclosing_scope"))[0], high_scope) return false } -fun function_value_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { +fun function_value_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var curr_time = get_time() var visited = hash_set<*ast_node>() var lambdas = set<*ast_node>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { lambdas.add(syntax_ast_pair.second->translation_unit.lambdas) // do in order so that inner lambdas are done before outer ones, so enclosed // variables can propegate outwards @@ -101,10 +101,10 @@ fun function_value_lower(name_ast_map: *map,*ast_node }) }) var all_types = hash_set<*type>() - var function_value_creation_points = vector() - var function_value_call_points = vector() - var closed_over_uses = vector>>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + var function_value_creation_points = vec() + var function_value_call_points = vec() + var closed_over_uses = vec>>() + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var t = get_ast_type(node) if (t) all_types.add(t) @@ -177,18 +177,18 @@ fun function_value_lower(name_ast_map: *map,*ast_node var lambda_struct_type = type_ptr(new_type_def) - var lambda_call_type = type_ptr(vector(lambda_struct_type) + t.parameter_types, t.return_type, 0, false, false, true) + var lambda_call_type = type_ptr(vec(lambda_struct_type) + t.parameter_types, t.return_type, 0, false, false, true) // create parameters var lambda_call_func_param = ast_identifier_ptr("func_struct", lambda_struct_type, null()) - var lambda_call_parameters = vector(lambda_call_func_param) + cleaned->parameter_types.map(fun(t:*type): *ast_node { + var lambda_call_parameters = vec(lambda_call_func_param) + cleaned->parameter_types.map(fun(t:*type): *ast_node { return ast_identifier_ptr("pass_through_param", t, null()) }) - var lambda_call_function = ast_function_ptr(string("lambda_call"), lambda_call_type, lambda_call_parameters, false) + var lambda_call_function = ast_function_ptr(str("lambda_call"), lambda_call_type, lambda_call_parameters, false) // create call body with if, etc var if_statement = ast_if_statement_ptr(access_expression(lambda_call_func_param, "data")) lambda_call_function->function.body_statement = ast_code_block_ptr(if_statement) if_statement->if_statement.then_part = ast_code_block_ptr(ast_return_statement_ptr(ast_function_call_ptr(access_expression(lambda_call_func_param, "func_closure"), - vector(access_expression(lambda_call_func_param, "data")) + lambda_call_parameters.slice(1,-1)))) + vec(access_expression(lambda_call_func_param, "data")) + lambda_call_parameters.slice(1,-1)))) if_statement->if_statement.else_part = ast_code_block_ptr(ast_return_statement_ptr(ast_function_call_ptr(access_expression(lambda_call_func_param, "func"), lambda_call_parameters.slice(1,-1)))) @@ -209,7 +209,7 @@ fun function_value_lower(name_ast_map: *map,*ast_node lambdas.for_each(fun(l: *ast_node) { var closure_struct_type: *type if (l->function.closed_variables.size()) { - var new_type_def_name = string("closure_struct_") + closure_id++ + var new_type_def_name = str("closure_struct_") + closure_id++ var new_type_def = ast_type_def_ptr(new_type_def_name) l->function.closed_variables.for_each(fun(v: *ast_node) { var closed_var_type = v->identifier.type @@ -226,8 +226,8 @@ fun function_value_lower(name_ast_map: *map,*ast_node } var return_type = lambda_type_to_struct_type_and_call_func[*l->function.type].first - var creation_type = type_ptr(vector<*type>(), return_type, 0, false, false, true) - lambda_creation_funcs[l] = ast_function_ptr(l->function.name + "_creation", creation_type, vector<*ast_node>(), false); + var creation_type = type_ptr(vec<*type>(), return_type, 0, false, false, true) + lambda_creation_funcs[l] = ast_function_ptr(l->function.name + "_creation", creation_type, vec<*ast_node>(), false); var body = ast_code_block_ptr() var ident = ast_identifier_ptr("to_ret", return_type, body) body->code_block.children.add(ast_declaration_statement_ptr(ident, null())) @@ -249,7 +249,7 @@ fun function_value_lower(name_ast_map: *map,*ast_node body->code_block.children.add(ast_assignment_statement_ptr(access_expression(closure_param, v->identifier.name), closed_param)) }) } else { - body->code_block.children.add(ast_assignment_statement_ptr(access_expression(ident, "data"), ast_value_ptr(string("0"), type_ptr(base_type::void_return(), 1)))) + body->code_block.children.add(ast_assignment_statement_ptr(access_expression(ident, "data"), ast_value_ptr(str("0"), type_ptr(base_type::void_return(), 1)))) } body->code_block.children.add(ast_return_statement_ptr(ident)) lambda_creation_funcs[l]->function.body_statement = body @@ -264,16 +264,16 @@ fun function_value_lower(name_ast_map: *map,*ast_node }) curr_time = split(curr_time, "\tfunction_value_call_points.forEach") function_value_creation_points.for_each(fun(p: function_parent_block) { - var lambda_creation_params = vector<*ast_node>() + var lambda_creation_params = vec<*ast_node>() // add the declaration of the closure struct to the enclosing code block if (p.function->function.closed_variables.size()) { // pull closure type off lambda creation func parameter var closure_type = get_ast_type(lambda_creation_funcs[p.function]->function.parameters[0])->clone_with_decreased_indirection() var closure_struct_ident = ast_identifier_ptr("closure_struct", closure_type, p.parent_block) p.parent_block->code_block.children.add(0,ast_declaration_statement_ptr(closure_struct_ident, null())) - lambda_creation_params.add(make_operator_call("&", vector(closure_struct_ident))) + lambda_creation_params.add(make_operator_call("&", vec(closure_struct_ident))) p.function->function.closed_variables.for_each(fun(v: *ast_node) { - var addr_of = make_operator_call("&", vector(v)) + var addr_of = make_operator_call("&", vec(v)) if (p.parent_function->function.closed_variables.contains(v)) { closed_over_uses.add(make_pair(v, make_pair(addr_of, p.parent_function))) } @@ -295,7 +295,7 @@ fun function_value_lower(name_ast_map: *map,*ast_node var parent = p.second.first var lambda = p.second.second var closure_param = lambda->function.parameters[0] - replace_with_in(variable, make_operator_call("*", vector(access_expression(closure_param, variable->identifier.name))), parent) + replace_with_in(variable, make_operator_call("*", vec(access_expression(closure_param, variable->identifier.name))), parent) }) curr_time = split(curr_time, "\tclosed_over_uses") // now we can make them raw diff --git a/stdlib/grammer.krak b/stdlib/grammer.krak index 30f4074..e97bcce 100644 --- a/stdlib/grammer.krak +++ b/stdlib/grammer.krak @@ -1,5 +1,5 @@ -import string -import vector +import str +import vec import set import stack import map @@ -9,9 +9,9 @@ import io import util import serialize -fun split_into_words(gram_str: string::string): vector::vector { - // var out.construct(): vector::vector - var out.construct(): vector::vector +fun split_into_words(gram_str: str::str): vec::vec { + // var out.construct(): vec::vec + var out.construct(): vec::vec var begin = 0 for (var i = 0; i < gram_str.length(); i++;) { if (gram_str[i] == '#') { @@ -26,7 +26,7 @@ fun split_into_words(gram_str: string::string): vector::vector { i++ // if we hit a " we check to see if an odd number of backslashes preceed it // (meaning that the " is escaped), and if so, we move on. Otherwise, we found - // the end of the quoted string + // the end of the quoted str if (gram_str[i] == '"') { var escaped = 0 while (gram_str[i-(1+escaped)] == '\\') escaped++ @@ -51,25 +51,25 @@ fun split_into_words(gram_str: string::string): vector::vector { return out } -fun load_grammer(gram_str: string::string): grammer { +fun load_grammer(gram_str: str::str): grammer { var gram.construct(): grammer var leftSide = symbol::symbol("", false) var doLeftSide = true - var rightSide = vector::vector() - /*split_into_words(io::read_file(path)).for_each(fun(word: string::string) {*/ + var rightSide = vec::vec() + /*split_into_words(io::read_file(path)).for_each(fun(word: str::str) {*/ /*io::print("word: "); io::println(word);*/ /*})*/ /*return gram*/ - split_into_words(gram_str).for_each(fun(word: string::string) { + split_into_words(gram_str).for_each(fun(word: str::str) { /*io::print("word: "); io::println(word)*/ if (word == "=") { // do nothing } else if (word == "|") { gram.rules.add(rule(leftSide, rightSide)) - rightSide = vector::vector() + rightSide = vec::vec() } else if (word == ";") { gram.rules.add(rule(leftSide, rightSide)) - rightSide = vector::vector() + rightSide = vec::vec() doLeftSide = true } else { if (doLeftSide) { @@ -80,7 +80,7 @@ fun load_grammer(gram_str: string::string): grammer { // ok, we support both plain terminals "hia*" // and decorated terminals "hia*":hi_with_as // so first check to find the ending " and see if it's - // the end of the string + // the end of the str var last_quote = word.length()-1 while(word[last_quote] != '"') last_quote-- if (last_quote != word.length()-1) { @@ -103,9 +103,9 @@ fun load_grammer(gram_str: string::string): grammer { } obj grammer (Object, Serializable) { - var rules: vector::vector + var rules: vec::vec var non_terminals: set::set - var terminals: vector::vector> + var terminals: vec::vec> var first_set_map: map::map> var parse_table: table @@ -135,15 +135,15 @@ obj grammer (Object, Serializable) { parse_table.destruct() } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(rules) + serialize::serialize(non_terminals) + serialize::serialize(terminals) + serialize::serialize(first_set_map) + serialize::serialize(parse_table) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { // get everything constructed before the assignment /*construct()*/ - /*util::unpack(rules, pos) = serialize::unserialize>(it, pos)*/ + /*util::unpack(rules, pos) = serialize::unserialize>(it, pos)*/ /*util::unpack(non_terminals, pos) = serialize::unserialize>(it, pos)*/ - /*util::unpack(terminals, pos) = serialize::unserialize>>(it, pos)*/ + /*util::unpack(terminals, pos) = serialize::unserialize>>(it, pos)*/ /*util::unpack(first_set_map, pos) = serialize::unserialize>>(it, pos)*/ /*util::unpack(parse_table, pos) = serialize::unserialize(it, pos)*/ @@ -177,7 +177,7 @@ obj grammer (Object, Serializable) { }) } } - fun first_vector(rhs: ref vector::vector): set::set { + fun first_vector(rhs: ref vec::vec): set::set { var toRet = set::set() if (rhs.size) { for (var i = 0; i < rhs.size; i++;) { @@ -199,8 +199,8 @@ obj grammer (Object, Serializable) { } fun calculate_state_automaton() { - var first_state = closure(state(vector::vector(rules[0].with_lookahead(set::set(symbol::eof_symbol()))))) - var states = vector::vector(first_state) // vector instead of set because we need to iterate by index + var first_state = closure(state(vec::vec(rules[0].with_lookahead(set::set(symbol::eof_symbol()))))) + var states = vec::vec(first_state) // vec instead of set because we need to iterate by index var newItems = stack::stack(0) // 0 is the index of the first and only item in states var count = 0 while (newItems.size()) { @@ -240,7 +240,7 @@ obj grammer (Object, Serializable) { /*states.for_each(fun(i: ref state) {*/ /*io::println("STATE:\n")*/ /*i.items.for_each(fun(r: ref rule) {*/ - /*io::println(string::string("\t") + r.to_string())*/ + /*io::println(str::str("\t") + r.to_string())*/ /*})*/ /*})*/ io::println(" there were : states") @@ -254,7 +254,7 @@ obj grammer (Object, Serializable) { initial.items = closure(initial.items) return initial } - fun closure(initial: ref vector::vector): vector::vector { + fun closure(initial: ref vec::vec): vec::vec { var continueIt = true //var count = 0 while (continueIt) { @@ -286,7 +286,7 @@ obj grammer (Object, Serializable) { //io::println("and") //io::println(r.to_string()) //io::println("with") - //var result = string::string("|lookahead {") + //var result = str::str("|lookahead {") //newLookahead.for_each(fun(i: symbol::symbol) { //result += i.to_string() //}) @@ -317,7 +317,7 @@ obj grammer (Object, Serializable) { fun goto(I: ref state, X: ref symbol::symbol): state { // loop through i, find all that have thing::= something . X more, // add thing ::= something X . more - var jPrime = vector::vector() + var jPrime = vec::vec() I.items.for_each(fun(i: ref rule) { if (!i.at_end() && i.next() == X) jPrime.add(i.advanced()) @@ -326,18 +326,18 @@ obj grammer (Object, Serializable) { return state(closure(jPrime)) } - fun to_string(): string::string { - var result = string::string("grammer rules:") - rules.for_each( fun(i : rule) { result += string::string("\n\t") + i.to_string(); } ) + fun to_string(): str::str { + var result = str::str("grammer rules:") + rules.for_each( fun(i : rule) { result += str::str("\n\t") + i.to_string(); } ) result += "\nnon_terminals:" - non_terminals.for_each( fun(i : symbol::symbol) { result += string::string("\n\t") + i.to_string(); } ) + non_terminals.for_each( fun(i : symbol::symbol) { result += str::str("\n\t") + i.to_string(); } ) result += "\nterminals:" - terminals.for_each( fun(i : util::pair) { result += string::string("\n\t") + i.first.to_string() + ": " + i.second.regexString; } ) + terminals.for_each( fun(i : util::pair) { result += str::str("\n\t") + i.first.to_string() + ": " + i.second.regexString; } ) return result } } -fun rule(lhs: symbol::symbol, rhs: vector::vector): rule { +fun rule(lhs: symbol::symbol, rhs: vec::vec): rule { var toRet.construct(): rule toRet.lhs = lhs toRet.rhs = rhs @@ -346,19 +346,19 @@ fun rule(lhs: symbol::symbol, rhs: vector::vector): rule { obj rule (Object, Serializable) { var lhs: symbol::symbol - var rhs: vector::vector + var rhs: vec::vec var position: int var lookahead: set::set - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(lhs) + serialize::serialize(rhs) + serialize::serialize(position) + serialize::serialize(lookahead) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { /*var tempLhs = symbol::invalid_symbol()*/ - /*var tempRhs = vector::vector()*/ + /*var tempRhs = vec::vec()*/ /*var tempLookahead = set::set()*/ /*util::unpack(tempLhs, pos) = serialize::unserialize(it, pos)*/ - /*util::unpack(tempRhs, pos) = serialize::unserialize>(it, pos)*/ + /*util::unpack(tempRhs, pos) = serialize::unserialize>(it, pos)*/ /*util::unpack(position, pos) = serialize::unserialize(it, pos)*/ /*util::unpack(tempLookahead, pos) = serialize::unserialize>(it, pos)*/ @@ -405,10 +405,10 @@ obj rule (Object, Serializable) { fun next(): ref symbol::symbol { return rhs[position] } - fun after(): vector::vector { + fun after(): vec::vec { return rhs.slice(position, -1) } - fun after_next(): vector::vector { + fun after_next(): vec::vec { return rhs.slice(position + 1, -1) } fun at_end(): bool { @@ -430,11 +430,11 @@ obj rule (Object, Serializable) { return toRet } - fun to_string(): string::string { + fun to_string(): str::str { var result = lhs.name + " -> " for (var i = 0; i < rhs.size; i++;) if (i == position) - result += string::string(" . ") + rhs[i].to_string() + ", "; + result += str::str(" . ") + rhs[i].to_string() + ", "; else result += rhs[i].to_string() + ", "; if (position == rhs.size) @@ -448,18 +448,18 @@ obj rule (Object, Serializable) { } } -fun state(itemsIn: ref vector::vector): state { +fun state(itemsIn: ref vec::vec): state { var toRet.construct(itemsIn): state return toRet } obj state (Object) { - var items: vector::vector + var items: vec::vec fun construct(): *state { items.construct() } - fun construct(itemsIn: ref vector::vector): *state { + fun construct(itemsIn: ref vec::vec): *state { items.copy_construct(&itemsIn) } fun copy_construct(other: *state) { @@ -475,8 +475,8 @@ obj state (Object) { fun operator==(other: ref state):bool { return items == other.items } - fun to_string(): string::string { - return string::string("woo a state") + fun to_string(): str::str { + return str::str("woo a state") } } @@ -539,8 +539,8 @@ obj action { } obj table (Object, Serializable) { - // a 2 dimensional table made of a vector and a map that maps from stateno & symbol to a vector of parse actions - var items: vector::vector>> + // a 2 dimensional table made of a vec and a map that maps from stateno & symbol to a vec of parse actions + var items: vec::vec>> fun construct(): *table { items.construct() @@ -555,18 +555,18 @@ obj table (Object, Serializable) { fun destruct() { items.destruct() } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(items) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { /*construct()*/ - /*util::unpack(items, pos) = serialize::unserialize>>>(it, pos)*/ + /*util::unpack(items, pos) = serialize::unserialize>>>(it, pos)*/ pos = items.unserialize(it, pos) return pos } fun expand_to(include_state: int) { while (include_state >= items.size) - items.addEnd(map::map>()) + items.addEnd(map::map>()) } // we always "clean" the symbol before using it so that having different data doesn't // prevent us from finding the symbol in the table @@ -579,7 +579,7 @@ obj table (Object, Serializable) { if (items[from_state].contains_key(cleaned_symbol)) items[from_state][cleaned_symbol].addEnd(action(action_type::push(), to_state)) else - items[from_state].set(cleaned_symbol, vector::vector(action(action_type::push(), to_state))) + items[from_state].set(cleaned_symbol, vec::vec(action(action_type::push(), to_state))) } fun add_reduce(from_state: int, on_symbol: ref symbol::symbol, by_rule_no: int, rule_position: int) { expand_to(from_state) @@ -587,7 +587,7 @@ obj table (Object, Serializable) { if (items[from_state].contains_key(cleaned_symbol)) items[from_state][cleaned_symbol].addEnd(action(action_type::reduce(), by_rule_no, rule_position)) else - items[from_state].set(cleaned_symbol, vector::vector(action(action_type::reduce(), by_rule_no, rule_position))) + items[from_state].set(cleaned_symbol, vec::vec(action(action_type::reduce(), by_rule_no, rule_position))) } fun add_accept(from_state: int, on_symbol: ref symbol::symbol) { expand_to(from_state) @@ -595,13 +595,13 @@ obj table (Object, Serializable) { if (items[from_state].contains_key(cleaned_symbol)) items[from_state][cleaned_symbol].addEnd(action(action_type::accept(), 0)) else - items[from_state].set(cleaned_symbol, vector::vector(action(action_type::accept(), 0))) + items[from_state].set(cleaned_symbol, vec::vec(action(action_type::accept(), 0))) } - fun get(state: int, on_symbol: ref symbol::symbol): vector::vector { + fun get(state: int, on_symbol: ref symbol::symbol): vec::vec { var cleaned_symbol = clean_symbol(on_symbol) if (items[state].contains_key(cleaned_symbol)) return items[state][cleaned_symbol] - return vector::vector() + return vec::vec() } fun get_shift(state: int, on_symbol: ref symbol::symbol): action { var actions = get(state, on_symbol) @@ -615,17 +615,17 @@ obj table (Object, Serializable) { io::println(on_symbol.to_string()) return action(action_type::invalid(),-1) } - fun get_reduces(state: int, on_symbol: ref symbol::symbol): vector::vector { + fun get_reduces(state: int, on_symbol: ref symbol::symbol): vec::vec { return get(state, on_symbol).filter(fun(act: action):bool { return act.act == action_type::reduce(); }) } fun print_string() { - /*return string::string("woo a table of size: ") + items.size*/ + /*return str::str("woo a table of size: ") + items.size*/ io::print("woo a table of size: ") io::println(items.size) for (var i = 0; i < items.size; i++;) { io::print("for state: ") io::println(i) - items[i].for_each(fun(sym: symbol::symbol, actions: vector::vector) { + items[i].for_each(fun(sym: symbol::symbol, actions: vec::vec) { actions.for_each(fun(action: action) { io::print("\ton symbol: ") io::print(sym.to_string()) diff --git a/stdlib/hash_map.krak b/stdlib/hash_map.krak index 5e43d86..b9c0920 100644 --- a/stdlib/hash_map.krak +++ b/stdlib/hash_map.krak @@ -1,4 +1,4 @@ -import vector +import vec import map import io import serialize @@ -15,7 +15,7 @@ fun hash_map(key: ref T, value: ref U): hash_map { } obj hash_map (Object, Serializable) { - var data: vector::vector> + var data: vec::vec> var size: int fun construct(): *hash_map { @@ -35,10 +35,10 @@ obj hash_map (Object, Serializable) { fun destruct() { data.destruct() } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(data) + serialize::serialize(size) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { pos = data.unserialize(it, pos) util::unpack(size, pos) = serialize::unserialize(it, pos) return pos @@ -53,7 +53,7 @@ obj hash_map (Object, Serializable) { if (!data[(key_hash%data.size) cast int].contains_key(key)) { size++ if (size > data.size) { - var new_data.construct(size*2): vector::vector> + var new_data.construct(size*2): vec::vec> for (var i = 0; i < size*2; i++;) new_data.addEnd(map::map()) for_each(fun(key: T, value: U) { diff --git a/stdlib/hash_set.krak b/stdlib/hash_set.krak index b7ea193..a310cfc 100644 --- a/stdlib/hash_set.krak +++ b/stdlib/hash_set.krak @@ -1,5 +1,5 @@ import hash_map -import vector +import vec import io import serialize import set @@ -15,7 +15,7 @@ fun hash_set(item: T): hash_set { return toRet } -fun from_vector(items: vector::vector): hash_set { +fun from_vector(items: vec::vec): hash_set { var toRet.construct() : hash_set items.for_each( fun(item: T) toRet.add(item); ) return toRet @@ -37,10 +37,10 @@ obj hash_set (Object, Serializable) { fun operator=(rhs: ref hash_set) { data = rhs.data } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(data) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { return data.unserialize(it, pos) } // the old unnecessary template to prevent generation @@ -119,7 +119,7 @@ obj hash_set (Object, Serializable) { var prev_size = 0 while (prev_size != size()) { prev_size = size() - var to_add.construct(size()): vector::vector + var to_add.construct(size()): vec::vec for_each(fun(i: T) { func(i).for_each(fun(j: T) { to_add.add(j); }) }) diff --git a/stdlib/importer.krak b/stdlib/importer.krak index 41f44d3..1cd199d 100644 --- a/stdlib/importer.krak +++ b/stdlib/importer.krak @@ -1,24 +1,24 @@ import symbol:* import tree:* -import vector:* +import vec:* import stack:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* import ast_transformation:* import parser:* -fun import(file_name: string, parsers: ref vector, ast_pass: ref ast_transformation, import_paths: vector): map,*ast_node>> { - var name_ast_map = map,*ast_node>>() +fun import(file_name: str, parsers: ref vec, ast_pass: ref ast_transformation, import_paths: vec): map,*ast_node>> { + var name_ast_map = map,*ast_node>>() // lambda closes over our fix-up list - var imports_to_fix = vector<*ast_node>() - var import_first_pass = fun(file_name_idx: pair) { + var imports_to_fix = vec<*ast_node>() + var import_first_pass = fun(file_name_idx: pair) { var file_name = file_name_idx.first - var file = string() - import_paths.for_each(fun(path: string) { + var file = str() + import_paths.for_each(fun(path: str) { if (file_exists(path + file_name)) { file = read_file(path + file_name) } else { @@ -47,11 +47,11 @@ fun import(file_name: string, parsers: ref vector, ast_pass: ref ast_tra } printlnerr() printlnerr("**Second Pass**") - name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree, *ast_node>) ast_pass.second_pass(tree_pair.first, tree_pair.second);) + name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree, *ast_node>) ast_pass.second_pass(tree_pair.first, tree_pair.second);) printlnerr("**Third Pass**") - name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree, *ast_node>) ast_pass.third_pass(tree_pair.first, tree_pair.second);) + name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree, *ast_node>) ast_pass.third_pass(tree_pair.first, tree_pair.second);) printlnerr("**Fourth Pass**") - name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree, *ast_node>) ast_pass.fourth_pass(tree_pair.first, tree_pair.second);) + name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree, *ast_node>) ast_pass.fourth_pass(tree_pair.first, tree_pair.second);) return name_ast_map } diff --git a/stdlib/interpreter.krak b/stdlib/interpreter.krak index 50dd110..70de6d7 100644 --- a/stdlib/interpreter.krak +++ b/stdlib/interpreter.krak @@ -2,7 +2,7 @@ import mem:* import math:* import map:* import stack:* -import string:* +import str:* import util:* import tree:* import symbol:* @@ -65,7 +65,7 @@ fun wrap_value(val: *ast_node): value { var value_str = val->value.string_value var value_type = val->value.value_type if (value_str[0] == '"') { // " // Comment hack for emacs now - var to_ret = string() + var to_ret = str() // triple quoted strings if (value_str[1] == '"' && value_str.length() > 2 && value_str[2] == '"') value_str = value_str.slice(3,-4) @@ -131,8 +131,8 @@ fun wrap_value(val: *ast_node): value { return value::void_nothing() } fun unwrap_value(val: value): *ast_node { - // string, char, bool, floating - var value_string = string() + // str, char, bool, floating + var value_string = str() match (get_real_value(val)) { value::boolean(data) value_string = to_string(data) value::character(data) value_string = to_string(data) @@ -148,7 +148,7 @@ fun unwrap_value(val: value): *ast_node { value::void_nothing() error("trying to unwrap a void into an ast_value_ptr") value::pointer(point) { if (point.second->base == base_type::character() && point.second->indirection == 1) - value_string = string("\"") + string((point.first) cast *char) + "\"" + value_string = str("\"") + str((point.first) cast *char) + "\"" else error("trying to unwrap a pointer into an ast_value_ptr") } @@ -212,7 +212,7 @@ fun truthy(v: ref value):bool { } error("untruthy value") } -fun do_basic_op(func_name: string, a: value, b: value): value { +fun do_basic_op(func_name: str, a: value, b: value): value { match (get_real_value(a)) { value::boolean(av) return do_basic_op_second_half(func_name, av, b, null()) value::character(av) return do_basic_op_second_half(func_name, av, b, null()) @@ -249,18 +249,18 @@ fun do_basic_op(func_name: string, a: value, b: value): value { } else if (func_name == "-") { ptr = ((av.first) cast *char - inc_in_bytes) cast *void } else { - println(string("pointer arithmatic is not +, -, ==, or !=: ") + func_name + ", b is: ") + println(str("pointer arithmatic is not +, -, ==, or !=: ") + func_name + ", b is: ") print_value(b) - error(string("pointer arithmatic is not +, -, ==, or !=: ") + func_name) + error(str("pointer arithmatic is not +, -, ==, or !=: ") + func_name) } return value::pointer(make_pair(ptr, av.second)) } - value::void_nothing() error(string("basic op called with void_nothing as first param: ") + func_name) - value::object_like() error(string("basic op called with object_like as first param: ") + func_name) + value::void_nothing() error(str("basic op called with void_nothing as first param: ") + func_name) + value::object_like() error(str("basic op called with object_like as first param: ") + func_name) } - error(string("basic op called with something wrong as first param: ") + func_name) + error(str("basic op called with something wrong as first param: ") + func_name) } -fun do_basic_op_second_half(func_name: string, av: T, b: value, ptr_type: *type): value { +fun do_basic_op_second_half(func_name: str, av: T, b: value, ptr_type: *type): value { // because of the trickery in do_basic_op, if either param is a pointer, it's b match (get_real_value(b)) { value::boolean(bv) return do_op(func_name, av, bv, ptr_type) @@ -274,15 +274,15 @@ fun do_basic_op_second_half(func_name: string, av: T, b: value, ptr_type: *ty value::ulong_int(bv) return do_op(func_name, av, bv, ptr_type) value::floating(bv) return do_floating_op(func_name, av, bv, ptr_type) value::double_precision(bv) return do_floating_op(func_name, av, bv, ptr_type) - value::void_nothing() error(string("basic op called with void_nothing as second param: ") + func_name) - value::object_like() error(string("basic op called with object_like as second param: ") + func_name) + value::void_nothing() error(str("basic op called with void_nothing as second param: ") + func_name) + value::object_like() error(str("basic op called with object_like as second param: ") + func_name) // if one is a pointer, we want it to be a value::pointer(bv) return do_basic_op(func_name, b, raw_to_value(av)) } print_value(b) - error(string("basic op called with something wrong as second param: ") + func_name) + error(str("basic op called with something wrong as second param: ") + func_name) } -fun do_op(op: string, a: T, b: U, ptr_type: *type): value { +fun do_op(op: str, a: T, b: U, ptr_type: *type): value { if (op == "+") return raw_to_value(a + b) if (op == "-") return raw_to_value(a - b) if (op == "*") return raw_to_value(a * b) @@ -299,7 +299,7 @@ fun do_op(op: string, a: T, b: U, ptr_type: *type): value { if (op == "&") return raw_to_value(a & b) error(("Invalid op: ") + op) } -fun do_basic_floating_op_second_half(func_name: string, av: T, b: value, ptr_type: *type): value { +fun do_basic_floating_op_second_half(func_name: str, av: T, b: value, ptr_type: *type): value { // because of the trickery in do_basic_op, if either param is a pointer, it's b match (get_real_value(b)) { value::boolean(bv) return do_floating_op(func_name, av, bv, ptr_type) @@ -313,15 +313,15 @@ fun do_basic_floating_op_second_half(func_name: string, av: T, b: value, ptr_ value::ulong_int(bv) return do_floating_op(func_name, av, bv, ptr_type) value::floating(bv) return do_floating_op(func_name, av, bv, ptr_type) value::double_precision(bv) return do_floating_op(func_name, av, bv, ptr_type) - value::void_nothing() error(string("basic op called with void_nothing as second param: ") + func_name) - value::object_like() error(string("basic op called with object_like as second param: ") + func_name) + value::void_nothing() error(str("basic op called with void_nothing as second param: ") + func_name) + value::object_like() error(str("basic op called with object_like as second param: ") + func_name) // if one is a pointer, we want it to be a value::pointer(bv) return do_basic_op(func_name, b, raw_to_value(av)) } print_value(b) - error(string("basic op called with something wrong as second param: ") + func_name) + error(str("basic op called with something wrong as second param: ") + func_name) } -fun do_floating_op(op: string, a: T, b: U, ptr_type: *type): value { +fun do_floating_op(op: str, a: T, b: U, ptr_type: *type): value { if (op == "+") return raw_to_value(a + b) if (op == "-") return raw_to_value(a - b) if (op == "*") return raw_to_value(a * b) @@ -391,7 +391,7 @@ fun get_real_value(v: value): value { base_type::double_precision() return value::double_precision(*(var_ptr) cast *double) base_type::function() return value::function(*(var_ptr) cast *pair<*ast_node,*map<*ast_node,value>>) } - error(string("Cannot get real value from variable: ") + variable.second->to_string()) + error(str("Cannot get real value from variable: ") + variable.second->to_string()) } fun wrap_into_variable(v: value): value { if (is_variable(v)) @@ -482,7 +482,7 @@ fun cast_value(v: value, to_type: *type): value { value::double_precision(data) return get_real_value(v) } } - error(string("Bad cast to ") + to_type->to_string()) + error(str("Bad cast to ") + to_type->to_string()) } fun cast_value_second_half(v: value): value { match (get_real_value(v)) { @@ -542,7 +542,7 @@ fun type_size_and_alignment(t: *type): pair { base_type::floating() return make_pair(#sizeof, #sizeof) base_type::double_precision() return make_pair(#sizeof, #sizeof) } - error(string("Invalid type for type_size: ") + t->to_string()) + error(str("Invalid type for type_size: ") + t->to_string()) } fun offset_into_struct(struct_type: *type, ident: *ast_node): ulong { var offset: ulong = 0 @@ -574,23 +574,23 @@ fun pop_and_free(var_stack: *stack>) { }) } -fun call_main(name_ast_map: ref map,*ast_node>>) { - var results = vector<*ast_node>() - name_ast_map.for_each(fun(key: string, value: pair<*tree,*ast_node>) { - results += scope_lookup(string("main"), value.second) +fun call_main(name_ast_map: ref map,*ast_node>>) { + var results = vec<*ast_node>() + name_ast_map.for_each(fun(key: str, value: pair<*tree,*ast_node>) { + results += scope_lookup(str("main"), value.second) }) if (results.size != 1) - error(string("wrong number of mains to call: ") + results.size) + error(str("wrong number of mains to call: ") + results.size) var globals = setup_globals(name_ast_map) - var result = call_function(results[0], vector(), &globals) + var result = call_function(results[0], vec(), &globals) } fun evaluate_constant_expression(node: *ast_node): value return interpret(node, null>>(), value::void_nothing(), null(), null>()).first fun evaluate_with_globals(node: *ast_node, globals: *map<*ast_node, value>): value return interpret(node, null>>(), value::void_nothing(), null(), globals).first -fun setup_globals(name_ast_map: ref map,*ast_node>>): map<*ast_node, value> { +fun setup_globals(name_ast_map: ref map,*ast_node>>): map<*ast_node, value> { var globals = map<*ast_node, value>() - name_ast_map.for_each(fun(key: string, value: pair<*tree,*ast_node>) { + name_ast_map.for_each(fun(key: str, value: pair<*tree,*ast_node>) { value.second->translation_unit.children.for_each(fun(child: *ast_node) { if (is_declaration_statement(child)) { var declaration = child->declaration_statement @@ -607,7 +607,7 @@ fun setup_globals(name_ast_map: ref map,*ast_node>>): *(stdin_pointer) cast **void = stdin; globals[declaration.identifier] = value::variable(make_pair(stdin_pointer, stdin_type)) } else { - error(string("unknown extern: ") + identifier.name) + error(str("unknown extern: ") + identifier.name) } } else { globals[declaration.identifier] = value::variable(make_pair(calloc(type_size(identifier.type)), identifier.type)) @@ -666,8 +666,8 @@ fun interpret_function_call(func_call: *ast_node, var_stack: *stack() - var parameter_sources = vector<*ast_node>() + var parameters = vec() + var parameter_sources = vec<*ast_node>() // if we don't have to copy_construct params (is an operator, or has no object params) if (func_name == "&" || !func_call_parameters.any_true(fun(p: *ast_node): bool return get_ast_type(p)->is_object() && get_ast_type(p)->indirection == 0;)) { parameters = func_call_parameters.map(fun(p: *ast_node): value return interpret(p, var_stack, enclosing_object, enclosing_func, globals).first;) @@ -679,7 +679,7 @@ fun interpret_function_call(func_call: *ast_node, var_stack: *stack()), control_flow::nor()) + return make_pair(do_basic_op_second_half(str("-"), 0, parameters[0], null()), control_flow::nor()) if (func_name == "!") return make_pair(value::boolean(!truthy(get_real_value(parameters[0]))), control_flow::nor()) if (func_name == "++p" || func_name == "--p") { @@ -705,7 +705,7 @@ fun interpret_function_call(func_call: *ast_node, var_stack: *stackfunction.body_statement) - error(string("trying to call unsupported extern function: ") + func_name) + error(str("trying to call unsupported extern function: ") + func_name) } else { // not the operator & and at least one object like parameter parameter_sources = func_call_parameters @@ -722,17 +722,17 @@ fun interpret_function_call(func_call: *ast_node, var_stack: *stack, globals: *map<*ast_node, value>): value { +fun call_function(func: *ast_node, parameters: vec, globals: *map<*ast_node, value>): value { var var_stack = stack>() var_stack.push(map<*ast_node,value>()) - var result = call_function(func, parameters, vector<*ast_node>(), &var_stack, map<*ast_node,value>(), value::void_nothing(), value::void_nothing(), null(), globals) + var result = call_function(func, parameters, vec<*ast_node>(), &var_stack, map<*ast_node,value>(), value::void_nothing(), value::void_nothing(), null(), globals) pop_and_free(&var_stack) return result } // call_function can be called with either parameter values in parameters or ast expressions in parameter_sources // this is to allow easy function calling if we already have the values (for main, say, or to make our job if it's not // an operator easier), but we need to be able to be called with ast_expressions too so we can properly copy_construct once -fun call_function(func: *ast_node, parameters: vector, parameter_sources: vector<*ast_node>, var_stack: *stack>, possible_closure_map: ref map<*ast_node, value>, enclosing_object: value, new_enclosing_object: value, enclosing_func: *ast_node, globals: *map<*ast_node, value>): value { +fun call_function(func: *ast_node, parameters: vec, parameter_sources: vec<*ast_node>, var_stack: *stack>, possible_closure_map: ref map<*ast_node, value>, enclosing_object: value, new_enclosing_object: value, enclosing_func: *ast_node, globals: *map<*ast_node, value>): value { // will need adjustment if (!is_function(func)) error("Can't handle not function function calls (can do regular method, is this chained or something?)") @@ -745,7 +745,7 @@ fun call_function(func: *ast_node, parameters: vector, parameter_sources: if (parameter_sources.size == 0) { /*println(func_name + " being called with parameter values")*/ if (parameters.size != func->function.parameters.size) - error(string("calling function ") + func->function.name + " with wrong number of parameters (values)") + error(str("calling function ") + func->function.name + " with wrong number of parameters (values)") for (var i = 0; i < parameters.size; i++;) { var param_type = get_ast_type(func)->parameter_types[i] var param_ident = func->function.parameters[i] @@ -765,7 +765,7 @@ fun call_function(func: *ast_node, parameters: vector, parameter_sources: /*println(func_name + " being called with parameter sources")*/ // need to pull from parameter_sources instead if (parameter_sources.size != func->function.parameters.size) - error(string("calling function ") + func->function.name + " with wrong number of parameters (sources)") + error(str("calling function ") + func->function.name + " with wrong number of parameters (sources)") for (var i = 0; i < parameter_sources.size; i++;) { var param_type = get_ast_type(func)->parameter_types[i] var param_ident = func->function.parameters[i] @@ -790,7 +790,7 @@ fun call_function(func: *ast_node, parameters: vector, parameter_sources: } return to_ret } -fun call_built_in_extern(func_name: string, parameters: vector): value { +fun call_built_in_extern(func_name: str, parameters: vec): value { for (var i = 0; i < parameters.size; i++;) parameters[i] = get_real_value(parameters[i]) if (func_name == "printf") { @@ -866,7 +866,7 @@ fun call_built_in_extern(func_name: string, parameters: vector): value { assert(parameters.size == 1 && is_pointer(parameters[0]), "Calling pclose with wrong params") return value::integer(pclose(parameters[0].pointer.first)) } else { - error(string("trying to call invalid func: ") + func_name) + error(str("trying to call invalid func: ") + func_name) } return value::void_nothing() } @@ -1017,7 +1017,7 @@ fun interpret_identifier(ident: *ast_node, var_stack: *stacksize(); i++;) { - println(string("level: ") + i) + println(str("level: ") + i) var_stack->from_top(i).for_each(fun(key: *ast_node, v: value) print(get_ast_name(key) + " ");) println() } @@ -1031,7 +1031,7 @@ fun interpret_identifier(ident: *ast_node, var_stack: *stackidentifier.name) + error(str("Cannot find variable: ") + ident->identifier.name) } fun interpret_cast(node: *ast_node, var_stack: *stack>, enclosing_object: value, enclosing_func: *ast_node, globals: *map<*ast_node, value>): pair { return make_pair(cast_value(interpret(node->cast.value, var_stack, enclosing_object, enclosing_func, globals).first, node->cast.to_type), control_flow::nor()) @@ -1040,7 +1040,7 @@ fun interpret_compiler_intrinsic(node: *ast_node, var_stack: *stackcompiler_intrinsic.intrinsic if (intrinsic_name == "sizeof") return make_pair(value::ulong_int(type_size(node->compiler_intrinsic.type_parameters[0])), control_flow::nor()) - error(string("bad intrinsic: ") + intrinsic_name) + error(str("bad intrinsic: ") + intrinsic_name) } fun interpret_value(val: *ast_node): pair return make_pair(wrap_value(val), control_flow::nor()) @@ -1062,6 +1062,6 @@ fun interpret(node: *ast_node, var_stack: *stack>, enclosi ast_node::compiler_intrinsic(backing) return interpret_compiler_intrinsic(node, var_stack) ast_node::value(backing) return interpret_value(node) } - error(string("Cannot interpret node: ") + get_ast_name(node)) + error(str("Cannot interpret node: ") + get_ast_name(node)) } diff --git a/stdlib/io.krak b/stdlib/io.krak index 2da1cbc..9e826c5 100644 --- a/stdlib/io.krak +++ b/stdlib/io.krak @@ -1,5 +1,5 @@ -import string; -import vector; +import str; +import vec; import mem:* ext fun printf(fmt_str: *char, ...): int @@ -10,16 +10,16 @@ ext fun fgets(buff: *char, size: int, file: *void): *char ext var stdin: *void // dead simple stdin -fun get_line(prompt: string::string, line_size: int): string::string { +fun get_line(prompt: str::str, line_size: int): str::str { print(prompt) return get_line(line_size) } -fun get_line(line_size: int): string::string +fun get_line(line_size: int): str::str return get_line(line_size, stdin) -fun get_line(line_size: int, file: *void): string::string { +fun get_line(line_size: int, file: *void): str::str { var buff = new(line_size) fgets(buff, line_size, file) - var to_ret = string::string(buff) + var to_ret = str::str(buff) delete(buff) return to_ret.slice(0,-2) // remove '\n' } @@ -29,7 +29,7 @@ fun printlnerr(toPrint: T) : void { } fun printlnerr() printerr("\n") -fun printerr(toPrint: string::string) : void { +fun printerr(toPrint: str::str) : void { var charArr = toPrint.toCharArray() printerr(charArr) delete(charArr) @@ -51,9 +51,9 @@ fun println() print("\n") fun print(toPrint: char) : void - print(string::string(toPrint)) + print(str::str(toPrint)) -fun print(toPrint: string::string) : void { +fun print(toPrint: str::str) : void { var charArr = toPrint.toCharArray() print(charArr) delete(charArr) @@ -65,7 +65,7 @@ fun print(toPrint: bool) { print("false") } fun print(toPrint: T): void - print(string::to_string(toPrint)) + print(str::to_string(toPrint)) // Ok, just some DEAD simple file io for now ext fun fopen(path: *char, mode: *char): *void @@ -75,7 +75,7 @@ ext fun ftell(file: *void): long ext fun fseek(file: *void, offset: long, whence: int): int ext fun fread(ptr: *void, size: ulong, nmemb: ulong, file: *void): ulong ext fun fwrite(ptr: *void, size: ulong, nmemb: ulong, file: *void): ulong -fun file_exists(path: string::string): bool { +fun file_exists(path: str::str): bool { var char_path = path.toCharArray() defer delete(char_path) var fp = fopen(char_path, "r") @@ -85,13 +85,13 @@ fun file_exists(path: string::string): bool { } return false } -fun read_file(path: string::string): string::string { +fun read_file(path: str::str): str::str { if (!file_exists(path)) - return string::string() - var toRet.construct(read_file_binary(path)): string::string + return str::str() + var toRet.construct(read_file_binary(path)): str::str return toRet } -fun write_file(path: string::string, data: string::string) { +fun write_file(path: str::str, data: str::str) { var char_path = path.toCharArray() defer delete(char_path) var char_data = data.toCharArray() @@ -100,7 +100,7 @@ fun write_file(path: string::string, data: string::string) { fprintf(fp, "%s", char_data) fclose(fp) } -fun read_file_binary(path: string::string): vector::vector { +fun read_file_binary(path: str::str): vec::vec { var char_path = path.toCharArray() defer delete(char_path) var fp = fopen(char_path, "r") @@ -111,13 +111,13 @@ fun read_file_binary(path: string::string): vector::vector { var readSize = fread((data) cast *void, (1) cast ulong, (size) cast ulong, fp) fclose(fp) data[readSize] = 0 - var toRet.construct((size) cast int): vector::vector + var toRet.construct((size) cast int): vec::vec for (var i = 0; i < size; i++;) toRet.add(data[i]) delete(data) return toRet } -fun write_file_binary(path: string::string, vdata: vector::vector) { +fun write_file_binary(path: str::str, vdata: vec::vec) { var char_path = path.toCharArray() defer delete(char_path) var data = vdata.getBackingMemory() diff --git a/stdlib/lexer.krak b/stdlib/lexer.krak index 8a9284c..be5138f 100644 --- a/stdlib/lexer.krak +++ b/stdlib/lexer.krak @@ -1,10 +1,10 @@ import regex import symbol -import string -import vector +import str +import vec import util -fun lexer(regs: vector::vector): lexer { +fun lexer(regs: vec::vec): lexer { /*var toRet:lexer*/ var toRet.construct() :lexer regs.for_each( fun(reg: regex::regex) { @@ -13,7 +13,7 @@ fun lexer(regs: vector::vector): lexer { return toRet } -fun lexer(regs: vector::vector>): lexer { +fun lexer(regs: vec::vec>): lexer { /*var toRet:lexer*/ var toRet.construct() :lexer regs.for_each( fun(reg: util::pair) @@ -23,8 +23,8 @@ fun lexer(regs: vector::vector>): lexer } obj lexer (Object) { - var regs: vector::vector> - var input: string::string + var regs: vec::vec> + var input: str::str var position: int var line_number: int fun construct(): *lexer { @@ -48,19 +48,19 @@ obj lexer (Object) { destruct() copy_construct(&old) } - fun add_regex(name: string::string, newOne: regex::regex) { + fun add_regex(name: str::str, newOne: regex::regex) { regs.add(util::make_pair(name,newOne)) } - fun add_regex(newOne: util::pair) { + fun add_regex(newOne: util::pair) { regs.add(newOne) } fun add_regex(newOne: regex::regex) { regs.add(util::make_pair(newOne.regexString, newOne)) } fun add_regex(newOne: *char) { - regs.add(util::make_pair(string::string(newOne), regex::regex(newOne))) + regs.add(util::make_pair(str::str(newOne), regex::regex(newOne))) } - fun set_input(in: ref string::string) { + fun set_input(in: ref str::str) { position = 0 line_number = 1 input = in diff --git a/stdlib/map.krak b/stdlib/map.krak index b37cfbd..9728272 100644 --- a/stdlib/map.krak +++ b/stdlib/map.krak @@ -1,4 +1,4 @@ -import vector +import vec import mem import io import serialize @@ -15,8 +15,8 @@ fun map(key: ref T, value: ref U): map { } obj map (Object, Serializable) { - var keys: vector::vector - var values: vector::vector + var keys: vec::vec + var values: vec::vec fun construct(): *map { keys.construct() @@ -35,10 +35,10 @@ obj map (Object, Serializable) { keys.destruct() values.destruct() } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(keys) + serialize::serialize(values) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { pos = keys.unserialize(it, pos) pos = values.unserialize(it, pos) return pos diff --git a/stdlib/matrix.krak b/stdlib/matrix.krak index 3632ec2..80235c3 100644 --- a/stdlib/matrix.krak +++ b/stdlib/matrix.krak @@ -1,8 +1,8 @@ -import vector:*; +import vec:*; import io:*; obj matrix (Object) { - var data: vector; + var data: vec; var rows: int; var cols: int; diff --git a/stdlib/node_counter.krak b/stdlib/node_counter.krak index a59467e..b774eeb 100644 --- a/stdlib/node_counter.krak +++ b/stdlib/node_counter.krak @@ -1,9 +1,9 @@ import symbol:* import tree:* -import vector:* +import vec:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* @@ -11,19 +11,19 @@ import ast_transformation:* import pass_common:* -fun node_counter(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { +fun node_counter(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var counter = node_counter_helper(name_ast_map, ast_to_syntax) - println(string("Number of nodes touched: ") + counter) + println(str("Number of nodes touched: ") + counter) } -fun node_counter_test(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { +fun node_counter_test(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var counter = node_counter_helper(name_ast_map, ast_to_syntax) if (counter > 10000) println("more than 10000 nodes!") } -fun node_counter_helper(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>): int { +fun node_counter_helper(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>): int { var counter = 0 var visited = hash_set<*ast_node>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { counter++ } diff --git a/stdlib/obj_lower.krak b/stdlib/obj_lower.krak index c94cd46..7a34da3 100644 --- a/stdlib/obj_lower.krak +++ b/stdlib/obj_lower.krak @@ -1,9 +1,9 @@ import symbol:* import tree:* -import vector:* +import vec:* import map:* import util:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* @@ -30,13 +30,13 @@ import hash_set:* PASS THREE THROUGH name_ast_map 4 change all methods to take in self, change all method calls to pass in self, change all in method references to be explicit */ -fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { +fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var visited1 = hash_set<*ast_node>() var visited2 = hash_set<*ast_node>() var visited3 = hash_set<*ast_node>() var functions_visited_for_construct_in_destruct_out = hash_set<*ast_node>() var all_type_defs = set<*ast_node>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { // Pass 1 var ensure_block_and_munge: fun(*ast_node,*stack<*ast_node>,*hash_set<*ast_node>):bool = fun(node: *ast_node, parent_chain: *stack<*ast_node>, visited: *hash_set<*ast_node>):bool { match(*node) { @@ -66,11 +66,11 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ add_to_scope("~enclosing_scope", node, backing.statement) } var condition = backing.condition - backing.condition = ast_value_ptr(string("true"), type_ptr(base_type::boolean())) + backing.condition = ast_value_ptr(str("true"), type_ptr(base_type::boolean())) // objects do not coerce to booleans, so it should be ok for this not to be a ref var condition_ident = ast_identifier_ptr("condition_temp", get_ast_type(condition), backing.statement) backing.statement->code_block.children.add(0, ast_declaration_statement_ptr(condition_ident, condition)) - var condition_if = ast_if_statement_ptr(make_operator_call("!", vector(condition_ident))) + var condition_if = ast_if_statement_ptr(make_operator_call("!", vec(condition_ident))) condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt()) backing.statement->code_block.children.add(1, condition_if) } @@ -83,7 +83,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ backing.init = null() // the do_update goes in the block above the for var update_ident = ast_identifier_ptr("do_update", type_ptr(base_type::boolean()), parent_chain->top()) - add_before_in(ast_declaration_statement_ptr(update_ident, ast_value_ptr(string("false"), type_ptr(base_type::boolean()))), + add_before_in(ast_declaration_statement_ptr(update_ident, ast_value_ptr(str("false"), type_ptr(base_type::boolean()))), node, parent_chain->top()) var update_if = ast_if_statement_ptr(update_ident) add_to_scope("~enclosing_scope", backing.body, update_if) @@ -91,14 +91,14 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ add_to_scope("~enclosing_scope", update_if, update_if->if_statement.then_part) backing.update = null() backing.body->code_block.children.add(0, update_if) - backing.body->code_block.children.add(1, ast_assignment_statement_ptr(update_ident, ast_value_ptr(string("true"), type_ptr(base_type::boolean())))) + backing.body->code_block.children.add(1, ast_assignment_statement_ptr(update_ident, ast_value_ptr(str("true"), type_ptr(base_type::boolean())))) var condition = backing.condition - backing.condition = ast_value_ptr(string("true"), type_ptr(base_type::boolean())) + backing.condition = ast_value_ptr(str("true"), type_ptr(base_type::boolean())) // objects do not coerce to booleans, so it should be ok for this not to be a ref var condition_ident = ast_identifier_ptr("condition_temp", get_ast_type(condition), backing.body) backing.body->code_block.children.add(2, ast_declaration_statement_ptr(condition_ident, condition)) - var condition_if = ast_if_statement_ptr(make_operator_call("!", vector(condition_ident))) + var condition_if = ast_if_statement_ptr(make_operator_call("!", vec(condition_ident))) condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt()) backing.body->code_block.children.add(3, condition_if) } @@ -109,7 +109,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ }) // make sure all blockes munged before we move ahead - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var visit = hash_set<*ast_node>() var short_check: fun(*ast_node,*stack<*ast_node>,*hash_set<*ast_node>): bool = fun(node: *ast_node, parent_chain: *stack<*ast_node>, visited: *hash_set<*ast_node>): bool { match(*node) { @@ -130,7 +130,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ error("Bad in 2") } ast_node::function_call(backing) { - var func_name = string() + var func_name = str() if (is_function(backing.func)) { func_name = backing.func->function.name if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/" @@ -148,7 +148,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ var short_circuit_declaration = ast_declaration_statement_ptr(short_circuit_result, backing.parameters[0]) var condition = short_circuit_result if (func_name == "||") - condition = make_operator_call("!", vector(condition)) + condition = make_operator_call("!", vec(condition)) var short_circuit_if = ast_if_statement_ptr(condition) add_to_scope("~enclosing_scope", parent_chain->from_top(enclosing_block_idx), short_circuit_if) // how to get proper parent scoping working for this part @@ -202,12 +202,12 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ else in_function_param_type = get_ast_type(param)->clone_without_ref() var param_type = get_ast_type(param) - if (!in_function_param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "copy_construct", vector(param_type->clone_with_indirection(1))))) { + if (!in_function_param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "copy_construct", vec(param_type->clone_with_indirection(1))))) { var temp_ident = ast_identifier_ptr("temporary_param_boom", param_type->clone_without_ref(), replace_in) add_to_scope("temporary_param_boom", temp_ident, replace_in) add_to_scope("~enclosing_scope", replace_in, temp_ident) var declaration = ast_declaration_statement_ptr(temp_ident, null()) - var copy_in = make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(param)))) + var copy_in = make_method_call(temp_ident, "copy_construct", vec(make_operator_call("&", vec(param)))) add_before_in(declaration, replace_before, replace_in) add_before_in(copy_in, replace_before, replace_in) backing.parameters[i] = temp_ident @@ -220,8 +220,8 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ add_to_scope("~enclosing_scope", replace_in, temp_return) var declaration = ast_declaration_statement_ptr(temp_return, node) add_before_in(declaration, replace_before, replace_in) - if (has_method(func_return_type->type_def, "destruct", vector<*type>())) { - add_before_in(ast_defer_statement_ptr(make_method_call(temp_return, "destruct", vector<*ast_node>())), + if (has_method(func_return_type->type_def, "destruct", vec<*type>())) { + add_before_in(ast_defer_statement_ptr(make_method_call(temp_return, "destruct", vec<*ast_node>())), replace_before, replace_in) } replace_with_in(node, temp_return, parent_chain) @@ -237,12 +237,12 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ var order = 0; backing.parameters.for_each(fun(param: *ast_node) { var param_type = get_ast_type(param) - if (!param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "destruct", vector<*type>()))) { + if (!param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "destruct", vec<*type>()))) { // the first pass ensures a code_block child if (!is_code_block(backing.body_statement)) error("BUT WHY") backing.body_statement->code_block.children.add(order++, - ast_defer_statement_ptr(make_method_call(param, "destruct", vector<*ast_node>()))) + ast_defer_statement_ptr(make_method_call(param, "destruct", vec<*ast_node>()))) } }) } @@ -251,20 +251,20 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ if (is_translation_unit(parent_chain->top()) || is_type_def(parent_chain->top())) return; if (!ident_type->is_ref && ident_type->indirection == 0 && ident_type->is_object()) { - if (backing.expression && has_method(ident_type->type_def, "copy_construct", vector(get_ast_type(backing.expression)->clone_with_increased_indirection()))) { + if (backing.expression && has_method(ident_type->type_def, "copy_construct", vec(get_ast_type(backing.expression)->clone_with_increased_indirection()))) { var temp_cpy_ctst = ast_identifier_ptr("temp_declaration_copy_construct", get_ast_type(backing.expression)->clone_without_ref(), parent_chain->top()) add_to_scope("temp_declaration_copy_construct", temp_cpy_ctst, parent_chain->top()) add_to_scope("~enclosing_scope", parent_chain->top(), temp_cpy_ctst) var declaration = ast_declaration_statement_ptr(temp_cpy_ctst, backing.expression) - add_after_in(make_method_call(backing.identifier, "copy_construct", vector(make_operator_call("&", vector(temp_cpy_ctst)))), + add_after_in(make_method_call(backing.identifier, "copy_construct", vec(make_operator_call("&", vec(temp_cpy_ctst)))), node, parent_chain->top()) // do second so the order's right add_after_in(declaration, node, parent_chain->top()) backing.expression = null() } - if (has_method(ident_type->type_def, "destruct", vector<*type>())) { - add_after_in(ast_defer_statement_ptr(make_method_call(backing.identifier, "destruct", vector<*ast_node>())), + if (has_method(ident_type->type_def, "destruct", vec<*type>())) { + add_after_in(ast_defer_statement_ptr(make_method_call(backing.identifier, "destruct", vec<*ast_node>())), node, parent_chain->top()) } } @@ -283,7 +283,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ error("this would by unusual") if (return_value) { if (get_ast_type(enclosing_function)->return_type->is_ref) - return_value = make_operator_call("&", vector(return_value)) + return_value = make_operator_call("&", vec(return_value)) var temp_return = ast_identifier_ptr("temp_boom_return", get_ast_type(return_value)->clone_without_ref(), block) add_to_scope("temp_boom_return", temp_return, block) add_to_scope("~enclosing_scope", block, temp_return) @@ -294,7 +294,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ // dereference so that the real ref can take it back if (get_ast_type(enclosing_function)->return_type->is_ref) - temp_return = make_operator_call("*", vector(temp_return)) + temp_return = make_operator_call("*", vec(temp_return)) backing.return_value = temp_return } } @@ -335,7 +335,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ add_to_scope("~enclosing_scope", method, this_ident) method->function.type->parameter_types.add(0, this_type) }) - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { // Pass 4 var unmethod: fun(*ast_node,*stack<*ast_node>,*hash_set<*ast_node>): bool = fun(node: *ast_node, parent_chain: *stack<*ast_node>, visited: *hash_set<*ast_node>): bool { match(*node) { @@ -344,7 +344,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ if (is_dot_style_method_call(node)) { var this_ident = backing.func->function_call.parameters[0] if (backing.func->function_call.func->function.name == ".") - this_ident = make_operator_call("&", vector(this_ident)) + this_ident = make_operator_call("&", vec(this_ident)) backing.func = backing.func->function_call.parameters[1] backing.parameters.add(0, this_ident) } else { @@ -372,7 +372,7 @@ fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_ var this_ident = parent_chain->item_from_top_satisfying(fun(i: *ast_node): bool { return is_function(i) && fun_to_obj.get_with_default(i, null()) == enclosing_obj })->function.parameters[0] - replace_with_in(node, make_operator_call("->", vector(this_ident, node)), parent_chain) + replace_with_in(node, make_operator_call("->", vec(this_ident, node)), parent_chain) } } } diff --git a/stdlib/os.krak b/stdlib/os.krak index 0f8033f..aaa5a10 100644 --- a/stdlib/os.krak +++ b/stdlib/os.krak @@ -1,8 +1,8 @@ -import string:* +import str:* import mem:* import io:* -fun system(call_string: string):int { +fun system(call_string: str):int { var c_call_string = call_string.toCharArray() var result = system(c_call_string) delete(c_call_string) @@ -14,7 +14,7 @@ fun exit() exit(0) ext fun popen(command: *char, mode: *char): *void ext fun pclose(file: *void): int -fun from_system_command(command: string, line_size: int): string { +fun from_system_command(command: str, line_size: int): str { var command_string = command.toCharArray() defer delete(command_string) var p = popen(command_string, "r") @@ -22,10 +22,10 @@ fun from_system_command(command: string, line_size: int): string { pclose(p) return to_ret } -fun get_time(): long { return string_to_num(from_system_command(string("date +%s"), 50)); } +fun get_time(): long { return string_to_num(from_system_command(str("date +%s"), 50)); } fun split(time: long, split_label: *char): long { var new_time = get_time() - print(string(split_label) + ": ") + print(str(split_label) + ": ") println(new_time - time) return new_time } diff --git a/stdlib/parser.krak b/stdlib/parser.krak index acf468e..e8b0dcb 100644 --- a/stdlib/parser.krak +++ b/stdlib/parser.krak @@ -2,25 +2,25 @@ import grammer:* import symbol:* import lexer:* import tree:* -import vector:* +import vec:* import stack:* import map:* import hash_map:* import util:* -import string:* +import str:* import mem:* import io:* obj parser (Object) { - var input: vector + var input: vec var gram: *grammer var lex: *lexer var gss: gss var to_reduce: stack var to_shift: stack< pair<*tree, int> > - var SPPFStepNodes: vector< pair<*tree, int> > + var SPPFStepNodes: vec< pair<*tree, int> > var packed_map: map<*tree, bool> - var reduces_to_null_map: map, bool> + var reduces_to_null_map: map, bool> fun construct(grammerIn: *grammer, lexIn: *lexer): *parser { input.construct() @@ -63,7 +63,7 @@ obj parser (Object) { reduces_to_null_map.destruct() } - fun parse_input(inputStr: string, name: string): *tree { + fun parse_input(inputStr: str, name: str): *tree { input.clear() gss.clear() to_reduce.clear() @@ -144,7 +144,7 @@ obj parser (Object) { fun reducer(i: int) { var curr_reduction = to_reduce.pop() gss.get_reachable_paths(curr_reduction.from, max(0, curr_reduction.length-1)). - for_each(fun(path: ref vector<*tree>) { + for_each(fun(path: ref vec<*tree>) { var path_edges = range(path.size-1).map(fun(indx: int): *tree { return gss.get_edge(path[indx], path[indx+1]);}).reverse() if (curr_reduction.length != 0) { path_edges.addEnd(curr_reduction.label) @@ -262,7 +262,7 @@ obj parser (Object) { } to_shift = next_shifts } - fun add_children(parent: *tree, children: vector<*tree>, nullable_parts: *tree) { + fun add_children(parent: *tree, children: vec<*tree>, nullable_parts: *tree) { if (nullable_parts) children.add(nullable_parts) if (!belongs_to_family(parent, children)) { @@ -285,7 +285,7 @@ obj parser (Object) { } } } - fun belongs_to_family(node: *tree, nodes: vector<*tree>): bool { + fun belongs_to_family(node: *tree, nodes: vec<*tree>): bool { for (var i = 0; i < nodes.size; i++;) { var contains_one = false for (var j = 0; j < node->children.size; j++;) { @@ -300,7 +300,7 @@ obj parser (Object) { } return true } - fun are_packed(nodes: vector<*tree>): bool { + fun are_packed(nodes: vec<*tree>): bool { return nodes.any_true(fun(it: *tree):bool { return packed_map.contains_key(it) && packed_map[it]; }) } fun set_packed(node: *tree, packed: bool) { @@ -330,7 +330,7 @@ obj parser (Object) { } obj gss (Object) { - var data: vector>> + var data: vec>> var edges: hash_map< pair<*tree, *tree>, *tree > fun construct(): *gss { @@ -347,7 +347,7 @@ obj gss (Object) { edges.destruct() } fun clear() { - data.for_each(fun(second: ref vector<*tree>) second.for_each(fun(node: *tree) delete(node););) + data.for_each(fun(second: ref vec<*tree>) second.for_each(fun(node: *tree) delete(node););) data.clear() edges.clear() } @@ -356,7 +356,7 @@ obj gss (Object) { } fun add_to_frontier(frontier: int, node: *tree) { while(data.size <= frontier) - data.addEnd(vector<*tree>()) + data.addEnd(vec<*tree>()) data[frontier].addEnd(node) } fun frontier_is_empty(frontier: int): bool { @@ -391,9 +391,9 @@ obj gss (Object) { return i return -1 } - fun get_reachable_paths(start: *tree, length: int): vector>> { - var paths = vector>>() - var recursive_path_find: fun(*tree, int, vector<*tree>):void = fun(start: *tree, length: int, current_path: vector<*tree>) { + fun get_reachable_paths(start: *tree, length: int): vec>> { + var paths = vec>>() + var recursive_path_find: fun(*tree, int, vec<*tree>):void = fun(start: *tree, length: int, current_path: vec<*tree>) { current_path.addEnd(start) if (!length) { paths.addEnd(current_path) @@ -403,7 +403,7 @@ obj gss (Object) { recursive_path_find(child, length-1, current_path) }) } - recursive_path_find(start, length, vector<*tree>()) + recursive_path_find(start, length, vec<*tree>()) return paths } } @@ -453,14 +453,14 @@ obj reduction (Object) { } } -fun syntax_tree_to_dot(root: *tree): string { - var ret = string("digraph Kaken {\n") +fun syntax_tree_to_dot(root: *tree): str { + var ret = str("digraph Kaken {\n") var counter = 0 - var node_name_map = map<*tree, string>() - var get_name = fun(node: *tree): string { + var node_name_map = map<*tree, str>() + var get_name = fun(node: *tree): str { if (node_name_map.contains_key(node)) return node_name_map[node] - var escaped = string("") + var escaped = str("") node->data.to_string().data.for_each(fun(c: char) { if (c != '"' && c != '\\') escaped += c @@ -478,7 +478,7 @@ fun syntax_tree_to_dot(root: *tree): string { node->children.for_each(fun(child: *tree) { if (!child) return; // where on earth does the null come from - ret += string("\"") + get_name(node) + "\" -> \"" + get_name(child) + "\"\n"; + ret += str("\"") + get_name(node) + "\" -> \"" + get_name(child) + "\"\n"; helper(child) }) } diff --git a/stdlib/pass_common.krak b/stdlib/pass_common.krak index b1bdabb..98e4b4c 100644 --- a/stdlib/pass_common.krak +++ b/stdlib/pass_common.krak @@ -1,9 +1,9 @@ import ast_nodes:* import mem:* import util:* -import vector:* +import vec:* import stack:* -import string:* +import str:* import hash_set:* fun get_first_terminal(source: *tree): *tree { @@ -15,8 +15,8 @@ fun get_first_terminal(source: *tree): *tree { return null>() return get_first_terminal(source->children.first()) } -fun error(source: *tree, message: *char) error(source, string(message)); -fun error(source: *tree, message: string) { +fun error(source: *tree, message: *char) error(source, str(message)); +fun error(source: *tree, message: str) { var first = get_first_terminal(source) if (first) error("***error |" + concat_symbol_tree(source) + "| *** " + first->data.source + ": " + first->data.position + " " + message) @@ -36,20 +36,20 @@ fun is_dot_style_method_call(node: *ast_node): bool { is_function(node->function_call.func->function_call.func) && (node->function_call.func->function_call.func->function.name == "->" || node->function_call.func->function_call.func->function.name == ".") && is_function(node->function_call.func->function_call.parameters[1]) && - (is_type_def(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(string("~enclosing_scope"))[0]) || + (is_type_def(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(str("~enclosing_scope"))[0]) || // or if it's a templated method (yes, this has gotten uuuuugly) - is_type_def(get_ast_scope(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(string("~enclosing_scope"))[0])->get(string("~enclosing_scope"))[0]) || + is_type_def(get_ast_scope(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(str("~enclosing_scope"))[0])->get(str("~enclosing_scope"))[0]) || // or it's in an adt - is_adt_def(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(string("~enclosing_scope"))[0])) + is_adt_def(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(str("~enclosing_scope"))[0])) // should get uglier when we have to figure out if it's just an inside lambda } -fun function_satisfies_params(node: *ast_node, param_types: vector<*type>): bool { +fun function_satisfies_params(node: *ast_node, param_types: vec<*type>): bool { var func_type = get_ast_type(node) var func_param_types = func_type->parameter_types if (!func_type->is_variadic && func_param_types.size != param_types.size) { - var param_string = string() + var param_string = str() param_types.for_each(fun(t: *type) param_string += t->to_string() + ", ";) - /*println(string("type sizes don't match ") + param_types.size + " with needed " + param_string)*/ + /*println(str("type sizes don't match ") + param_types.size + " with needed " + param_string)*/ return false } else if (param_types.size < func_param_types.size) { return false @@ -59,16 +59,16 @@ fun function_satisfies_params(node: *ast_node, param_types: vector<*type>): bool for (var j = 0; j < func_param_types.size; j++;) { // don't care about references if (!func_param_types[j]->equality(param_types[j], false)) { - /*println(string("types don't match ") + func_param_types[j]->to_string() + " with needed " + param_types[j]->to_string())*/ + /*println(str("types don't match ") + func_param_types[j]->to_string() + " with needed " + param_types[j]->to_string())*/ if (func_param_types[j]->to_string() == param_types[j]->to_string()) - error(string("types aren't equal, but their string rep is (and ref doesn't even matter): ") + func_param_types[j]->to_string() + " vs " + param_types[j]->to_string() ) + error(str("types aren't equal, but their str rep is (and ref doesn't even matter): ") + func_param_types[j]->to_string() + " vs " + param_types[j]->to_string() ) return false } } return true } -fun access_expression(left: *ast_node, right: *char): *ast_node return access_expression(left, string(right)) -fun access_expression(left: *ast_node, right: ref string): *ast_node { +fun access_expression(left: *ast_node, right: *char): *ast_node return access_expression(left, str(right)) +fun access_expression(left: *ast_node, right: ref str): *ast_node { var ltype = get_ast_type(left) if (!ltype->is_object()) error("Trying to generate an access expression and the left side is not an object") @@ -77,10 +77,10 @@ fun access_expression(left: *ast_node, right: ref string): *ast_node { error("Trying to generate an access expression, can't find right: " + right) if (ltype->indirection) - return make_operator_call("->", vector(left, ident)) - return make_operator_call(".", vector(left, ident)) + return make_operator_call("->", vec(left, ident)) + return make_operator_call(".", vec(left, ident)) } -fun function_lookup(name: string, scope: *ast_node, param_types: vector<*type>): *ast_node { +fun function_lookup(name: str, scope: *ast_node, param_types: vec<*type>): *ast_node { var results = scope_lookup(name, scope) for (var i = 0; i < results.size; i++;) { if ((is_function(results[i]) || (is_identifier(results[i]) && get_ast_type(results[i])->is_function())) && function_satisfies_params(results[i], param_types)) { @@ -89,22 +89,22 @@ fun function_lookup(name: string, scope: *ast_node, param_types: vector<*type>): } return null() } -fun identifier_lookup(name: ref string, scope: *ast_node): *ast_node { - /*println(string("doing identifier lookup for: ") + name)*/ +fun identifier_lookup(name: ref str, scope: *ast_node): *ast_node { + /*println(str("doing identifier lookup for: ") + name)*/ var results = scope_lookup(name, scope) if (!results.size) { - /*println(string("identifier lookup failed for ") + name)*/ + /*println(str("identifier lookup failed for ") + name)*/ return null() } return results[0] } -fun scope_lookup(name: ref string, scope: *ast_node): vector<*ast_node> { +fun scope_lookup(name: ref str, scope: *ast_node): vec<*ast_node> { // println("*****Doing a name lookup for*****") // println(name) - var results = vector(scope) - name.split("::").for_each(fun(i: string) { - // println(string("based on split, looking up: ") + i) - var next_results = vector<*ast_node>() + var results = vec(scope) + name.split("::").for_each(fun(i: str) { + // println(str("based on split, looking up: ") + i) + var next_results = vec<*ast_node>() results.for_each(fun(s: *ast_node) { // print("looking in scope: ") // println(s) @@ -117,12 +117,12 @@ fun scope_lookup(name: ref string, scope: *ast_node): vector<*ast_node> { }) return results } -fun scope_lookup_helper(name: ref string, scope: *ast_node, visited: set<*ast_node>): vector<*ast_node> { +fun scope_lookup_helper(name: ref str, scope: *ast_node, visited: set<*ast_node>): vec<*ast_node> { // need to do properly scopded lookups // print("scope is: ") - // get_ast_scope(scope)->for_each(fun(key: string, value: vector<*ast_node>) print(key + " ");) + // get_ast_scope(scope)->for_each(fun(key: str, value: vec<*ast_node>) print(key + " ");) // println() - var results = vector<*ast_node>() + var results = vec<*ast_node>() // prevent re-checking the same one... if (visited.contains(scope)) return results @@ -131,8 +131,8 @@ fun scope_lookup_helper(name: ref string, scope: *ast_node, visited: set<*ast_no // println(name + " is in scope, adding to results") results += get_ast_scope(scope)->get(name) } - if (get_ast_scope(scope)->contains_key(string("~enclosing_scope"))) - results += scope_lookup_helper(name, get_ast_scope(scope)->get(string("~enclosing_scope"))[0], visited) + if (get_ast_scope(scope)->contains_key(str("~enclosing_scope"))) + results += scope_lookup_helper(name, get_ast_scope(scope)->get(str("~enclosing_scope"))[0], visited) if (is_translation_unit(scope)) { scope->translation_unit.children.for_each(fun(child: *ast_node) { if (is_import(child)) { @@ -145,98 +145,98 @@ fun scope_lookup_helper(name: ref string, scope: *ast_node, visited: set<*ast_no } else { // println(name + " is not imported (this time)") // print("import imports") - // child->import.imported.for_each(fun(it: string) print(it + " ");) + // child->import.imported.for_each(fun(it: str) print(it + " ");) } } }) } return results } -fun has_method(object: *ast_node, name: *char, parameter_types: vector<*type>): bool return has_method(object, string(name), parameter_types); -fun has_method(object: *ast_node, name: string, parameter_types: vector<*type>): bool { +fun has_method(object: *ast_node, name: *char, parameter_types: vec<*type>): bool return has_method(object, str(name), parameter_types); +fun has_method(object: *ast_node, name: str, parameter_types: vec<*type>): bool { var to_ret = function_lookup(name, object, parameter_types) || false return to_ret } fun get_from_scope(node: *ast_node, member: *char): *ast_node - return get_from_scope(node, string(member)) -fun get_from_scope(node: *ast_node, member: string): *ast_node { + return get_from_scope(node, str(member)) +fun get_from_scope(node: *ast_node, member: str): *ast_node { /*if (get_ast_scope(node)->contains(member))*/ return get_ast_scope(node)->get(member).first() /*return null()*/ } -fun make_method_call(object_ident: *ast_node, name: *char, parameters: vector<*ast_node>): *ast_node return make_method_call(object_ident, string(name), parameters); -fun make_method_call(object_ident: *ast_node, name: string, parameters: vector<*ast_node>): *ast_node { +fun make_method_call(object_ident: *ast_node, name: *char, parameters: vec<*ast_node>): *ast_node return make_method_call(object_ident, str(name), parameters); +fun make_method_call(object_ident: *ast_node, name: str, parameters: vec<*ast_node>): *ast_node { // note that this type_def is the adt_def if this is an adt type var method = function_lookup(name, get_ast_type(object_ident)->type_def, parameters.map(fun(param: *ast_node): *type return get_ast_type(param);)) return make_method_call(object_ident, method, parameters) } -fun make_method_call(object_ident: *ast_node, method: *ast_node, parameters: vector<*ast_node>): *ast_node { +fun make_method_call(object_ident: *ast_node, method: *ast_node, parameters: vec<*ast_node>): *ast_node { var access_op = "." if (get_ast_type(object_ident)->indirection) access_op = "->" - var method_access = ast_function_call_ptr(get_builtin_function(string(access_op), vector(get_ast_type(object_ident), get_ast_type(method))), vector(object_ident, method)) + var method_access = ast_function_call_ptr(get_builtin_function(str(access_op), vec(get_ast_type(object_ident), get_ast_type(method))), vec(object_ident, method)) return ast_function_call_ptr(method_access, parameters) } -fun make_operator_call(func: *char, params: vector<*ast_node>): *ast_node return make_operator_call(string(func), params); -fun make_operator_call(func: string, params: vector<*ast_node>): *ast_node { +fun make_operator_call(func: *char, params: vec<*ast_node>): *ast_node return make_operator_call(str(func), params); +fun make_operator_call(func: str, params: vec<*ast_node>): *ast_node { return ast_function_call_ptr(get_builtin_function(func, params.map(fun(p:*ast_node): *type return get_ast_type(p);)), params) } -fun get_builtin_function(name: *char, param_types: vector<*type>): *ast_node - return get_builtin_function(string(name), param_types, null>()) -fun get_builtin_function(name: string, param_types: vector<*type>): *ast_node +fun get_builtin_function(name: *char, param_types: vec<*type>): *ast_node + return get_builtin_function(str(name), param_types, null>()) +fun get_builtin_function(name: str, param_types: vec<*type>): *ast_node return get_builtin_function(name, param_types, null>()) -fun get_builtin_function(name: string, param_types: vector<*type>, syntax: *tree): *ast_node { +fun get_builtin_function(name: str, param_types: vec<*type>, syntax: *tree): *ast_node { // none of the builtin functions should take in references param_types = param_types.map(fun(t: *type): *type return t->clone_without_ref();) if (name == "==" || name == "!=" || name == ">" || name == "<" || name == "<=" || name == ">" || name == ">=" || name == "&&" || name == "||" || name == "!") - return ast_function_ptr(name, type_ptr(param_types, type_ptr(base_type::boolean()), 0, false, false, true), vector<*ast_node>(), false) + return ast_function_ptr(name, type_ptr(param_types, type_ptr(base_type::boolean()), 0, false, false, true), vec<*ast_node>(), false) if (name == "." || name == "->") { if (name == "->" && param_types[0]->indirection == 0) - error(syntax, string("drereferencing not a pointer: ") + name) + error(syntax, str("drereferencing not a pointer: ") + name) else if (name == "." && param_types[0]->indirection != 0) - error(syntax, string("dot operator on a pointer: ") + name) + error(syntax, str("dot operator on a pointer: ") + name) else - return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, true), vector<*ast_node>(), false) + return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, true), vec<*ast_node>(), false) } if (name == "[]") { if (param_types[0]->indirection == 0) - error(syntax, string("drereferencing not a pointer: ") + name) + error(syntax, str("drereferencing not a pointer: ") + name) else - return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, true), vector<*ast_node>(), false) + return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, true), vec<*ast_node>(), false) } if (name == "&" && param_types.size == 1) - return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_increased_indirection(), 0, false, false, true), vector<*ast_node>(), false) + return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_increased_indirection(), 0, false, false, true), vec<*ast_node>(), false) if (name == "*" && param_types.size == 1) { if (param_types[0]->indirection == 0) - error(syntax, string("drereferencing not a pointer: ") + name) + error(syntax, str("drereferencing not a pointer: ") + name) else - return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, true), vector<*ast_node>(), false) + return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, true), vec<*ast_node>(), false) } if (param_types.size > 1 && param_types[1]->rank() > param_types[0]->rank()) - return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, true), vector<*ast_node>(), false) - return ast_function_ptr(name, type_ptr(param_types, param_types[0], 0, false, false, true), vector<*ast_node>(), false) + return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, true), vec<*ast_node>(), false) + return ast_function_ptr(name, type_ptr(param_types, param_types[0], 0, false, false, true), vec<*ast_node>(), false) } fun possible_object_equality(lvalue: *ast_node, rvalue: *ast_node): *ast_node { var ltype = get_ast_type(lvalue) var rtype = get_ast_type(rvalue) - if (ltype->indirection == 0 && (ltype->is_object() && has_method(ltype->type_def, "operator==", vector(rtype)))) { - return make_method_call(lvalue, "operator==", vector(rvalue)) + if (ltype->indirection == 0 && (ltype->is_object() && has_method(ltype->type_def, "operator==", vec(rtype)))) { + return make_method_call(lvalue, "operator==", vec(rvalue)) } else if (ltype->is_object()) // return false if object but no operator== (right now don't try for templated) - return ast_value_ptr(string("false"), type_ptr(base_type::boolean())) - return make_operator_call("==", vector(lvalue, rvalue)) + return ast_value_ptr(str("false"), type_ptr(base_type::boolean())) + return make_operator_call("==", vec(lvalue, rvalue)) } -fun concat_symbol_tree(node: *tree): string { - var str.construct(): string +fun concat_symbol_tree(node: *tree): str { + var str.construct(): str if (node->data.data != "no_value") str += node->data.data node->children.for_each(fun(child: *tree) str += concat_symbol_tree(child);) return str } fun get_node(lookup: *char, parent: *tree): *tree { - return get_node(string(lookup), parent) + return get_node(str(lookup), parent) } -fun get_node(lookup: string, parent: *tree): *tree { +fun get_node(lookup: str, parent: *tree): *tree { var results = get_nodes(lookup, parent) if (results.size > 1) error(parent, "get node too many results!") @@ -244,32 +244,32 @@ fun get_node(lookup: string, parent: *tree): *tree { return results[0] return null>() } -fun get_nodes(lookup: *char, parent: *tree): vector<*tree> { - return get_nodes(string(lookup), parent) +fun get_nodes(lookup: *char, parent: *tree): vec<*tree> { + return get_nodes(str(lookup), parent) } -fun get_nodes(lookup: string, parent: *tree): vector<*tree> { +fun get_nodes(lookup: str, parent: *tree): vec<*tree> { return parent->children.filter(fun(node: *tree):bool return node->data.name == lookup;) } fun add_to_scope(name: *char, to_add: *ast_node, add_to: *ast_node) { - add_to_scope(string(name), to_add, add_to) + add_to_scope(str(name), to_add, add_to) } -fun add_to_scope(name: string, to_add: *ast_node, add_to: *ast_node) { +fun add_to_scope(name: str, to_add: *ast_node, add_to: *ast_node) { var add_to_map = get_ast_scope(add_to) if (add_to_map->contains_key(name)) (*add_to_map)[name].add(to_add) else - add_to_map->set(name, vector(to_add)) + add_to_map->set(name, vec(to_add)) } // for now, needs source to already be in a variable for copy_constructing fun assign_or_copy_construct_statement(lvalue: *ast_node, rvalue: *ast_node): *ast_node { var ltype = get_ast_type(lvalue) - if (ltype->indirection == 0 && (ltype->is_object() && has_method(ltype->type_def, "copy_construct", vector(ltype->clone_with_increased_indirection())))) - return make_method_call(lvalue, "copy_construct", vector(make_operator_call("&", vector(rvalue)))) + if (ltype->indirection == 0 && (ltype->is_object() && has_method(ltype->type_def, "copy_construct", vec(ltype->clone_with_increased_indirection())))) + return make_method_call(lvalue, "copy_construct", vec(make_operator_call("&", vec(rvalue)))) return ast_assignment_statement_ptr(lvalue, rvalue) } -fun get_children_pointer(node: *ast_node): *vector<*ast_node> { - var bc = null>() +fun get_children_pointer(node: *ast_node): *vec<*ast_node> { + var bc = null>() match(*node) { ast_node::translation_unit(backing) bc = &node->translation_unit.children ast_node::code_block(backing) bc = &node->code_block.children @@ -288,7 +288,7 @@ fun remove(orig: *ast_node, in: *ast_node): *ast_node { return temp } } - error(string("cannot remove inside ") + get_ast_name(in)) + error(str("cannot remove inside ") + get_ast_name(in)) } fun replace_with_in(orig: *ast_node, new: *ast_node, in: *stack<*ast_node>) replace_with_in(orig, new, in->top()) @@ -385,12 +385,12 @@ fun replace_with_in(orig: *ast_node, new: *ast_node, in: *ast_node) { return } } - error(string("cannot replace_with_in inside ") + get_ast_name(in)) + error(str("cannot replace_with_in inside ") + get_ast_name(in)) } -fun add_before_in(to_add: ref vector<*ast_node>, before: *ast_node, in: *stack<*ast_node>) +fun add_before_in(to_add: ref vec<*ast_node>, before: *ast_node, in: *stack<*ast_node>) to_add.for_each(fun(n: *ast_node) add_before_in(n, before, in);) -fun add_before_in(to_add: vector<*ast_node>, before: *ast_node, in: *ast_node) +fun add_before_in(to_add: vec<*ast_node>, before: *ast_node, in: *ast_node) to_add.for_each(fun(n: *ast_node) add_before_in(n, before, in);) fun add_before_in(to_add: *ast_node, before: *ast_node, in: *stack<*ast_node>) @@ -404,7 +404,7 @@ fun add_before_in(to_add: *ast_node, before: *ast_node, in: *ast_node) { return } } - error(string("cannot add_before_in to ") + get_ast_name(in)) + error(str("cannot add_before_in to ") + get_ast_name(in)) } fun add_after_in(to_add: *ast_node, before: *ast_node, in: *stack<*ast_node>) add_after_in(to_add, before, in->top()) @@ -417,7 +417,7 @@ fun add_after_in(to_add: *ast_node, before: *ast_node, in: *ast_node) { return } } - error(string("cannot add_after_in to ") + get_ast_name(in)) + error(str("cannot add_after_in to ") + get_ast_name(in)) } fun empty_pass_first_half(): fun(*ast_node, *stack<*ast_node>, *hash_set<*ast_node>): bool { @@ -503,13 +503,13 @@ fun is_legal_parameter_node_type(n: *ast_node): bool { error("is_legal_parameter_node_type with no type") } -fun get_fully_scoped_name(n: *ast_node): string { +fun get_fully_scoped_name(n: *ast_node): str { if (!n) - return string("NULL") - var above = string() + return str("NULL") + var above = str() var scope_map = get_ast_scope(n); - if (scope_map && scope_map->contains_key(string("~enclosing_scope"))) - above = get_fully_scoped_name(scope_map->get(string("~enclosing_scope"))[0]) + if (scope_map && scope_map->contains_key(str("~enclosing_scope"))) + above = get_fully_scoped_name(scope_map->get(str("~enclosing_scope"))[0]) return above + "::" + get_ast_name(n) } diff --git a/stdlib/poset.krak b/stdlib/poset.krak index 305b1f7..39c7e8e 100644 --- a/stdlib/poset.krak +++ b/stdlib/poset.krak @@ -1,4 +1,4 @@ -import vector:* +import vec:* import queue:* import map:* import set:* @@ -44,8 +44,8 @@ obj poset (Object) { }) return depends_on } - fun get_sorted(): vector { - var sorted = vector() + fun get_sorted(): vec { + var sorted = vec() var to_do = queue() // because we're going to destructivly update var temp_adj_matrix = adj_matrix diff --git a/stdlib/queue.krak b/stdlib/queue.krak index ac87c02..0cc51d3 100644 --- a/stdlib/queue.krak +++ b/stdlib/queue.krak @@ -1,6 +1,6 @@ import stack import serialize -import vector +import vec fun queue() : queue { var out.construct() : queue @@ -32,11 +32,11 @@ obj queue (Object, Serializable) { stack2 = other.stack2 } - fun serialize() : vector::vector { + fun serialize() : vec::vec { return serialize::serialize(stack1)+serialize::serialize(stack2) } - fun unserialize(it : ref vector::vector, pos : int) : int { + fun unserialize(it : ref vec::vec, pos : int) : int { pos = stack1.unserialize(it,pos) pos = stack2.unserialize(it,pos) return pos diff --git a/stdlib/ref_lower.krak b/stdlib/ref_lower.krak index af9b1e4..d584a5b 100644 --- a/stdlib/ref_lower.krak +++ b/stdlib/ref_lower.krak @@ -1,11 +1,11 @@ import symbol:* import tree:* import map:* -import vector:* +import vec:* import set:* import hash_set:* import util:* -import string:* +import str:* import mem:* import io:* import ast_nodes:* @@ -37,12 +37,12 @@ fun remove_ref(t: *type) { } } -fun ref_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { - var remove_ref_type_set = set>() +fun ref_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { + var remove_ref_type_set = set>() var modify_reference_use_set = set>() var modify_return_set = set<*ast_node>() var visited = hash_set<*ast_node>() - name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { + name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree,*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { match(*node) { ast_node::identifier(backing) { @@ -62,7 +62,7 @@ fun ref_lower(name_ast_map: *map,*ast_node>>, ast_to_ var func_type_params = get_ast_type(backing.func)->parameter_types for (var i = 0; i < func_type_params.size; i++;) if (func_type_params[i]->is_ref) - backing.parameters[i] = make_operator_call("&", vector(backing.parameters[i])) + backing.parameters[i] = make_operator_call("&", vec(backing.parameters[i])) // add the function call to the modify_reference_use set if the function returns a ref if (get_ast_type(backing.func)->return_type->is_ref) modify_reference_use_set.add(make_pair(node, parent_chain->top())) @@ -76,7 +76,7 @@ fun ref_lower(name_ast_map: *map,*ast_node>>, ast_to_ } run_on_tree(helper_before, empty_pass_second_half(), syntax_ast_pair.second, &visited) }) - remove_ref_type_set.for_each(fun(p: pair) { + remove_ref_type_set.for_each(fun(p: pair) { var t = p.second remove_ref(t) }) @@ -89,9 +89,9 @@ fun ref_lower(name_ast_map: *map,*ast_node>>, ast_to_ // note that we definitly want to replace the type for unused parameters, but we don't want to add the * for paramters // in function declarations or the new identifier in declaration statements (but we do for expressions in declaration statements) if (!is_identifier(p.first) || (!is_function(p.second) && (!is_declaration_statement(p.second) || p.second->declaration_statement.identifier != p.first))) - replace_with_in(p.first, make_operator_call("*", vector(p.first)), p.second); + replace_with_in(p.first, make_operator_call("*", vec(p.first)), p.second); }) modify_return_set.for_each(fun(r: *ast_node) { - r->return_statement.return_value = make_operator_call("&", vector(r->return_statement.return_value)); + r->return_statement.return_value = make_operator_call("&", vec(r->return_statement.return_value)); }) } diff --git a/stdlib/regex.krak b/stdlib/regex.krak index 7908a3c..22e0ed9 100644 --- a/stdlib/regex.krak +++ b/stdlib/regex.krak @@ -1,17 +1,17 @@ import io -import string +import str import util -import vector -import string +import vec +import str import mem import set import util import serialize fun regex(in: *char):regex { - return regex(string::string(in)) + return regex(str::str(in)) } -fun regex(in: string::string):regex { +fun regex(in: str::str):regex { var out.construct(in):regex return out } @@ -52,7 +52,7 @@ obj regexState (Object) { fun destruct():void { next_states.destruct() } - fun match_char(input: char, states: ref vector::vector, flags: *vector::vector, num_states: *int) { + fun match_char(input: char, states: ref vec::vec, flags: *vec::vec, num_states: *int) { next_states.for_each(fun(it:int) { if (states[it].characterBegin <= input && input <= states[it].characterEnd) { (*flags)[it] = true @@ -60,16 +60,16 @@ obj regexState (Object) { } }) } - fun is_end(states: ref vector::vector):bool { + fun is_end(states: ref vec::vec):bool { return next_states.any_true(fun(state: int):bool { return states[state].characterBegin == 1; }) } } obj regex (Object, Serializable) { - var regexString: string::string - var states: vector::vector - var flagsA: vector::vector - var flagsB: vector::vector + var regexString: str::str + var states: vec::vec + var flagsA: vec::vec + var flagsB: vec::vec var is_straight_string: bool fun construct(): *regex { @@ -80,12 +80,12 @@ obj regex (Object, Serializable) { is_straight_string = false return this } - fun construct(regexStringIn: string::string): *regex { + fun construct(regexStringIn: str::str): *regex { regexString.copy_construct(®exStringIn) states.construct() is_straight_string = true for (var i = 0; i < regexString.length(); i++;) { - // simple implementation doesn't count escaped characters as straight string + // simple implementation doesn't count escaped characters as straight str if (regexString[i] == '\\' || regexString[i] == '(' || regexString[i] == ')' || regexString[i] == '[' || regexString[i] == '*' || regexString[i] == '+' || regexString[i] == '?' || regexString[i] == '|') { is_straight_string = false break @@ -117,10 +117,10 @@ obj regex (Object, Serializable) { flagsA.destruct() flagsB.destruct() } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(regexString) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { pos = regexString.unserialize(it, pos) states.construct() construct(regexString) @@ -138,7 +138,7 @@ obj regex (Object, Serializable) { copy_construct(&other) } - fun compile(regex_string: string::string): util::pair> { + fun compile(regex_string: str::str): util::pair> { /*io::println(regex_string)*/ var first = states.size; states.add(regexState()) var previous_begin = set::set() @@ -168,7 +168,7 @@ obj regex (Object, Serializable) { var perenEnd = i + 1 for (var depth = 1; depth > 0; perenEnd++;) { if (perenEnd >= regex_string.length()) - util::error(string::string("can't find matching peren in: ") + regex_string) + util::error(str::str("can't find matching peren in: ") + regex_string) // be careful, this isn't quite right yet /*var not_non_special = perenEnd == 0 || (regex_string[perenEnd-1] != '\\' && regex_string[perenEnd-1] != '[' && (perenEnd+1 >= regex_string.length() || regex_string[perenEnd+1] != ']'))*/ var not_non_special = perenEnd == 0 || (regex_string[perenEnd-1] != '[' && (perenEnd+1 >= regex_string.length() || regex_string[perenEnd+1] != ']')) @@ -223,8 +223,8 @@ obj regex (Object, Serializable) { return beginAndEnd } - fun long_match(to_match: *char): int { return long_match(string::string(to_match)); } - fun long_match(to_match: string::string): int return long_match(to_match.getBackingMemory(), 0, to_match.length()) + fun long_match(to_match: *char): int { return long_match(str::str(to_match)); } + fun long_match(to_match: str::str): int return long_match(to_match.getBackingMemory(), 0, to_match.length()) fun long_match(to_match: *char, position: int, end: int): int { if (is_straight_string) { if (regexString.length() > end-position) diff --git a/stdlib/serialize.krak b/stdlib/serialize.krak index d4b193b..58d7708 100644 --- a/stdlib/serialize.krak +++ b/stdlib/serialize.krak @@ -1,27 +1,27 @@ -import vector +import vec import mem import util -fun serialize(it: T): vector::vector { +fun serialize(it: T): vec::vec { return it.serialize() } -fun serialize(it: T): vector::vector { +fun serialize(it: T): vec::vec { var char_data = (&it) cast *char - var toRet = vector::vector() + var toRet = vec::vec() for (var i = 0; i < #sizeof; i++;) toRet.add(char_data[i]) return toRet } // dead simple wrapper for ease of use -fun unserialize(it: ref vector::vector): T { +fun unserialize(it: ref vec::vec): T { return unserialize(it, 0).first } -fun unserialize(it: ref vector::vector, pos: int): util::pair { +fun unserialize(it: ref vec::vec, pos: int): util::pair { return util::make_pair(*(it.getBackingMemory()+pos) cast *T, pos + (#sizeof) cast int) } -fun unserialize(it: ref vector::vector, pos: int): util::pair { +fun unserialize(it: ref vec::vec, pos: int): util::pair { var toRet: T pos = toRet.unserialize(it, pos) return util::make_pair(toRet, pos) diff --git a/stdlib/set.krak b/stdlib/set.krak index 40b87b1..c2b9e48 100644 --- a/stdlib/set.krak +++ b/stdlib/set.krak @@ -1,4 +1,4 @@ -import vector +import vec import io import serialize @@ -13,14 +13,14 @@ fun set(item: T): set { return toRet } -fun from_vector(items: vector::vector): set { +fun from_vector(items: vec::vec): set { var toRet.construct() : set items.for_each( fun(item: T) toRet.add(item); ) return toRet } obj set (Object, Serializable) { - var data: vector::vector + var data: vec::vec fun construct(): *set { data.construct() return this @@ -35,10 +35,10 @@ obj set (Object, Serializable) { fun operator=(rhs: ref set) { data = rhs.data } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(data) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { return data.unserialize(it, pos) } fun operator==(rhs: ref set): bool { @@ -87,7 +87,7 @@ obj set (Object, Serializable) { fun add(items: ref set) { items.for_each( fun(item: ref T) add(item); ) } - fun add(items: ref vector::vector) { + fun add(items: ref vec::vec) { items.for_each( fun(item: ref T) add(item); ) } fun remove(item: ref T) { diff --git a/stdlib/stack.krak b/stdlib/stack.krak index ed6f05d..9928639 100644 --- a/stdlib/stack.krak +++ b/stdlib/stack.krak @@ -1,8 +1,8 @@ -import vector +import vec import serialize import util -fun stack_from_vector(i: vector::vector): stack { +fun stack_from_vector(i: vec::vec): stack { var to_ret.construct(): stack to_ret.data = i return to_ret @@ -19,7 +19,7 @@ fun stack(in:T):stack { } obj stack (Object, Serializable) { - var data: vector::vector + var data: vec::vec fun construct(): *stack { data.construct() return this @@ -37,10 +37,10 @@ obj stack (Object, Serializable) { fun operator=(other: ref stack) { data = other.data } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(data) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { return data.unserialize(it, pos) } fun push(it: ref T) { @@ -81,7 +81,7 @@ obj stack (Object, Serializable) { fun for_each_reverse(func: fun(T):void) { data.for_each_reverse(func) } - fun reverse_vector(): vector::vector + fun reverse_vector(): vec::vec return data.reverse() fun index_from_top_satisfying(func_raw: run(T):bool): int { var temp_lambda = fun(i: T):bool { return func_raw(i); } diff --git a/stdlib/string.krak b/stdlib/str.krak similarity index 61% rename from stdlib/string.krak rename to stdlib/str.krak index 6003f12..a522252 100644 --- a/stdlib/string.krak +++ b/stdlib/str.krak @@ -1,43 +1,43 @@ -import vector +import vec import util import mem import serialize import io ext fun snprintf(to_str: *char, num: ulong, format: *char, ...): int -fun to_string(in: float): string +fun to_string(in: float): str return to_string((in) cast double) -fun to_string(in: double): string { +fun to_string(in: double): str { var how_much = snprintf(mem::null(), (0) cast ulong, "%f", in) var int_str = mem::new(how_much+2) snprintf(int_str, (how_much+1) cast ulong, "%f", in) - var to_ret = string(int_str) + var to_ret = str(int_str) mem::delete(int_str) return to_ret } -fun to_string(in: bool): string - if (in) return string("true") - else return string("false") -fun to_string(in: char): string - return string(in) -fun to_string(in: uchar): string +fun to_string(in: bool): str + if (in) return str("true") + else return str("false") +fun to_string(in: char): str + return str(in) +fun to_string(in: uchar): str return to_string_num(in) -fun to_string(in: short): string +fun to_string(in: short): str return to_string_num(in) -fun to_string(in: ushort): string +fun to_string(in: ushort): str return to_string_num(in) -fun to_string(in: int): string +fun to_string(in: int): str return to_string_num(in) -fun to_string(in: uint): string +fun to_string(in: uint): str return to_string_num(in) -fun to_string(in: long): string +fun to_string(in: long): str return to_string_num(in) -fun to_string(in: ulong): string +fun to_string(in: ulong): str return to_string_num(in) -fun to_string(in: *T): string - return string("ptr:<") + to_string_num((in) cast ulong) + ">" +fun to_string(in: *T): str + return str("ptr:<") + to_string_num((in) cast ulong) + ">" -fun string_to_num(it: string): T { +fun string_to_num(it: str): T { var is_negative = false if (it[0] == '-') { is_negative = true @@ -54,17 +54,17 @@ fun string_to_num(it: string): T { return -result return result } -fun string_to_double(it: string): double { +fun string_to_double(it: str): double { var parts = it.split('.') var divisor = 1 for (var i = 0; i < parts[1].length(); i++;) divisor *= 10 return string_to_num(parts[0]) + (string_to_num(parts[1])) cast double / divisor } -fun to_string_num(in: T): string { +fun to_string_num(in: T): str { if (in == 0) - return string("0") - var ret = string() + return str("0") + var ret = str() var pos = 0 var is_neg = false if (in > 0) { @@ -74,45 +74,45 @@ fun to_string_num(in: T): string { is_neg = true } while (pos) { - ret = string((pos%10 + '0') cast char) + ret + ret = str((pos%10 + '0') cast char) + ret pos = pos / 10 } if (is_neg) - return string("-") + ret + return str("-") + ret return ret } -fun operator+(first: *char, second: ref string): string { - return string(first) + second +fun operator+(first: *char, second: ref str): str { + return str(first) + second } -fun operator+(first: int, second: ref string): string { +fun operator+(first: int, second: ref str): str { return to_string(first) + second } -fun string(in:*char):string { - var out.construct(in):string +fun str(in:*char):str { + var out.construct(in):str return out } -fun string(in:char):string { - var out.construct():string +fun str(in:char):str { + var out.construct():str out += in return out } -fun string():string { - return string("") +fun str():str { + return str("") } -obj string (Object, Serializable, Hashable) { - var data: vector::vector; - fun construct(): *string { +obj str (Object, Serializable, Hashable) { + var data: vec::vec; + fun construct(): *str { data.construct(); return this; } - fun construct(ammt: int): *string { + fun construct(ammt: int): *str { data.construct(ammt); return this; } - fun construct(str: *char): *string { + fun construct(str: *char): *str { var len = 0 while (str[len] != 0) len++ data.construct(len); @@ -121,15 +121,15 @@ obj string (Object, Serializable, Hashable) { // no null terminator return this; } - fun construct(vec: ref vector::vector): *string { + fun construct(vec: ref vec::vec): *str { data.copy_construct(&vec); return this; } - fun construct(str: ref string): *string { + fun construct(str: ref str): *str { return construct(str.data); } - fun copy_construct(old: *string): void { + fun copy_construct(old: *str): void { data.copy_construct(&old->data) } @@ -149,7 +149,7 @@ obj string (Object, Serializable, Hashable) { construct(str) } - fun operator=(str: ref string): void { + fun operator=(str: ref str): void { data = str.data } @@ -157,16 +157,16 @@ obj string (Object, Serializable, Hashable) { data.destruct() } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(data) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { return data.unserialize(it, pos) } fun operator[](index: int): ref char { return data[index]; } - fun slice(first: int, second: int): string { - var new.construct(data.slice(first,second)): string + fun slice(first: int, second: int): str { + var new.construct(data.slice(first,second)): str return new } fun set(index: int, toSet: char) { @@ -174,9 +174,9 @@ obj string (Object, Serializable, Hashable) { } fun length():int { return data.size; } - fun operator!=(other: ref string): bool return !(*this ==other) + fun operator!=(other: ref str): bool return !(*this ==other) fun operator!=(other: *char): bool return !(*this==other) - fun operator==(other: ref string): bool { + fun operator==(other: ref str): bool { // you were too good for this world //return length() == other.length() && !util::range(length()).any_true(fun(i: int):bool { return data[i] != other[i]; } ) if (data.size != other.data.size) @@ -187,28 +187,28 @@ obj string (Object, Serializable, Hashable) { return true } fun operator==(other: *char): bool { - var str.construct(other) : string + var str.construct(other) : str return *this == str } - fun operator+(c: char): string { + fun operator+(c: char): str { var to_ret = *this to_ret += c return to_ret } - fun operator+(integer: int): string return *this + to_string(integer); - fun operator+(integer: long): string return *this + to_string(integer); - fun operator+(integer: ulong): string return *this + to_string(integer); - /*fun operator+(b: bool): string return *this + to_string(b);*/ + fun operator+(integer: int): str return *this + to_string(integer); + fun operator+(integer: long): str return *this + to_string(integer); + fun operator+(integer: ulong): str return *this + to_string(integer); + /*fun operator+(b: bool): str return *this + to_string(b);*/ - fun operator+(str: *char): string { - var newStr.construct(str):string - var ret.construct(data + newStr.data):string + fun operator+(str: *char): str { + var newStr.construct(str):str + var ret.construct(data + newStr.data):str return ret } - fun operator+(str: ref string): string { - var ret.construct(data + str.data):string + fun operator+(str: ref str): str { + var ret.construct(data + str.data):str return ret } @@ -221,12 +221,12 @@ obj string (Object, Serializable, Hashable) { } fun operator+=(str: *char): void { - var newStr.construct(str):string + var newStr.construct(str):str data += newStr.data } - fun operator+=(str: ref string): void { - //var newStr.construct(str):string + fun operator+=(str: ref str): void { + //var newStr.construct(str):str //data += newStr.data data += str.data } @@ -241,14 +241,14 @@ obj string (Object, Serializable, Hashable) { } fun getBackingMemory(): *char return data.getBackingMemory(); - fun split(delim: *char): vector::vector return split(string(delim)) - fun split(delim: string): vector::vector { - var out.construct(): vector::vector - var current = string("") + fun split(delim: *char): vec::vec return split(str(delim)) + fun split(delim: str): vec::vec { + var out.construct(): vec::vec + var current = str("") for (var i = 0; i < data.size; i++;) { if (i < data.size-delim.length() && slice(i, i+delim.length()) == delim) { out.add(current) - current = string("") + current = str("") i += delim.length()-1 } else { current += data[i] @@ -259,14 +259,14 @@ obj string (Object, Serializable, Hashable) { } fun first(): char return data.first() fun last(): char return data.last() - fun lines(): vector::vector return split('\n') - fun split(delim: char): vector::vector { - var out.construct(): vector::vector - var current = string("") + fun lines(): vec::vec return split('\n') + fun split(delim: char): vec::vec { + var out.construct(): vec::vec + var current = str("") for (var i = 0; i < data.size; i++;) { if (data[i] == delim) { out.add(current) - current = string("") + current = str("") } else { current += data[i] } @@ -274,7 +274,7 @@ obj string (Object, Serializable, Hashable) { out.add(current) return out } - fun join(to_join: ref vector::vector): string { + fun join(to_join: ref vec::vec): str { var to_ret = to_join.first() for (var i = 1; i < to_join.size; i++;) to_ret += *this + to_join[i] diff --git a/stdlib/symbol.krak b/stdlib/symbol.krak index 46bfdad..472f4a3 100644 --- a/stdlib/symbol.krak +++ b/stdlib/symbol.krak @@ -1,50 +1,50 @@ -import string +import str import serialize -import vector +import vec import util fun null_symbol(): symbol { - var toRet.construct(string::string("$NULL$"), false, string::string("$NULL$")): symbol + var toRet.construct(str::str("$NULL$"), false, str::str("$NULL$")): symbol return toRet } fun eof_symbol(): symbol { - var toRet.construct(string::string("$EOF$"), false, string::string("$EOF$")): symbol + var toRet.construct(str::str("$EOF$"), false, str::str("$EOF$")): symbol return toRet } fun invalid_symbol(): symbol { - var toRet.construct(string::string("$INVALID$"), false, string::string("$INVALID$")): symbol + var toRet.construct(str::str("$INVALID$"), false, str::str("$INVALID$")): symbol return toRet } fun symbol(nameIn: *char, terminalIn: bool): symbol { - var toRet.construct(string::string(nameIn), terminalIn, string::string("no_value")): symbol + var toRet.construct(str::str(nameIn), terminalIn, str::str("no_value")): symbol return toRet } -fun symbol(nameIn: ref string::string, terminalIn: bool): symbol { - var toRet.construct(nameIn, terminalIn, string::string("no_value")): symbol +fun symbol(nameIn: ref str::str, terminalIn: bool): symbol { + var toRet.construct(nameIn, terminalIn, str::str("no_value")): symbol return toRet } fun symbol(nameIn: *char, terminalIn: bool, dataIn: *char): symbol { - var toRet.construct(string::string(nameIn), terminalIn, string::string(dataIn)): symbol + var toRet.construct(str::str(nameIn), terminalIn, str::str(dataIn)): symbol return toRet } -fun symbol(nameIn: ref string::string, terminalIn: bool, dataIn: ref string::string): symbol return symbol(nameIn, terminalIn, dataIn, 0) +fun symbol(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str): symbol return symbol(nameIn, terminalIn, dataIn, 0) -fun symbol(nameIn: ref string::string, terminalIn: bool, dataIn: ref string::string, position: int): symbol { +fun symbol(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str, position: int): symbol { var toRet.construct(nameIn, terminalIn, dataIn): symbol toRet.position = position return toRet } obj symbol (Object, Serializable) { - var data: string::string - var name: string::string + var data: str::str + var name: str::str var terminal: bool - var source: string::string + var source: str::str var position: int fun construct(): *symbol { @@ -55,7 +55,7 @@ obj symbol (Object, Serializable) { position = 0 return this } - fun construct(nameIn: ref string::string, terminalIn: bool, dataIn: ref string::string): *symbol { + fun construct(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str): *symbol { name.construct(nameIn) terminal = terminalIn data.construct(dataIn) @@ -79,13 +79,13 @@ obj symbol (Object, Serializable) { destruct() copy_construct(&old) } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(data) + serialize::serialize(name) + serialize::serialize(terminal) + serialize::serialize(source) + serialize::serialize(position) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { /*construct()*/ - /*util::unpack(data, pos) = serialize::unserialize(it, pos)*/ - /*util::unpack(name, pos) = serialize::unserialize(it, pos)*/ + /*util::unpack(data, pos) = serialize::unserialize(it, pos)*/ + /*util::unpack(name, pos) = serialize::unserialize(it, pos)*/ pos = data.unserialize(it, pos) pos = name.unserialize(it, pos) util::unpack(terminal, pos) = serialize::unserialize(it, pos) @@ -102,7 +102,7 @@ obj symbol (Object, Serializable) { fun operator!=(other: ref symbol): bool { return !(*this == other); } - fun to_string(): string::string { + fun to_string(): str::str { var terminalString: *char if (terminal) terminalString = "true" diff --git a/stdlib/tree.krak b/stdlib/tree.krak index 6fc60a9..4a23941 100644 --- a/stdlib/tree.krak +++ b/stdlib/tree.krak @@ -1,9 +1,9 @@ import mem -import vector +import vec obj tree (Object) { var data: T - var children: vector::vector<*tree> + var children: vec::vec<*tree> fun construct(dataIn: T): *tree { mem::maybe_copy_construct(&data, &dataIn) children.construct() diff --git a/stdlib/type.krak b/stdlib/type.krak index 2e2875f..5ba07c4 100644 --- a/stdlib/type.krak +++ b/stdlib/type.krak @@ -1,6 +1,6 @@ import mem:* -import string:* -import vector:* +import str:* +import vec:* import set:* import ast_nodes:* import io:* @@ -31,8 +31,8 @@ adt base_type { fun type_ptr(): *type { return new()->construct() } -fun type_ptr(definition: *ast_node): *type return type_ptr(definition, set()); -fun type_ptr(definition: *ast_node, traits: set): *type { +fun type_ptr(definition: *ast_node): *type return type_ptr(definition, set()); +fun type_ptr(definition: *ast_node, traits: set): *type { return new()->construct(definition, traits) } fun type_ptr(base: base_type): *type return type_ptr(base, 0, false); @@ -40,22 +40,22 @@ fun type_ptr(base: base_type, indirection: int): *type return type_ptr(base, ind fun type_ptr(base: base_type, indirection: int, is_ref: bool): *type { return new()->construct(base, indirection, is_ref) } -fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int, is_ref: bool, is_variadic: bool, is_raw: bool): *type +fun type_ptr(parameters: vec<*type>, return_type: *type, indirection: int, is_ref: bool, is_variadic: bool, is_raw: bool): *type return new()->construct(parameters, return_type, indirection, is_ref, is_variadic, is_raw) -fun type_ptr(traits: set): *type { +fun type_ptr(traits: set): *type { return new()->construct(traits) } obj type (Object) { var base: base_type - var parameter_types: vector<*type> + var parameter_types: vec<*type> var is_variadic: bool var is_raw: bool var return_type: *type var indirection: int var type_def: *ast_node - var traits: set + var traits: set var is_ref: bool fun construct(): *type { base.copy_construct(&base_type::none()) @@ -69,7 +69,7 @@ obj type (Object) { is_raw = false return this } - fun construct(traits_in: set): *type { + fun construct(traits_in: set): *type { base.copy_construct(&base_type::template_type()) parameter_types.construct() indirection = 0 @@ -93,7 +93,7 @@ obj type (Object) { is_raw = false return this } - fun construct(type_def_in: *ast_node, traits_in: set): *type { + fun construct(type_def_in: *ast_node, traits_in: set): *type { base.copy_construct(&base_type::object()) parameter_types.construct() indirection = 0 @@ -105,7 +105,7 @@ obj type (Object) { is_raw = false return this } - fun construct(parameter_types_in: vector<*type>, return_type_in: *type, indirection_in: int, is_ref_in: bool, is_variadic_in: bool, is_raw_in: bool): *type { + fun construct(parameter_types_in: vec<*type>, return_type_in: *type, indirection_in: int, is_ref_in: bool, is_variadic_in: bool, is_raw_in: bool): *type { base.copy_construct(&base_type::function()) parameter_types.copy_construct(¶meter_types_in) return_type = return_type_in @@ -151,16 +151,16 @@ obj type (Object) { return base == other.base && deref_equality(return_type, other.return_type) && indirection == other.indirection && deref_equality(type_def, other.type_def) && traits == other.traits } - fun to_string(): string return to_string(true); - fun to_string(include_traits: bool): string { - var trait_string = string() + fun to_string(): str return to_string(true); + fun to_string(include_traits: bool): str { + var trait_string = str() if (include_traits) { - trait_string = string(":[") - traits.for_each(fun(t: string) trait_string += t;) + trait_string = str(":[") + traits.for_each(fun(t: str) trait_string += t;) trait_string += "] " } - var indr_string = string("") + var indr_string = str("") if (is_ref) indr_string += " ref " if (is_variadic) @@ -169,30 +169,30 @@ obj type (Object) { indr_string += " raw " for (var i = 0; i < indirection; i++;) indr_string += "*" match (base) { - base_type::none() return indr_string + string("none") + trait_string + base_type::none() return indr_string + str("none") + trait_string base_type::object() return indr_string + get_ast_name(type_def) + trait_string base_type::no_type_adt_option() return indr_string + "no_type_adt_option" + trait_string - base_type::template() return indr_string + string("template") + trait_string - base_type::template_type() return indr_string + string("template_type") + trait_string - base_type::void_return() return indr_string + string("void_return") + trait_string - base_type::boolean() return indr_string + string("boolean") + trait_string - base_type::character() return indr_string + string("character") + trait_string - base_type::short_int() return indr_string + string("short") + trait_string - base_type::integer() return indr_string + string("integer") + trait_string - base_type::long_int() return indr_string + string("long") + trait_string - base_type::ucharacter() return indr_string + string("ucharacter") + trait_string - base_type::ushort_int() return indr_string + string("ushort") + trait_string - base_type::uinteger() return indr_string + string("uinteger") + trait_string - base_type::ulong_int() return indr_string + string("ulong") + trait_string - base_type::floating() return indr_string + string("floating") + trait_string - base_type::double_precision() return indr_string + string("double_precision") + trait_string + base_type::template() return indr_string + str("template") + trait_string + base_type::template_type() return indr_string + str("template_type") + trait_string + base_type::void_return() return indr_string + str("void_return") + trait_string + base_type::boolean() return indr_string + str("boolean") + trait_string + base_type::character() return indr_string + str("character") + trait_string + base_type::short_int() return indr_string + str("short") + trait_string + base_type::integer() return indr_string + str("integer") + trait_string + base_type::long_int() return indr_string + str("long") + trait_string + base_type::ucharacter() return indr_string + str("ucharacter") + trait_string + base_type::ushort_int() return indr_string + str("ushort") + trait_string + base_type::uinteger() return indr_string + str("uinteger") + trait_string + base_type::ulong_int() return indr_string + str("ulong") + trait_string + base_type::floating() return indr_string + str("floating") + trait_string + base_type::double_precision() return indr_string + str("double_precision") + trait_string base_type::function() { - var temp = indr_string + string("fun(") + var temp = indr_string + str("fun(") parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + ", ";) return temp + ")" + return_type->to_string() + trait_string } } - return string("impossible type, indirection:") + indirection + return str("impossible type, indirection:") + indirection } fun rank(): int { if (indirection > 0) diff --git a/stdlib/util.krak b/stdlib/util.krak index ff416f2..ecea4a3 100644 --- a/stdlib/util.krak +++ b/stdlib/util.krak @@ -1,10 +1,10 @@ import mem import io import os -import string +import str import set import map -import vector +import vec import serialize @@ -14,8 +14,8 @@ fun do_nothing() {} fun is_object(): bool return false fun is_object(): bool return true -fun error(message: *char) error(string::string(message)); -fun error(message: string::string) { +fun error(message: *char) error(str::str(message)); +fun error(message: str::str) { io::printlnerr("****ERROR****") io::printlnerr(message) os::exit(-1) @@ -24,7 +24,7 @@ fun assert(works: bool, message: *char) { if (!works) error(message) } -fun assert(works: bool, message: string::string) { +fun assert(works: bool, message: str::str) { if (!works) error(message) } @@ -110,10 +110,10 @@ obj pair (Object, Serializable, Hashable) { mem::maybe_destruct(&first) mem::maybe_destruct(&second) } - fun serialize(): vector::vector { + fun serialize(): vec::vec { return serialize::serialize(first) + serialize::serialize(second) } - fun unserialize(it: ref vector::vector, pos: int): int { + fun unserialize(it: ref vec::vec, pos: int): int { // can't use unpack :( (b/c we can't make an already constructed empty one) var first_pair = serialize::unserialize(it, pos) var second_pair = serialize::unserialize(it, first_pair.second) @@ -158,8 +158,8 @@ obj range { for (var i = begin; i < end; i+= step;) func(i) } - fun map(func: fun(int): T): vector::vector { - var ret.construct( (end-begin)/step + 1 ) : vector::vector + fun map(func: fun(int): T): vec::vec { + var ret.construct( (end-begin)/step + 1 ) : vec::vec for (var i = begin; i < end; i+= step;) ret.addEnd(func(i)) return ret diff --git a/stdlib/vector.krak b/stdlib/vec.krak similarity index 83% rename from stdlib/vector.krak rename to stdlib/vec.krak index 3526c01..16daf7e 100644 --- a/stdlib/vector.krak +++ b/stdlib/vec.krak @@ -4,36 +4,36 @@ import io:*; import serialize:*; import util:*; -fun vector():vector { - var out.construct():vector +fun vec():vec { + var out.construct():vec return out } -fun vector(in:T):vector { - var out.construct():vector +fun vec(in:T):vec { + var out.construct():vec out.add(in) return out } -obj vector (Object, Serializable) { +obj vec (Object, Serializable) { var data: *T; var size: int; var available: int; - fun construct(): *vector { + fun construct(): *vec { size = 0; available = 8; data = new(8); return this; } - fun construct(ammt: int): *vector { + fun construct(ammt: int): *vec { size = 0; available = ammt; data = new(ammt); return this; } - fun copy_construct(old: *vector): void { + fun copy_construct(old: *vec): void { construct(old->available) size = old->size if (is_object()) { @@ -43,7 +43,7 @@ obj vector (Object, Serializable) { memmove((data) cast *void, (old->data) cast *void, size * #sizeof) } } - fun swap(other: ref vector) { + fun swap(other: ref vec) { var temp_data = data var temp_size = size var temp_available = available @@ -54,13 +54,13 @@ obj vector (Object, Serializable) { other.size = temp_size other.available = temp_available } - fun serialize(): vector { + fun serialize(): vec { var toRet = serialize(size) for (var i = 0; i < size; i++;) toRet += serialize(data[i]) return toRet } - fun unserialize(it: ref vector, pos: int): int { + fun unserialize(it: ref vec, pos: int): int { unpack(size, pos) = unserialize(it, pos) data = new(size) available = size @@ -83,7 +83,7 @@ obj vector (Object, Serializable) { size = s } - fun operator=(other:ref vector):void { + fun operator=(other:ref vec):void { if (size < other.size) { destruct() copy_construct(&other) @@ -99,16 +99,16 @@ obj vector (Object, Serializable) { } } - fun operator+(other: ref vector):vector { - var newVec.construct(size+other.size):vector + fun operator+(other: ref vec):vec { + var newVec.construct(size+other.size):vec for (var i = 0; i < size; i++;) newVec.addEnd(get(i)) for (var i = 0; i < other.size; i++;) newVec.addEnd(other.get(i)) return newVec } - fun operator+(other: ref T):vector { - var newVec.copy_construct(this):vector + fun operator+(other: ref T):vec { + var newVec.copy_construct(this):vec newVec.addEnd(other) return newVec } @@ -117,19 +117,19 @@ obj vector (Object, Serializable) { addEnd(other) } - fun operator+=(other: ref vector):void { + fun operator+=(other: ref vec):void { for (var i = 0; i < other.size; i++;) addEnd(other.get(i)) } - fun clone(): vector { - var newVec.construct(size): vector + fun clone(): vec { + var newVec.construct(size): vec for (var i = 0; i < size; i++;) newVec.addEnd(data[i]) return newVec } - fun reverse(): vector { - var newVec.construct(size): vector + fun reverse(): vec { + var newVec.construct(size): vec for (var i = 0; i < size; i++;) newVec.addEnd(data[(size-i)-1]) return newVec @@ -148,12 +148,12 @@ obj vector (Object, Serializable) { return true; } - fun slice(start: int, end: int): vector { + fun slice(start: int, end: int): vec { if (start < 0) start += size + 1 if (end < 0) end += size + 1 - var new.construct(end-start): vector + var new.construct(end-start): vec for (var i = start; i < end; i++;) new.add(data[i]) return new @@ -172,7 +172,7 @@ obj vector (Object, Serializable) { println("Vector access out of bounds! Retuning 0th element as sanest option"); print("Vector tried to access element: "); println(index); - print("Max Index of vector: "); + print("Max Index of vec: "); println(size-1); while(true) {} return data[0]; @@ -184,7 +184,7 @@ obj vector (Object, Serializable) { // This is a template for the interesting reason that structs // can not be compared for equality in C, and maybe we haven't defined equality - // on an object that we want to put in a vector. In this way we avoid the problem + // on an object that we want to put in a vec. In this way we avoid the problem // by not generating this function unless it's called - we also get the ability to // do a find index using a different type, which could be fun. fun find(value: ref U): int { @@ -199,7 +199,7 @@ obj vector (Object, Serializable) { } // yep - fun operator==(other: ref vector):bool { + fun operator==(other: ref vec):bool { if (size != other.size) return false for (var i = 0; i < size; i++;) @@ -213,7 +213,7 @@ obj vector (Object, Serializable) { return; data[index] = dataIn; } - fun add_all(dataIn: ref vector): void { + fun add_all(dataIn: ref vec): void { for (var i = 0; i < dataIn.size; i++;) addEnd(dataIn[i]); } @@ -283,20 +283,20 @@ obj vector (Object, Serializable) { for (var i = 0; i < size; i++;) data[i] = func(data[i]) } - fun map(func: fun(ref T):U):vector { - var newVec.construct(size): vector + fun map(func: fun(ref T):U):vec { + var newVec.construct(size): vec for (var i = 0; i < size; i++;) newVec.addEnd(func(data[i])) return newVec } - fun map(func: fun(T):U):vector { - var newVec.construct(size): vector + fun map(func: fun(T):U):vec { + var newVec.construct(size): vec for (var i = 0; i < size; i++;) newVec.addEnd(func(data[i])) return newVec } - fun flatten_map(func: fun(T):vector):vector { - var newVec.construct(size): vector + fun flatten_map(func: fun(T):vec):vec { + var newVec.construct(size): vec for (var i = 0; i < size; i++;) { var to_add = func(data[i]) for (var j = 0; j < to_add.size; j++;) @@ -305,8 +305,8 @@ obj vector (Object, Serializable) { return newVec } fun find_first_satisfying(func: fun(T):bool): T return filter(func)[0] - fun filter(func: fun(T):bool):vector { - var newVec.construct(): vector + fun filter(func: fun(T):bool):vec { + var newVec.construct(): vec for (var i = 0; i < size; i++;) if (func(data[i])) newVec.addEnd(data[i]) diff --git a/stdlib/vector_literals.krak b/stdlib/vec_literals.krak similarity index 50% rename from stdlib/vector_literals.krak rename to stdlib/vec_literals.krak index f772791..c8f2eb2 100644 --- a/stdlib/vector_literals.krak +++ b/stdlib/vec_literals.krak @@ -1,22 +1,22 @@ -import vector +import vec -fun vector(first:T, second:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) return out } -fun vector(first:T, second:T, third:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T, third:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) out.add(third) return out } -fun vector(first:T, second:T, third:T, fourth:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T, third:T, fourth:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) out.add(third) @@ -24,8 +24,8 @@ fun vector(first:T, second:T, third:T, fourth:T):vector::vector { return out } -fun vector(first:T, second:T, third:T, fourth:T, fifth:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T, third:T, fourth:T, fifth:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) out.add(third) @@ -34,8 +34,8 @@ fun vector(first:T, second:T, third:T, fourth:T, fifth:T):vector::vector { return out } -fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) out.add(third) @@ -45,8 +45,8 @@ fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T):vector::ve return out } -fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) out.add(third) @@ -57,8 +57,8 @@ fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T) return out } -fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) out.add(third) @@ -70,8 +70,8 @@ fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, return out } -fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T, ninth:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T, ninth:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) out.add(third) @@ -84,8 +84,8 @@ fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, return out } -fun vector(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T, ninth:T, tenth:T, eleventh:T, twelveth:T):vector::vector { - var out.construct():vector::vector +fun vec(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T, ninth:T, tenth:T, eleventh:T, twelveth:T):vec::vec { + var out.construct():vec::vec out.add(first) out.add(second) out.add(third)