shortening of str and vec

This commit is contained in:
Nathan Braswell
2018-05-22 19:43:54 -04:00
parent bc2c7b3b3e
commit eefa752d55
39 changed files with 1122 additions and 1122 deletions

View File

@@ -2,7 +2,7 @@ import io:*
import grammer:* import grammer:*
import lexer:* import lexer:*
import parser:* import parser:*
import string:* import str:*
import util:* import util:*
import symbol:* import symbol:*
import tree:* import tree:*
@@ -22,16 +22,16 @@ import address_of_ensure_variable_lower:*
import c_line_control:* import c_line_control:*
import node_counter:* import node_counter:*
import c_generator:* import c_generator:*
import vector:* import vec:*
import set:* import set:*
fun main(argc: int, argv: **char):int { fun main(argc: int, argv: **char):int {
var curr_time = get_time() var curr_time = get_time()
// delay construction until we either load it or copy construct it // delay construction until we either load it or copy construct it
var gram: grammer 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 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 compiled_version = 1
var file_contents = read_file(file_name) var file_contents = read_file(file_name)
var loaded_and_valid = false var loaded_and_valid = false
@@ -40,13 +40,13 @@ fun main(argc: int, argv: **char):int {
if (argc <= 1) { 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...") 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" compiled_name += ".expr"
file_contents = string("RealGoal = boolean_expression ;\n") + file_contents file_contents = str("RealGoal = boolean_expression ;\n") + file_contents
doing_repl = true 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_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) +*/ /*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(string("git rev-parse HEAD"), 100) +*/ /*", commit: " + from_system_command(str("git rev-parse HEAD"), 100) +*/
/*", compile date: " + from_system_command(string("date"), 100) */ /*", compile date: " + from_system_command(str("date"), 100) */
/*return version_string.toCharArray()*/ /*return version_string.toCharArray()*/
/*}())*/ /*}())*/
/*println(version_c_string)*/ /*println(version_c_string)*/
@@ -54,13 +54,13 @@ fun main(argc: int, argv: **char):int {
} }
var input_file_offset = 1 var input_file_offset = 1
var interpret_instead = false var interpret_instead = false
var opt_str = string("-O2") var opt_str = str("-O2")
var line_ctrl = false var line_ctrl = false
var compile_c = true var compile_c = true
var positional_args = vector<string>() var positional_args = vec<str>()
var flags = set<string>() var flags = set<str>()
for (var i = 1; i < argc; i++;) { for (var i = 1; i < argc; i++;) {
var arg_str = string(argv[i]) var arg_str = str(argv[i])
if (arg_str == "-i") { if (arg_str == "-i") {
interpret_instead = true interpret_instead = true
} else if (arg_str.length() > 2 && arg_str.slice(0,2) == "-O") { } 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.add(arg_str)
} }
} }
/*positional_args.for_each(fun(i:string) println("positional_arg: " + i);)*/ /*positional_args.for_each(fun(i:str) println("positional_arg: " + i);)*/
flags.for_each(fun(i:string) println("flag: " + i);) flags.for_each(fun(i:str) println("flag: " + i);)
if (file_exists(compiled_name)) { if (file_exists(compiled_name)) {
var pos = 0 var pos = 0
@@ -84,8 +84,8 @@ fun main(argc: int, argv: **char):int {
var saved_version = 0 var saved_version = 0
unpack(saved_version, pos) = unserialize<int>(binary, pos) unpack(saved_version, pos) = unserialize<int>(binary, pos)
if (saved_version == compiled_version) { if (saved_version == compiled_version) {
var cached_contents = string() var cached_contents = str()
unpack(cached_contents, pos) = unserialize<string>(binary, pos) unpack(cached_contents, pos) = unserialize<str>(binary, pos)
if (cached_contents == file_contents) { if (cached_contents == file_contents) {
loaded_and_valid = true loaded_and_valid = true
pos = gram.unserialize(binary, pos) pos = gram.unserialize(binary, pos)
@@ -118,33 +118,33 @@ fun main(argc: int, argv: **char):int {
/*var parse7.construct(&gram): parser*/ /*var parse7.construct(&gram): parser*/
/*var parse8.construct(&gram): parser*/ /*var parse8.construct(&gram): parser*/
var ast_pass.construct(): ast_transformation var ast_pass.construct(): ast_transformation
var parsers = vector(parse1) var parsers = vec(parse1)
/*var parsers = vector(parse1,parse2,parse3,parse4)*/ /*var parsers = vec(parse1,parse2,parse3,parse4)*/
/*var parsers = vector(parse1,parse2,parse3,parse4,parse5,parse6)*/ /*var parsers = vec(parse1,parse2,parse3,parse4,parse5,parse6)*/
/*var parsers = vector(parse1,parse2,parse3,parse4,parse5,parse6,parse7,parse8)*/ /*var parsers = vec(parse1,parse2,parse3,parse4,parse5,parse6,parse7,parse8)*/
// This is our REPL loop // This is our REPL loop
var scope = ast_translation_unit_ptr(string("stdin")) var scope = ast_translation_unit_ptr(str("stdin"))
if (doing_repl) { if (doing_repl) {
/*var globals = setup_globals(importer.name_ast_map)*/ /*var globals = setup_globals(importer.name_ast_map)*/
while (doing_repl) { while (doing_repl) {
var line = get_line(string("> "), 100) var line = get_line(str("> "), 100)
if (line == "end") if (line == "end")
return 0 return 0
var parse = parse1.parse_input(line, string("stdin")) var parse = parse1.parse_input(line, str("stdin"))
trim(parse) trim(parse)
var ast_expression = ast_pass.transform_expression(parse, scope, map<string, *type>()) var ast_expression = ast_pass.transform_expression(parse, scope, map<str, *type>())
print_value(evaluate_constant_expression(ast_expression)) print_value(evaluate_constant_expression(ast_expression))
/*print_value(evaluate_with_globals(ast_expression, &globals))*/ /*print_value(evaluate_with_globals(ast_expression, &globals))*/
} }
} }
var kraken_file_name = positional_args[0] 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) if (positional_args.size > 1)
executable_name = positional_args[1] executable_name = positional_args[1]
curr_time = split(curr_time, "Finish setup") 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") curr_time = split(curr_time, "Import")
// Passes // Passes
/*printlnerr("Counting Nodes")*/ /*printlnerr("Counting Nodes")*/

View File

@@ -2,15 +2,15 @@ import symbol:*
import tree:* import tree:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
import ast_transformation:* import ast_transformation:*
import pass_common:* import pass_common:*
fun address_of_ensure_variable_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun address_of_ensure_variable_lower(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var visited = hash_set<*ast_node>() var visited = hash_set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
match(*node) { match(*node) {
ast_node::function_call(backing) { ast_node::function_call(backing) {

View File

@@ -1,9 +1,9 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
@@ -13,16 +13,16 @@ import hash_set:*
import pass_common:* import pass_common:*
fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun adt_lower(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var type_def_option_map = map<*ast_node, vector<*ast_node>>() var type_def_option_map = map<*ast_node, vec<*ast_node>>()
var visited1 = hash_set<*ast_node>() var visited1 = hash_set<*ast_node>()
var visited2 = hash_set<*ast_node>() var visited2 = hash_set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
match(*node) { match(*node) {
ast_node::adt_def(backing) { ast_node::adt_def(backing) {
/*println(backing.name + ": transforming!")*/ /*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); var replacement = ast_type_def_ptr(backing.name, false);
// we're going to be replacing adt_def in the same ptr, so this works // 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 replacement->type_def.self_type = node->adt_def.self_type
@@ -35,27 +35,27 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
}) })
var option_union_type = type_ptr(option_union) var option_union_type = type_ptr(option_union)
option_union->type_def.self_type = option_union_type 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<ast_node>())) replacement->type_def.variables.add(ast_declaration_statement_ptr(option_union_ident, null<ast_node>()))
add_to_scope("data", option_union_ident, replacement) 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<ast_node>())) replacement->type_def.variables.add(ast_declaration_statement_ptr(flag, null<ast_node>()))
add_to_scope("flag", flag, replacement) add_to_scope("flag", flag, replacement)
add_before_in(option_union, node, parent_chain) 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 var idx = 0
node->adt_def.option_funcs.for_each(fun(func: *ast_node) { node->adt_def.option_funcs.for_each(fun(func: *ast_node) {
var adt_type = replacement->type_def.self_type var adt_type = replacement->type_def.self_type
var block = ast_code_block_ptr() var block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", func, block) add_to_scope("~enclosing_scope", func, block)
func->function.body_statement = 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<ast_node>())) block->code_block.children.add(ast_declaration_statement_ptr(to_ret, null<ast_node>()))
var value = ast_value_ptr(to_string(idx), type_ptr(base_type::integer())) 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 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) { if (func->function.parameters.size) {
// do copy_construct if it should // do copy_construct if it should
block->code_block.children.add(assign_or_copy_construct_statement(lvalue, func->function.parameters[0])) 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<string, pair<*tree<symbol>,*ast_node>>, ast_to_
var func_this = func->function.this_param var func_this = func->function.this_param
if (func->function.name == "operator==") { if (func->function.name == "operator==") {
var other = func->function.parameters[0] 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))))) 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(string("false"), type_ptr(base_type::boolean()))) 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) block->code_block.children.add(if_stmt)
for (var i = 0; i < type_def_option_map[node].size; i++;) { 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()) if (get_ast_type(type_def_option_map[node][i])->is_empty_adt_option())
continue 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 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 our_option = make_operator_call(".", vec(make_operator_call("->", vec(func_this, option_union_ident)), option))
var their_option = make_operator_call(".", vector(make_operator_call(".", vector(other, 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)) 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(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!=") { } else if (func->function.name == "operator!=") {
var other = func->function.parameters[0] 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") { } else if (func->function.name == "construct") {
var value = ast_value_ptr(string("-1"), type_ptr(base_type::integer())) var value = ast_value_ptr(str("-1"), type_ptr(base_type::integer()))
block->code_block.children.add(ast_assignment_statement_ptr(make_operator_call("->", vector(func_this, flag)), value)) 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)) block->code_block.children.add(ast_return_statement_ptr(func_this))
} else if (func->function.name == "copy_construct") { } else if (func->function.name == "copy_construct") {
var other = func->function.parameters[0] 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++;) { 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()) if (get_ast_type(type_def_option_map[node][i])->is_empty_adt_option())
continue 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 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 our_option = make_operator_call(".", vec(make_operator_call("->", vec(func_this, option_union_ident)), option))
var their_option = make_operator_call(".", vector(make_operator_call("->", vector(other, 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) if_stmt_inner->if_statement.then_part = assign_or_copy_construct_statement(our_option, their_option)
block->code_block.children.add(if_stmt_inner) block->code_block.children.add(if_stmt_inner)
} }
} else if (func->function.name == "operator=") { } else if (func->function.name == "operator=") {
var other = func->function.parameters[0] 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, "destruct", vec<*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, "copy_construct", vec(make_operator_call("&", vec(other)))))
} else if (func->function.name == "destruct") { } else if (func->function.name == "destruct") {
for (var i = 0; i < type_def_option_map[node].size; i++;) { for (var i = 0; i < type_def_option_map[node].size; i++;) {
var option = type_def_option_map[node][i] var option = type_def_option_map[node][i]
var option_type = get_ast_type(option) var option_type = get_ast_type(option)
if (option_type->is_empty_adt_option()) if (option_type->is_empty_adt_option())
continue continue
if (option_type->indirection == 0 && option_type->is_object() && has_method(option_type->type_def, "destruct", vector<*type>())) { 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("==", 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 our_option = make_operator_call(".", vector(make_operator_call("->", vector(func_this, option_union_ident)), option)) 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", vector<*ast_node>()) if_stmt_inner->if_statement.then_part = make_method_call(our_option, "destruct", vec<*ast_node>())
block->code_block.children.add(if_stmt_inner) block->code_block.children.add(if_stmt_inner)
} }
} }
@@ -138,16 +138,16 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
} }
run_on_tree(helper_before, empty_pass_second_half(), syntax_ast_pair.second, &visited1) 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<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var second_helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var second_helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
match(*node) { match(*node) {
ast_node::match_statement(backing) { ast_node::match_statement(backing) {
var block = ast_code_block_ptr() 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) 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 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<ast_node>())) block->code_block.children.add(ast_declaration_statement_ptr(holder, null<ast_node>()))
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) { backing.cases.for_each(fun(case_stmt: *ast_node) {
var option = case_stmt->case_statement.option var option = case_stmt->case_statement.option
var flag = get_from_scope(get_ast_type(value)->type_def, "flag") var flag = get_from_scope(get_ast_type(value)->type_def, "flag")
@@ -158,14 +158,14 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
for (var i = 0; i < type_def_option_map[get_ast_type(value)->type_def].size; i++;) 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) if (type_def_option_map[get_ast_type(value)->type_def][i] == option)
option_num = i; 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 if_stmt = ast_if_statement_ptr(condition)
var inner_block = ast_code_block_ptr() var inner_block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", block, inner_block) add_to_scope("~enclosing_scope", block, inner_block)
var unpack_ident = case_stmt->case_statement.unpack_ident var unpack_ident = case_stmt->case_statement.unpack_ident
if (unpack_ident) { if (unpack_ident) {
var get_option = make_operator_call(".", vector(make_operator_call("->", vector(holder, data)), option)) var get_option = make_operator_call(".", vec(make_operator_call("->", vec(holder, data)), option))
get_option = make_operator_call("&", vector(get_option)) get_option = make_operator_call("&", vec(get_option))
unpack_ident->identifier.type = unpack_ident->identifier.type->clone_with_ref() 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)) 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<string, pair<*tree<symbol>,*ast_node>>, ast_to_
return; return;
/*println(backing.parameters[1]->identifier.name + " getting, adt . or -> call!")*/ /*println(backing.parameters[1]->identifier.name + " getting, adt . or -> call!")*/
var object = node->function_call.parameters[0] 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.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(".", vector(get_ast_type(get_from_scope(left_type->type_def, "data")), get_ast_type(backing.parameters[1]))) 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])))
} }
} }
} }

View File

@@ -1,12 +1,12 @@
import tree:* import tree:*
import symbol:* import symbol:*
import vector:* import vec:*
import vector_literals:* import vec_literals:*
import set:* import set:*
import stack:* import stack:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import type:* import type:*
@@ -42,7 +42,7 @@ fun ast_node_ptr(node: ast_node): *ast_node {
to_ret->copy_construct(&node) to_ret->copy_construct(&node)
return to_ret 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 obj_var.construct(name): translation_unit
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::translation_unit(obj_var)) ptr->copy_construct(&ast_node::translation_unit(obj_var))
@@ -55,11 +55,11 @@ fun is_translation_unit(node: *ast_node): bool {
return false return false
} }
obj translation_unit (Object) { obj translation_unit (Object) {
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var children: vector<*ast_node> var children: vec<*ast_node>
var lambdas: vector<*ast_node> var lambdas: vec<*ast_node>
var name: string var name: str
fun construct(nameIn: string): *translation_unit { fun construct(nameIn: str): *translation_unit {
scope.construct() scope.construct()
children.construct() children.construct()
lambdas.construct() lambdas.construct()
@@ -86,7 +86,7 @@ obj translation_unit (Object) {
return children == other.children && name == other.name && lambdas == other.lambdas 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 to_ret.construct(name, containing_tu): import
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::import(to_ret)) ptr->copy_construct(&ast_node::import(to_ret))
@@ -99,13 +99,13 @@ fun is_import(node: *ast_node): bool {
return false return false
} }
obj import (Object) { obj import (Object) {
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var imported: set<string> var imported: set<str>
var containing_translation_unit: *ast_node var containing_translation_unit: *ast_node
var translation_unit: *ast_node var translation_unit: *ast_node
var name: string var name: str
var starred: bool var starred: bool
fun construct(nameIn: string, containing_tu: *ast_node): *import { fun construct(nameIn: str, containing_tu: *ast_node): *import {
scope.construct() scope.construct()
imported.construct() imported.construct()
name.copy_construct(&nameIn) 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 { 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) 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 to_ret.construct(name, type, enclosing_scope, is_extern): identifier
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::identifier(to_ret)) ptr->copy_construct(&ast_node::identifier(to_ret))
@@ -154,12 +154,12 @@ fun is_identifier(node: *ast_node): bool {
return false return false
} }
obj identifier (Object) { obj identifier (Object) {
var name: string var name: str
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var type: *type var type: *type
var enclosing_scope: *ast_node var enclosing_scope: *ast_node
var is_extern: bool 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) name.copy_construct(&name_in)
scope.construct() scope.construct()
type = type_in 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 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: str): *ast_node {*/
fun ast_type_def_ptr(name: ref string): *ast_node { fun ast_type_def_ptr(name: ref str): *ast_node {
return ast_type_def_ptr(name, false) return ast_type_def_ptr(name, false)
} }
fun ast_type_def_ptr(name: ref string, is_union: bool): *ast_node { fun ast_type_def_ptr(name: ref str, is_union: bool): *ast_node {
var to_ret.construct(name, is_union): type_def var to_ret.construct(name, is_union): type_def
/*var to_ret.construct(name): type_def*/ /*var to_ret.construct(name): type_def*/
var ptr = new<ast_node>() var ptr = new<ast_node>()
@@ -204,15 +204,15 @@ fun is_type_def(node: *ast_node): bool {
return false return false
} }
obj type_def (Object) { obj type_def (Object) {
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var name: string var name: str
var self_type: *type var self_type: *type
var variables: vector<*ast_node> var variables: vec<*ast_node>
var methods: vector<*ast_node> var methods: vec<*ast_node>
var is_union: bool var is_union: bool
fun construct(nameIn: ref string, is_unionIn: bool): *type_def { fun construct(nameIn: ref str, is_unionIn: bool): *type_def {
/*fun construct(nameIn: ref string): *type_def {*/ /*fun construct(nameIn: ref str): *type_def {*/
/*fun construct(nameIn: string): *type_def {*/ /*fun construct(nameIn: str): *type_def {*/
scope.construct() scope.construct()
name.copy_construct(&nameIn) name.copy_construct(&nameIn)
is_union = is_unionIn 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*/ /*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 to_ret.construct(name): adt_def
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::adt_def(to_ret)) ptr->copy_construct(&ast_node::adt_def(to_ret))
@@ -257,13 +257,13 @@ fun is_adt_def(node: *ast_node): bool {
return false return false
} }
obj adt_def (Object) { obj adt_def (Object) {
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var name: string var name: str
var self_type: *type var self_type: *type
var options: vector<*ast_node> var options: vec<*ast_node>
var option_funcs: vector<*ast_node> var option_funcs: vec<*ast_node>
var regular_funcs: vector<*ast_node> var regular_funcs: vec<*ast_node>
fun construct(nameIn: string): *adt_def { fun construct(nameIn: str): *adt_def {
scope.construct() scope.construct()
name.copy_construct(&nameIn) name.copy_construct(&nameIn)
self_type = null<type>() self_type = null<type>()
@@ -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 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<ast_node>(), is_extern, false) return ast_function_ptr(name, type, parameters, null<ast_node>(), 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 to_ret.construct(name, type, parameters, this_param, is_extern, is_variadic): function
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::function(to_ret)) ptr->copy_construct(&ast_node::function(to_ret))
@@ -310,16 +310,16 @@ fun is_function(node: *ast_node): bool {
return false return false
} }
obj function (Object) { obj function (Object) {
var name: string var name: str
var type: *type var type: *type
var parameters: vector<*ast_node> var parameters: vec<*ast_node>
var this_param: *ast_node var this_param: *ast_node
var closed_variables: set<*ast_node> var closed_variables: set<*ast_node>
var body_statement: *ast_node var body_statement: *ast_node
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var is_extern: bool var is_extern: bool
var is_variadic: 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) name.copy_construct(&name_in)
parameters.copy_construct(&parameters_in) parameters.copy_construct(&parameters_in)
this_param = this_param_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 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<symbol>, template_types: vector<string>, template_type_replacements: map<string, *type>, is_function: bool): *ast_node { fun ast_template_ptr(name: str, syntax_node: *tree<symbol>, template_types: vec<str>, template_type_replacements: map<str, *type>, is_function: bool): *ast_node {
var to_ret.construct(name, syntax_node, template_types, template_type_replacements, is_function): template var to_ret.construct(name, syntax_node, template_types, template_type_replacements, is_function): template
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::template(to_ret)) ptr->copy_construct(&ast_node::template(to_ret))
@@ -369,15 +369,15 @@ fun is_template(node: *ast_node): bool {
return false return false
} }
obj template (Object) { obj template (Object) {
var name: string var name: str
var syntax_node: *tree<symbol> var syntax_node: *tree<symbol>
var instantiated: vector<*ast_node> var instantiated: vec<*ast_node>
var template_types: vector<string> var template_types: vec<str>
var template_type_replacements: map<string, *type> var template_type_replacements: map<str, *type>
var instantiated_map: map<vector<type>, *ast_node> var instantiated_map: map<vec<type>, *ast_node>
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var is_function: bool var is_function: bool
fun construct(name_in: string, syntax_node_in: *tree<symbol>, template_types_in: vector<string>, template_type_replacements_in: map<string, *type>, is_function: bool): *template { fun construct(name_in: str, syntax_node_in: *tree<symbol>, template_types_in: vec<str>, template_type_replacements_in: map<str, *type>, is_function: bool): *template {
name.copy_construct(&name_in) name.copy_construct(&name_in)
syntax_node = syntax_node_in syntax_node = syntax_node_in
instantiated.construct() instantiated.construct()
@@ -434,8 +434,8 @@ fun is_code_block(node: *ast_node): bool {
return false return false
} }
obj code_block (Object) { obj code_block (Object) {
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var children: vector<*ast_node> var children: vec<*ast_node>
fun construct(): *code_block { fun construct(): *code_block {
scope.construct() scope.construct()
children.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 // 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 then_part: *ast_node
var else_part: *ast_node var else_part: *ast_node
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
fun construct(condition_in: *ast_node): *if_statement { fun construct(condition_in: *ast_node): *if_statement {
condition = condition_in condition = condition_in
then_part = null<ast_node>() then_part = null<ast_node>()
@@ -512,9 +512,9 @@ fun is_match_statement(node: *ast_node): bool {
return false return false
} }
obj match_statement (Object) { obj match_statement (Object) {
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var value: *ast_node var value: *ast_node
var cases: vector<*ast_node> var cases: vec<*ast_node>
fun construct(value_in: *ast_node): *match_statement { fun construct(value_in: *ast_node): *match_statement {
scope.construct() scope.construct()
value = value_in value = value_in
@@ -551,7 +551,7 @@ fun is_case_statement(node: *ast_node): bool {
return false return false
} }
obj case_statement (Object) { obj case_statement (Object) {
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var option: *ast_node var option: *ast_node
var unpack_ident: *ast_node var unpack_ident: *ast_node
var statement: *ast_node var statement: *ast_node
@@ -594,7 +594,7 @@ fun is_while_loop(node: *ast_node): bool {
obj while_loop (Object) { obj while_loop (Object) {
var condition: *ast_node var condition: *ast_node
var statement: *ast_node var statement: *ast_node
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
fun construct(condition_in: *ast_node): *while_loop { fun construct(condition_in: *ast_node): *while_loop {
condition = condition_in condition = condition_in
statement = null<ast_node>() statement = null<ast_node>()
@@ -634,7 +634,7 @@ obj for_loop (Object) {
var condition: *ast_node var condition: *ast_node
var update: *ast_node var update: *ast_node
var body: *ast_node var body: *ast_node
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
fun construct(): *for_loop { fun construct(): *for_loop {
scope.construct() scope.construct()
init = null<ast_node>() init = null<ast_node>()
@@ -675,7 +675,7 @@ fun is_return_statement(node: *ast_node): bool {
} }
obj return_statement (Object) { obj return_statement (Object) {
var return_value: *ast_node var return_value: *ast_node
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
fun construct(return_value_in: *ast_node): *return_statement { fun construct(return_value_in: *ast_node): *return_statement {
return_value = return_value_in return_value = return_value_in
scope.construct() scope.construct()
@@ -847,7 +847,7 @@ fun is_if_comp(node: *ast_node): bool {
return false return false
} }
obj if_comp (Object) { obj if_comp (Object) {
var wanted_generator: string var wanted_generator: str
var statement: *ast_node var statement: *ast_node
fun construct(): *if_comp { fun construct(): *if_comp {
wanted_generator.construct() wanted_generator.construct()
@@ -881,11 +881,11 @@ fun is_simple_passthrough(node: *ast_node): bool {
return false return false
} }
obj simple_passthrough (Object) { obj simple_passthrough (Object) {
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
var passthrough_str: string var passthrough_str: str
var in_params: vector<pair<*ast_node,string>> var in_params: vec<pair<*ast_node,str>>
var out_params: vector<pair<*ast_node,string>> var out_params: vec<pair<*ast_node,str>>
var linker_str: string var linker_str: str
fun construct(): *simple_passthrough { fun construct(): *simple_passthrough {
scope.construct() scope.construct()
passthrough_str.construct() passthrough_str.construct()
@@ -917,7 +917,7 @@ obj simple_passthrough (Object) {
out_params == other.out_params && linker_str == other.linker_str 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 to_ret.construct(func, parameters): function_call
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::function_call(to_ret)) 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) { obj function_call (Object) {
var func: *ast_node var func: *ast_node
var parameters: vector<*ast_node> var parameters: vec<*ast_node>
fun construct(func_in: *ast_node, parameters_in: vector<*ast_node>): *function_call { fun construct(func_in: *ast_node, parameters_in: vec<*ast_node>): *function_call {
func = func_in func = func_in
parameters.copy_construct(&parameters_in) parameters.copy_construct(&parameters_in)
return this return this
@@ -952,7 +952,7 @@ obj function_call (Object) {
return func == func && parameters == other.parameters 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 to_ret.construct(intrinsic, parameters, type_parameters, return_type): compiler_intrinsic
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::compiler_intrinsic(to_ret)) ptr->copy_construct(&ast_node::compiler_intrinsic(to_ret))
@@ -965,11 +965,11 @@ fun is_compiler_intrinsic(node: *ast_node): bool {
return false return false
} }
obj compiler_intrinsic (Object) { obj compiler_intrinsic (Object) {
var intrinsic: string var intrinsic: str
var parameters: vector<*ast_node> var parameters: vec<*ast_node>
var type_parameters: vector<*type> var type_parameters: vec<*type>
var return_type: *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) intrinsic.copy_construct(&intrinsic_in)
parameters.copy_construct(&parameters_in) parameters.copy_construct(&parameters_in)
type_parameters.copy_construct(&type_parameters_in) type_parameters.copy_construct(&type_parameters_in)
@@ -1030,7 +1030,7 @@ obj cast (Object) {
return value == other.value && to_type == other.to_type 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 to_ret.construct(string_value, value_type): value
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::value(to_ret)) ptr->copy_construct(&ast_node::value(to_ret))
@@ -1043,10 +1043,10 @@ fun is_value(node: *ast_node): bool {
return false return false
} }
obj value (Object) { obj value (Object) {
var string_value: string var string_value: str
var value_type: *type var value_type: *type
var scope: map<string, vector<*ast_node>> var scope: map<str, vec<*ast_node>>
fun construct(string_value_in: string, value_type_in: *type): *value { fun construct(string_value_in: str, value_type_in: *type): *value {
scope.construct() scope.construct()
string_value.copy_construct(&string_value_in) string_value.copy_construct(&string_value_in)
value_type = value_type_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) { match (*node) {
// Don't get lambdas, let them come up naturally in passes (so can get enclosing function and stuff) // 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 + backing.lambdas*/
ast_node::translation_unit(backing) return backing.children ast_node::translation_unit(backing) return backing.children
ast_node::import(backing) return vector<*ast_node>() ast_node::import(backing) return vec<*ast_node>()
ast_node::identifier(backing) return vector<*ast_node>() ast_node::identifier(backing) return vec<*ast_node>()
ast_node::type_def(backing) return backing.variables + backing.methods ast_node::type_def(backing) return backing.variables + backing.methods
ast_node::adt_def(backing) return backing.options + backing.option_funcs ast_node::adt_def(backing) return backing.options + backing.option_funcs
ast_node::function(backing) return backing.parameters + backing.body_statement ast_node::function(backing) return backing.parameters + backing.body_statement
ast_node::template(backing) return backing.instantiated ast_node::template(backing) return backing.instantiated
ast_node::code_block(backing) return backing.children 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::if_statement(backing) return vec(backing.condition, backing.then_part, backing.else_part)
ast_node::match_statement(backing) return vector(backing.value) + backing.cases ast_node::match_statement(backing) return vec(backing.value) + backing.cases
ast_node::case_statement(backing) return vector(backing.option, backing.unpack_ident, backing.statement) ast_node::case_statement(backing) return vec(backing.option, backing.unpack_ident, backing.statement)
ast_node::while_loop(backing) return vector(backing.condition, backing.statement) ast_node::while_loop(backing) return vec(backing.condition, backing.statement)
ast_node::for_loop(backing) return vector(backing.init, backing.condition, backing.update, backing.body) ast_node::for_loop(backing) return vec(backing.init, backing.condition, backing.update, backing.body)
ast_node::return_statement(backing) return vector(backing.return_value) ast_node::return_statement(backing) return vec(backing.return_value)
ast_node::branching_statement(backing) return vector<*ast_node>() ast_node::branching_statement(backing) return vec<*ast_node>()
ast_node::defer_statement(backing) return vector(backing.statement) ast_node::defer_statement(backing) return vec(backing.statement)
ast_node::assignment_statement(backing) return vector(backing.to, backing.from) ast_node::assignment_statement(backing) return vec(backing.to, backing.from)
ast_node::declaration_statement(backing) return vector(backing.identifier, backing.expression, backing.init_method_call) ast_node::declaration_statement(backing) return vec(backing.identifier, backing.expression, backing.init_method_call)
ast_node::if_comp(backing) return vector<*ast_node>(backing.statement) ast_node::if_comp(backing) return vec<*ast_node>(backing.statement)
ast_node::simple_passthrough(backing) return vector<*ast_node>() ast_node::simple_passthrough(backing) return vec<*ast_node>()
ast_node::function_call(backing) return vector(backing.func) + backing.parameters ast_node::function_call(backing) return vec(backing.func) + backing.parameters
ast_node::compiler_intrinsic(backing) return backing.parameters ast_node::compiler_intrinsic(backing) return backing.parameters
ast_node::cast(backing) return vector<*ast_node>(backing.value) ast_node::cast(backing) return vec<*ast_node>(backing.value)
ast_node::value(backing) return vector<*ast_node>() 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) { match (*node) {
ast_node::translation_unit(backing) return string("translation_unit: ") + backing.name ast_node::translation_unit(backing) return str("translation_unit: ") + backing.name
ast_node::import(backing) return string("import: ") + backing.name + "; [" + backing.imported.reduce(fun(name: string, acc: string): string return acc + " " + name;, string()) + " ]" ast_node::import(backing) return str("import: ") + backing.name + "; [" + backing.imported.reduce(fun(name: str, acc: str): str return acc + " " + name;, str()) + " ]"
ast_node::identifier(backing) return string("identifier: ") + backing.name + ": " + backing.type->to_string() ast_node::identifier(backing) return str("identifier: ") + backing.name + ": " + backing.type->to_string()
ast_node::type_def(backing) { ast_node::type_def(backing) {
/*if (backing.is_union)*/ /*if (backing.is_union)*/
/*return string("type_def union: ") + backing.name*/ /*return str("type_def union: ") + backing.name*/
/*else*/ /*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) { ast_node::function(backing) {
if (backing.is_extern) 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 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::template(backing) return str("template: ") + backing.name
ast_node::code_block(backing) return string("code_block") ast_node::code_block(backing) return str("code_block")
ast_node::if_statement(backing) return string("if_statement") ast_node::if_statement(backing) return str("if_statement")
ast_node::match_statement(backing) return string("match_statement") ast_node::match_statement(backing) return str("match_statement")
ast_node::case_statement(backing) return string("case_statement") ast_node::case_statement(backing) return str("case_statement")
ast_node::while_loop(backing) return string("while_loop") ast_node::while_loop(backing) return str("while_loop")
ast_node::for_loop(backing) return string("for_loop") ast_node::for_loop(backing) return str("for_loop")
ast_node::return_statement(backing) return string("return_statement") ast_node::return_statement(backing) return str("return_statement")
ast_node::branching_statement(backing) return string("branching_statement") ast_node::branching_statement(backing) return str("branching_statement")
ast_node::defer_statement(backing) return string("defer_statement") ast_node::defer_statement(backing) return str("defer_statement")
ast_node::assignment_statement(backing) return string("assignment_statement") ast_node::assignment_statement(backing) return str("assignment_statement")
ast_node::declaration_statement(backing) return string("declaration_statement") ast_node::declaration_statement(backing) return str("declaration_statement")
ast_node::if_comp(backing) return string("if_comp: ") + backing.wanted_generator ast_node::if_comp(backing) return str("if_comp: ") + backing.wanted_generator
ast_node::simple_passthrough(backing) return string("simple_passthrough: , string:") + backing.passthrough_str ast_node::simple_passthrough(backing) return str("simple_passthrough: , str:") + backing.passthrough_str
ast_node::function_call(backing) return string("function_call:") + get_ast_name(backing.func) + "(" + backing.parameters.size + ")" ast_node::function_call(backing) return str("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::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 string("cast: ") + get_ast_name(backing.value) + ": " + backing.to_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 string("value: ") + backing.string_value + ": " + backing.value_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<string,vector<*ast_node>> { fun get_ast_scope(node: *ast_node): *map<str,vec<*ast_node>> {
match (*node) { match (*node) {
ast_node::translation_unit() return &node->translation_unit.scope ast_node::translation_unit() return &node->translation_unit.scope
ast_node::import() return &node->import.scope ast_node::import() return &node->import.scope
@@ -1158,7 +1158,7 @@ fun get_ast_scope(node: *ast_node): *map<string,vector<*ast_node>> {
ast_node::simple_passthrough() return &node->simple_passthrough.scope ast_node::simple_passthrough() return &node->simple_passthrough.scope
ast_node::value() return &node->value.scope ast_node::value() return &node->value.scope
} }
return null<map<string,vector<*ast_node>>>() return null<map<str,vec<*ast_node>>>()
} }
fun get_ast_type(node: *ast_node): *type { fun get_ast_type(node: *ast_node): *type {
match (*node) { match (*node) {
@@ -1172,14 +1172,14 @@ fun get_ast_type(node: *ast_node): *type {
return null<type>() return null<type>()
} }
fun ast_to_dot(root: *ast_node): string { fun ast_to_dot(root: *ast_node): str {
var ret = string("digraph Kaken {\n") var ret = str("digraph Kaken {\n")
var counter = 0 var counter = 0
var node_name_map = map<*ast_node, string>() var node_name_map = map<*ast_node, str>()
var get_name = fun(node: *ast_node): string { var get_name = fun(node: *ast_node): str {
if (node_name_map.contains_key(node)) if (node_name_map.contains_key(node))
return node_name_map[node] return node_name_map[node]
var escaped = string("") var escaped = str("")
get_ast_name(node).data.for_each(fun(c: char) { get_ast_name(node).data.for_each(fun(c: char) {
if (c != '"') if (c != '"')
escaped += 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) { get_ast_children(node).for_each(fun(child: *ast_node) {
if (!child || done_set.contains(child)) if (!child || done_set.contains(child))
return; // where on earth does the null come from 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) helper(child)
}) })
} }

View File

@@ -1,11 +1,11 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import queue:* import queue:*
import stack:* import stack:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import os:* import os:*
@@ -16,7 +16,7 @@ import pass_common:*
adt search_type { adt search_type {
none, none,
function: vector<*type> function: vec<*type>
} }
obj ast_transformation (Object) { obj ast_transformation (Object) {
@@ -44,7 +44,7 @@ obj ast_transformation (Object) {
fourth_pass_worklist.destruct() fourth_pass_worklist.destruct()
} }
// first pass defines all type_defs (objects and aliases), ADTs, and top-level if-comps/passthroughs // 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<symbol>): pair<*ast_node, vector<*ast_node>> { fun first_pass(file_name: str, parse_tree: *tree<symbol>): pair<*ast_node, vec<*ast_node>> {
var translation_unit = ast_translation_unit_ptr(file_name) var translation_unit = ast_translation_unit_ptr(file_name)
parse_tree->children.for_each(fun(child: *tree<symbol>) { parse_tree->children.for_each(fun(child: *tree<symbol>) {
if (child->data.name == "type_def") { if (child->data.name == "type_def") {
@@ -52,7 +52,7 @@ obj ast_transformation (Object) {
} else if (child->data.name == "adt_def") { } else if (child->data.name == "adt_def") {
var name = concat_symbol_tree(get_node("identifier", child)) var name = concat_symbol_tree(get_node("identifier", child))
var adt_def_node = ast_adt_def_ptr(name) 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) translation_unit->translation_unit.children.add(adt_def_node)
ast_to_syntax.set(adt_def_node, child) ast_to_syntax.set(adt_def_node, child)
add_to_scope("~enclosing_scope", translation_unit, adt_def_node) add_to_scope("~enclosing_scope", translation_unit, adt_def_node)
@@ -65,9 +65,9 @@ obj ast_transformation (Object) {
}) })
// now do all imports // 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 // 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<symbol>) { parse_tree->children.for_each(fun(child: *tree<symbol>) {
if (child->data.name == "import") { if (child->data.name == "import") {
var import_identifier_children = get_nodes("identifier", child) var import_identifier_children = get_nodes("identifier", child)
@@ -77,24 +77,24 @@ obj ast_transformation (Object) {
translation_unit->translation_unit.children.add(import_node) translation_unit->translation_unit.children.add(import_node)
ast_to_syntax.set(import_node, child) ast_to_syntax.set(import_node, child)
add_to_scope("~enclosing_scope", translation_unit, import_node) 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<symbol>):string return concat_symbol_tree(ident);)) import_node->import.imported = from_vector(import_identifier_children.slice(1,-1).map(fun(ident: *tree<symbol>):str return concat_symbol_tree(ident);))
if (get_node("\"\\*\"", child)) if (get_node("\"\\*\"", child))
import_node->import.starred = true import_node->import.starred = true
} }
}) })
return make_pair(translation_unit, imports) return make_pair(translation_unit, imports)
} }
fun transform_traits(traits_node: *tree<symbol>): set<string> { fun transform_traits(traits_node: *tree<symbol>): set<str> {
if (!traits_node) if (!traits_node)
return set<string>() return set<str>()
return from_vector(get_nodes("scoped_identifier", traits_node).map(fun(s: *tree<symbol>): string return concat_symbol_tree(s);)) return from_vector(get_nodes("scoped_identifier", traits_node).map(fun(s: *tree<symbol>): str return concat_symbol_tree(s);))
} }
fun first_pass_type_def(child: *tree<symbol>, scope: *ast_node, instantiate_template: bool): *ast_node { fun first_pass_type_def(child: *tree<symbol>, scope: *ast_node, instantiate_template: bool): *ast_node {
var name = concat_symbol_tree(get_node("identifier", child)) var name = concat_symbol_tree(get_node("identifier", child))
var template_dec = get_node("template_dec", child) var template_dec = get_node("template_dec", child)
if (template_dec && !instantiate_template) { if (template_dec && !instantiate_template) {
var template_types = vector<string>() var template_types = vec<str>()
var template_type_replacements = map<string, *type>() var template_type_replacements = map<str, *type>()
// XXX add traits // XXX add traits
get_nodes("template_param", template_dec).for_each(fun(template_param: *tree<symbol>) { get_nodes("template_param", template_dec).for_each(fun(template_param: *tree<symbol>) {
template_types.add(concat_symbol_tree(get_node("identifier", template_param))) 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<symbol>) { parse_tree->children.for_each(fun(child: *tree<symbol>) {
if (child->data.name == "function") { if (child->data.name == "function") {
// also handles templated function // also handles templated function
var function_node = second_pass_function(child, translation_unit, map<string, *type>(), true) var function_node = second_pass_function(child, translation_unit, map<str, *type>(), true)
translation_unit->translation_unit.children.add(function_node) translation_unit->translation_unit.children.add(function_node)
ast_to_syntax.set(function_node, child) ast_to_syntax.set(function_node, child)
} else if (child->data.name == "declaration_statement") { } 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?)... // 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<string, *type>())) translation_unit->translation_unit.children.add(transform_declaration_statement(child, translation_unit, map<str, *type>()))
} }
}) })
// work on the ones already started // work on the ones already started
translation_unit->translation_unit.children.for_each(fun(node: *ast_node) { translation_unit->translation_unit.children.for_each(fun(node: *ast_node) {
match(*node) { match(*node) {
ast_node::type_def(backing) second_pass_type_def(ast_to_syntax[node], node, translation_unit, map<string, *type>()) ast_node::type_def(backing) second_pass_type_def(ast_to_syntax[node], node, translation_unit, map<str, *type>())
ast_node::adt_def(backing) second_pass_adt_def(ast_to_syntax[node], node, translation_unit, map<string, *type>()) ast_node::adt_def(backing) second_pass_adt_def(ast_to_syntax[node], node, translation_unit, map<str, *type>())
} }
}) })
} }
fun second_pass_type_def(type_def_syntax: *tree<symbol>, node: *ast_node, scope: *ast_node, template_replacements: map<string, *type>) { fun second_pass_type_def(type_def_syntax: *tree<symbol>, node: *ast_node, scope: *ast_node, template_replacements: map<str, *type>) {
type_def_syntax->children.for_each(fun(child: *tree<symbol>) { type_def_syntax->children.for_each(fun(child: *tree<symbol>) {
if (child->data.name == "declaration_statement") { if (child->data.name == "declaration_statement") {
var declaration_node = transform_declaration_statement(child, node, template_replacements) 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<symbol>, node: *ast_node, scope: *ast_node, template_replacements: map<string, *type>) { fun second_pass_adt_def(adt_def_syntax: *tree<symbol>, node: *ast_node, scope: *ast_node, template_replacements: map<str, *type>) {
get_nodes("adt_option", adt_def_syntax).for_each(fun(adt_option: *tree<symbol>) { get_nodes("adt_option", adt_def_syntax).for_each(fun(adt_option: *tree<symbol>) {
var ident_type: *type var ident_type: *type
var type_syntax = get_node("type", adt_option) var type_syntax = get_node("type", adt_option)
@@ -171,9 +171,9 @@ obj ast_transformation (Object) {
var function_node = null<ast_node>() var function_node = null<ast_node>()
if (type_syntax) { if (type_syntax) {
var identifier_param = ast_identifier_ptr(option_name, ident_type, node) 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 { } 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(option_name, function_node, node)
add_to_scope("~enclosing_scope", node, function_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 // 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 // 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 equals_param = ast_identifier_ptr(str("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 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(string("in"), node->adt_def.self_type->clone_with_indirection(1,false), 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(string("in"), node->adt_def.self_type->clone_with_indirection(0,true), node) var assign_param = ast_identifier_ptr(str("in"), node->adt_def.self_type->clone_with_indirection(0,true), node)
vector( vec(
make_pair("operator==", ast_function_ptr(string("operator=="), make_pair("operator==", ast_function_ptr(str("operator=="),
type_ptr(vector(equals_param->identifier.type), type_ptr(base_type::boolean()), 0, false, false, true), type_ptr(vec(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)), 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(string("operator!="), make_pair("operator!=", ast_function_ptr(str("operator!="),
type_ptr(vector(nequals_param->identifier.type), type_ptr(base_type::boolean()), 0, false, false, true), type_ptr(vec(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)), 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(string("construct"), make_pair("construct", ast_function_ptr(str("construct"),
type_ptr(vector<*type>(), node->adt_def.self_type->clone_with_increased_indirection(), 0, false, false, true), type_ptr(vec<*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)), 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(string("copy_construct"), make_pair("copy_construct", ast_function_ptr(str("copy_construct"),
type_ptr(vector(copy_construct_param->identifier.type), type_ptr(base_type::void_return()), 0, false, false, true), type_ptr(vec(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)), 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(string("operator="), make_pair("operator=", ast_function_ptr(str("operator="),
type_ptr(vector(assign_param->identifier.type), type_ptr(base_type::void_return()), 0, false, false, true), type_ptr(vec(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)), 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(string("destruct"), make_pair("destruct", ast_function_ptr(str("destruct"),
type_ptr(vector<*type>(), type_ptr(base_type::void_return()), 0, false, false, true), type_ptr(vec<*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)) 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>) { ).for_each(fun(func_pair: pair<*char, *ast_node>) {
node->adt_def.regular_funcs.add(func_pair.second) 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) add_to_scope("~enclosing_scope", node, func_pair.second)
}) })
} }
fun second_pass_function(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>, do_raw_template: bool): *ast_node { fun second_pass_function(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>, do_raw_template: bool): *ast_node {
var func_identifier_node = get_node("func_identifier", 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) if (func_identifier_node)
function_name = concat_symbol_tree(func_identifier_node) function_name = concat_symbol_tree(func_identifier_node)
var template_dec = get_node("template_dec", node) var template_dec = get_node("template_dec", node)
if (do_raw_template && template_dec) { if (do_raw_template && template_dec) {
var template_types = vector<string>() var template_types = vec<str>()
var template_type_replacements = map<string, *type>() var template_type_replacements = map<str, *type>()
get_nodes("template_param", template_dec).for_each(fun(template_param: *tree<symbol>) { get_nodes("template_param", template_dec).for_each(fun(template_param: *tree<symbol>) {
template_types.add(concat_symbol_tree(get_node("identifier", template_param))) 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)))) 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()) if (return_type->is_none())
error(node, "return type none") error(node, "return type none")
// transform parameters // transform parameters
var parameters = vector<*ast_node>() var parameters = vec<*ast_node>()
get_nodes("typed_parameter", node).for_each(fun(child: *tree<symbol>) { get_nodes("typed_parameter", node).for_each(fun(child: *tree<symbol>) {
// note the temporary null<ast_node>() which gets replaced below, as the dependency is circular // note the temporary null<ast_node>() which gets replaced below, as the dependency is circular
var param_type = transform_type(get_node("type", child), scope, template_replacements) 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)) { if (is_type_def(scope)) {
this_param = ast_identifier_ptr("this", scope->type_def.self_type->clone_with_indirection(1), scope) this_param = ast_identifier_ptr("this", scope->type_def.self_type->clone_with_indirection(1), scope)
} else if (is_template(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)) { if (is_type_def(parent_scope)) {
this_param = ast_identifier_ptr("this", parent_scope->type_def.self_type->clone_with_indirection(1), 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 // also same body problem as below
backing.methods.for_each(fun(method: *ast_node) { backing.methods.for_each(fun(method: *ast_node) {
if (!is_template(method)) if (!is_template(method))
method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, map<string, *type>()) method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, map<str, *type>())
}) })
} }
ast_node::function(backing) { ast_node::function(backing) {
// make sure not a template // make sure not a template
if (!backing.is_extern) if (!backing.is_extern)
backing.body_statement = transform_statement(get_node("statement", ast_to_syntax[node]), node, map<string, *type>()) backing.body_statement = transform_statement(get_node("statement", ast_to_syntax[node]), node, map<str, *type>())
} }
} }
}) })
parse_tree->children.for_each(fun(child: *tree<symbol>) { parse_tree->children.for_each(fun(child: *tree<symbol>) {
if (child->data.name == "compiler_intrinsic") { if (child->data.name == "compiler_intrinsic") {
translation_unit->translation_unit.children.add(transform_compiler_intrinsic(child, translation_unit, map<string, *type>())) translation_unit->translation_unit.children.add(transform_compiler_intrinsic(child, translation_unit, map<str, *type>()))
} }
}) })
} }
@@ -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 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)) if (is_template(method))
return 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 template_types = template->template.template_types
var real_types = template->template.instantiated_map.reverse_get(partially_inst_type_def) var real_types = template->template.instantiated_map.reverse_get(partially_inst_type_def)
var replacements = map<string, *type>() var replacements = map<str, *type>()
for (var i = 0; i < template_types.size; i++;) for (var i = 0; i < template_types.size; i++;)
replacements.set(template_types[i], real_types[i].clone()) replacements.set(template_types[i], real_types[i].clone())
method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, replacements) method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, replacements)
}) })
} }
} }
fun transform_type(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *type { fun transform_type(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *type {
// check for references and step down // check for references and step down
// always get to pre-reffed level // always get to pre-reffed level
var is_ref = get_node("\"ref\"", node) != null<tree<symbol>>() var is_ref = get_node("\"ref\"", node) != null<tree<symbol>>()
@@ -332,7 +332,7 @@ obj ast_transformation (Object) {
var real_types_deref = real_types.map(fun(t:*type):type return *t;) var real_types_deref = real_types.map(fun(t:*type):type return *t;)
var results = scope_lookup(name, scope) var results = scope_lookup(name, scope)
var fitting_types = vector<pair<*ast_node, int>>() var fitting_types = vec<pair<*ast_node, int>>()
for (var i = 0; i < results.size; i++;) { for (var i = 0; i < results.size; i++;) {
if (!is_template(results[i]) || results[i]->template.is_function) if (!is_template(results[i]) || results[i]->template.is_function)
continue continue
@@ -343,7 +343,7 @@ obj ast_transformation (Object) {
var num_satisfied_traits = 0 var num_satisfied_traits = 0
var satisfied_traits = true 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++;) { 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) && 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) (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)) { if (results[i]->template.instantiated_map.contains_key(real_types_deref)) {
inst_type = results[i]->template.instantiated_map[real_types_deref] inst_type = results[i]->template.instantiated_map[real_types_deref]
} else { } else {
var typeStr = string() var typeStr = str()
real_types_deref.for_each(fun(t: type) typeStr += t.to_string(false) + " ";) real_types_deref.for_each(fun(t: type) typeStr += t.to_string(false) + " ";)
results[i]->template.instantiated_map.for_each(fun(key: vector<type>, value: *ast_node) { results[i]->template.instantiated_map.for_each(fun(key: vec<type>, value: *ast_node) {
var hasTypStr = string() var hasTypStr = str()
key.for_each(fun(t: type) hasTypStr += t.to_string(false) + " ";) key.for_each(fun(t: type) hasTypStr += t.to_string(false) + " ";)
if (typeStr == hasTypStr) if (typeStr == hasTypStr)
error(node, "they're equal but really shouldnt be") 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) return type_ptr(base_type::none(), indirection, is_ref)
} }
} }
fun transform(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node return transform(node, scope, search_type::none(), template_replacements) fun transform(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node return transform(node, scope, search_type::none(), template_replacements)
fun transform(node: *tree<symbol>, scope: *ast_node, searching_for: search_type, template_replacements: map<string, *type>): *ast_node { fun transform(node: *tree<symbol>, scope: *ast_node, searching_for: search_type, template_replacements: map<str, *type>): *ast_node {
var name = node->data.name var name = node->data.name
if (name == "identifier" || name == "scoped_identifier") { if (name == "identifier" || name == "scoped_identifier") {
return transform_identifier(node, scope, searching_for) return transform_identifier(node, scope, searching_for)
@@ -488,7 +488,7 @@ obj ast_transformation (Object) {
error(node, "FAILED TO TRANSFORM") error(node, "FAILED TO TRANSFORM")
return null<ast_node>() return null<ast_node>()
} }
fun transform_all(nodes: vector<*tree<symbol>>, scope: *ast_node, template_replacements: map<string, *type>): vector<*ast_node> { fun transform_all(nodes: vec<*tree<symbol>>, scope: *ast_node, template_replacements: map<str, *type>): vec<*ast_node> {
return nodes.map(fun(node: *tree<symbol>): *ast_node return transform(node, scope, template_replacements);) return nodes.map(fun(node: *tree<symbol>): *ast_node return transform(node, scope, template_replacements);)
} }
fun transform_identifier(node: *tree<symbol>, scope: *ast_node, searching_for: search_type): *ast_node { fun transform_identifier(node: *tree<symbol>, scope: *ast_node, searching_for: search_type): *ast_node {
@@ -496,7 +496,7 @@ obj ast_transformation (Object) {
var name = concat_symbol_tree(node) var name = concat_symbol_tree(node)
if (name == "this") { if (name == "this") {
while (!is_function(scope) || scope->function.this_param == null<ast_node>()) while (!is_function(scope) || scope->function.this_param == null<ast_node>())
scope = get_ast_scope(scope)->get(string("~enclosing_scope"))[0] scope = get_ast_scope(scope)->get(str("~enclosing_scope"))[0]
if (!is_function(scope)) if (!is_function(scope))
error(node, "Couldn't find this") error(node, "Couldn't find this")
return scope->function.this_param return scope->function.this_param
@@ -530,7 +530,7 @@ obj ast_transformation (Object) {
if (value_str.length() > 3 && value_str[1] == '"' && value_str[2] == '"') { if (value_str.length() > 3 && value_str[1] == '"' && value_str[2] == '"') {
value_str = value_str.slice(3,-4) value_str = value_str.slice(3,-4)
} else { } else {
var new_str.construct(end-start): string var new_str.construct(end-start): str
var escaped = false var escaped = false
for (var i = 1; i < value_str.length()-1; i++;) { for (var i = 1; i < value_str.length()-1; i++;) {
if (escaped) { if (escaped) {
@@ -588,7 +588,7 @@ obj ast_transformation (Object) {
} }
return ast_value_ptr(value_str, value_type) return ast_value_ptr(value_str, value_type)
} }
fun transform_code_block(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_code_block(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var new_block = ast_code_block_ptr() var new_block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", scope, new_block) add_to_scope("~enclosing_scope", scope, new_block)
new_block->code_block.children = transform_all(node->children, new_block, template_replacements) 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<symbol>, scope: *ast_node): *ast_node { fun transform_if_comp(node: *tree<symbol>, scope: *ast_node): *ast_node {
var new_if_comp = ast_if_comp_ptr() 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.wanted_generator = concat_symbol_tree(get_node("identifier", node))
new_if_comp->if_comp.statement = transform_statement(get_node("statement", node), scope, map<string, *type>()) new_if_comp->if_comp.statement = transform_statement(get_node("statement", node), scope, map<str, *type>())
return new_if_comp return new_if_comp
} }
fun transform_simple_passthrough(node: *tree<symbol>, scope: *ast_node): *ast_node { fun transform_simple_passthrough(node: *tree<symbol>, scope: *ast_node): *ast_node {
var new_passthrough = ast_simple_passthrough_ptr() 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) 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) var passthrough_params = get_node("passthrough_params", node)
if (!passthrough_params) if (!passthrough_params)
return new_passthrough 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) new_passthrough->simple_passthrough.linker_str = concat_symbol_tree(linker_str).slice(1,-2)
return new_passthrough return new_passthrough
} }
fun transform_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
return transform(node->children[0], scope, template_replacements); return transform(node->children[0], scope, template_replacements);
} }
fun transform_declaration_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_declaration_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
// this might have an init position method call // this might have an init position method call
var identifiers = get_nodes("identifier", node) var identifiers = get_nodes("identifier", node)
var name = concat_symbol_tree(identifiers[0]) var name = concat_symbol_tree(identifiers[0])
@@ -663,7 +663,7 @@ obj ast_transformation (Object) {
} }
return declaration return declaration
} }
fun transform_assignment_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_assignment_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var to_assign = transform(get_node("boolean_expression", node), scope, template_replacements) var to_assign = transform(get_node("boolean_expression", node), scope, template_replacements)
// for []= overloading // for []= overloading
if (get_node("\"=\"", node)) { if (get_node("\"=\"", node)) {
@@ -673,7 +673,7 @@ obj ast_transformation (Object) {
if (get_node("\"]\"", inner_unarad)) { if (get_node("\"]\"", inner_unarad)) {
var assign_to = transform(get_node("unarad", inner_unarad), scope, template_replacements) 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 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) { if (possible_bracket_assign) {
return 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) var assign_to = transform(get_node("factor", node), scope, template_replacements)
if (get_node("\"=\"", node)) { 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) { if (possible_assign) {
return possible_assign return possible_assign
} }
} else if (get_node("\"\\+=\"", node)) { } 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) { if (possible_assign) {
return 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)) { } 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) { if (possible_assign) {
return 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)) { } 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) { if (possible_assign) {
return 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)){ } 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) { if (possible_assign) {
return 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)){ } 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) { if (possible_assign) {
return 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) var assignment = ast_assignment_statement_ptr(assign_to, to_assign)
return assignment return assignment
} }
fun transform_if_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_if_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
if (get_node("AmbiguityInner", node)) if (get_node("AmbiguityInner", node))
error(node, "Ambigious two ifs with one else!") 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)) 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] if_statement->if_statement.else_part = statements[1]
return if_statement return if_statement
} }
fun transform_while_loop(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_while_loop(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var while_loop = ast_while_loop_ptr(transform_expression(get_node("boolean_expression", node), scope, template_replacements)) 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) add_to_scope("~enclosing_scope", scope, while_loop)
while_loop->while_loop.statement = transform(get_node("statement", node), while_loop, template_replacements) while_loop->while_loop.statement = transform(get_node("statement", node), while_loop, template_replacements)
return while_loop return while_loop
} }
fun transform_for_loop(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_for_loop(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var for_loop = ast_for_loop_ptr() var for_loop = ast_for_loop_ptr()
add_to_scope("~enclosing_scope", scope, for_loop) add_to_scope("~enclosing_scope", scope, for_loop)
var statements = get_nodes("statement", node) 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) for_loop->for_loop.body = transform(statements[2], for_loop, template_replacements)
return for_loop return for_loop
} }
fun transform_return_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_return_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var return_value = get_node("boolean_expression", node) var return_value = get_node("boolean_expression", node)
var to_ret: *ast_node var to_ret: *ast_node
if (return_value) 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::break_stmt())
return ast_branching_statement_ptr(branching_type::continue_stmt()) return ast_branching_statement_ptr(branching_type::continue_stmt())
} }
fun transform_defer_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_defer_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
return ast_defer_statement_ptr(transform(node->children[0], scope, template_replacements)) return ast_defer_statement_ptr(transform(node->children[0], scope, template_replacements))
} }
fun transform_match_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_match_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var to_ret = ast_match_statement_ptr(transform(get_node("boolean_expression", node), scope, template_replacements)) 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<symbol>) to_ret->match_statement.cases.add(transform_case_statement(syntax, scope, template_replacements));) get_nodes("case_statement", node).for_each(fun(syntax: *tree<symbol>) to_ret->match_statement.cases.add(transform_case_statement(syntax, scope, template_replacements));)
return to_ret return to_ret
} }
fun transform_case_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_case_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var to_ret = ast_case_statement_ptr() 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);) 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) 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_adt = the_adts[0]
var the_option_name = concat_symbol_tree(get_node("identifier", get_node("scoped_identifier", node))) var the_option_name = concat_symbol_tree(get_node("identifier", get_node("scoped_identifier", node)))
// ADD IN ERROR CHECKING HERE // 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) to_ret->case_statement.statement = transform(get_node("statement", node), to_ret, template_replacements)
return to_ret return to_ret
} }
fun transform_function_call(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_function_call(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
// don't bother with a full transform for parameters with their own function, just get the boolean expression and transform it // 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<symbol>): *ast_node return transform(get_node("boolean_expression", child), scope, template_replacements);) var parameters = get_nodes("parameter", node).map(fun(child: *tree<symbol>): *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);) 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) var f = ast_function_call_ptr(func, parameters)
return f return f
} }
fun transform_compiler_intrinsic(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_compiler_intrinsic(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var parameters = get_nodes("parameter", node).map(fun(child: *tree<symbol>): *ast_node return transform(get_node("boolean_expression", child), scope, template_replacements);) var parameters = get_nodes("parameter", node).map(fun(child: *tree<symbol>): *ast_node return transform(get_node("boolean_expression", child), scope, template_replacements);)
var type_parameters = get_nodes("type", node).map(fun(child: *tree<symbol>): *type return transform_type(child, scope, template_replacements);) var type_parameters = get_nodes("type", node).map(fun(child: *tree<symbol>): *type return transform_type(child, scope, template_replacements);)
var intrinsic_name = concat_symbol_tree(get_node("identifier", node)) 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()) intrinsic_return_type = type_ptr(base_type::ulong_int())
return ast_compiler_intrinsic_ptr(intrinsic_name, parameters, type_parameters, intrinsic_return_type) return ast_compiler_intrinsic_ptr(intrinsic_name, parameters, type_parameters, intrinsic_return_type)
} }
fun transform_lambda(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node { fun transform_lambda(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var function_node = second_pass_function(node, scope, template_replacements, false) 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) 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) scope->translation_unit.lambdas.add(function_node)
return function_node return function_node
} }
fun transform_expression(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node fun transform_expression(node: *tree<symbol>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node
return transform_expression(node, scope, search_type::none(), template_replacements) return transform_expression(node, scope, search_type::none(), template_replacements)
fun transform_expression(node: *tree<symbol>, scope: *ast_node, searching_for: search_type, template_replacements: map<string, *type>): *ast_node { fun transform_expression(node: *tree<symbol>, scope: *ast_node, searching_for: search_type, template_replacements: map<str, *type>): *ast_node {
var func_name = string() var func_name = str()
var parameters = vector<*ast_node>() var parameters = vec<*ast_node>()
if (node->children.size == 1) { if (node->children.size == 1) {
var possible_value = transform(node->children[0], scope, searching_for, template_replacements) var possible_value = transform(node->children[0], scope, searching_for, template_replacements)
if (!possible_value) match (searching_for) { 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<tree<symbol>>(), scope, type_vec, template_replacements, map<string, *type>()); search_type::function(type_vec) possible_value = find_or_instantiate_template_function(concat_symbol_tree(node->children[0]), null<tree<symbol>>(), scope, type_vec, template_replacements, map<str, *type>());
} }
if (!possible_value) if (!possible_value)
error(node, concat_symbol_tree(node) + ": HAS NO POSSIBLE FUNCTION OR FUNCTION TEMPLATE SOLUTIONS\nlooking for: " + 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) if (n)
return s+","+n->to_string() return s+","+n->to_string()
else else
return s+",null" return s+",null"
}, string()) + ")") }, str()) + ")")
return possible_value return possible_value
} else if (node->children.size == 2) { } else if (node->children.size == 2) {
var template_inst = get_node("template_inst", node) var template_inst = get_node("template_inst", node)
@@ -855,7 +855,7 @@ obj ast_transformation (Object) {
match (searching_for) { match (searching_for) {
// I guess this should never happen? // I guess this should never happen?
search_type::none() error(template_inst, "TEMPLATE LOOKUP WITHOUT PERENS ()") 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<string, *type>()) search_type::function(type_vec) result = find_or_instantiate_template_function(concat_symbol_tree(identifier), template_inst, scope, type_vec, template_replacements, map<str, *type>())
} }
if (!result) if (!result)
error(node, "Could not find templated function " + concat_symbol_tree(identifier) + " even though had a template_inst") 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 == "++") { if (check_if_post == "--" || check_if_post == "++") {
// give the post-operators a special suffix so the c_generator knows to emit them 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" 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 { } else {
func_name = concat_symbol_tree(node->children[0]) 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 { } else {
func_name = concat_symbol_tree(node->children[1]) func_name = concat_symbol_tree(node->children[1])
@@ -890,8 +890,8 @@ obj ast_transformation (Object) {
if (!second_param) match (searching_for) { if (!second_param) match (searching_for) {
search_type::function(type_vec) { search_type::function(type_vec) {
var template_inst = get_node("template_inst", node) var template_inst = get_node("template_inst", node)
var inherited_replacements = map<string, *type>() var inherited_replacements = map<str, *type>()
var parent = get_ast_scope(get_ast_type(first_param)->type_def)->get(string("~enclosing_scope"))[0] var parent = get_ast_scope(get_ast_type(first_param)->type_def)->get(str("~enclosing_scope"))[0]
if (is_template(parent)) if (is_template(parent))
for (var i = 0; i < parent->template.template_types.size; i++;) 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() 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); 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) { if (!second_param) {
error(node, "Could not find method " + method_name + " on the right side of (. or ->) " + concat_symbol_tree(node->children[0]) + 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 // 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) { 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]) + 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 { } else {
second_param = transform(node->children[2], scope, template_replacements) 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?)");) 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);) 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 possible_overload_call
return ast_function_call_ptr(get_builtin_function(func_name, parameter_types, node), parameters) 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<string, *type>): *ast_node { fun find_and_make_any_operator_overload_call(func_name: str, parameters: vec<*ast_node>, scope: *ast_node, template_replacements: map<str, *type>): *ast_node {
var parameter_types = parameters.map(fun(param: *ast_node): *type return get_ast_type(param);) var parameter_types = parameters.map(fun(param: *ast_node): *type return get_ast_type(param);)
var possible_overload = null<ast_node>() var possible_overload = null<ast_node>()
if (parameter_types[0]->is_object() && parameter_types[0]->indirection == 0) { 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) { if (!possible_overload) {
var inherited_replacements = map<string, *type>() var inherited_replacements = map<str, *type>()
var parent = get_ast_scope(parameter_types.first()->type_def)->get(string("~enclosing_scope"))[0] var parent = get_ast_scope(parameter_types.first()->type_def)->get(str("~enclosing_scope"))[0]
if (is_template(parent)) { if (is_template(parent)) {
for (var i = 0; i < parent->template.template_types.size; i++;) 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() 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<tree<symbol>>(), 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<tree<symbol>>(), parameter_types.first()->type_def, parameter_types.slice(1,-1), template_replacements, inherited_replacements)
} }
if (possible_overload) if (possible_overload)
return make_method_call(parameters.first(), possible_overload, parameters.slice(1,-1)) 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) if (!possible_overload)
possible_overload = find_or_instantiate_template_function(string("operator")+func_name, null<tree<symbol>>(), scope, parameter_types, template_replacements, map<string, *type>()) possible_overload = find_or_instantiate_template_function(str("operator")+func_name, null<tree<symbol>>(), scope, parameter_types, template_replacements, map<str, *type>())
if (possible_overload) if (possible_overload)
return ast_function_call_ptr(possible_overload, parameters) return ast_function_call_ptr(possible_overload, parameters)
return null<ast_node>() return null<ast_node>()
} }
fun find_or_instantiate_template_function(name: string, template_inst: *tree<symbol>, scope: *ast_node, param_types: vector<*type>, template_replacements: map<string, *type>, replacements_base: map<string, *type>): *ast_node { fun find_or_instantiate_template_function(name: str, template_inst: *tree<symbol>, scope: *ast_node, param_types: vec<*type>, template_replacements: map<str, *type>, replacements_base: map<str, *type>): *ast_node {
// replacments base is for templated methods starting off with the replacements of their parent (possibly templated) object // replacments base is for templated methods starting off with the replacements of their parent (possibly templated) object
var results = scope_lookup(name, scope) var results = scope_lookup(name, scope)
var real_types = vector<*type>() var real_types = vec<*type>()
var real_types_deref = vector<type>() var real_types_deref = vec<type>()
var had_real_types = false var had_real_types = false
if (template_inst) { if (template_inst) {
real_types = get_nodes("type", template_inst).map(fun(t: *tree<symbol>): *type return transform_type(t, scope, template_replacements);) real_types = get_nodes("type", template_inst).map(fun(t: *tree<symbol>): *type return transform_type(t, scope, template_replacements);)
real_types_deref = real_types.map(fun(t:*type):type return *t;) real_types_deref = real_types.map(fun(t:*type):type return *t;)
had_real_types = true had_real_types = true
} }
var fitting_functions = vector<pair<*ast_node, int>>() var fitting_functions = vec<pair<*ast_node, int>>()
for (var i = 0; i < results.size; i++;) { for (var i = 0; i < results.size; i++;) {
if (is_template(results[i]) && results[i]->template.is_function) { if (is_template(results[i]) && results[i]->template.is_function) {
var template_types = results[i]->template.template_types var template_types = results[i]->template.template_types
@@ -965,8 +965,8 @@ obj ast_transformation (Object) {
if (!had_real_types) { if (!had_real_types) {
var unify_template_type_replacements = template_type_replacements var unify_template_type_replacements = template_type_replacements
// reset the vars, cuz we might be iterating through multiple of them // reset the vars, cuz we might be iterating through multiple of them
real_types = vector<*type>() real_types = vec<*type>()
real_types_deref = vector<type>() real_types_deref = vec<type>()
// Template Function Instance Inference time // Template Function Instance Inference time
var typed_params = get_nodes("typed_parameter", results[i]->template.syntax_node).map(fun(t: *tree<symbol>): *tree<symbol> return get_node("type",t);) var typed_params = get_nodes("typed_parameter", results[i]->template.syntax_node).map(fun(t: *tree<symbol>): *tree<symbol> return get_node("type",t);)
if (param_types.size != typed_params.size) if (param_types.size != typed_params.size)
@@ -982,7 +982,7 @@ obj ast_transformation (Object) {
continue continue
var num_satisfied_traits = 0 var num_satisfied_traits = 0
var satisfied_traits = true 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 // check if already instantiated
var inst_func = null<ast_node>() var inst_func = null<ast_node>()
for (var j = 0; j < template_types.size; j++;) { 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) (real_types[j]->indirection == 0 || template_type_replacements[template_types[j]]->traits.size() == 0)
template_type_replacements[template_types[j]] = real_types[j] 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 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 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<symbol>, param_type: *type, new_map: *map<string, *type>, template_replacements: map<string, *type>) { fun unify_type(template_type: *tree<symbol>, param_type: *type, new_map: *map<str, *type>, template_replacements: map<str, *type>) {
// first get rid of the reference if we have it - we don't care for unification // first get rid of the reference if we have it - we don't care for unification
if (get_node("pre_reffed", template_type)) if (get_node("pre_reffed", template_type))
template_type = get_node("pre_reffed", template_type) template_type = get_node("pre_reffed", template_type)
@@ -1052,7 +1052,7 @@ fun unify_type(template_type: *tree<symbol>, param_type: *type, new_map: *map<st
else return; else return;
} else if (get_node("template_inst", template_type)) { } else if (get_node("template_inst", template_type)) {
if (param_type->is_object()) { if (param_type->is_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)) { if (is_template(enclosing_template)) {
var inst_params = enclosing_template->template.instantiated_map.reverse_get(param_type->type_def) 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)) var template_params = get_nodes("type", get_node("template_inst", template_type))

View File

@@ -4,7 +4,7 @@ import mem:*
import map:* import map:*
import hash_map:* import hash_map:*
import stack:* import stack:*
import string:* import str:*
import util:* import util:*
import tree:* import tree:*
import symbol:* import symbol:*
@@ -53,7 +53,7 @@ fun type_size_and_alignment(t: *type): pair<ulong,ulong> {
base_type::floating() return make_pair(#sizeof<float>, #sizeof<float>) base_type::floating() return make_pair(#sizeof<float>, #sizeof<float>)
base_type::double_precision() return make_pair(#sizeof<double>, #sizeof<double>) base_type::double_precision() return make_pair(#sizeof<double>, #sizeof<double>)
} }
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 { 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 == 2) return operand_size::b16()
if (size == 4) return operand_size::b32() if (size == 4) return operand_size::b32()
if (size == 8) return operand_size::b64() if (size == 8) return operand_size::b64()
error(string("invalid operand size ") + size) error(str("invalid operand size ") + size)
} }
adt byte_inst { adt byte_inst {
nop, nop,
@@ -163,57 +163,57 @@ obj test {
var offset: long var offset: long
} }
fun to_string(s: operand_size): string { fun to_string(s: operand_size): str {
match (s) { match (s) {
operand_size::b8() return string("8") operand_size::b8() return str("8")
operand_size::b16() return string("16") operand_size::b16() return str("16")
operand_size::b32() return string("32") operand_size::b32() return str("32")
operand_size::b64() return string("64") 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) { match (b) {
byte_inst::nop() return string("nop") byte_inst::nop() return str("nop")
byte_inst::imm(i) return string("r") + i.to_reg + " = imm " + i.val byte_inst::imm(i) return str("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::add(a) return str("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::addi(a) return str("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::smul(a) return str("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::umul(a) return str("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::sdiv(a) return str("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::udiv(a) return str("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::mod(a) return str("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::and(a) return str("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::shl(a) return str("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::shr(a) return str("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::sar(a) return str("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::or(a) return str("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::xor(a) return str("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::not(a) return str("r") + a.to_reg + " = ~r" + a.a
byte_inst::gz(a) return string("r") + a.to_reg + " = r" + a.a + " > 0" byte_inst::gz(a) return str("r") + a.to_reg + " = r" + a.a + " > 0"
byte_inst::lz(a) return string("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 string("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 string("r") + l.reg + " = ldr" + to_string(l.size) + " r" + l.base_reg + " (" + l.offset + ")" 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::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::jmp(j) return str("jmp(pc += ") + j + ")"
byte_inst::jz(j) return string("jmp(r") + j.reg + " == 0, pc += " + j.offset + ")" byte_inst::jz(j) return str("jmp(r") + j.reg + " == 0, pc += " + j.offset + ")"
byte_inst::call(c) return string("call pc = r") + c byte_inst::call(c) return str("call pc = r") + c
byte_inst::ret() return string("ret") 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<bytecode_function>, instructions: ref vector<byte_inst>): string { fun bytecode_to_string(functions: ref vec<bytecode_function>, instructions: ref vec<byte_inst>): str {
return string("\n").join(functions.map(fun(bb: ref bytecode_function): string return bb.to_string(instructions);)) 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 var to_ret.construct(name, start): bytecode_function
return to_ret return to_ret
} }
obj bytecode_function (Object) { obj bytecode_function (Object) {
var name: string var name: str
var instruction_start: int var instruction_start: int
var instruction_end: int var instruction_end: int
var var_to_frame_offset: map<*ast_node, int> var var_to_frame_offset: map<*ast_node, int>
@@ -227,7 +227,7 @@ obj bytecode_function (Object) {
frame_size = 0 frame_size = 0
return this 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_start = instruction_start_in
instruction_end = 0 instruction_end = 0
name.copy_construct(&name_in) name.copy_construct(&name_in)
@@ -250,7 +250,7 @@ obj bytecode_function (Object) {
name.destruct() name.destruct()
var_to_frame_offset.destruct() var_to_frame_offset.destruct()
} }
fun to_string(instructions: ref vector<byte_inst>): string { fun to_string(instructions: ref vec<byte_inst>): str {
var res = name + "(frame size " + frame_size + "):\n" var res = name + "(frame size " + frame_size + "):\n"
res += "\t frame layout\n" res += "\t frame layout\n"
res += "\t\tsaved RBP : RPB = 0\n" res += "\t\tsaved RBP : RPB = 0\n"
@@ -259,7 +259,7 @@ obj bytecode_function (Object) {
}) })
res += "\n\t bytecode\n" res += "\n\t bytecode\n"
for (var i = instruction_start; i < instruction_end; i++;) 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 return res
} }
} }
@@ -268,13 +268,13 @@ obj bytecode_generator (Object) {
var reg_counter: int var reg_counter: int
var reg_max: int var reg_max: int
var id_counter: int var id_counter: int
var ast_name_map: hash_map<*ast_node, string> var ast_name_map: hash_map<*ast_node, str>
var functions: vector<bytecode_function> var functions: vec<bytecode_function>
var node_function_idx: map<*ast_node, int> var node_function_idx: map<*ast_node, int>
var instructions: vector<byte_inst> var instructions: vec<byte_inst>
var fixup_function_addresses: vector<pair<int, *ast_node>> var fixup_function_addresses: vec<pair<int, *ast_node>>
var fixup_break_addresses: stack<vector<int>> var fixup_break_addresses: stack<vec<int>>
var fixup_continue_addresses: stack<vector<int>> var fixup_continue_addresses: stack<vec<int>>
fun construct(): *bytecode_generator { fun construct(): *bytecode_generator {
id_counter = 0 id_counter = 0
ast_name_map.construct() ast_name_map.construct()
@@ -314,7 +314,7 @@ obj bytecode_generator (Object) {
fixup_break_addresses.destruct() fixup_break_addresses.destruct()
fixup_continue_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 get_reg(): int return reg_counter++;
fun peek_reg(): int return reg_counter; fun peek_reg(): int return reg_counter;
fun reset_reg() reset_reg(3); fun reset_reg() reset_reg(3);
@@ -324,11 +324,11 @@ obj bytecode_generator (Object) {
} }
reg_counter = to reg_counter = to
} }
/*fun generate_bytecode(name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>): pair<vector<bytecode_function>, vector<byte_inst>> {*/ /*fun generate_bytecode(name_ast_map: map<str, pair<*tree<symbol>,*ast_node>>): pair<vec<bytecode_function>, vec<byte_inst>> {*/
fun generate_bytecode(name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>) { fun generate_bytecode(name_ast_map: map<str, pair<*tree<symbol>,*ast_node>>) {
// iterate through asts // iterate through asts
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree<symbol>,*ast_node>) {
// iterate through children for each ast // iterate through children for each ast
tree_pair.second->translation_unit.lambdas.for_each(fun(child: *ast_node) { tree_pair.second->translation_unit.lambdas.for_each(fun(child: *ast_node) {
generate_function_definition(child) generate_function_definition(child)
@@ -502,8 +502,8 @@ obj bytecode_generator (Object) {
var jz_index = instructions.size var jz_index = instructions.size
emit_jz(cond_reg,0) emit_jz(cond_reg,0)
reset_reg() reset_reg()
fixup_break_addresses.push(vector<int>()) fixup_break_addresses.push(vec<int>())
fixup_continue_addresses.push(vector<int>()) fixup_continue_addresses.push(vec<int>())
generate(node->while_loop.statement) generate(node->while_loop.statement)
reset_reg() reset_reg()
emit_jmp(top_index - instructions.size) emit_jmp(top_index - instructions.size)
@@ -529,8 +529,8 @@ obj bytecode_generator (Object) {
var jz_index = instructions.size var jz_index = instructions.size
emit_jz(cond_reg,0) emit_jz(cond_reg,0)
reset_reg() reset_reg()
fixup_break_addresses.push(vector<int>()) fixup_break_addresses.push(vec<int>())
fixup_continue_addresses.push(vector<int>()) fixup_continue_addresses.push(vec<int>())
generate(node->for_loop.body) generate(node->for_loop.body)
reset_reg() reset_reg()
@@ -853,7 +853,7 @@ obj bytecode_generator (Object) {
} }
error("Bad node") 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); var maybe_it = ast_name_map.get_ptr_or_null(node);
if (maybe_it) if (maybe_it)
return *maybe_it return *maybe_it
@@ -1073,7 +1073,7 @@ obj bytecode_generator (Object) {
println("evaling main") println("evaling main")
println(bytecode_to_string(functions, instructions)) println(bytecode_to_string(functions, instructions))
var main_entry = functions.find_first_satisfying(fun(block: bytecode_function): bool return block.name == "main";) var main_entry = functions.find_first_satisfying(fun(block: bytecode_function): bool return block.name == "main";)
var registers.construct(reg_max): vector<long> var registers.construct(reg_max): vec<long>
registers.size = reg_max registers.size = reg_max
registers[1] = 0xdeadbeefcafebabe registers[1] = 0xdeadbeefcafebabe
var stack_size = 8 * 1024 * 1024 var stack_size = 8 * 1024 * 1024
@@ -1083,7 +1083,7 @@ obj bytecode_generator (Object) {
stack[-i + -1] = 0 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 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++;) { 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]) { match(instructions[i]) {
byte_inst::nop() {} byte_inst::nop() {}
byte_inst::imm(i) registers[i.to_reg] = i.val byte_inst::imm(i) registers[i.to_reg] = i.val
@@ -1206,7 +1206,7 @@ obj bytecode_generator (Object) {
*(registers[0] + #sizeof<*char> + #sizeof<ulong>) cast **char, *(registers[0] + #sizeof<*char> + #sizeof<ulong>) cast **char,
*(registers[0] + #sizeof<*char> + #sizeof<ulong> + #sizeof<*char>) cast *double)) cast long *(registers[0] + #sizeof<*char> + #sizeof<ulong> + #sizeof<*char>) cast *double)) cast long
else else
error(string("bad extern call number") + func_start) error(str("bad extern call number") + func_start)
} else { } else {
/*registers[0] -= register_size*/ /*registers[0] -= register_size*/
registers[0] = 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("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)")*/ /*println("first part of memory is (after push)")*/
/*for (var i = 0; i < 8*8; i+=8;) {*/ /*for (var i = 0; i < 8*8; i+=8;) {*/
/*print(string("-") + i + string(": "))*/ /*print(str("-") + i + str(": "))*/
/*for (var j = 0; j < 8; j++;) {*/ /*for (var j = 0; j < 8; j++;) {*/
/*if (j == 4)*/ /*if (j == 4)*/
/*print(" ")*/ /*print(" ")*/
@@ -1235,7 +1235,7 @@ obj bytecode_generator (Object) {
/*print("returning!")*/ /*print("returning!")*/
/*println("first part of memory is")*/ /*println("first part of memory is")*/
/*for (var i = 0; i < 8*8; i+=8;) {*/ /*for (var i = 0; i < 8*8; i+=8;) {*/
/*print(string("-") + i + string(": "))*/ /*print(str("-") + i + str(": "))*/
/*for (var j = 0; j < 8; j++;) {*/ /*for (var j = 0; j < 8; j++;) {*/
/*if (j == 4)*/ /*if (j == 4)*/
/*print(" ")*/ /*print(" ")*/
@@ -1246,13 +1246,13 @@ obj bytecode_generator (Object) {
/*}*/ /*}*/
/*println("Done")*/ /*println("Done")*/
if (pc == 0) { 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] var value = registers[2]
println(string("returning from main, value is ") + value) println(str("returning from main, value is ") + value)
return value return value
} else { } else {
i = pc - 1 i = pc - 1
/*println(string("returning to ") + pc)*/ /*println(str("returning to ") + pc)*/
} }
} }
} }

View File

@@ -3,7 +3,7 @@ import mem:*
import map:* import map:*
import hash_map:* import hash_map:*
import stack:* import stack:*
import string:* import str:*
import util:* import util:*
import tree:* import tree:*
import symbol:* import symbol:*
@@ -14,69 +14,69 @@ import poset:*
obj c_generator (Object) { obj c_generator (Object) {
var id_counter: int var id_counter: int
var ast_name_map: hash_map<*ast_node, string> var ast_name_map: hash_map<*ast_node, str>
var used_names: hash_set<string> var used_names: hash_set<str>
var function_type_map: map<type, string> var function_type_map: map<type, str>
var replacement_map: map<string, string> var replacement_map: map<str, str>
var longest_replacement: int var longest_replacement: int
var function_typedef_string: string var function_typedef_string: str
var linker_string: string var linker_string: str
fun construct(): *c_generator { fun construct(): *c_generator {
id_counter = 0 id_counter = 0
ast_name_map.construct() ast_name_map.construct()
used_names.construct() used_names.construct()
// to avoid using c keywords // to avoid using c keywords
used_names.add(string("extern")) used_names.add(str("extern"))
used_names.add(string("register")) used_names.add(str("register"))
function_type_map.construct() function_type_map.construct()
function_typedef_string.construct() function_typedef_string.construct()
linker_string.construct() linker_string.construct()
replacement_map.construct() replacement_map.construct()
replacement_map[string("+")] = string("plus") replacement_map[str("+")] = str("plus")
replacement_map[string("-")] = string("minus") replacement_map[str("-")] = str("minus")
replacement_map[string("*")] = string("star") replacement_map[str("*")] = str("star")
replacement_map[string("/")] = string("div") replacement_map[str("/")] = str("div")
replacement_map[string("%")] = string("mod") replacement_map[str("%")] = str("mod")
replacement_map[string("^")] = string("carat") replacement_map[str("^")] = str("carat")
replacement_map[string("&")] = string("amprsd") replacement_map[str("&")] = str("amprsd")
replacement_map[string("|")] = string("pipe") replacement_map[str("|")] = str("pipe")
replacement_map[string("~")] = string("tilde") replacement_map[str("~")] = str("tilde")
replacement_map[string("!")] = string("exlmtnpt") replacement_map[str("!")] = str("exlmtnpt")
replacement_map[string(",")] = string("comma") replacement_map[str(",")] = str("comma")
replacement_map[string("=")] = string("eq") replacement_map[str("=")] = str("eq")
replacement_map[string("++")] = string("dbplus") replacement_map[str("++")] = str("dbplus")
replacement_map[string("--")] = string("dbminus") replacement_map[str("--")] = str("dbminus")
replacement_map[string("<<")] = string("dbleft") replacement_map[str("<<")] = str("dbleft")
replacement_map[string(">>")] = string("dbright") replacement_map[str(">>")] = str("dbright")
replacement_map[string("::")] = string("scopeop") replacement_map[str("::")] = str("scopeop")
replacement_map[string(":")] = string("colon") replacement_map[str(":")] = str("colon")
replacement_map[string("==")] = string("dbq") replacement_map[str("==")] = str("dbq")
replacement_map[string("!=")] = string("notequals") replacement_map[str("!=")] = str("notequals")
replacement_map[string("&&")] = string("doubleamprsnd") replacement_map[str("&&")] = str("doubleamprsnd")
replacement_map[string("||")] = string("doublepipe") replacement_map[str("||")] = str("doublepipe")
replacement_map[string("+=")] = string("plusequals") replacement_map[str("+=")] = str("plusequals")
replacement_map[string("-=")] = string("minusequals") replacement_map[str("-=")] = str("minusequals")
replacement_map[string("/=")] = string("divequals") replacement_map[str("/=")] = str("divequals")
replacement_map[string("%=")] = string("modequals") replacement_map[str("%=")] = str("modequals")
replacement_map[string("^=")] = string("caratequals") replacement_map[str("^=")] = str("caratequals")
replacement_map[string("&=")] = string("amprsdequals") replacement_map[str("&=")] = str("amprsdequals")
replacement_map[string("|=")] = string("pipeequals") replacement_map[str("|=")] = str("pipeequals")
replacement_map[string("*=")] = string("starequals") replacement_map[str("*=")] = str("starequals")
replacement_map[string("<<=")] = string("doublerightequals") replacement_map[str("<<=")] = str("doublerightequals")
replacement_map[string("<")] = string("lt") replacement_map[str("<")] = str("lt")
replacement_map[string(">")] = string("gt") replacement_map[str(">")] = str("gt")
replacement_map[string(">>=")] = string("doubleleftequals") replacement_map[str(">>=")] = str("doubleleftequals")
replacement_map[string("(")] = string("openparen") replacement_map[str("(")] = str("openparen")
replacement_map[string(")")] = string("closeparen") replacement_map[str(")")] = str("closeparen")
replacement_map[string("[")] = string("obk") replacement_map[str("[")] = str("obk")
replacement_map[string("]")] = string("cbk") replacement_map[str("]")] = str("cbk")
replacement_map[string(" ")] = string("_") replacement_map[str(" ")] = str("_")
replacement_map[string(".")] = string("dot") replacement_map[str(".")] = str("dot")
replacement_map[string("->")] = string("arrow") replacement_map[str("->")] = str("arrow")
longest_replacement = 0 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) if (key.length() > longest_replacement)
longest_replacement = key.length() longest_replacement = key.length()
}) })
@@ -105,12 +105,12 @@ obj c_generator (Object) {
replacement_map.destruct() replacement_map.destruct()
linker_string.destruct() linker_string.destruct()
} }
fun get_id(): string return to_string(id_counter++); fun get_id(): str return to_string(id_counter++);
fun generate_function_prototype_and_header(child: *ast_node):pair<string,string> { fun generate_function_prototype_and_header(child: *ast_node):pair<str,str> {
var backing = child->function var backing = child->function
var parameter_types = string() var parameter_types = str()
var parameters = string() var parameters = str()
var decorated_name = string() var decorated_name = str()
if (backing.is_extern) if (backing.is_extern)
decorated_name = backing.name 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", 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 + ")") type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameters + ")")
} }
fun generate_c(name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax_in: map<*ast_node, *tree<symbol>> ): pair<string,string> { fun generate_c(name_ast_map: map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax_in: map<*ast_node, *tree<symbol>> ): pair<str,str> {
var prequal: string = "#include <stdbool.h>\n" var prequal: str = "#include <stdbool.h>\n"
var plain_typedefs: string = "\n/**Plain Typedefs**/\n" var plain_typedefs: str = "\n/**Plain Typedefs**/\n"
var top_level_c_passthrough: string = "" var top_level_c_passthrough: str = ""
var variable_extern_declarations: string = "" var variable_extern_declarations: str = ""
var structs: string = "\n/**Type Structs**/\n" var structs: str = "\n/**Type Structs**/\n"
function_typedef_string = "\n/**Typedefs**/\n" function_typedef_string = "\n/**Typedefs**/\n"
var function_prototypes: string = "\n/**Function Prototypes**/\n" var function_prototypes: str = "\n/**Function Prototypes**/\n"
var function_definitions: string = "\n/**Function Definitions**/\n" var function_definitions: str = "\n/**Function Definitions**/\n"
var variable_declarations: string = "\n/**Variable Declarations**/\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...) // 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) { var generate_function_definition = fun(child: *ast_node) {
@@ -155,7 +155,7 @@ obj c_generator (Object) {
var type_poset = poset<*ast_node>() var type_poset = poset<*ast_node>()
// iterate through asts // iterate through asts
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree<symbol>,*ast_node>) {
// iterate through children for each ast // iterate through children for each ast
// do lambdas seperatly, so we can reconstitute the enclosing object if it has one // 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) { 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) { type_poset.get_sorted().for_each(fun(vert: *ast_node) {
var base_name = get_name(vert) var base_name = get_name(vert)
plain_typedefs += string("typedef ") plain_typedefs += str("typedef ")
if (vert->type_def.is_union) { if (vert->type_def.is_union) {
plain_typedefs += "union " plain_typedefs += "union "
structs += "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) 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 identifier = node->declaration_statement.identifier
var ident_type = identifier->identifier.type var ident_type = identifier->identifier.type
var to_ret = type_to_c(identifier->identifier.type) + " " + get_name(identifier) var to_ret = type_to_c(identifier->identifier.type) + " " + get_name(identifier)
@@ -241,27 +241,27 @@ obj c_generator (Object) {
} }
return to_ret 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) 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) + "}" var if_str = "if (" + generate(node->if_statement.condition) + ") {\n" + generate(node->if_statement.then_part) + "}"
if (node->if_statement.else_part) if (node->if_statement.else_part)
if_str += " else {\n" + generate(node->if_statement.else_part) + "}" if_str += " else {\n" + generate(node->if_statement.else_part) + "}"
return if_str + "\n" 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) return "while (" + generate(node->while_loop.condition) + ")\n" + generate(node->while_loop.statement)
} }
fun generate_for_loop(node: *ast_node): string { fun generate_for_loop(node: *ast_node): str {
var init = string(";") var init = str(";")
if (node->for_loop.init) if (node->for_loop.init)
init = generate(node->for_loop.init) init = generate(node->for_loop.init)
var cond = string(";") var cond = str(";")
if (node->for_loop.condition) if (node->for_loop.condition)
cond = generate(node->for_loop.condition) cond = generate(node->for_loop.condition)
// gotta take off last semicolon // gotta take off last semicolon
var update = string() var update = str()
if (node->for_loop.update) { if (node->for_loop.update) {
update = generate(node->for_loop.update) update = generate(node->for_loop.update)
if (update.length() < 2) if (update.length() < 2)
@@ -270,33 +270,33 @@ obj c_generator (Object) {
} }
return "for (" + init + cond + "; " + update + ")\n" + generate(node->for_loop.body) 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) if (get_ast_type(node)->is_ref)
error("still existin ref in identifier") error("still existin ref in identifier")
return get_name(node) 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) if (node->return_statement.return_value)
return "return " + generate(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) { match(node->branching_statement.b_type) {
branching_type::break_stmt() return string("break") branching_type::break_stmt() return str("break")
branching_type::continue_stmt() return string("continue") 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) + "))" 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 var value = node->value.string_value
if (node->value.value_type->base == base_type::character() && node->value.value_type->indirection == 0) if (node->value.value_type->base == base_type::character() && node->value.value_type->indirection == 0)
return "'" + value + "'" return "'" + value + "'"
if (node->value.value_type->base != base_type::character() || node->value.value_type->indirection != 1) if (node->value.value_type->base != base_type::character() || node->value.value_type->indirection != 1)
return value return value
var to_ret = string("\"") //" var to_ret = str("\"") //"
value.for_each(fun(c: char) { value.for_each(fun(c: char) {
if (c == '\n') if (c == '\n')
to_ret += "\\n" to_ret += "\\n"
@@ -309,18 +309,18 @@ obj c_generator (Object) {
}) })
return to_ret + "\"" return to_ret + "\""
} }
fun generate_code_block(node: *ast_node): string { fun generate_code_block(node: *ast_node): str {
var to_ret = string("{\n") var to_ret = str("{\n")
node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child) + ";\n";) node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child) + ";\n";)
return to_ret + "}" return to_ret + "}"
} }
// this generates the function as a value, not the actual function // 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) 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 func_name = generate(node->function_call.func)
var call_string = string() var call_string = str()
var func_return_type = get_ast_type(node) var func_return_type = get_ast_type(node)
var parameters = node->function_call.parameters var parameters = node->function_call.parameters
@@ -365,23 +365,23 @@ obj c_generator (Object) {
return call_string 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.intrinsic == "sizeof") {
if (node->compiler_intrinsic.parameters.size || node->compiler_intrinsic.type_parameters.size != 1) if (node->compiler_intrinsic.parameters.size || node->compiler_intrinsic.type_parameters.size != 1)
error("wrong parameters to sizeof compiler intrinsic") error("wrong parameters to sizeof compiler intrinsic")
return "sizeof(" + type_to_c(node->compiler_intrinsic.type_parameters[0]) + ")" return "sizeof(" + type_to_c(node->compiler_intrinsic.type_parameters[0]) + ")"
} else if (node->compiler_intrinsic.intrinsic == "link") { } else if (node->compiler_intrinsic.intrinsic == "link") {
node->compiler_intrinsic.parameters.for_each(fun(value: *ast_node) { 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") error(node->compiler_intrinsic.intrinsic + ": unknown intrinsic")
return string("ERROR") return str("ERROR")
} }
fun generate(node: *ast_node): string { fun generate(node: *ast_node): str {
if (!node) return string("/*NULL*/") if (!node) return str("/*NULL*/")
match (*node) { match (*node) {
ast_node::declaration_statement(backing) return generate_declaration_statement(node) ast_node::declaration_statement(backing) return generate_declaration_statement(node)
ast_node::assignment_statement(backing) return generate_assignment_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::value(backing) return generate_value(node)
ast_node::identifier(backing) return generate_identifier(node) ast_node::identifier(backing) return generate_identifier(node)
} }
error(string("COULD NOT GENERATE ") + get_ast_name(node)) error(str("COULD NOT GENERATE ") + get_ast_name(node))
return string("/* COULD NOT GENERATE */") return str("/* COULD NOT GENERATE */")
} }
fun type_to_c(type: *type): string { fun type_to_c(type: *type): str {
var indirection = string() var indirection = str()
if (type->is_ref) error("still ref in type_to_c") //indirection += "/*ref*/ *" if (type->is_ref) error("still ref in type_to_c") //indirection += "/*ref*/ *"
for (var i = 0; i < type->indirection; i++;) indirection += "*" for (var i = 0; i < type->indirection; i++;) indirection += "*"
match (type->base) { match (type->base) {
base_type::none() return string("none") + indirection base_type::none() return str("none") + indirection
base_type::template() return string("template") + indirection base_type::template() return str("template") + indirection
base_type::template_type() return string("template_type") + indirection base_type::template_type() return str("template_type") + indirection
base_type::void_return() return string("void") + indirection base_type::void_return() return str("void") + indirection
base_type::boolean() return string("bool") + indirection base_type::boolean() return str("bool") + indirection
base_type::character() return string("char") + indirection base_type::character() return str("char") + indirection
base_type::ucharacter() return string("unsigned char") + indirection base_type::ucharacter() return str("unsigned char") + indirection
base_type::short_int() return string("short") + indirection base_type::short_int() return str("short") + indirection
base_type::ushort_int() return string("unsigned short") + indirection base_type::ushort_int() return str("unsigned short") + indirection
base_type::integer() return string("int") + indirection base_type::integer() return str("int") + indirection
base_type::uinteger() return string("unsigned int") + indirection base_type::uinteger() return str("unsigned int") + indirection
base_type::long_int() return string("long") + indirection base_type::long_int() return str("long") + indirection
base_type::ulong_int() return string("unsigned long") + indirection base_type::ulong_int() return str("unsigned long") + indirection
base_type::floating() return string("float") + indirection base_type::floating() return str("float") + indirection
base_type::double_precision() return string("double") + indirection base_type::double_precision() return str("double") + indirection
base_type::object() return get_name(type->type_def) + indirection base_type::object() return get_name(type->type_def) + indirection
base_type::function() { base_type::function() {
type = type->clone_with_indirection(0,false) type = type->clone_with_indirection(0,false)
if (!function_type_map.contains_key(*type)) { if (!function_type_map.contains_key(*type)) {
var temp_name = string("function") + get_id() var temp_name = str("function") + get_id()
var temp = string() var temp = str()
type->parameter_types.for_each(fun(parameter_type: *type) { 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)) temp_name += "_" + cify_name(type_to_c(parameter_type))
}) })
if (type->is_raw) 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 else
error(type->to_string() + " is not raw!") error(type->to_string() + " is not raw!")
// again, the indirection // again, the indirection
@@ -443,23 +443,23 @@ obj c_generator (Object) {
return function_type_map[*type] + indirection 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)) 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); var maybe_it = ast_name_map.get_ptr_or_null(node);
if (maybe_it) if (maybe_it)
return *maybe_it return *maybe_it
var result = string("impossible name") var result = str("impossible name")
var make_unique = true var make_unique = true
match (*node) { match (*node) {
ast_node::type_def(backing) { 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) result = cify_name(backing.name)
if (is_template(upper)) 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) { ast_node::function(backing) {
// be careful, operators like . come through this // be careful, operators like . come through this
@@ -468,11 +468,11 @@ obj c_generator (Object) {
make_unique = false make_unique = false
} else { } else {
result = "fun_" result = "fun_"
var upper = backing.scope.get_with_default(string("~enclosing_scope"), vector(null<ast_node>()))[0] var upper = backing.scope.get_with_default(str("~enclosing_scope"), vec(null<ast_node>()))[0]
if (upper && is_type_def(upper)) if (upper && is_type_def(upper))
result += get_name(upper) + "_" result += get_name(upper) + "_"
result += cify_name(node->function.name) 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) { ast_node::identifier(backing) {
@@ -489,8 +489,8 @@ obj c_generator (Object) {
used_names.add(result) used_names.add(result)
return result return result
} }
fun cify_name(name: string): string { fun cify_name(name: str): str {
var to_ret = string() var to_ret = str()
for (var i = 0; i < name.length(); i++;) { for (var i = 0; i < name.length(); i++;) {
var replaced = false var replaced = false
for (var j = longest_replacement; j > 0; j--;) { for (var j = longest_replacement; j > 0; j--;) {

View File

@@ -1,9 +1,9 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
@@ -11,19 +11,19 @@ import ast_transformation:*
import pass_common:* import pass_common:*
fun get_line(node: *tree<symbol>, name: string): *ast_node { fun get_line(node: *tree<symbol>, name: str): *ast_node {
var to_ret = ast_simple_passthrough_ptr() 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 return to_ret
} }
fun c_line_control(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun c_line_control(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var first = true var first = true
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
/*var helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {*/ /*var helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {*/
/*match(*node) {*/ /*match(*node) {*/
/*if (is_code_block(parent_chain->top()) && ast_to_syntax->contains_key(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())*/ /*add_before_in(get_line(ast_to_syntax->get(node), name), node, parent_chain->top())*/
/*}*/ /*}*/
/*}*/ /*}*/

View File

@@ -1,9 +1,9 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
@@ -13,11 +13,11 @@ import hash_set:*
import pass_common:* import pass_common:*
fun ctce_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun ctce_lower(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var visited = hash_set<*ast_node>() var visited = hash_set<*ast_node>()
var globals = setup_globals(*name_ast_map) var globals = setup_globals(*name_ast_map)
var ctce_passes = vector<*ast_node>() var ctce_passes = vec<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
match(*node) { match(*node) {
ast_node::compiler_intrinsic(backing) { ast_node::compiler_intrinsic(backing) {
@@ -35,9 +35,9 @@ fun ctce_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to
}) })
ctce_passes.for_each(fun(func: *ast_node) { ctce_passes.for_each(fun(func: *ast_node) {
// don't want to pick up the ast_node::value // don't want to pick up the ast_node::value
var params = vector<interpreter::value>() var params = vec<interpreter::value>()
// easier to pick up types from the function itself // 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((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]))) params.add(interpreter::value::pointer(make_pair((ast_to_syntax) cast *void, func->function.type->parameter_types[1])))
call_function(func, params, &globals) call_function(func, params, &globals)

View File

@@ -1,9 +1,9 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
@@ -12,9 +12,9 @@ import hash_set:*
import pass_common:* import pass_common:*
fun defer_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun defer_lower(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var visited = hash_set<*ast_node>() var visited = hash_set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var defer_triple_stack = stack<stack<stack<*ast_node>>>() var defer_triple_stack = stack<stack<stack<*ast_node>>>()
var loop_stack = stack(-1) var loop_stack = stack(-1)
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {

View File

@@ -1,10 +1,10 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import map:* import map:*
import util:* import util:*
import type:* import type:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* 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 { fun in_scope_chain(node: *ast_node, high_scope: *ast_node): bool {
if (node == high_scope) if (node == high_scope)
return true return true
if (get_ast_scope(node)->contains_key(string("~enclosing_scope"))) if (get_ast_scope(node)->contains_key(str("~enclosing_scope")))
return in_scope_chain(get_ast_scope(node)->get(string("~enclosing_scope"))[0], high_scope) return in_scope_chain(get_ast_scope(node)->get(str("~enclosing_scope"))[0], high_scope)
return false return false
} }
fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun function_value_lower(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var curr_time = get_time() var curr_time = get_time()
var visited = hash_set<*ast_node>() var visited = hash_set<*ast_node>()
var lambdas = set<*ast_node>() var lambdas = set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
lambdas.add(syntax_ast_pair.second->translation_unit.lambdas) lambdas.add(syntax_ast_pair.second->translation_unit.lambdas)
// do in order so that inner lambdas are done before outer ones, so enclosed // do in order so that inner lambdas are done before outer ones, so enclosed
// variables can propegate outwards // variables can propegate outwards
@@ -101,10 +101,10 @@ fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node
}) })
}) })
var all_types = hash_set<*type>() var all_types = hash_set<*type>()
var function_value_creation_points = vector<function_parent_block>() var function_value_creation_points = vec<function_parent_block>()
var function_value_call_points = vector<function_parent_block>() var function_value_call_points = vec<function_parent_block>()
var closed_over_uses = vector<pair<*ast_node, pair<*ast_node, *ast_node>>>() var closed_over_uses = vec<pair<*ast_node, pair<*ast_node, *ast_node>>>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
var t = get_ast_type(node) var t = get_ast_type(node)
if (t) all_types.add(t) if (t) all_types.add(t)
@@ -177,18 +177,18 @@ fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node
var lambda_struct_type = type_ptr(new_type_def) 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 // create parameters
var lambda_call_func_param = ast_identifier_ptr("func_struct", lambda_struct_type, null<ast_node>()) var lambda_call_func_param = ast_identifier_ptr("func_struct", lambda_struct_type, null<ast_node>())
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<ast_node>()) return ast_identifier_ptr("pass_through_param", t, null<ast_node>())
}) })
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 // create call body with if, etc
var if_statement = ast_if_statement_ptr(access_expression(lambda_call_func_param, "data")) 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) 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"), 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"), 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)))) lambda_call_parameters.slice(1,-1))))
@@ -209,7 +209,7 @@ fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node
lambdas.for_each(fun(l: *ast_node) { lambdas.for_each(fun(l: *ast_node) {
var closure_struct_type: *type var closure_struct_type: *type
if (l->function.closed_variables.size()) { 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) var new_type_def = ast_type_def_ptr(new_type_def_name)
l->function.closed_variables.for_each(fun(v: *ast_node) { l->function.closed_variables.for_each(fun(v: *ast_node) {
var closed_var_type = v->identifier.type var closed_var_type = v->identifier.type
@@ -226,8 +226,8 @@ fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node
} }
var return_type = lambda_type_to_struct_type_and_call_func[*l->function.type].first 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) 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, vector<*ast_node>(), false); lambda_creation_funcs[l] = ast_function_ptr(l->function.name + "_creation", creation_type, vec<*ast_node>(), false);
var body = ast_code_block_ptr() var body = ast_code_block_ptr()
var ident = ast_identifier_ptr("to_ret", return_type, body) var ident = ast_identifier_ptr("to_ret", return_type, body)
body->code_block.children.add(ast_declaration_statement_ptr(ident, null<ast_node>())) body->code_block.children.add(ast_declaration_statement_ptr(ident, null<ast_node>()))
@@ -249,7 +249,7 @@ fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node
body->code_block.children.add(ast_assignment_statement_ptr(access_expression(closure_param, v->identifier.name), closed_param)) body->code_block.children.add(ast_assignment_statement_ptr(access_expression(closure_param, v->identifier.name), closed_param))
}) })
} else { } 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)) body->code_block.children.add(ast_return_statement_ptr(ident))
lambda_creation_funcs[l]->function.body_statement = body lambda_creation_funcs[l]->function.body_statement = body
@@ -264,16 +264,16 @@ fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node
}) })
curr_time = split(curr_time, "\tfunction_value_call_points.forEach") curr_time = split(curr_time, "\tfunction_value_call_points.forEach")
function_value_creation_points.for_each(fun(p: function_parent_block) { 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 // add the declaration of the closure struct to the enclosing code block
if (p.function->function.closed_variables.size()) { if (p.function->function.closed_variables.size()) {
// pull closure type off lambda creation func parameter // 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_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) 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<ast_node>())) p.parent_block->code_block.children.add(0,ast_declaration_statement_ptr(closure_struct_ident, null<ast_node>()))
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) { 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)) { if (p.parent_function->function.closed_variables.contains(v)) {
closed_over_uses.add(make_pair(v, make_pair(addr_of, p.parent_function))) 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<string, pair<*tree<symbol>,*ast_node
var parent = p.second.first var parent = p.second.first
var lambda = p.second.second var lambda = p.second.second
var closure_param = lambda->function.parameters[0] 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") curr_time = split(curr_time, "\tclosed_over_uses")
// now we can make them raw // now we can make them raw

View File

@@ -1,5 +1,5 @@
import string import str
import vector import vec
import set import set
import stack import stack
import map import map
@@ -9,9 +9,9 @@ import io
import util import util
import serialize import serialize
fun split_into_words(gram_str: string::string): vector::vector<string::string> { fun split_into_words(gram_str: str::str): vec::vec<str::str> {
// var out.construct(): vector::vector<string> // var out.construct(): vec::vec<str>
var out.construct(): vector::vector<string::string> var out.construct(): vec::vec<str::str>
var begin = 0 var begin = 0
for (var i = 0; i < gram_str.length(); i++;) { for (var i = 0; i < gram_str.length(); i++;) {
if (gram_str[i] == '#') { if (gram_str[i] == '#') {
@@ -26,7 +26,7 @@ fun split_into_words(gram_str: string::string): vector::vector<string::string> {
i++ i++
// if we hit a " we check to see if an odd number of backslashes preceed it // 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 // (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] == '"') { if (gram_str[i] == '"') {
var escaped = 0 var escaped = 0
while (gram_str[i-(1+escaped)] == '\\') escaped++ while (gram_str[i-(1+escaped)] == '\\') escaped++
@@ -51,25 +51,25 @@ fun split_into_words(gram_str: string::string): vector::vector<string::string> {
return out return out
} }
fun load_grammer(gram_str: string::string): grammer { fun load_grammer(gram_str: str::str): grammer {
var gram.construct(): grammer var gram.construct(): grammer
var leftSide = symbol::symbol("", false) var leftSide = symbol::symbol("", false)
var doLeftSide = true var doLeftSide = true
var rightSide = vector::vector<symbol::symbol>() var rightSide = vec::vec<symbol::symbol>()
/*split_into_words(io::read_file(path)).for_each(fun(word: string::string) {*/ /*split_into_words(io::read_file(path)).for_each(fun(word: str::str) {*/
/*io::print("word: "); io::println(word);*/ /*io::print("word: "); io::println(word);*/
/*})*/ /*})*/
/*return gram*/ /*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)*/ /*io::print("word: "); io::println(word)*/
if (word == "=") { if (word == "=") {
// do nothing // do nothing
} else if (word == "|") { } else if (word == "|") {
gram.rules.add(rule(leftSide, rightSide)) gram.rules.add(rule(leftSide, rightSide))
rightSide = vector::vector<symbol::symbol>() rightSide = vec::vec<symbol::symbol>()
} else if (word == ";") { } else if (word == ";") {
gram.rules.add(rule(leftSide, rightSide)) gram.rules.add(rule(leftSide, rightSide))
rightSide = vector::vector<symbol::symbol>() rightSide = vec::vec<symbol::symbol>()
doLeftSide = true doLeftSide = true
} else { } else {
if (doLeftSide) { if (doLeftSide) {
@@ -80,7 +80,7 @@ fun load_grammer(gram_str: string::string): grammer {
// ok, we support both plain terminals "hia*" // ok, we support both plain terminals "hia*"
// and decorated terminals "hia*":hi_with_as // and decorated terminals "hia*":hi_with_as
// so first check to find the ending " and see if it's // 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 var last_quote = word.length()-1
while(word[last_quote] != '"') last_quote-- while(word[last_quote] != '"') last_quote--
if (last_quote != word.length()-1) { if (last_quote != word.length()-1) {
@@ -103,9 +103,9 @@ fun load_grammer(gram_str: string::string): grammer {
} }
obj grammer (Object, Serializable) { obj grammer (Object, Serializable) {
var rules: vector::vector<rule> var rules: vec::vec<rule>
var non_terminals: set::set<symbol::symbol> var non_terminals: set::set<symbol::symbol>
var terminals: vector::vector<util::pair<symbol::symbol, regex::regex>> var terminals: vec::vec<util::pair<symbol::symbol, regex::regex>>
var first_set_map: map::map<symbol::symbol, set::set<symbol::symbol>> var first_set_map: map::map<symbol::symbol, set::set<symbol::symbol>>
var parse_table: table var parse_table: table
@@ -135,15 +135,15 @@ obj grammer (Object, Serializable) {
parse_table.destruct() parse_table.destruct()
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(rules) + serialize::serialize(non_terminals) + serialize::serialize(terminals) + serialize::serialize(first_set_map) + serialize::serialize(parse_table) 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<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
// get everything constructed before the assignment // get everything constructed before the assignment
/*construct()*/ /*construct()*/
/*util::unpack(rules, pos) = serialize::unserialize<vector::vector<rule>>(it, pos)*/ /*util::unpack(rules, pos) = serialize::unserialize<vec::vec<rule>>(it, pos)*/
/*util::unpack(non_terminals, pos) = serialize::unserialize<set::set<symbol::symbol>>(it, pos)*/ /*util::unpack(non_terminals, pos) = serialize::unserialize<set::set<symbol::symbol>>(it, pos)*/
/*util::unpack(terminals, pos) = serialize::unserialize<vector::vector<util::pair<symbol::symbol, regex::regex>>>(it, pos)*/ /*util::unpack(terminals, pos) = serialize::unserialize<vec::vec<util::pair<symbol::symbol, regex::regex>>>(it, pos)*/
/*util::unpack(first_set_map, pos) = serialize::unserialize<map::map<symbol::symbol, set::set<symbol::symbol>>>(it, pos)*/ /*util::unpack(first_set_map, pos) = serialize::unserialize<map::map<symbol::symbol, set::set<symbol::symbol>>>(it, pos)*/
/*util::unpack(parse_table, pos) = serialize::unserialize<table>(it, pos)*/ /*util::unpack(parse_table, pos) = serialize::unserialize<table>(it, pos)*/
@@ -177,7 +177,7 @@ obj grammer (Object, Serializable) {
}) })
} }
} }
fun first_vector(rhs: ref vector::vector<symbol::symbol>): set::set<symbol::symbol> { fun first_vector(rhs: ref vec::vec<symbol::symbol>): set::set<symbol::symbol> {
var toRet = set::set<symbol::symbol>() var toRet = set::set<symbol::symbol>()
if (rhs.size) { if (rhs.size) {
for (var i = 0; i < rhs.size; i++;) { for (var i = 0; i < rhs.size; i++;) {
@@ -199,8 +199,8 @@ obj grammer (Object, Serializable) {
} }
fun calculate_state_automaton() { fun calculate_state_automaton() {
var first_state = closure(state(vector::vector(rules[0].with_lookahead(set::set(symbol::eof_symbol()))))) var first_state = closure(state(vec::vec(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 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 newItems = stack::stack(0) // 0 is the index of the first and only item in states
var count = 0 var count = 0
while (newItems.size()) { while (newItems.size()) {
@@ -240,7 +240,7 @@ obj grammer (Object, Serializable) {
/*states.for_each(fun(i: ref state) {*/ /*states.for_each(fun(i: ref state) {*/
/*io::println("STATE:\n")*/ /*io::println("STATE:\n")*/
/*i.items.for_each(fun(r: ref rule) {*/ /*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") io::println(" there were : states")
@@ -254,7 +254,7 @@ obj grammer (Object, Serializable) {
initial.items = closure(initial.items) initial.items = closure(initial.items)
return initial return initial
} }
fun closure(initial: ref vector::vector<rule>): vector::vector<rule> { fun closure(initial: ref vec::vec<rule>): vec::vec<rule> {
var continueIt = true var continueIt = true
//var count = 0 //var count = 0
while (continueIt) { while (continueIt) {
@@ -286,7 +286,7 @@ obj grammer (Object, Serializable) {
//io::println("and") //io::println("and")
//io::println(r.to_string()) //io::println(r.to_string())
//io::println("with") //io::println("with")
//var result = string::string("|lookahead {") //var result = str::str("|lookahead {")
//newLookahead.for_each(fun(i: symbol::symbol) { //newLookahead.for_each(fun(i: symbol::symbol) {
//result += i.to_string() //result += i.to_string()
//}) //})
@@ -317,7 +317,7 @@ obj grammer (Object, Serializable) {
fun goto(I: ref state, X: ref symbol::symbol): state { fun goto(I: ref state, X: ref symbol::symbol): state {
// loop through i, find all that have thing::= something . X more, // loop through i, find all that have thing::= something . X more,
// add thing ::= something X . more // add thing ::= something X . more
var jPrime = vector::vector<rule>() var jPrime = vec::vec<rule>()
I.items.for_each(fun(i: ref rule) { I.items.for_each(fun(i: ref rule) {
if (!i.at_end() && i.next() == X) if (!i.at_end() && i.next() == X)
jPrime.add(i.advanced()) jPrime.add(i.advanced())
@@ -326,18 +326,18 @@ obj grammer (Object, Serializable) {
return state(closure(jPrime)) return state(closure(jPrime))
} }
fun to_string(): string::string { fun to_string(): str::str {
var result = string::string("grammer rules:") var result = str::str("grammer rules:")
rules.for_each( fun(i : rule) { result += string::string("\n\t") + i.to_string(); } ) rules.for_each( fun(i : rule) { result += str::str("\n\t") + i.to_string(); } )
result += "\nnon_terminals:" 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:" result += "\nterminals:"
terminals.for_each( fun(i : util::pair<symbol::symbol, regex::regex>) { result += string::string("\n\t") + i.first.to_string() + ": " + i.second.regexString; } ) terminals.for_each( fun(i : util::pair<symbol::symbol, regex::regex>) { result += str::str("\n\t") + i.first.to_string() + ": " + i.second.regexString; } )
return result return result
} }
} }
fun rule(lhs: symbol::symbol, rhs: vector::vector<symbol::symbol>): rule { fun rule(lhs: symbol::symbol, rhs: vec::vec<symbol::symbol>): rule {
var toRet.construct(): rule var toRet.construct(): rule
toRet.lhs = lhs toRet.lhs = lhs
toRet.rhs = rhs toRet.rhs = rhs
@@ -346,19 +346,19 @@ fun rule(lhs: symbol::symbol, rhs: vector::vector<symbol::symbol>): rule {
obj rule (Object, Serializable) { obj rule (Object, Serializable) {
var lhs: symbol::symbol var lhs: symbol::symbol
var rhs: vector::vector<symbol::symbol> var rhs: vec::vec<symbol::symbol>
var position: int var position: int
var lookahead: set::set<symbol::symbol> var lookahead: set::set<symbol::symbol>
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(lhs) + serialize::serialize(rhs) + serialize::serialize(position) + serialize::serialize(lookahead) return serialize::serialize(lhs) + serialize::serialize(rhs) + serialize::serialize(position) + serialize::serialize(lookahead)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
/*var tempLhs = symbol::invalid_symbol()*/ /*var tempLhs = symbol::invalid_symbol()*/
/*var tempRhs = vector::vector<symbol::symbol>()*/ /*var tempRhs = vec::vec<symbol::symbol>()*/
/*var tempLookahead = set::set<symbol::symbol>()*/ /*var tempLookahead = set::set<symbol::symbol>()*/
/*util::unpack(tempLhs, pos) = serialize::unserialize<symbol::symbol>(it, pos)*/ /*util::unpack(tempLhs, pos) = serialize::unserialize<symbol::symbol>(it, pos)*/
/*util::unpack(tempRhs, pos) = serialize::unserialize<vector::vector<symbol::symbol>>(it, pos)*/ /*util::unpack(tempRhs, pos) = serialize::unserialize<vec::vec<symbol::symbol>>(it, pos)*/
/*util::unpack(position, pos) = serialize::unserialize<int>(it, pos)*/ /*util::unpack(position, pos) = serialize::unserialize<int>(it, pos)*/
/*util::unpack(tempLookahead, pos) = serialize::unserialize<set::set<symbol::symbol>>(it, pos)*/ /*util::unpack(tempLookahead, pos) = serialize::unserialize<set::set<symbol::symbol>>(it, pos)*/
@@ -405,10 +405,10 @@ obj rule (Object, Serializable) {
fun next(): ref symbol::symbol { fun next(): ref symbol::symbol {
return rhs[position] return rhs[position]
} }
fun after(): vector::vector<symbol::symbol> { fun after(): vec::vec<symbol::symbol> {
return rhs.slice(position, -1) return rhs.slice(position, -1)
} }
fun after_next(): vector::vector<symbol::symbol> { fun after_next(): vec::vec<symbol::symbol> {
return rhs.slice(position + 1, -1) return rhs.slice(position + 1, -1)
} }
fun at_end(): bool { fun at_end(): bool {
@@ -430,11 +430,11 @@ obj rule (Object, Serializable) {
return toRet return toRet
} }
fun to_string(): string::string { fun to_string(): str::str {
var result = lhs.name + " -> " var result = lhs.name + " -> "
for (var i = 0; i < rhs.size; i++;) for (var i = 0; i < rhs.size; i++;)
if (i == position) if (i == position)
result += string::string(" . ") + rhs[i].to_string() + ", "; result += str::str(" . ") + rhs[i].to_string() + ", ";
else else
result += rhs[i].to_string() + ", "; result += rhs[i].to_string() + ", ";
if (position == rhs.size) if (position == rhs.size)
@@ -448,18 +448,18 @@ obj rule (Object, Serializable) {
} }
} }
fun state(itemsIn: ref vector::vector<rule>): state { fun state(itemsIn: ref vec::vec<rule>): state {
var toRet.construct(itemsIn): state var toRet.construct(itemsIn): state
return toRet return toRet
} }
obj state (Object) { obj state (Object) {
var items: vector::vector<rule> var items: vec::vec<rule>
fun construct(): *state { fun construct(): *state {
items.construct() items.construct()
} }
fun construct(itemsIn: ref vector::vector<rule>): *state { fun construct(itemsIn: ref vec::vec<rule>): *state {
items.copy_construct(&itemsIn) items.copy_construct(&itemsIn)
} }
fun copy_construct(other: *state) { fun copy_construct(other: *state) {
@@ -475,8 +475,8 @@ obj state (Object) {
fun operator==(other: ref state):bool { fun operator==(other: ref state):bool {
return items == other.items return items == other.items
} }
fun to_string(): string::string { fun to_string(): str::str {
return string::string("woo a state") return str::str("woo a state")
} }
} }
@@ -539,8 +539,8 @@ obj action {
} }
obj table (Object, Serializable) { 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 // a 2 dimensional table made of a vec and a map that maps from stateno & symbol to a vec of parse actions
var items: vector::vector<map::map<symbol::symbol, vector::vector<action>>> var items: vec::vec<map::map<symbol::symbol, vec::vec<action>>>
fun construct(): *table { fun construct(): *table {
items.construct() items.construct()
@@ -555,18 +555,18 @@ obj table (Object, Serializable) {
fun destruct() { fun destruct() {
items.destruct() items.destruct()
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(items) return serialize::serialize(items)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
/*construct()*/ /*construct()*/
/*util::unpack(items, pos) = serialize::unserialize<vector::vector<map::map<symbol::symbol, vector::vector<action>>>>(it, pos)*/ /*util::unpack(items, pos) = serialize::unserialize<vec::vec<map::map<symbol::symbol, vec::vec<action>>>>(it, pos)*/
pos = items.unserialize(it, pos) pos = items.unserialize(it, pos)
return pos return pos
} }
fun expand_to(include_state: int) { fun expand_to(include_state: int) {
while (include_state >= items.size) while (include_state >= items.size)
items.addEnd(map::map<symbol::symbol, vector::vector<action>>()) items.addEnd(map::map<symbol::symbol, vec::vec<action>>())
} }
// we always "clean" the symbol before using it so that having different data doesn't // we always "clean" the symbol before using it so that having different data doesn't
// prevent us from finding the symbol in the table // 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)) if (items[from_state].contains_key(cleaned_symbol))
items[from_state][cleaned_symbol].addEnd(action(action_type::push(), to_state)) items[from_state][cleaned_symbol].addEnd(action(action_type::push(), to_state))
else 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) { fun add_reduce(from_state: int, on_symbol: ref symbol::symbol, by_rule_no: int, rule_position: int) {
expand_to(from_state) expand_to(from_state)
@@ -587,7 +587,7 @@ obj table (Object, Serializable) {
if (items[from_state].contains_key(cleaned_symbol)) if (items[from_state].contains_key(cleaned_symbol))
items[from_state][cleaned_symbol].addEnd(action(action_type::reduce(), by_rule_no, rule_position)) items[from_state][cleaned_symbol].addEnd(action(action_type::reduce(), by_rule_no, rule_position))
else 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) { fun add_accept(from_state: int, on_symbol: ref symbol::symbol) {
expand_to(from_state) expand_to(from_state)
@@ -595,13 +595,13 @@ obj table (Object, Serializable) {
if (items[from_state].contains_key(cleaned_symbol)) if (items[from_state].contains_key(cleaned_symbol))
items[from_state][cleaned_symbol].addEnd(action(action_type::accept(), 0)) items[from_state][cleaned_symbol].addEnd(action(action_type::accept(), 0))
else 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<action> { fun get(state: int, on_symbol: ref symbol::symbol): vec::vec<action> {
var cleaned_symbol = clean_symbol(on_symbol) var cleaned_symbol = clean_symbol(on_symbol)
if (items[state].contains_key(cleaned_symbol)) if (items[state].contains_key(cleaned_symbol))
return items[state][cleaned_symbol] return items[state][cleaned_symbol]
return vector::vector<action>() return vec::vec<action>()
} }
fun get_shift(state: int, on_symbol: ref symbol::symbol): action { fun get_shift(state: int, on_symbol: ref symbol::symbol): action {
var actions = get(state, on_symbol) var actions = get(state, on_symbol)
@@ -615,17 +615,17 @@ obj table (Object, Serializable) {
io::println(on_symbol.to_string()) io::println(on_symbol.to_string())
return action(action_type::invalid(),-1) return action(action_type::invalid(),-1)
} }
fun get_reduces(state: int, on_symbol: ref symbol::symbol): vector::vector<action> { fun get_reduces(state: int, on_symbol: ref symbol::symbol): vec::vec<action> {
return get(state, on_symbol).filter(fun(act: action):bool { return act.act == action_type::reduce(); }) return get(state, on_symbol).filter(fun(act: action):bool { return act.act == action_type::reduce(); })
} }
fun print_string() { 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::print("woo a table of size: ")
io::println(items.size) io::println(items.size)
for (var i = 0; i < items.size; i++;) { for (var i = 0; i < items.size; i++;) {
io::print("for state: ") io::print("for state: ")
io::println(i) io::println(i)
items[i].for_each(fun(sym: symbol::symbol, actions: vector::vector<action>) { items[i].for_each(fun(sym: symbol::symbol, actions: vec::vec<action>) {
actions.for_each(fun(action: action) { actions.for_each(fun(action: action) {
io::print("\ton symbol: ") io::print("\ton symbol: ")
io::print(sym.to_string()) io::print(sym.to_string())

View File

@@ -1,4 +1,4 @@
import vector import vec
import map import map
import io import io
import serialize import serialize
@@ -15,7 +15,7 @@ fun hash_map<T,U>(key: ref T, value: ref U): hash_map<T,U> {
} }
obj hash_map<T,U> (Object, Serializable) { obj hash_map<T,U> (Object, Serializable) {
var data: vector::vector<map::map<T,U>> var data: vec::vec<map::map<T,U>>
var size: int var size: int
fun construct(): *hash_map<T,U> { fun construct(): *hash_map<T,U> {
@@ -35,10 +35,10 @@ obj hash_map<T,U> (Object, Serializable) {
fun destruct() { fun destruct() {
data.destruct() data.destruct()
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(data) + serialize::serialize(size) return serialize::serialize(data) + serialize::serialize(size)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
pos = data.unserialize(it, pos) pos = data.unserialize(it, pos)
util::unpack(size, pos) = serialize::unserialize<int>(it, pos) util::unpack(size, pos) = serialize::unserialize<int>(it, pos)
return pos return pos
@@ -53,7 +53,7 @@ obj hash_map<T,U> (Object, Serializable) {
if (!data[(key_hash%data.size) cast int].contains_key(key)) { if (!data[(key_hash%data.size) cast int].contains_key(key)) {
size++ size++
if (size > data.size) { if (size > data.size) {
var new_data.construct(size*2): vector::vector<map::map<T,U>> var new_data.construct(size*2): vec::vec<map::map<T,U>>
for (var i = 0; i < size*2; i++;) for (var i = 0; i < size*2; i++;)
new_data.addEnd(map::map<T,U>()) new_data.addEnd(map::map<T,U>())
for_each(fun(key: T, value: U) { for_each(fun(key: T, value: U) {

View File

@@ -1,5 +1,5 @@
import hash_map import hash_map
import vector import vec
import io import io
import serialize import serialize
import set import set
@@ -15,7 +15,7 @@ fun hash_set<T>(item: T): hash_set<T> {
return toRet return toRet
} }
fun from_vector<T>(items: vector::vector<T>): hash_set<T> { fun from_vector<T>(items: vec::vec<T>): hash_set<T> {
var toRet.construct() : hash_set<T> var toRet.construct() : hash_set<T>
items.for_each( fun(item: T) toRet.add(item); ) items.for_each( fun(item: T) toRet.add(item); )
return toRet return toRet
@@ -37,10 +37,10 @@ obj hash_set<T> (Object, Serializable) {
fun operator=(rhs: ref hash_set<T>) { fun operator=(rhs: ref hash_set<T>) {
data = rhs.data data = rhs.data
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(data) return serialize::serialize(data)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
return data.unserialize(it, pos) return data.unserialize(it, pos)
} }
// the old unnecessary template to prevent generation // the old unnecessary template to prevent generation
@@ -119,7 +119,7 @@ obj hash_set<T> (Object, Serializable) {
var prev_size = 0 var prev_size = 0
while (prev_size != size()) { while (prev_size != size()) {
prev_size = size() prev_size = size()
var to_add.construct(size()): vector::vector<T> var to_add.construct(size()): vec::vec<T>
for_each(fun(i: T) { for_each(fun(i: T) {
func(i).for_each(fun(j: T) { to_add.add(j); }) func(i).for_each(fun(j: T) { to_add.add(j); })
}) })

View File

@@ -1,24 +1,24 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import stack:* import stack:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
import ast_transformation:* import ast_transformation:*
import parser:* import parser:*
fun import(file_name: string, parsers: ref vector<parser>, ast_pass: ref ast_transformation, import_paths: vector<string>): map<string, pair<*tree<symbol>,*ast_node>> { fun import(file_name: str, parsers: ref vec<parser>, ast_pass: ref ast_transformation, import_paths: vec<str>): map<str, pair<*tree<symbol>,*ast_node>> {
var name_ast_map = map<string, pair<*tree<symbol>,*ast_node>>() var name_ast_map = map<str, pair<*tree<symbol>,*ast_node>>()
// lambda closes over our fix-up list // lambda closes over our fix-up list
var imports_to_fix = vector<*ast_node>() var imports_to_fix = vec<*ast_node>()
var import_first_pass = fun(file_name_idx: pair<string,int>) { var import_first_pass = fun(file_name_idx: pair<str,int>) {
var file_name = file_name_idx.first var file_name = file_name_idx.first
var file = string() var file = str()
import_paths.for_each(fun(path: string) { import_paths.for_each(fun(path: str) {
if (file_exists(path + file_name)) { if (file_exists(path + file_name)) {
file = read_file(path + file_name) file = read_file(path + file_name)
} else { } else {
@@ -47,11 +47,11 @@ fun import(file_name: string, parsers: ref vector<parser>, ast_pass: ref ast_tra
} }
printlnerr() printlnerr()
printlnerr("**Second Pass**") printlnerr("**Second Pass**")
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>, *ast_node>) ast_pass.second_pass(tree_pair.first, tree_pair.second);) name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree<symbol>, *ast_node>) ast_pass.second_pass(tree_pair.first, tree_pair.second);)
printlnerr("**Third Pass**") printlnerr("**Third Pass**")
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>, *ast_node>) ast_pass.third_pass(tree_pair.first, tree_pair.second);) name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree<symbol>, *ast_node>) ast_pass.third_pass(tree_pair.first, tree_pair.second);)
printlnerr("**Fourth Pass**") printlnerr("**Fourth Pass**")
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>, *ast_node>) ast_pass.fourth_pass(tree_pair.first, tree_pair.second);) name_ast_map.for_each(fun(name: str, tree_pair: pair<*tree<symbol>, *ast_node>) ast_pass.fourth_pass(tree_pair.first, tree_pair.second);)
return name_ast_map return name_ast_map
} }

View File

@@ -2,7 +2,7 @@ import mem:*
import math:* import math:*
import map:* import map:*
import stack:* import stack:*
import string:* import str:*
import util:* import util:*
import tree:* import tree:*
import symbol:* import symbol:*
@@ -65,7 +65,7 @@ fun wrap_value(val: *ast_node): value {
var value_str = val->value.string_value var value_str = val->value.string_value
var value_type = val->value.value_type var value_type = val->value.value_type
if (value_str[0] == '"') { // " // Comment hack for emacs now if (value_str[0] == '"') { // " // Comment hack for emacs now
var to_ret = string() var to_ret = str()
// triple quoted strings // triple quoted strings
if (value_str[1] == '"' && value_str.length() > 2 && value_str[2] == '"') if (value_str[1] == '"' && value_str.length() > 2 && value_str[2] == '"')
value_str = value_str.slice(3,-4) value_str = value_str.slice(3,-4)
@@ -131,8 +131,8 @@ fun wrap_value(val: *ast_node): value {
return value::void_nothing() return value::void_nothing()
} }
fun unwrap_value(val: value): *ast_node { fun unwrap_value(val: value): *ast_node {
// string, char, bool, floating // str, char, bool, floating
var value_string = string() var value_string = str()
match (get_real_value(val)) { match (get_real_value(val)) {
value::boolean(data) value_string = to_string(data) value::boolean(data) value_string = to_string(data)
value::character(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::void_nothing() error("trying to unwrap a void into an ast_value_ptr")
value::pointer(point) { value::pointer(point) {
if (point.second->base == base_type::character() && point.second->indirection == 1) 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 else
error("trying to unwrap a pointer into an ast_value_ptr") error("trying to unwrap a pointer into an ast_value_ptr")
} }
@@ -212,7 +212,7 @@ fun truthy(v: ref value):bool {
} }
error("untruthy value") 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)) { match (get_real_value(a)) {
value::boolean(av) return do_basic_op_second_half(func_name, av, b, null<type>()) value::boolean(av) return do_basic_op_second_half(func_name, av, b, null<type>())
value::character(av) return do_basic_op_second_half(func_name, av, b, null<type>()) value::character(av) return do_basic_op_second_half(func_name, av, b, null<type>())
@@ -249,18 +249,18 @@ fun do_basic_op(func_name: string, a: value, b: value): value {
} else if (func_name == "-") { } else if (func_name == "-") {
ptr = ((av.first) cast *char - inc_in_bytes) cast *void ptr = ((av.first) cast *char - inc_in_bytes) cast *void
} else { } 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) 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)) 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::void_nothing() error(str("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::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<T>(func_name: string, av: T, b: value, ptr_type: *type): value { fun do_basic_op_second_half<T>(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 // because of the trickery in do_basic_op, if either param is a pointer, it's b
match (get_real_value(b)) { match (get_real_value(b)) {
value::boolean(bv) return do_op(func_name, av, bv, ptr_type) value::boolean(bv) return do_op(func_name, av, bv, ptr_type)
@@ -274,15 +274,15 @@ fun do_basic_op_second_half<T>(func_name: string, av: T, b: value, ptr_type: *ty
value::ulong_int(bv) return do_op(func_name, av, bv, ptr_type) 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::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::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::void_nothing() error(str("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::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 // 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)) value::pointer(bv) return do_basic_op(func_name, b, raw_to_value(av))
} }
print_value(b) 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<T,U>(op: string, a: T, b: U, ptr_type: *type): value { fun do_op<T,U>(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) 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<T,U>(op: string, a: T, b: U, ptr_type: *type): value {
if (op == "&") return raw_to_value(a & b) if (op == "&") return raw_to_value(a & b)
error(("Invalid op: ") + op) error(("Invalid op: ") + op)
} }
fun do_basic_floating_op_second_half<T>(func_name: string, av: T, b: value, ptr_type: *type): value { fun do_basic_floating_op_second_half<T>(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 // because of the trickery in do_basic_op, if either param is a pointer, it's b
match (get_real_value(b)) { match (get_real_value(b)) {
value::boolean(bv) return do_floating_op(func_name, av, bv, ptr_type) value::boolean(bv) return do_floating_op(func_name, av, bv, ptr_type)
@@ -313,15 +313,15 @@ fun do_basic_floating_op_second_half<T>(func_name: string, av: T, b: value, ptr_
value::ulong_int(bv) return do_floating_op(func_name, av, bv, ptr_type) 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::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::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::void_nothing() error(str("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::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 // 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)) value::pointer(bv) return do_basic_op(func_name, b, raw_to_value(av))
} }
print_value(b) 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<T,U>(op: string, a: T, b: U, ptr_type: *type): value { fun do_floating_op<T,U>(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) 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::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>>) 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 { fun wrap_into_variable(v: value): value {
if (is_variable(v)) 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) 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<T>(v: value): value { fun cast_value_second_half<T>(v: value): value {
match (get_real_value(v)) { match (get_real_value(v)) {
@@ -542,7 +542,7 @@ fun type_size_and_alignment(t: *type): pair<ulong,ulong> {
base_type::floating() return make_pair(#sizeof<float>, #sizeof<float>) base_type::floating() return make_pair(#sizeof<float>, #sizeof<float>)
base_type::double_precision() return make_pair(#sizeof<double>, #sizeof<double>) base_type::double_precision() return make_pair(#sizeof<double>, #sizeof<double>)
} }
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 { fun offset_into_struct(struct_type: *type, ident: *ast_node): ulong {
var offset: ulong = 0 var offset: ulong = 0
@@ -574,23 +574,23 @@ fun pop_and_free(var_stack: *stack<map<*ast_node, value>>) {
}) })
} }
fun call_main(name_ast_map: ref map<string, pair<*tree<symbol>,*ast_node>>) { fun call_main(name_ast_map: ref map<str, pair<*tree<symbol>,*ast_node>>) {
var results = vector<*ast_node>() var results = vec<*ast_node>()
name_ast_map.for_each(fun(key: string, value: pair<*tree<symbol>,*ast_node>) { name_ast_map.for_each(fun(key: str, value: pair<*tree<symbol>,*ast_node>) {
results += scope_lookup(string("main"), value.second) results += scope_lookup(str("main"), value.second)
}) })
if (results.size != 1) 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 globals = setup_globals(name_ast_map)
var result = call_function(results[0], vector<value>(), &globals) var result = call_function(results[0], vec<value>(), &globals)
} }
fun evaluate_constant_expression(node: *ast_node): value fun evaluate_constant_expression(node: *ast_node): value
return interpret(node, null<stack<map<*ast_node, value>>>(), value::void_nothing(), null<ast_node>(), null<map<*ast_node, value>>()).first return interpret(node, null<stack<map<*ast_node, value>>>(), value::void_nothing(), null<ast_node>(), null<map<*ast_node, value>>()).first
fun evaluate_with_globals(node: *ast_node, globals: *map<*ast_node, value>): value fun evaluate_with_globals(node: *ast_node, globals: *map<*ast_node, value>): value
return interpret(node, null<stack<map<*ast_node, value>>>(), value::void_nothing(), null<ast_node>(), globals).first return interpret(node, null<stack<map<*ast_node, value>>>(), value::void_nothing(), null<ast_node>(), globals).first
fun setup_globals(name_ast_map: ref map<string, pair<*tree<symbol>,*ast_node>>): map<*ast_node, value> { fun setup_globals(name_ast_map: ref map<str, pair<*tree<symbol>,*ast_node>>): map<*ast_node, value> {
var globals = map<*ast_node, value>() var globals = map<*ast_node, value>()
name_ast_map.for_each(fun(key: string, value: pair<*tree<symbol>,*ast_node>) { name_ast_map.for_each(fun(key: str, value: pair<*tree<symbol>,*ast_node>) {
value.second->translation_unit.children.for_each(fun(child: *ast_node) { value.second->translation_unit.children.for_each(fun(child: *ast_node) {
if (is_declaration_statement(child)) { if (is_declaration_statement(child)) {
var declaration = child->declaration_statement var declaration = child->declaration_statement
@@ -607,7 +607,7 @@ fun setup_globals(name_ast_map: ref map<string, pair<*tree<symbol>,*ast_node>>):
*(stdin_pointer) cast **void = stdin; *(stdin_pointer) cast **void = stdin;
globals[declaration.identifier] = value::variable(make_pair(stdin_pointer, stdin_type)) globals[declaration.identifier] = value::variable(make_pair(stdin_pointer, stdin_type))
} else { } else {
error(string("unknown extern: ") + identifier.name) error(str("unknown extern: ") + identifier.name)
} }
} else { } else {
globals[declaration.identifier] = value::variable(make_pair(calloc(type_size(identifier.type)), identifier.type)) 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<map<*ast_nod
} }
// so here we either do an operator, call call_func with value parameters, or call call_func with ast_expressions // so here we either do an operator, call call_func with value parameters, or call call_func with ast_expressions
// (so we can properly copy_construct if necessary) // (so we can properly copy_construct if necessary)
var parameters = vector<value>() var parameters = vec<value>()
var parameter_sources = vector<*ast_node>() var parameter_sources = vec<*ast_node>()
// if we don't have to copy_construct params (is an operator, or has no object params) // 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;)) { 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;) 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<map<*ast_nod
return make_pair(do_basic_op(func_name, parameters[0], parameters[1]), control_flow::nor()) return make_pair(do_basic_op(func_name, parameters[0], parameters[1]), control_flow::nor())
// do negate by subtracting from zero // do negate by subtracting from zero
if (func_name == "-") if (func_name == "-")
return make_pair(do_basic_op_second_half(string("-"), 0, parameters[0], null<type>()), control_flow::nor()) return make_pair(do_basic_op_second_half(str("-"), 0, parameters[0], null<type>()), control_flow::nor())
if (func_name == "!") if (func_name == "!")
return make_pair(value::boolean(!truthy(get_real_value(parameters[0]))), control_flow::nor()) return make_pair(value::boolean(!truthy(get_real_value(parameters[0]))), control_flow::nor())
if (func_name == "++p" || func_name == "--p") { if (func_name == "++p" || func_name == "--p") {
@@ -705,7 +705,7 @@ fun interpret_function_call(func_call: *ast_node, var_stack: *stack<map<*ast_nod
if (func_name == "*" || func_name == "[]") { if (func_name == "*" || func_name == "[]") {
var dereference_val = parameters[0] var dereference_val = parameters[0]
if (func_name == "[]") if (func_name == "[]")
dereference_val = do_basic_op(string("+"), parameters[0], parameters[1]) dereference_val = do_basic_op(str("+"), parameters[0], parameters[1])
if (!is_pointer(get_real_value(parameters[0]))) if (!is_pointer(get_real_value(parameters[0])))
error("Trying to take dereference not a pointer") error("Trying to take dereference not a pointer")
return make_pair(dereference_pointer_into_variable(dereference_val), control_flow::nor()) return make_pair(dereference_pointer_into_variable(dereference_val), control_flow::nor())
@@ -714,7 +714,7 @@ fun interpret_function_call(func_call: *ast_node, var_stack: *stack<map<*ast_nod
if (func_name == "printf" || func_name == "malloc" || func_name == "free" || func_name == "memmove" || func_name == "fflush" || func_name == "snprintf" || func_name == "fopen" || func_name == "fclose" || func_name == "ftell" || func_name == "fseek" || func_name == "fread" || func_name == "fwrite" || func_name == "atan" || func_name == "atan2" || func_name == "acos" || func_name == "asin" || func_name == "tan" || func_name == "cos" || func_name == "sin" || func_name == "fgets" || func_name == "popen" || func_name == "pclose") if (func_name == "printf" || func_name == "malloc" || func_name == "free" || func_name == "memmove" || func_name == "fflush" || func_name == "snprintf" || func_name == "fopen" || func_name == "fclose" || func_name == "ftell" || func_name == "fseek" || func_name == "fread" || func_name == "fwrite" || func_name == "atan" || func_name == "atan2" || func_name == "acos" || func_name == "asin" || func_name == "tan" || func_name == "cos" || func_name == "sin" || func_name == "fgets" || func_name == "popen" || func_name == "pclose")
return make_pair(call_built_in_extern(func_name, parameters), control_flow::nor()) return make_pair(call_built_in_extern(func_name, parameters), control_flow::nor())
if (!func_call_func->function.body_statement) if (!func_call_func->function.body_statement)
error(string("trying to call unsupported extern function: ") + func_name) error(str("trying to call unsupported extern function: ") + func_name)
} else { } else {
// not the operator & and at least one object like parameter // not the operator & and at least one object like parameter
parameter_sources = func_call_parameters parameter_sources = func_call_parameters
@@ -722,17 +722,17 @@ fun interpret_function_call(func_call: *ast_node, var_stack: *stack<map<*ast_nod
return make_pair(call_function(func_call_func, parameters, parameter_sources, var_stack, possible_closure_map, enclosing_object, new_enclosing_object, enclosing_func, globals), control_flow::nor()) return make_pair(call_function(func_call_func, parameters, parameter_sources, var_stack, possible_closure_map, enclosing_object, new_enclosing_object, enclosing_func, globals), control_flow::nor())
} }
fun call_function(func: *ast_node, parameters: vector<value>, globals: *map<*ast_node, value>): value { fun call_function(func: *ast_node, parameters: vec<value>, globals: *map<*ast_node, value>): value {
var var_stack = stack<map<*ast_node, value>>() var var_stack = stack<map<*ast_node, value>>()
var_stack.push(map<*ast_node,value>()) 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<ast_node>(), globals) var result = call_function(func, parameters, vec<*ast_node>(), &var_stack, map<*ast_node,value>(), value::void_nothing(), value::void_nothing(), null<ast_node>(), globals)
pop_and_free(&var_stack) pop_and_free(&var_stack)
return result return result
} }
// call_function can be called with either parameter values in parameters or ast expressions in parameter_sources // 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 // 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 // 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<value>, parameter_sources: vector<*ast_node>, var_stack: *stack<map<*ast_node, value>>, 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<value>, parameter_sources: vec<*ast_node>, var_stack: *stack<map<*ast_node, value>>, 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 // will need adjustment
if (!is_function(func)) if (!is_function(func))
error("Can't handle not function function calls (can do regular method, is this chained or something?)") 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<value>, parameter_sources:
if (parameter_sources.size == 0) { if (parameter_sources.size == 0) {
/*println(func_name + " being called with parameter values")*/ /*println(func_name + " being called with parameter values")*/
if (parameters.size != func->function.parameters.size) 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++;) { for (var i = 0; i < parameters.size; i++;) {
var param_type = get_ast_type(func)->parameter_types[i] var param_type = get_ast_type(func)->parameter_types[i]
var param_ident = func->function.parameters[i] var param_ident = func->function.parameters[i]
@@ -765,7 +765,7 @@ fun call_function(func: *ast_node, parameters: vector<value>, parameter_sources:
/*println(func_name + " being called with parameter sources")*/ /*println(func_name + " being called with parameter sources")*/
// need to pull from parameter_sources instead // need to pull from parameter_sources instead
if (parameter_sources.size != func->function.parameters.size) 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++;) { for (var i = 0; i < parameter_sources.size; i++;) {
var param_type = get_ast_type(func)->parameter_types[i] var param_type = get_ast_type(func)->parameter_types[i]
var param_ident = func->function.parameters[i] var param_ident = func->function.parameters[i]
@@ -790,7 +790,7 @@ fun call_function(func: *ast_node, parameters: vector<value>, parameter_sources:
} }
return to_ret return to_ret
} }
fun call_built_in_extern(func_name: string, parameters: vector<value>): value { fun call_built_in_extern(func_name: str, parameters: vec<value>): value {
for (var i = 0; i < parameters.size; i++;) for (var i = 0; i < parameters.size; i++;)
parameters[i] = get_real_value(parameters[i]) parameters[i] = get_real_value(parameters[i])
if (func_name == "printf") { if (func_name == "printf") {
@@ -866,7 +866,7 @@ fun call_built_in_extern(func_name: string, parameters: vector<value>): value {
assert(parameters.size == 1 && is_pointer(parameters[0]), "Calling pclose with wrong params") assert(parameters.size == 1 && is_pointer(parameters[0]), "Calling pclose with wrong params")
return value::integer(pclose(parameters[0].pointer.first)) return value::integer(pclose(parameters[0].pointer.first))
} else { } else {
error(string("trying to call invalid func: ") + func_name) error(str("trying to call invalid func: ") + func_name)
} }
return value::void_nothing() return value::void_nothing()
} }
@@ -1017,7 +1017,7 @@ fun interpret_identifier(ident: *ast_node, var_stack: *stack<map<*ast_node, valu
return make_pair((*globals)[ident], control_flow::nor()) return make_pair((*globals)[ident], control_flow::nor())
println("couldn't find " + get_ast_name(ident) + " in interpret identifier, scope:") println("couldn't find " + get_ast_name(ident) + " in interpret identifier, scope:")
for (var i = 0; i < var_stack->size(); i++;) { for (var i = 0; i < var_stack->size(); 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) + " ");) var_stack->from_top(i).for_each(fun(key: *ast_node, v: value) print(get_ast_name(key) + " ");)
println() println()
} }
@@ -1031,7 +1031,7 @@ fun interpret_identifier(ident: *ast_node, var_stack: *stack<map<*ast_node, valu
print("no object scope: ") print("no object scope: ")
print_value(enclosing_object) print_value(enclosing_object)
} }
error(string("Cannot find variable: ") + ident->identifier.name) error(str("Cannot find variable: ") + ident->identifier.name)
} }
fun interpret_cast(node: *ast_node, var_stack: *stack<map<*ast_node, value>>, enclosing_object: value, enclosing_func: *ast_node, globals: *map<*ast_node, value>): pair<value, control_flow> { fun interpret_cast(node: *ast_node, var_stack: *stack<map<*ast_node, value>>, enclosing_object: value, enclosing_func: *ast_node, globals: *map<*ast_node, value>): pair<value, control_flow> {
return make_pair(cast_value(interpret(node->cast.value, var_stack, enclosing_object, enclosing_func, globals).first, node->cast.to_type), control_flow::nor()) 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: *stack<map<*ast_nod
var intrinsic_name = node->compiler_intrinsic.intrinsic var intrinsic_name = node->compiler_intrinsic.intrinsic
if (intrinsic_name == "sizeof") if (intrinsic_name == "sizeof")
return make_pair(value::ulong_int(type_size(node->compiler_intrinsic.type_parameters[0])), control_flow::nor()) 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<value, control_flow> fun interpret_value(val: *ast_node): pair<value, control_flow>
return make_pair(wrap_value(val), control_flow::nor()) return make_pair(wrap_value(val), control_flow::nor())
@@ -1062,6 +1062,6 @@ fun interpret(node: *ast_node, var_stack: *stack<map<*ast_node, value>>, enclosi
ast_node::compiler_intrinsic(backing) return interpret_compiler_intrinsic(node, var_stack) ast_node::compiler_intrinsic(backing) return interpret_compiler_intrinsic(node, var_stack)
ast_node::value(backing) return interpret_value(node) 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))
} }

View File

@@ -1,5 +1,5 @@
import string; import str;
import vector; import vec;
import mem:* import mem:*
ext fun printf(fmt_str: *char, ...): int 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 ext var stdin: *void
// dead simple stdin // 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) print(prompt)
return get_line(line_size) 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) 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<char>(line_size) var buff = new<char>(line_size)
fgets(buff, line_size, file) fgets(buff, line_size, file)
var to_ret = string::string(buff) var to_ret = str::str(buff)
delete(buff) delete(buff)
return to_ret.slice(0,-2) // remove '\n' return to_ret.slice(0,-2) // remove '\n'
} }
@@ -29,7 +29,7 @@ fun printlnerr<T>(toPrint: T) : void {
} }
fun printlnerr() fun printlnerr()
printerr("\n") printerr("\n")
fun printerr(toPrint: string::string) : void { fun printerr(toPrint: str::str) : void {
var charArr = toPrint.toCharArray() var charArr = toPrint.toCharArray()
printerr(charArr) printerr(charArr)
delete(charArr) delete(charArr)
@@ -51,9 +51,9 @@ fun println()
print("\n") print("\n")
fun print(toPrint: char) : void 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() var charArr = toPrint.toCharArray()
print(charArr) print(charArr)
delete(charArr) delete(charArr)
@@ -65,7 +65,7 @@ fun print(toPrint: bool) {
print("false") print("false")
} }
fun print<T>(toPrint: T): void fun print<T>(toPrint: T): void
print(string::to_string(toPrint)) print(str::to_string(toPrint))
// Ok, just some DEAD simple file io for now // Ok, just some DEAD simple file io for now
ext fun fopen(path: *char, mode: *char): *void 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 fseek(file: *void, offset: long, whence: int): int
ext fun fread(ptr: *void, size: ulong, nmemb: ulong, file: *void): ulong ext fun fread(ptr: *void, size: ulong, nmemb: ulong, file: *void): ulong
ext fun fwrite(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() var char_path = path.toCharArray()
defer delete(char_path) defer delete(char_path)
var fp = fopen(char_path, "r") var fp = fopen(char_path, "r")
@@ -85,13 +85,13 @@ fun file_exists(path: string::string): bool {
} }
return false return false
} }
fun read_file(path: string::string): string::string { fun read_file(path: str::str): str::str {
if (!file_exists(path)) if (!file_exists(path))
return string::string() return str::str()
var toRet.construct(read_file_binary(path)): string::string var toRet.construct(read_file_binary(path)): str::str
return toRet 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() var char_path = path.toCharArray()
defer delete(char_path) defer delete(char_path)
var char_data = data.toCharArray() var char_data = data.toCharArray()
@@ -100,7 +100,7 @@ fun write_file(path: string::string, data: string::string) {
fprintf(fp, "%s", char_data) fprintf(fp, "%s", char_data)
fclose(fp) fclose(fp)
} }
fun read_file_binary(path: string::string): vector::vector<char> { fun read_file_binary(path: str::str): vec::vec<char> {
var char_path = path.toCharArray() var char_path = path.toCharArray()
defer delete(char_path) defer delete(char_path)
var fp = fopen(char_path, "r") var fp = fopen(char_path, "r")
@@ -111,13 +111,13 @@ fun read_file_binary(path: string::string): vector::vector<char> {
var readSize = fread((data) cast *void, (1) cast ulong, (size) cast ulong, fp) var readSize = fread((data) cast *void, (1) cast ulong, (size) cast ulong, fp)
fclose(fp) fclose(fp)
data[readSize] = 0 data[readSize] = 0
var toRet.construct((size) cast int): vector::vector<char> var toRet.construct((size) cast int): vec::vec<char>
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
toRet.add(data[i]) toRet.add(data[i])
delete(data) delete(data)
return toRet return toRet
} }
fun write_file_binary(path: string::string, vdata: vector::vector<char>) { fun write_file_binary(path: str::str, vdata: vec::vec<char>) {
var char_path = path.toCharArray() var char_path = path.toCharArray()
defer delete(char_path) defer delete(char_path)
var data = vdata.getBackingMemory() var data = vdata.getBackingMemory()

View File

@@ -1,10 +1,10 @@
import regex import regex
import symbol import symbol
import string import str
import vector import vec
import util import util
fun lexer(regs: vector::vector<regex::regex>): lexer { fun lexer(regs: vec::vec<regex::regex>): lexer {
/*var toRet:lexer*/ /*var toRet:lexer*/
var toRet.construct() :lexer var toRet.construct() :lexer
regs.for_each( fun(reg: regex::regex) { regs.for_each( fun(reg: regex::regex) {
@@ -13,7 +13,7 @@ fun lexer(regs: vector::vector<regex::regex>): lexer {
return toRet return toRet
} }
fun lexer(regs: vector::vector<util::pair<symbol::symbol, regex::regex>>): lexer { fun lexer(regs: vec::vec<util::pair<symbol::symbol, regex::regex>>): lexer {
/*var toRet:lexer*/ /*var toRet:lexer*/
var toRet.construct() :lexer var toRet.construct() :lexer
regs.for_each( fun(reg: util::pair<symbol::symbol, regex::regex>) regs.for_each( fun(reg: util::pair<symbol::symbol, regex::regex>)
@@ -23,8 +23,8 @@ fun lexer(regs: vector::vector<util::pair<symbol::symbol, regex::regex>>): lexer
} }
obj lexer (Object) { obj lexer (Object) {
var regs: vector::vector<util::pair<string::string, regex::regex>> var regs: vec::vec<util::pair<str::str, regex::regex>>
var input: string::string var input: str::str
var position: int var position: int
var line_number: int var line_number: int
fun construct(): *lexer { fun construct(): *lexer {
@@ -48,19 +48,19 @@ obj lexer (Object) {
destruct() destruct()
copy_construct(&old) 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)) regs.add(util::make_pair(name,newOne))
} }
fun add_regex(newOne: util::pair<string::string,regex::regex>) { fun add_regex(newOne: util::pair<str::str,regex::regex>) {
regs.add(newOne) regs.add(newOne)
} }
fun add_regex(newOne: regex::regex) { fun add_regex(newOne: regex::regex) {
regs.add(util::make_pair(newOne.regexString, newOne)) regs.add(util::make_pair(newOne.regexString, newOne))
} }
fun add_regex(newOne: *char) { 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 position = 0
line_number = 1 line_number = 1
input = in input = in

View File

@@ -1,4 +1,4 @@
import vector import vec
import mem import mem
import io import io
import serialize import serialize
@@ -15,8 +15,8 @@ fun map<T,U>(key: ref T, value: ref U): map<T,U> {
} }
obj map<T,U> (Object, Serializable) { obj map<T,U> (Object, Serializable) {
var keys: vector::vector<T> var keys: vec::vec<T>
var values: vector::vector<U> var values: vec::vec<U>
fun construct(): *map<T,U> { fun construct(): *map<T,U> {
keys.construct() keys.construct()
@@ -35,10 +35,10 @@ obj map<T,U> (Object, Serializable) {
keys.destruct() keys.destruct()
values.destruct() values.destruct()
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(keys) + serialize::serialize(values) return serialize::serialize(keys) + serialize::serialize(values)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
pos = keys.unserialize(it, pos) pos = keys.unserialize(it, pos)
pos = values.unserialize(it, pos) pos = values.unserialize(it, pos)
return pos return pos

View File

@@ -1,8 +1,8 @@
import vector:*; import vec:*;
import io:*; import io:*;
obj matrix (Object) { obj matrix (Object) {
var data: vector<double>; var data: vec<double>;
var rows: int; var rows: int;
var cols: int; var cols: int;

View File

@@ -1,9 +1,9 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
@@ -11,19 +11,19 @@ import ast_transformation:*
import pass_common:* import pass_common:*
fun node_counter(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun node_counter(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var counter = node_counter_helper(name_ast_map, ast_to_syntax) 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<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun node_counter_test(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var counter = node_counter_helper(name_ast_map, ast_to_syntax) var counter = node_counter_helper(name_ast_map, ast_to_syntax)
if (counter > 10000) if (counter > 10000)
println("more than 10000 nodes!") println("more than 10000 nodes!")
} }
fun node_counter_helper(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>): int { fun node_counter_helper(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>): int {
var counter = 0 var counter = 0
var visited = hash_set<*ast_node>() var visited = hash_set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
counter++ counter++
} }

View File

@@ -1,9 +1,9 @@
import symbol:* import symbol:*
import tree:* import tree:*
import vector:* import vec:*
import map:* import map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
@@ -30,13 +30,13 @@ import hash_set:*
PASS THREE THROUGH name_ast_map 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 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<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun obj_lower(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var visited1 = hash_set<*ast_node>() var visited1 = hash_set<*ast_node>()
var visited2 = hash_set<*ast_node>() var visited2 = hash_set<*ast_node>()
var visited3 = hash_set<*ast_node>() var visited3 = hash_set<*ast_node>()
var functions_visited_for_construct_in_destruct_out = hash_set<*ast_node>() var functions_visited_for_construct_in_destruct_out = hash_set<*ast_node>()
var all_type_defs = set<*ast_node>() var all_type_defs = set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
// Pass 1 // 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 { 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) { match(*node) {
@@ -66,11 +66,11 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
add_to_scope("~enclosing_scope", node, backing.statement) add_to_scope("~enclosing_scope", node, backing.statement)
} }
var condition = backing.condition 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 // 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) 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)) 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()) condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt())
backing.statement->code_block.children.add(1, condition_if) backing.statement->code_block.children.add(1, condition_if)
} }
@@ -83,7 +83,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
backing.init = null<ast_node>() backing.init = null<ast_node>()
// the do_update goes in the block above the for // 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()) 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()) node, parent_chain->top())
var update_if = ast_if_statement_ptr(update_ident) var update_if = ast_if_statement_ptr(update_ident)
add_to_scope("~enclosing_scope", backing.body, update_if) add_to_scope("~enclosing_scope", backing.body, update_if)
@@ -91,14 +91,14 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
add_to_scope("~enclosing_scope", update_if, update_if->if_statement.then_part) add_to_scope("~enclosing_scope", update_if, update_if->if_statement.then_part)
backing.update = null<ast_node>() backing.update = null<ast_node>()
backing.body->code_block.children.add(0, update_if) 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 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 // 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) 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)) 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()) condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt())
backing.body->code_block.children.add(3, condition_if) backing.body->code_block.children.add(3, condition_if)
} }
@@ -109,7 +109,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
}) })
// make sure all blockes munged before we move ahead // make sure all blockes munged before we move ahead
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var visit = hash_set<*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 { 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) { match(*node) {
@@ -130,7 +130,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
error("Bad in 2") error("Bad in 2")
} }
ast_node::function_call(backing) { ast_node::function_call(backing) {
var func_name = string() var func_name = str()
if (is_function(backing.func)) { if (is_function(backing.func)) {
func_name = backing.func->function.name func_name = backing.func->function.name
if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/" if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/"
@@ -148,7 +148,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
var short_circuit_declaration = ast_declaration_statement_ptr(short_circuit_result, backing.parameters[0]) var short_circuit_declaration = ast_declaration_statement_ptr(short_circuit_result, backing.parameters[0])
var condition = short_circuit_result var condition = short_circuit_result
if (func_name == "||") if (func_name == "||")
condition = make_operator_call("!", vector(condition)) condition = make_operator_call("!", vec(condition))
var short_circuit_if = ast_if_statement_ptr(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) 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 // how to get proper parent scoping working for this part
@@ -202,12 +202,12 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
else else
in_function_param_type = get_ast_type(param)->clone_without_ref() in_function_param_type = get_ast_type(param)->clone_without_ref()
var param_type = get_ast_type(param) 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) 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("temporary_param_boom", temp_ident, replace_in)
add_to_scope("~enclosing_scope", replace_in, temp_ident) add_to_scope("~enclosing_scope", replace_in, temp_ident)
var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>()) var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>())
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(declaration, replace_before, replace_in)
add_before_in(copy_in, replace_before, replace_in) add_before_in(copy_in, replace_before, replace_in)
backing.parameters[i] = temp_ident backing.parameters[i] = temp_ident
@@ -220,8 +220,8 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
add_to_scope("~enclosing_scope", replace_in, temp_return) add_to_scope("~enclosing_scope", replace_in, temp_return)
var declaration = ast_declaration_statement_ptr(temp_return, node) var declaration = ast_declaration_statement_ptr(temp_return, node)
add_before_in(declaration, replace_before, replace_in) add_before_in(declaration, replace_before, replace_in)
if (has_method(func_return_type->type_def, "destruct", vector<*type>())) { if (has_method(func_return_type->type_def, "destruct", vec<*type>())) {
add_before_in(ast_defer_statement_ptr(make_method_call(temp_return, "destruct", vector<*ast_node>())), add_before_in(ast_defer_statement_ptr(make_method_call(temp_return, "destruct", vec<*ast_node>())),
replace_before, replace_in) replace_before, replace_in)
} }
replace_with_in(node, temp_return, parent_chain) replace_with_in(node, temp_return, parent_chain)
@@ -237,12 +237,12 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
var order = 0; var order = 0;
backing.parameters.for_each(fun(param: *ast_node) { backing.parameters.for_each(fun(param: *ast_node) {
var param_type = get_ast_type(param) 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 // the first pass ensures a code_block child
if (!is_code_block(backing.body_statement)) if (!is_code_block(backing.body_statement))
error("BUT WHY") error("BUT WHY")
backing.body_statement->code_block.children.add(order++, 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<string, pair<*tree<symbol>,*ast_node>>, ast_to_
if (is_translation_unit(parent_chain->top()) || is_type_def(parent_chain->top())) if (is_translation_unit(parent_chain->top()) || is_type_def(parent_chain->top()))
return; return;
if (!ident_type->is_ref && ident_type->indirection == 0 && ident_type->is_object()) { 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()) 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("temp_declaration_copy_construct", temp_cpy_ctst, parent_chain->top())
add_to_scope("~enclosing_scope", parent_chain->top(), temp_cpy_ctst) add_to_scope("~enclosing_scope", parent_chain->top(), temp_cpy_ctst)
var declaration = ast_declaration_statement_ptr(temp_cpy_ctst, backing.expression) 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()) node, parent_chain->top())
// do second so the order's right // do second so the order's right
add_after_in(declaration, add_after_in(declaration,
node, parent_chain->top()) node, parent_chain->top())
backing.expression = null<ast_node>() backing.expression = null<ast_node>()
} }
if (has_method(ident_type->type_def, "destruct", vector<*type>())) { if (has_method(ident_type->type_def, "destruct", vec<*type>())) {
add_after_in(ast_defer_statement_ptr(make_method_call(backing.identifier, "destruct", vector<*ast_node>())), add_after_in(ast_defer_statement_ptr(make_method_call(backing.identifier, "destruct", vec<*ast_node>())),
node, parent_chain->top()) node, parent_chain->top())
} }
} }
@@ -283,7 +283,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
error("this would by unusual") error("this would by unusual")
if (return_value) { if (return_value) {
if (get_ast_type(enclosing_function)->return_type->is_ref) 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) 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("temp_boom_return", temp_return, block)
add_to_scope("~enclosing_scope", block, temp_return) add_to_scope("~enclosing_scope", block, temp_return)
@@ -294,7 +294,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
// dereference so that the real ref can take it back // dereference so that the real ref can take it back
if (get_ast_type(enclosing_function)->return_type->is_ref) 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 backing.return_value = temp_return
} }
} }
@@ -335,7 +335,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
add_to_scope("~enclosing_scope", method, this_ident) add_to_scope("~enclosing_scope", method, this_ident)
method->function.type->parameter_types.add(0, this_type) method->function.type->parameter_types.add(0, this_type)
}) })
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
// Pass 4 // 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 { 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) { match(*node) {
@@ -344,7 +344,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
if (is_dot_style_method_call(node)) { if (is_dot_style_method_call(node)) {
var this_ident = backing.func->function_call.parameters[0] var this_ident = backing.func->function_call.parameters[0]
if (backing.func->function_call.func->function.name == ".") 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.func = backing.func->function_call.parameters[1]
backing.parameters.add(0, this_ident) backing.parameters.add(0, this_ident)
} else { } else {
@@ -372,7 +372,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
var this_ident = parent_chain->item_from_top_satisfying(fun(i: *ast_node): bool { 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<ast_node>()) == enclosing_obj return is_function(i) && fun_to_obj.get_with_default(i, null<ast_node>()) == enclosing_obj
})->function.parameters[0] })->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)
} }
} }
} }

View File

@@ -1,8 +1,8 @@
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
fun system(call_string: string):int { fun system(call_string: str):int {
var c_call_string = call_string.toCharArray() var c_call_string = call_string.toCharArray()
var result = system(c_call_string) var result = system(c_call_string)
delete(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 popen(command: *char, mode: *char): *void
ext fun pclose(file: *void): int 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() var command_string = command.toCharArray()
defer delete(command_string) defer delete(command_string)
var p = popen(command_string, "r") var p = popen(command_string, "r")
@@ -22,10 +22,10 @@ fun from_system_command(command: string, line_size: int): string {
pclose(p) pclose(p)
return to_ret return to_ret
} }
fun get_time(): long { return string_to_num<long>(from_system_command(string("date +%s"), 50)); } fun get_time(): long { return string_to_num<long>(from_system_command(str("date +%s"), 50)); }
fun split(time: long, split_label: *char): long { fun split(time: long, split_label: *char): long {
var new_time = get_time() var new_time = get_time()
print(string(split_label) + ": ") print(str(split_label) + ": ")
println(new_time - time) println(new_time - time)
return new_time return new_time
} }

View File

@@ -2,25 +2,25 @@ import grammer:*
import symbol:* import symbol:*
import lexer:* import lexer:*
import tree:* import tree:*
import vector:* import vec:*
import stack:* import stack:*
import map:* import map:*
import hash_map:* import hash_map:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
obj parser (Object) { obj parser (Object) {
var input: vector<symbol> var input: vec<symbol>
var gram: *grammer var gram: *grammer
var lex: *lexer var lex: *lexer
var gss: gss var gss: gss
var to_reduce: stack<reduction> var to_reduce: stack<reduction>
var to_shift: stack< pair<*tree<int>, int> > var to_shift: stack< pair<*tree<int>, int> >
var SPPFStepNodes: vector< pair<*tree<symbol>, int> > var SPPFStepNodes: vec< pair<*tree<symbol>, int> >
var packed_map: map<*tree<symbol>, bool> var packed_map: map<*tree<symbol>, bool>
var reduces_to_null_map: map<vector<symbol>, bool> var reduces_to_null_map: map<vec<symbol>, bool>
fun construct(grammerIn: *grammer, lexIn: *lexer): *parser { fun construct(grammerIn: *grammer, lexIn: *lexer): *parser {
input.construct() input.construct()
@@ -63,7 +63,7 @@ obj parser (Object) {
reduces_to_null_map.destruct() reduces_to_null_map.destruct()
} }
fun parse_input(inputStr: string, name: string): *tree<symbol> { fun parse_input(inputStr: str, name: str): *tree<symbol> {
input.clear() input.clear()
gss.clear() gss.clear()
to_reduce.clear() to_reduce.clear()
@@ -144,7 +144,7 @@ obj parser (Object) {
fun reducer(i: int) { fun reducer(i: int) {
var curr_reduction = to_reduce.pop() var curr_reduction = to_reduce.pop()
gss.get_reachable_paths(curr_reduction.from, max(0, curr_reduction.length-1)). gss.get_reachable_paths(curr_reduction.from, max(0, curr_reduction.length-1)).
for_each(fun(path: ref vector<*tree<int>>) { for_each(fun(path: ref vec<*tree<int>>) {
var path_edges = range(path.size-1).map(fun(indx: int): *tree<symbol> { return gss.get_edge(path[indx], path[indx+1]);}).reverse() var path_edges = range(path.size-1).map(fun(indx: int): *tree<symbol> { return gss.get_edge(path[indx], path[indx+1]);}).reverse()
if (curr_reduction.length != 0) { if (curr_reduction.length != 0) {
path_edges.addEnd(curr_reduction.label) path_edges.addEnd(curr_reduction.label)
@@ -262,7 +262,7 @@ obj parser (Object) {
} }
to_shift = next_shifts to_shift = next_shifts
} }
fun add_children(parent: *tree<symbol>, children: vector<*tree<symbol>>, nullable_parts: *tree<symbol>) { fun add_children(parent: *tree<symbol>, children: vec<*tree<symbol>>, nullable_parts: *tree<symbol>) {
if (nullable_parts) if (nullable_parts)
children.add(nullable_parts) children.add(nullable_parts)
if (!belongs_to_family(parent, children)) { if (!belongs_to_family(parent, children)) {
@@ -285,7 +285,7 @@ obj parser (Object) {
} }
} }
} }
fun belongs_to_family(node: *tree<symbol>, nodes: vector<*tree<symbol>>): bool { fun belongs_to_family(node: *tree<symbol>, nodes: vec<*tree<symbol>>): bool {
for (var i = 0; i < nodes.size; i++;) { for (var i = 0; i < nodes.size; i++;) {
var contains_one = false var contains_one = false
for (var j = 0; j < node->children.size; j++;) { for (var j = 0; j < node->children.size; j++;) {
@@ -300,7 +300,7 @@ obj parser (Object) {
} }
return true return true
} }
fun are_packed(nodes: vector<*tree<symbol>>): bool { fun are_packed(nodes: vec<*tree<symbol>>): bool {
return nodes.any_true(fun(it: *tree<symbol>):bool { return packed_map.contains_key(it) && packed_map[it]; }) return nodes.any_true(fun(it: *tree<symbol>):bool { return packed_map.contains_key(it) && packed_map[it]; })
} }
fun set_packed(node: *tree<symbol>, packed: bool) { fun set_packed(node: *tree<symbol>, packed: bool) {
@@ -330,7 +330,7 @@ obj parser (Object) {
} }
obj gss (Object) { obj gss (Object) {
var data: vector<vector<*tree<int>>> var data: vec<vec<*tree<int>>>
var edges: hash_map< pair<*tree<int>, *tree<int>>, *tree<symbol> > var edges: hash_map< pair<*tree<int>, *tree<int>>, *tree<symbol> >
fun construct(): *gss { fun construct(): *gss {
@@ -347,7 +347,7 @@ obj gss (Object) {
edges.destruct() edges.destruct()
} }
fun clear() { fun clear() {
data.for_each(fun(second: ref vector<*tree<int>>) second.for_each(fun(node: *tree<int>) delete(node););) data.for_each(fun(second: ref vec<*tree<int>>) second.for_each(fun(node: *tree<int>) delete(node););)
data.clear() data.clear()
edges.clear() edges.clear()
} }
@@ -356,7 +356,7 @@ obj gss (Object) {
} }
fun add_to_frontier(frontier: int, node: *tree<int>) { fun add_to_frontier(frontier: int, node: *tree<int>) {
while(data.size <= frontier) while(data.size <= frontier)
data.addEnd(vector<*tree<int>>()) data.addEnd(vec<*tree<int>>())
data[frontier].addEnd(node) data[frontier].addEnd(node)
} }
fun frontier_is_empty(frontier: int): bool { fun frontier_is_empty(frontier: int): bool {
@@ -391,9 +391,9 @@ obj gss (Object) {
return i return i
return -1 return -1
} }
fun get_reachable_paths(start: *tree<int>, length: int): vector<vector<*tree<int>>> { fun get_reachable_paths(start: *tree<int>, length: int): vec<vec<*tree<int>>> {
var paths = vector<vector<*tree<int>>>() var paths = vec<vec<*tree<int>>>()
var recursive_path_find: fun(*tree<int>, int, vector<*tree<int>>):void = fun(start: *tree<int>, length: int, current_path: vector<*tree<int>>) { var recursive_path_find: fun(*tree<int>, int, vec<*tree<int>>):void = fun(start: *tree<int>, length: int, current_path: vec<*tree<int>>) {
current_path.addEnd(start) current_path.addEnd(start)
if (!length) { if (!length) {
paths.addEnd(current_path) paths.addEnd(current_path)
@@ -403,7 +403,7 @@ obj gss (Object) {
recursive_path_find(child, length-1, current_path) recursive_path_find(child, length-1, current_path)
}) })
} }
recursive_path_find(start, length, vector<*tree<int>>()) recursive_path_find(start, length, vec<*tree<int>>())
return paths return paths
} }
} }
@@ -453,14 +453,14 @@ obj reduction (Object) {
} }
} }
fun syntax_tree_to_dot(root: *tree<symbol>): string { fun syntax_tree_to_dot(root: *tree<symbol>): str {
var ret = string("digraph Kaken {\n") var ret = str("digraph Kaken {\n")
var counter = 0 var counter = 0
var node_name_map = map<*tree<symbol>, string>() var node_name_map = map<*tree<symbol>, str>()
var get_name = fun(node: *tree<symbol>): string { var get_name = fun(node: *tree<symbol>): str {
if (node_name_map.contains_key(node)) if (node_name_map.contains_key(node))
return node_name_map[node] return node_name_map[node]
var escaped = string("") var escaped = str("")
node->data.to_string().data.for_each(fun(c: char) { node->data.to_string().data.for_each(fun(c: char) {
if (c != '"' && c != '\\') if (c != '"' && c != '\\')
escaped += c escaped += c
@@ -478,7 +478,7 @@ fun syntax_tree_to_dot(root: *tree<symbol>): string {
node->children.for_each(fun(child: *tree<symbol>) { node->children.for_each(fun(child: *tree<symbol>) {
if (!child) if (!child)
return; // where on earth does the null come from 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) helper(child)
}) })
} }

View File

@@ -1,9 +1,9 @@
import ast_nodes:* import ast_nodes:*
import mem:* import mem:*
import util:* import util:*
import vector:* import vec:*
import stack:* import stack:*
import string:* import str:*
import hash_set:* import hash_set:*
fun get_first_terminal(source: *tree<symbol>): *tree<symbol> { fun get_first_terminal(source: *tree<symbol>): *tree<symbol> {
@@ -15,8 +15,8 @@ fun get_first_terminal(source: *tree<symbol>): *tree<symbol> {
return null<tree<symbol>>() return null<tree<symbol>>()
return get_first_terminal(source->children.first()) return get_first_terminal(source->children.first())
} }
fun error(source: *tree<symbol>, message: *char) error(source, string(message)); fun error(source: *tree<symbol>, message: *char) error(source, str(message));
fun error(source: *tree<symbol>, message: string) { fun error(source: *tree<symbol>, message: str) {
var first = get_first_terminal(source) var first = get_first_terminal(source)
if (first) if (first)
error("***error |" + concat_symbol_tree(source) + "| *** " + first->data.source + ": " + first->data.position + " " + message) 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) && 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 == ".") && (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_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) // 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 // 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 // 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_type = get_ast_type(node)
var func_param_types = func_type->parameter_types var func_param_types = func_type->parameter_types
if (!func_type->is_variadic && func_param_types.size != param_types.size) { 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() + ", ";) 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 return false
} else if (param_types.size < func_param_types.size) { } else if (param_types.size < func_param_types.size) {
return false 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++;) { for (var j = 0; j < func_param_types.size; j++;) {
// don't care about references // don't care about references
if (!func_param_types[j]->equality(param_types[j], false)) { 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()) 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 false
} }
} }
return true 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: *char): *ast_node return access_expression(left, str(right))
fun access_expression(left: *ast_node, right: ref string): *ast_node { fun access_expression(left: *ast_node, right: ref str): *ast_node {
var ltype = get_ast_type(left) var ltype = get_ast_type(left)
if (!ltype->is_object()) if (!ltype->is_object())
error("Trying to generate an access expression and the left side is not an 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) error("Trying to generate an access expression, can't find right: " + right)
if (ltype->indirection) if (ltype->indirection)
return make_operator_call("->", vector(left, ident)) return make_operator_call("->", vec(left, ident))
return make_operator_call(".", vector(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) var results = scope_lookup(name, scope)
for (var i = 0; i < results.size; i++;) { 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)) { 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<ast_node>() return null<ast_node>()
} }
fun identifier_lookup(name: ref string, scope: *ast_node): *ast_node { fun identifier_lookup(name: ref str, scope: *ast_node): *ast_node {
/*println(string("doing identifier lookup for: ") + name)*/ /*println(str("doing identifier lookup for: ") + name)*/
var results = scope_lookup(name, scope) var results = scope_lookup(name, scope)
if (!results.size) { if (!results.size) {
/*println(string("identifier lookup failed for ") + name)*/ /*println(str("identifier lookup failed for ") + name)*/
return null<ast_node>() return null<ast_node>()
} }
return results[0] 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("*****Doing a name lookup for*****")
// println(name) // println(name)
var results = vector(scope) var results = vec(scope)
name.split("::").for_each(fun(i: string) { name.split("::").for_each(fun(i: str) {
// println(string("based on split, looking up: ") + i) // println(str("based on split, looking up: ") + i)
var next_results = vector<*ast_node>() var next_results = vec<*ast_node>()
results.for_each(fun(s: *ast_node) { results.for_each(fun(s: *ast_node) {
// print("looking in scope: ") // print("looking in scope: ")
// println(s) // println(s)
@@ -117,12 +117,12 @@ fun scope_lookup(name: ref string, scope: *ast_node): vector<*ast_node> {
}) })
return results 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 // need to do properly scopded lookups
// print("scope is: ") // 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() // println()
var results = vector<*ast_node>() var results = vec<*ast_node>()
// prevent re-checking the same one... // prevent re-checking the same one...
if (visited.contains(scope)) if (visited.contains(scope))
return results 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") // println(name + " is in scope, adding to results")
results += get_ast_scope(scope)->get(name) results += get_ast_scope(scope)->get(name)
} }
if (get_ast_scope(scope)->contains_key(string("~enclosing_scope"))) if (get_ast_scope(scope)->contains_key(str("~enclosing_scope")))
results += scope_lookup_helper(name, get_ast_scope(scope)->get(string("~enclosing_scope"))[0], visited) results += scope_lookup_helper(name, get_ast_scope(scope)->get(str("~enclosing_scope"))[0], visited)
if (is_translation_unit(scope)) { if (is_translation_unit(scope)) {
scope->translation_unit.children.for_each(fun(child: *ast_node) { scope->translation_unit.children.for_each(fun(child: *ast_node) {
if (is_import(child)) { if (is_import(child)) {
@@ -145,98 +145,98 @@ fun scope_lookup_helper(name: ref string, scope: *ast_node, visited: set<*ast_no
} else { } else {
// println(name + " is not imported (this time)") // println(name + " is not imported (this time)")
// print("import imports") // 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 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: *char, parameter_types: vec<*type>): bool return has_method(object, str(name), parameter_types);
fun has_method(object: *ast_node, name: string, parameter_types: vector<*type>): bool { fun has_method(object: *ast_node, name: str, parameter_types: vec<*type>): bool {
var to_ret = function_lookup(name, object, parameter_types) || false var to_ret = function_lookup(name, object, parameter_types) || false
return to_ret return to_ret
} }
fun get_from_scope(node: *ast_node, member: *char): *ast_node fun get_from_scope(node: *ast_node, member: *char): *ast_node
return get_from_scope(node, string(member)) return get_from_scope(node, str(member))
fun get_from_scope(node: *ast_node, member: string): *ast_node { fun get_from_scope(node: *ast_node, member: str): *ast_node {
/*if (get_ast_scope(node)->contains(member))*/ /*if (get_ast_scope(node)->contains(member))*/
return get_ast_scope(node)->get(member).first() return get_ast_scope(node)->get(member).first()
/*return null<ast_node>()*/ /*return null<ast_node>()*/
} }
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: *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: string, parameters: vector<*ast_node>): *ast_node { 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 // 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);)) 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) 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 = "." var access_op = "."
if (get_ast_type(object_ident)->indirection) if (get_ast_type(object_ident)->indirection)
access_op = "->" 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) 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: *char, params: vec<*ast_node>): *ast_node return make_operator_call(str(func), params);
fun make_operator_call(func: string, params: vector<*ast_node>): *ast_node { 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) 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 fun get_builtin_function(name: *char, param_types: vec<*type>): *ast_node
return get_builtin_function(string(name), param_types, null<tree<symbol>>()) return get_builtin_function(str(name), param_types, null<tree<symbol>>())
fun get_builtin_function(name: string, param_types: vector<*type>): *ast_node fun get_builtin_function(name: str, param_types: vec<*type>): *ast_node
return get_builtin_function(name, param_types, null<tree<symbol>>()) return get_builtin_function(name, param_types, null<tree<symbol>>())
fun get_builtin_function(name: string, param_types: vector<*type>, syntax: *tree<symbol>): *ast_node { fun get_builtin_function(name: str, param_types: vec<*type>, syntax: *tree<symbol>): *ast_node {
// none of the builtin functions should take in references // none of the builtin functions should take in references
param_types = param_types.map(fun(t: *type): *type return t->clone_without_ref();) 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 == "!") 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 == "." || name == "->") {
if (name == "->" && param_types[0]->indirection == 0) 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) 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 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 (name == "[]") {
if (param_types[0]->indirection == 0) if (param_types[0]->indirection == 0)
error(syntax, string("drereferencing not a pointer: ") + name) error(syntax, str("drereferencing not a pointer: ") + name)
else 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) 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 (name == "*" && param_types.size == 1) {
if (param_types[0]->indirection == 0) if (param_types[0]->indirection == 0)
error(syntax, string("drereferencing not a pointer: ") + name) error(syntax, str("drereferencing not a pointer: ") + name)
else 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()) 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[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), vector<*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 { fun possible_object_equality(lvalue: *ast_node, rvalue: *ast_node): *ast_node {
var ltype = get_ast_type(lvalue) var ltype = get_ast_type(lvalue)
var rtype = get_ast_type(rvalue) var rtype = get_ast_type(rvalue)
if (ltype->indirection == 0 && (ltype->is_object() && has_method(ltype->type_def, "operator==", vector(rtype)))) { if (ltype->indirection == 0 && (ltype->is_object() && has_method(ltype->type_def, "operator==", vec(rtype)))) {
return make_method_call(lvalue, "operator==", vector(rvalue)) return make_method_call(lvalue, "operator==", vec(rvalue))
} else if (ltype->is_object()) } else if (ltype->is_object())
// return false if object but no operator== (right now don't try for templated) // 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 ast_value_ptr(str("false"), type_ptr(base_type::boolean()))
return make_operator_call("==", vector(lvalue, rvalue)) return make_operator_call("==", vec(lvalue, rvalue))
} }
fun concat_symbol_tree(node: *tree<symbol>): string { fun concat_symbol_tree(node: *tree<symbol>): str {
var str.construct(): string var str.construct(): str
if (node->data.data != "no_value") if (node->data.data != "no_value")
str += node->data.data str += node->data.data
node->children.for_each(fun(child: *tree<symbol>) str += concat_symbol_tree(child);) node->children.for_each(fun(child: *tree<symbol>) str += concat_symbol_tree(child);)
return str return str
} }
fun get_node(lookup: *char, parent: *tree<symbol>): *tree<symbol> { fun get_node(lookup: *char, parent: *tree<symbol>): *tree<symbol> {
return get_node(string(lookup), parent) return get_node(str(lookup), parent)
} }
fun get_node(lookup: string, parent: *tree<symbol>): *tree<symbol> { fun get_node(lookup: str, parent: *tree<symbol>): *tree<symbol> {
var results = get_nodes(lookup, parent) var results = get_nodes(lookup, parent)
if (results.size > 1) if (results.size > 1)
error(parent, "get node too many results!") error(parent, "get node too many results!")
@@ -244,32 +244,32 @@ fun get_node(lookup: string, parent: *tree<symbol>): *tree<symbol> {
return results[0] return results[0]
return null<tree<symbol>>() return null<tree<symbol>>()
} }
fun get_nodes(lookup: *char, parent: *tree<symbol>): vector<*tree<symbol>> { fun get_nodes(lookup: *char, parent: *tree<symbol>): vec<*tree<symbol>> {
return get_nodes(string(lookup), parent) return get_nodes(str(lookup), parent)
} }
fun get_nodes(lookup: string, parent: *tree<symbol>): vector<*tree<symbol>> { fun get_nodes(lookup: str, parent: *tree<symbol>): vec<*tree<symbol>> {
return parent->children.filter(fun(node: *tree<symbol>):bool return node->data.name == lookup;) return parent->children.filter(fun(node: *tree<symbol>):bool return node->data.name == lookup;)
} }
fun add_to_scope(name: *char, to_add: *ast_node, add_to: *ast_node) { 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) var add_to_map = get_ast_scope(add_to)
if (add_to_map->contains_key(name)) if (add_to_map->contains_key(name))
(*add_to_map)[name].add(to_add) (*add_to_map)[name].add(to_add)
else 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 // 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 { fun assign_or_copy_construct_statement(lvalue: *ast_node, rvalue: *ast_node): *ast_node {
var ltype = get_ast_type(lvalue) 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())))) 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", vector(make_operator_call("&", vector(rvalue)))) return make_method_call(lvalue, "copy_construct", vec(make_operator_call("&", vec(rvalue))))
return ast_assignment_statement_ptr(lvalue, rvalue) return ast_assignment_statement_ptr(lvalue, rvalue)
} }
fun get_children_pointer(node: *ast_node): *vector<*ast_node> { fun get_children_pointer(node: *ast_node): *vec<*ast_node> {
var bc = null<vector<*ast_node>>() var bc = null<vec<*ast_node>>()
match(*node) { match(*node) {
ast_node::translation_unit(backing) bc = &node->translation_unit.children ast_node::translation_unit(backing) bc = &node->translation_unit.children
ast_node::code_block(backing) bc = &node->code_block.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 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>) fun replace_with_in(orig: *ast_node, new: *ast_node, in: *stack<*ast_node>)
replace_with_in(orig, new, in->top()) 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 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);) 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);) 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>) 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 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>) fun add_after_in(to_add: *ast_node, before: *ast_node, in: *stack<*ast_node>)
add_after_in(to_add, before, in->top()) 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 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 { 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") 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) if (!n)
return string("NULL") return str("NULL")
var above = string() var above = str()
var scope_map = get_ast_scope(n); var scope_map = get_ast_scope(n);
if (scope_map && scope_map->contains_key(string("~enclosing_scope"))) if (scope_map && scope_map->contains_key(str("~enclosing_scope")))
above = get_fully_scoped_name(scope_map->get(string("~enclosing_scope"))[0]) above = get_fully_scoped_name(scope_map->get(str("~enclosing_scope"))[0])
return above + "::" + get_ast_name(n) return above + "::" + get_ast_name(n)
} }

View File

@@ -1,4 +1,4 @@
import vector:* import vec:*
import queue:* import queue:*
import map:* import map:*
import set:* import set:*
@@ -44,8 +44,8 @@ obj poset<T> (Object) {
}) })
return depends_on return depends_on
} }
fun get_sorted(): vector<T> { fun get_sorted(): vec<T> {
var sorted = vector<T>() var sorted = vec<T>()
var to_do = queue<T>() var to_do = queue<T>()
// because we're going to destructivly update // because we're going to destructivly update
var temp_adj_matrix = adj_matrix var temp_adj_matrix = adj_matrix

View File

@@ -1,6 +1,6 @@
import stack import stack
import serialize import serialize
import vector import vec
fun queue<T>() : queue<T> { fun queue<T>() : queue<T> {
var out.construct() : queue<T> var out.construct() : queue<T>
@@ -32,11 +32,11 @@ obj queue<T> (Object, Serializable) {
stack2 = other.stack2 stack2 = other.stack2
} }
fun serialize() : vector::vector<char> { fun serialize() : vec::vec<char> {
return serialize::serialize(stack1)+serialize::serialize(stack2) return serialize::serialize(stack1)+serialize::serialize(stack2)
} }
fun unserialize(it : ref vector::vector<char>, pos : int) : int { fun unserialize(it : ref vec::vec<char>, pos : int) : int {
pos = stack1.unserialize(it,pos) pos = stack1.unserialize(it,pos)
pos = stack2.unserialize(it,pos) pos = stack2.unserialize(it,pos)
return pos return pos

View File

@@ -1,11 +1,11 @@
import symbol:* import symbol:*
import tree:* import tree:*
import map:* import map:*
import vector:* import vec:*
import set:* import set:*
import hash_set:* import hash_set:*
import util:* import util:*
import string:* import str:*
import mem:* import mem:*
import io:* import io:*
import ast_nodes:* import ast_nodes:*
@@ -37,12 +37,12 @@ fun remove_ref(t: *type) {
} }
} }
fun ref_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) { fun ref_lower(name_ast_map: *map<str, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var remove_ref_type_set = set<pair<string,*type>>() var remove_ref_type_set = set<pair<str,*type>>()
var modify_reference_use_set = set<pair<*ast_node, *ast_node>>() var modify_reference_use_set = set<pair<*ast_node, *ast_node>>()
var modify_return_set = set<*ast_node>() var modify_return_set = set<*ast_node>()
var visited = hash_set<*ast_node>() var visited = hash_set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
match(*node) { match(*node) {
ast_node::identifier(backing) { ast_node::identifier(backing) {
@@ -62,7 +62,7 @@ fun ref_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
var func_type_params = get_ast_type(backing.func)->parameter_types var func_type_params = get_ast_type(backing.func)->parameter_types
for (var i = 0; i < func_type_params.size; i++;) for (var i = 0; i < func_type_params.size; i++;)
if (func_type_params[i]->is_ref) 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 // 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) if (get_ast_type(backing.func)->return_type->is_ref)
modify_reference_use_set.add(make_pair(node, parent_chain->top())) modify_reference_use_set.add(make_pair(node, parent_chain->top()))
@@ -76,7 +76,7 @@ fun ref_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
} }
run_on_tree(helper_before, empty_pass_second_half(), syntax_ast_pair.second, &visited) run_on_tree(helper_before, empty_pass_second_half(), syntax_ast_pair.second, &visited)
}) })
remove_ref_type_set.for_each(fun(p: pair<string, *type>) { remove_ref_type_set.for_each(fun(p: pair<str, *type>) {
var t = p.second var t = p.second
remove_ref(t) remove_ref(t)
}) })
@@ -89,9 +89,9 @@ fun ref_lower(name_ast_map: *map<string, pair<*tree<symbol>,*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 // 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) // 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))) 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) { 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));
}) })
} }

View File

@@ -1,17 +1,17 @@
import io import io
import string import str
import util import util
import vector import vec
import string import str
import mem import mem
import set import set
import util import util
import serialize import serialize
fun regex(in: *char):regex { 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 var out.construct(in):regex
return out return out
} }
@@ -52,7 +52,7 @@ obj regexState (Object) {
fun destruct():void { fun destruct():void {
next_states.destruct() next_states.destruct()
} }
fun match_char(input: char, states: ref vector::vector<regexState>, flags: *vector::vector<bool>, num_states: *int) { fun match_char(input: char, states: ref vec::vec<regexState>, flags: *vec::vec<bool>, num_states: *int) {
next_states.for_each(fun(it:int) { next_states.for_each(fun(it:int) {
if (states[it].characterBegin <= input && input <= states[it].characterEnd) { if (states[it].characterBegin <= input && input <= states[it].characterEnd) {
(*flags)[it] = true (*flags)[it] = true
@@ -60,16 +60,16 @@ obj regexState (Object) {
} }
}) })
} }
fun is_end(states: ref vector::vector<regexState>):bool { fun is_end(states: ref vec::vec<regexState>):bool {
return next_states.any_true(fun(state: int):bool { return states[state].characterBegin == 1; }) return next_states.any_true(fun(state: int):bool { return states[state].characterBegin == 1; })
} }
} }
obj regex (Object, Serializable) { obj regex (Object, Serializable) {
var regexString: string::string var regexString: str::str
var states: vector::vector<regexState> var states: vec::vec<regexState>
var flagsA: vector::vector<bool> var flagsA: vec::vec<bool>
var flagsB: vector::vector<bool> var flagsB: vec::vec<bool>
var is_straight_string: bool var is_straight_string: bool
fun construct(): *regex { fun construct(): *regex {
@@ -80,12 +80,12 @@ obj regex (Object, Serializable) {
is_straight_string = false is_straight_string = false
return this return this
} }
fun construct(regexStringIn: string::string): *regex { fun construct(regexStringIn: str::str): *regex {
regexString.copy_construct(&regexStringIn) regexString.copy_construct(&regexStringIn)
states.construct() states.construct()
is_straight_string = true is_straight_string = true
for (var i = 0; i < regexString.length(); i++;) { 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] == '|') { if (regexString[i] == '\\' || regexString[i] == '(' || regexString[i] == ')' || regexString[i] == '[' || regexString[i] == '*' || regexString[i] == '+' || regexString[i] == '?' || regexString[i] == '|') {
is_straight_string = false is_straight_string = false
break break
@@ -117,10 +117,10 @@ obj regex (Object, Serializable) {
flagsA.destruct() flagsA.destruct()
flagsB.destruct() flagsB.destruct()
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(regexString) return serialize::serialize(regexString)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
pos = regexString.unserialize(it, pos) pos = regexString.unserialize(it, pos)
states.construct() states.construct()
construct(regexString) construct(regexString)
@@ -138,7 +138,7 @@ obj regex (Object, Serializable) {
copy_construct(&other) copy_construct(&other)
} }
fun compile(regex_string: string::string): util::pair<int, set::set<int>> { fun compile(regex_string: str::str): util::pair<int, set::set<int>> {
/*io::println(regex_string)*/ /*io::println(regex_string)*/
var first = states.size; states.add(regexState()) var first = states.size; states.add(regexState())
var previous_begin = set::set<int>() var previous_begin = set::set<int>()
@@ -168,7 +168,7 @@ obj regex (Object, Serializable) {
var perenEnd = i + 1 var perenEnd = i + 1
for (var depth = 1; depth > 0; perenEnd++;) { for (var depth = 1; depth > 0; perenEnd++;) {
if (perenEnd >= regex_string.length()) 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 // 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] != '\\' && 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] != ']')) 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 return beginAndEnd
} }
fun long_match(to_match: *char): int { return long_match(string::string(to_match)); } fun long_match(to_match: *char): int { return long_match(str::str(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: str::str): int return long_match(to_match.getBackingMemory(), 0, to_match.length())
fun long_match(to_match: *char, position: int, end: int): int { fun long_match(to_match: *char, position: int, end: int): int {
if (is_straight_string) { if (is_straight_string) {
if (regexString.length() > end-position) if (regexString.length() > end-position)

View File

@@ -1,27 +1,27 @@
import vector import vec
import mem import mem
import util import util
fun serialize<T(Serializable)>(it: T): vector::vector<char> { fun serialize<T(Serializable)>(it: T): vec::vec<char> {
return it.serialize() return it.serialize()
} }
fun serialize<T>(it: T): vector::vector<char> { fun serialize<T>(it: T): vec::vec<char> {
var char_data = (&it) cast *char var char_data = (&it) cast *char
var toRet = vector::vector<char>() var toRet = vec::vec<char>()
for (var i = 0; i < #sizeof<T>; i++;) for (var i = 0; i < #sizeof<T>; i++;)
toRet.add(char_data[i]) toRet.add(char_data[i])
return toRet return toRet
} }
// dead simple wrapper for ease of use // dead simple wrapper for ease of use
fun unserialize<T>(it: ref vector::vector<char>): T { fun unserialize<T>(it: ref vec::vec<char>): T {
return unserialize<T>(it, 0).first return unserialize<T>(it, 0).first
} }
fun unserialize<T>(it: ref vector::vector<char>, pos: int): util::pair<T,int> { fun unserialize<T>(it: ref vec::vec<char>, pos: int): util::pair<T,int> {
return util::make_pair(*(it.getBackingMemory()+pos) cast *T, pos + (#sizeof<T>) cast int) return util::make_pair(*(it.getBackingMemory()+pos) cast *T, pos + (#sizeof<T>) cast int)
} }
fun unserialize<T(Serializable)>(it: ref vector::vector<char>, pos: int): util::pair<T,int> { fun unserialize<T(Serializable)>(it: ref vec::vec<char>, pos: int): util::pair<T,int> {
var toRet: T var toRet: T
pos = toRet.unserialize(it, pos) pos = toRet.unserialize(it, pos)
return util::make_pair(toRet, pos) return util::make_pair(toRet, pos)

View File

@@ -1,4 +1,4 @@
import vector import vec
import io import io
import serialize import serialize
@@ -13,14 +13,14 @@ fun set<T>(item: T): set<T> {
return toRet return toRet
} }
fun from_vector<T>(items: vector::vector<T>): set<T> { fun from_vector<T>(items: vec::vec<T>): set<T> {
var toRet.construct() : set<T> var toRet.construct() : set<T>
items.for_each( fun(item: T) toRet.add(item); ) items.for_each( fun(item: T) toRet.add(item); )
return toRet return toRet
} }
obj set<T> (Object, Serializable) { obj set<T> (Object, Serializable) {
var data: vector::vector<T> var data: vec::vec<T>
fun construct(): *set<T> { fun construct(): *set<T> {
data.construct() data.construct()
return this return this
@@ -35,10 +35,10 @@ obj set<T> (Object, Serializable) {
fun operator=(rhs: ref set<T>) { fun operator=(rhs: ref set<T>) {
data = rhs.data data = rhs.data
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(data) return serialize::serialize(data)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
return data.unserialize(it, pos) return data.unserialize(it, pos)
} }
fun operator==(rhs: ref set<T>): bool { fun operator==(rhs: ref set<T>): bool {
@@ -87,7 +87,7 @@ obj set<T> (Object, Serializable) {
fun add(items: ref set<T>) { fun add(items: ref set<T>) {
items.for_each( fun(item: ref T) add(item); ) items.for_each( fun(item: ref T) add(item); )
} }
fun add(items: ref vector::vector<T>) { fun add(items: ref vec::vec<T>) {
items.for_each( fun(item: ref T) add(item); ) items.for_each( fun(item: ref T) add(item); )
} }
fun remove(item: ref T) { fun remove(item: ref T) {

View File

@@ -1,8 +1,8 @@
import vector import vec
import serialize import serialize
import util import util
fun stack_from_vector<T>(i: vector::vector<T>): stack<T> { fun stack_from_vector<T>(i: vec::vec<T>): stack<T> {
var to_ret.construct(): stack<T> var to_ret.construct(): stack<T>
to_ret.data = i to_ret.data = i
return to_ret return to_ret
@@ -19,7 +19,7 @@ fun stack<T>(in:T):stack<T> {
} }
obj stack<T> (Object, Serializable) { obj stack<T> (Object, Serializable) {
var data: vector::vector<T> var data: vec::vec<T>
fun construct(): *stack<T> { fun construct(): *stack<T> {
data.construct() data.construct()
return this return this
@@ -37,10 +37,10 @@ obj stack<T> (Object, Serializable) {
fun operator=(other: ref stack<T>) { fun operator=(other: ref stack<T>) {
data = other.data data = other.data
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(data) return serialize::serialize(data)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
return data.unserialize(it, pos) return data.unserialize(it, pos)
} }
fun push(it: ref T) { fun push(it: ref T) {
@@ -81,7 +81,7 @@ obj stack<T> (Object, Serializable) {
fun for_each_reverse(func: fun(T):void) { fun for_each_reverse(func: fun(T):void) {
data.for_each_reverse(func) data.for_each_reverse(func)
} }
fun reverse_vector(): vector::vector<T> fun reverse_vector(): vec::vec<T>
return data.reverse() return data.reverse()
fun index_from_top_satisfying(func_raw: run(T):bool): int { fun index_from_top_satisfying(func_raw: run(T):bool): int {
var temp_lambda = fun(i: T):bool { return func_raw(i); } var temp_lambda = fun(i: T):bool { return func_raw(i); }

View File

@@ -1,43 +1,43 @@
import vector import vec
import util import util
import mem import mem
import serialize import serialize
import io import io
ext fun snprintf(to_str: *char, num: ulong, format: *char, ...): int 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) return to_string((in) cast double)
fun to_string(in: double): string { fun to_string(in: double): str {
var how_much = snprintf(mem::null<char>(), (0) cast ulong, "%f", in) var how_much = snprintf(mem::null<char>(), (0) cast ulong, "%f", in)
var int_str = mem::new<char>(how_much+2) var int_str = mem::new<char>(how_much+2)
snprintf(int_str, (how_much+1) cast ulong, "%f", in) 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) mem::delete(int_str)
return to_ret return to_ret
} }
fun to_string(in: bool): string fun to_string(in: bool): str
if (in) return string("true") if (in) return str("true")
else return string("false") else return str("false")
fun to_string(in: char): string fun to_string(in: char): str
return string(in) return str(in)
fun to_string(in: uchar): string fun to_string(in: uchar): str
return to_string_num(in) return to_string_num(in)
fun to_string(in: short): string fun to_string(in: short): str
return to_string_num(in) return to_string_num(in)
fun to_string(in: ushort): string fun to_string(in: ushort): str
return to_string_num(in) return to_string_num(in)
fun to_string(in: int): string fun to_string(in: int): str
return to_string_num(in) return to_string_num(in)
fun to_string(in: uint): string fun to_string(in: uint): str
return to_string_num(in) return to_string_num(in)
fun to_string(in: long): string fun to_string(in: long): str
return to_string_num(in) return to_string_num(in)
fun to_string(in: ulong): string fun to_string(in: ulong): str
return to_string_num(in) return to_string_num(in)
fun to_string<T>(in: *T): string fun to_string<T>(in: *T): str
return string("ptr:<") + to_string_num((in) cast ulong) + ">" return str("ptr:<") + to_string_num((in) cast ulong) + ">"
fun string_to_num<T>(it: string): T { fun string_to_num<T>(it: str): T {
var is_negative = false var is_negative = false
if (it[0] == '-') { if (it[0] == '-') {
is_negative = true is_negative = true
@@ -54,17 +54,17 @@ fun string_to_num<T>(it: string): T {
return -result return -result
return result return result
} }
fun string_to_double(it: string): double { fun string_to_double(it: str): double {
var parts = it.split('.') var parts = it.split('.')
var divisor = 1 var divisor = 1
for (var i = 0; i < parts[1].length(); i++;) divisor *= 10 for (var i = 0; i < parts[1].length(); i++;) divisor *= 10
return string_to_num<long>(parts[0]) + (string_to_num<long>(parts[1])) cast double / divisor return string_to_num<long>(parts[0]) + (string_to_num<long>(parts[1])) cast double / divisor
} }
fun to_string_num<T>(in: T): string { fun to_string_num<T>(in: T): str {
if (in == 0) if (in == 0)
return string("0") return str("0")
var ret = string() var ret = str()
var pos = 0 var pos = 0
var is_neg = false var is_neg = false
if (in > 0) { if (in > 0) {
@@ -74,45 +74,45 @@ fun to_string_num<T>(in: T): string {
is_neg = true is_neg = true
} }
while (pos) { while (pos) {
ret = string((pos%10 + '0') cast char) + ret ret = str((pos%10 + '0') cast char) + ret
pos = pos / 10 pos = pos / 10
} }
if (is_neg) if (is_neg)
return string("-") + ret return str("-") + ret
return ret return ret
} }
fun operator+(first: *char, second: ref string): string { fun operator+(first: *char, second: ref str): str {
return string(first) + second 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 return to_string(first) + second
} }
fun string(in:*char):string { fun str(in:*char):str {
var out.construct(in):string var out.construct(in):str
return out return out
} }
fun string(in:char):string { fun str(in:char):str {
var out.construct():string var out.construct():str
out += in out += in
return out return out
} }
fun string():string { fun str():str {
return string("") return str("")
} }
obj string (Object, Serializable, Hashable) { obj str (Object, Serializable, Hashable) {
var data: vector::vector<char>; var data: vec::vec<char>;
fun construct(): *string { fun construct(): *str {
data.construct(); data.construct();
return this; return this;
} }
fun construct(ammt: int): *string { fun construct(ammt: int): *str {
data.construct(ammt); data.construct(ammt);
return this; return this;
} }
fun construct(str: *char): *string { fun construct(str: *char): *str {
var len = 0 var len = 0
while (str[len] != 0) len++ while (str[len] != 0) len++
data.construct(len); data.construct(len);
@@ -121,15 +121,15 @@ obj string (Object, Serializable, Hashable) {
// no null terminator // no null terminator
return this; return this;
} }
fun construct(vec: ref vector::vector<char>): *string { fun construct(vec: ref vec::vec<char>): *str {
data.copy_construct(&vec); data.copy_construct(&vec);
return this; return this;
} }
fun construct(str: ref string): *string { fun construct(str: ref str): *str {
return construct(str.data); return construct(str.data);
} }
fun copy_construct(old: *string): void { fun copy_construct(old: *str): void {
data.copy_construct(&old->data) data.copy_construct(&old->data)
} }
@@ -149,7 +149,7 @@ obj string (Object, Serializable, Hashable) {
construct(str) construct(str)
} }
fun operator=(str: ref string): void { fun operator=(str: ref str): void {
data = str.data data = str.data
} }
@@ -157,16 +157,16 @@ obj string (Object, Serializable, Hashable) {
data.destruct() data.destruct()
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(data) return serialize::serialize(data)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
return data.unserialize(it, pos) return data.unserialize(it, pos)
} }
fun operator[](index: int): ref char { return data[index]; } fun operator[](index: int): ref char { return data[index]; }
fun slice(first: int, second: int): string { fun slice(first: int, second: int): str {
var new.construct(data.slice(first,second)): string var new.construct(data.slice(first,second)): str
return new return new
} }
fun set(index: int, toSet: char) { fun set(index: int, toSet: char) {
@@ -174,9 +174,9 @@ obj string (Object, Serializable, Hashable) {
} }
fun length():int { return data.size; } 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: *char): bool return !(*this==other)
fun operator==(other: ref string): bool { fun operator==(other: ref str): bool {
// you were too good for this world // 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]; } ) //return length() == other.length() && !util::range(length()).any_true(fun(i: int):bool { return data[i] != other[i]; } )
if (data.size != other.data.size) if (data.size != other.data.size)
@@ -187,28 +187,28 @@ obj string (Object, Serializable, Hashable) {
return true return true
} }
fun operator==(other: *char): bool { fun operator==(other: *char): bool {
var str.construct(other) : string var str.construct(other) : str
return *this == str return *this == str
} }
fun operator+(c: char): string { fun operator+(c: char): str {
var to_ret = *this var to_ret = *this
to_ret += c to_ret += c
return to_ret return to_ret
} }
fun operator+(integer: int): string return *this + to_string(integer); fun operator+(integer: int): str return *this + to_string(integer);
fun operator+(integer: long): string return *this + to_string(integer); fun operator+(integer: long): str return *this + to_string(integer);
fun operator+(integer: ulong): string return *this + to_string(integer); fun operator+(integer: ulong): str return *this + to_string(integer);
/*fun operator+(b: bool): string return *this + to_string(b);*/ /*fun operator+(b: bool): str return *this + to_string(b);*/
fun operator+(str: *char): string { fun operator+(str: *char): str {
var newStr.construct(str):string var newStr.construct(str):str
var ret.construct(data + newStr.data):string var ret.construct(data + newStr.data):str
return ret return ret
} }
fun operator+(str: ref string): string { fun operator+(str: ref str): str {
var ret.construct(data + str.data):string var ret.construct(data + str.data):str
return ret return ret
} }
@@ -221,12 +221,12 @@ obj string (Object, Serializable, Hashable) {
} }
fun operator+=(str: *char): void { fun operator+=(str: *char): void {
var newStr.construct(str):string var newStr.construct(str):str
data += newStr.data data += newStr.data
} }
fun operator+=(str: ref string): void { fun operator+=(str: ref str): void {
//var newStr.construct(str):string //var newStr.construct(str):str
//data += newStr.data //data += newStr.data
data += str.data data += str.data
} }
@@ -241,14 +241,14 @@ obj string (Object, Serializable, Hashable) {
} }
fun getBackingMemory(): *char return data.getBackingMemory(); fun getBackingMemory(): *char return data.getBackingMemory();
fun split(delim: *char): vector::vector<string> return split(string(delim)) fun split(delim: *char): vec::vec<str> return split(str(delim))
fun split(delim: string): vector::vector<string> { fun split(delim: str): vec::vec<str> {
var out.construct(): vector::vector<string> var out.construct(): vec::vec<str>
var current = string("") var current = str("")
for (var i = 0; i < data.size; i++;) { for (var i = 0; i < data.size; i++;) {
if (i < data.size-delim.length() && slice(i, i+delim.length()) == delim) { if (i < data.size-delim.length() && slice(i, i+delim.length()) == delim) {
out.add(current) out.add(current)
current = string("") current = str("")
i += delim.length()-1 i += delim.length()-1
} else { } else {
current += data[i] current += data[i]
@@ -259,14 +259,14 @@ obj string (Object, Serializable, Hashable) {
} }
fun first(): char return data.first() fun first(): char return data.first()
fun last(): char return data.last() fun last(): char return data.last()
fun lines(): vector::vector<string> return split('\n') fun lines(): vec::vec<str> return split('\n')
fun split(delim: char): vector::vector<string> { fun split(delim: char): vec::vec<str> {
var out.construct(): vector::vector<string> var out.construct(): vec::vec<str>
var current = string("") var current = str("")
for (var i = 0; i < data.size; i++;) { for (var i = 0; i < data.size; i++;) {
if (data[i] == delim) { if (data[i] == delim) {
out.add(current) out.add(current)
current = string("") current = str("")
} else { } else {
current += data[i] current += data[i]
} }
@@ -274,7 +274,7 @@ obj string (Object, Serializable, Hashable) {
out.add(current) out.add(current)
return out return out
} }
fun join(to_join: ref vector::vector<string>): string { fun join(to_join: ref vec::vec<str>): str {
var to_ret = to_join.first() var to_ret = to_join.first()
for (var i = 1; i < to_join.size; i++;) for (var i = 1; i < to_join.size; i++;)
to_ret += *this + to_join[i] to_ret += *this + to_join[i]

View File

@@ -1,50 +1,50 @@
import string import str
import serialize import serialize
import vector import vec
import util import util
fun null_symbol(): symbol { 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 return toRet
} }
fun eof_symbol(): symbol { 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 return toRet
} }
fun invalid_symbol(): symbol { 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 return toRet
} }
fun symbol(nameIn: *char, terminalIn: bool): symbol { 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 return toRet
} }
fun symbol(nameIn: ref string::string, terminalIn: bool): symbol { fun symbol(nameIn: ref str::str, terminalIn: bool): symbol {
var toRet.construct(nameIn, terminalIn, string::string("no_value")): symbol var toRet.construct(nameIn, terminalIn, str::str("no_value")): symbol
return toRet return toRet
} }
fun symbol(nameIn: *char, terminalIn: bool, dataIn: *char): symbol { 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 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 var toRet.construct(nameIn, terminalIn, dataIn): symbol
toRet.position = position toRet.position = position
return toRet return toRet
} }
obj symbol (Object, Serializable) { obj symbol (Object, Serializable) {
var data: string::string var data: str::str
var name: string::string var name: str::str
var terminal: bool var terminal: bool
var source: string::string var source: str::str
var position: int var position: int
fun construct(): *symbol { fun construct(): *symbol {
@@ -55,7 +55,7 @@ obj symbol (Object, Serializable) {
position = 0 position = 0
return this 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) name.construct(nameIn)
terminal = terminalIn terminal = terminalIn
data.construct(dataIn) data.construct(dataIn)
@@ -79,13 +79,13 @@ obj symbol (Object, Serializable) {
destruct() destruct()
copy_construct(&old) copy_construct(&old)
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(data) + serialize::serialize(name) + serialize::serialize(terminal) + serialize::serialize(source) + serialize::serialize(position) return serialize::serialize(data) + serialize::serialize(name) + serialize::serialize(terminal) + serialize::serialize(source) + serialize::serialize(position)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
/*construct()*/ /*construct()*/
/*util::unpack(data, pos) = serialize::unserialize<string::string>(it, pos)*/ /*util::unpack(data, pos) = serialize::unserialize<str::str>(it, pos)*/
/*util::unpack(name, pos) = serialize::unserialize<string::string>(it, pos)*/ /*util::unpack(name, pos) = serialize::unserialize<str::str>(it, pos)*/
pos = data.unserialize(it, pos) pos = data.unserialize(it, pos)
pos = name.unserialize(it, pos) pos = name.unserialize(it, pos)
util::unpack(terminal, pos) = serialize::unserialize<bool>(it, pos) util::unpack(terminal, pos) = serialize::unserialize<bool>(it, pos)
@@ -102,7 +102,7 @@ obj symbol (Object, Serializable) {
fun operator!=(other: ref symbol): bool { fun operator!=(other: ref symbol): bool {
return !(*this == other); return !(*this == other);
} }
fun to_string(): string::string { fun to_string(): str::str {
var terminalString: *char var terminalString: *char
if (terminal) if (terminal)
terminalString = "true" terminalString = "true"

View File

@@ -1,9 +1,9 @@
import mem import mem
import vector import vec
obj tree<T> (Object) { obj tree<T> (Object) {
var data: T var data: T
var children: vector::vector<*tree<T>> var children: vec::vec<*tree<T>>
fun construct(dataIn: T): *tree<T> { fun construct(dataIn: T): *tree<T> {
mem::maybe_copy_construct(&data, &dataIn) mem::maybe_copy_construct(&data, &dataIn)
children.construct() children.construct()

View File

@@ -1,6 +1,6 @@
import mem:* import mem:*
import string:* import str:*
import vector:* import vec:*
import set:* import set:*
import ast_nodes:* import ast_nodes:*
import io:* import io:*
@@ -31,8 +31,8 @@ adt base_type {
fun type_ptr(): *type { fun type_ptr(): *type {
return new<type>()->construct() return new<type>()->construct()
} }
fun type_ptr(definition: *ast_node): *type return type_ptr(definition, set<string>()); fun type_ptr(definition: *ast_node): *type return type_ptr(definition, set<str>());
fun type_ptr(definition: *ast_node, traits: set<string>): *type { fun type_ptr(definition: *ast_node, traits: set<str>): *type {
return new<type>()->construct(definition, traits) return new<type>()->construct(definition, traits)
} }
fun type_ptr(base: base_type): *type return type_ptr(base, 0, false); 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 { fun type_ptr(base: base_type, indirection: int, is_ref: bool): *type {
return new<type>()->construct(base, indirection, is_ref) return new<type>()->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<type>()->construct(parameters, return_type, indirection, is_ref, is_variadic, is_raw) return new<type>()->construct(parameters, return_type, indirection, is_ref, is_variadic, is_raw)
fun type_ptr(traits: set<string>): *type { fun type_ptr(traits: set<str>): *type {
return new<type>()->construct(traits) return new<type>()->construct(traits)
} }
obj type (Object) { obj type (Object) {
var base: base_type var base: base_type
var parameter_types: vector<*type> var parameter_types: vec<*type>
var is_variadic: bool var is_variadic: bool
var is_raw: bool var is_raw: bool
var return_type: *type var return_type: *type
var indirection: int var indirection: int
var type_def: *ast_node var type_def: *ast_node
var traits: set<string> var traits: set<str>
var is_ref: bool var is_ref: bool
fun construct(): *type { fun construct(): *type {
base.copy_construct(&base_type::none()) base.copy_construct(&base_type::none())
@@ -69,7 +69,7 @@ obj type (Object) {
is_raw = false is_raw = false
return this return this
} }
fun construct(traits_in: set<string>): *type { fun construct(traits_in: set<str>): *type {
base.copy_construct(&base_type::template_type()) base.copy_construct(&base_type::template_type())
parameter_types.construct() parameter_types.construct()
indirection = 0 indirection = 0
@@ -93,7 +93,7 @@ obj type (Object) {
is_raw = false is_raw = false
return this return this
} }
fun construct(type_def_in: *ast_node, traits_in: set<string>): *type { fun construct(type_def_in: *ast_node, traits_in: set<str>): *type {
base.copy_construct(&base_type::object()) base.copy_construct(&base_type::object())
parameter_types.construct() parameter_types.construct()
indirection = 0 indirection = 0
@@ -105,7 +105,7 @@ obj type (Object) {
is_raw = false is_raw = false
return this 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()) base.copy_construct(&base_type::function())
parameter_types.copy_construct(&parameter_types_in) parameter_types.copy_construct(&parameter_types_in)
return_type = return_type_in return_type = return_type_in
@@ -151,16 +151,16 @@ obj type (Object) {
return base == other.base && deref_equality(return_type, other.return_type) && return base == other.base && deref_equality(return_type, other.return_type) &&
indirection == other.indirection && deref_equality(type_def, other.type_def) && traits == other.traits indirection == other.indirection && deref_equality(type_def, other.type_def) && traits == other.traits
} }
fun to_string(): string return to_string(true); fun to_string(): str return to_string(true);
fun to_string(include_traits: bool): string { fun to_string(include_traits: bool): str {
var trait_string = string() var trait_string = str()
if (include_traits) { if (include_traits) {
trait_string = string(":[") trait_string = str(":[")
traits.for_each(fun(t: string) trait_string += t;) traits.for_each(fun(t: str) trait_string += t;)
trait_string += "] " trait_string += "] "
} }
var indr_string = string("") var indr_string = str("")
if (is_ref) if (is_ref)
indr_string += " ref " indr_string += " ref "
if (is_variadic) if (is_variadic)
@@ -169,30 +169,30 @@ obj type (Object) {
indr_string += " raw " indr_string += " raw "
for (var i = 0; i < indirection; i++;) indr_string += "*" for (var i = 0; i < indirection; i++;) indr_string += "*"
match (base) { 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::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::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() return indr_string + str("template") + trait_string
base_type::template_type() return indr_string + string("template_type") + trait_string base_type::template_type() return indr_string + str("template_type") + trait_string
base_type::void_return() return indr_string + string("void_return") + trait_string base_type::void_return() return indr_string + str("void_return") + trait_string
base_type::boolean() return indr_string + string("boolean") + trait_string base_type::boolean() return indr_string + str("boolean") + trait_string
base_type::character() return indr_string + string("character") + trait_string base_type::character() return indr_string + str("character") + trait_string
base_type::short_int() return indr_string + string("short") + trait_string base_type::short_int() return indr_string + str("short") + trait_string
base_type::integer() return indr_string + string("integer") + trait_string base_type::integer() return indr_string + str("integer") + trait_string
base_type::long_int() return indr_string + string("long") + trait_string base_type::long_int() return indr_string + str("long") + trait_string
base_type::ucharacter() return indr_string + string("ucharacter") + trait_string base_type::ucharacter() return indr_string + str("ucharacter") + trait_string
base_type::ushort_int() return indr_string + string("ushort") + trait_string base_type::ushort_int() return indr_string + str("ushort") + trait_string
base_type::uinteger() return indr_string + string("uinteger") + trait_string base_type::uinteger() return indr_string + str("uinteger") + trait_string
base_type::ulong_int() return indr_string + string("ulong") + trait_string base_type::ulong_int() return indr_string + str("ulong") + trait_string
base_type::floating() return indr_string + string("floating") + trait_string base_type::floating() return indr_string + str("floating") + trait_string
base_type::double_precision() return indr_string + string("double_precision") + trait_string base_type::double_precision() return indr_string + str("double_precision") + trait_string
base_type::function() { 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() + ", ";) parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + ", ";)
return temp + ")" + return_type->to_string() + trait_string return temp + ")" + return_type->to_string() + trait_string
} }
} }
return string("impossible type, indirection:") + indirection return str("impossible type, indirection:") + indirection
} }
fun rank(): int { fun rank(): int {
if (indirection > 0) if (indirection > 0)

View File

@@ -1,10 +1,10 @@
import mem import mem
import io import io
import os import os
import string import str
import set import set
import map import map
import vector import vec
import serialize import serialize
@@ -14,8 +14,8 @@ fun do_nothing() {}
fun is_object<T>(): bool return false fun is_object<T>(): bool return false
fun is_object<T(Object)>(): bool return true fun is_object<T(Object)>(): bool return true
fun error(message: *char) error(string::string(message)); fun error(message: *char) error(str::str(message));
fun error(message: string::string) { fun error(message: str::str) {
io::printlnerr("****ERROR****") io::printlnerr("****ERROR****")
io::printlnerr(message) io::printlnerr(message)
os::exit(-1) os::exit(-1)
@@ -24,7 +24,7 @@ fun assert(works: bool, message: *char) {
if (!works) if (!works)
error(message) error(message)
} }
fun assert(works: bool, message: string::string) { fun assert(works: bool, message: str::str) {
if (!works) if (!works)
error(message) error(message)
} }
@@ -110,10 +110,10 @@ obj pair<T,U> (Object, Serializable, Hashable) {
mem::maybe_destruct(&first) mem::maybe_destruct(&first)
mem::maybe_destruct(&second) mem::maybe_destruct(&second)
} }
fun serialize(): vector::vector<char> { fun serialize(): vec::vec<char> {
return serialize::serialize(first) + serialize::serialize(second) return serialize::serialize(first) + serialize::serialize(second)
} }
fun unserialize(it: ref vector::vector<char>, pos: int): int { fun unserialize(it: ref vec::vec<char>, pos: int): int {
// can't use unpack :( (b/c we can't make an already constructed empty one) // can't use unpack :( (b/c we can't make an already constructed empty one)
var first_pair = serialize::unserialize<T>(it, pos) var first_pair = serialize::unserialize<T>(it, pos)
var second_pair = serialize::unserialize<U>(it, first_pair.second) var second_pair = serialize::unserialize<U>(it, first_pair.second)
@@ -158,8 +158,8 @@ obj range {
for (var i = begin; i < end; i+= step;) for (var i = begin; i < end; i+= step;)
func(i) func(i)
} }
fun map<T>(func: fun(int): T): vector::vector<T> { fun map<T>(func: fun(int): T): vec::vec<T> {
var ret.construct( (end-begin)/step + 1 ) : vector::vector<T> var ret.construct( (end-begin)/step + 1 ) : vec::vec<T>
for (var i = begin; i < end; i+= step;) for (var i = begin; i < end; i+= step;)
ret.addEnd(func(i)) ret.addEnd(func(i))
return ret return ret

View File

@@ -4,36 +4,36 @@ import io:*;
import serialize:*; import serialize:*;
import util:*; import util:*;
fun vector<T>():vector<T> { fun vec<T>():vec<T> {
var out.construct():vector<T> var out.construct():vec<T>
return out return out
} }
fun vector<T>(in:T):vector<T> { fun vec<T>(in:T):vec<T> {
var out.construct():vector<T> var out.construct():vec<T>
out.add(in) out.add(in)
return out return out
} }
obj vector<T> (Object, Serializable) { obj vec<T> (Object, Serializable) {
var data: *T; var data: *T;
var size: int; var size: int;
var available: int; var available: int;
fun construct(): *vector<T> { fun construct(): *vec<T> {
size = 0; size = 0;
available = 8; available = 8;
data = new<T>(8); data = new<T>(8);
return this; return this;
} }
fun construct(ammt: int): *vector<T> { fun construct(ammt: int): *vec<T> {
size = 0; size = 0;
available = ammt; available = ammt;
data = new<T>(ammt); data = new<T>(ammt);
return this; return this;
} }
fun copy_construct(old: *vector<T>): void { fun copy_construct(old: *vec<T>): void {
construct(old->available) construct(old->available)
size = old->size size = old->size
if (is_object<T>()) { if (is_object<T>()) {
@@ -43,7 +43,7 @@ obj vector<T> (Object, Serializable) {
memmove((data) cast *void, (old->data) cast *void, size * #sizeof<T>) memmove((data) cast *void, (old->data) cast *void, size * #sizeof<T>)
} }
} }
fun swap(other: ref vector<T>) { fun swap(other: ref vec<T>) {
var temp_data = data var temp_data = data
var temp_size = size var temp_size = size
var temp_available = available var temp_available = available
@@ -54,13 +54,13 @@ obj vector<T> (Object, Serializable) {
other.size = temp_size other.size = temp_size
other.available = temp_available other.available = temp_available
} }
fun serialize(): vector<char> { fun serialize(): vec<char> {
var toRet = serialize(size) var toRet = serialize(size)
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
toRet += serialize(data[i]) toRet += serialize(data[i])
return toRet return toRet
} }
fun unserialize(it: ref vector<char>, pos: int): int { fun unserialize(it: ref vec<char>, pos: int): int {
unpack(size, pos) = unserialize<int>(it, pos) unpack(size, pos) = unserialize<int>(it, pos)
data = new<T>(size) data = new<T>(size)
available = size available = size
@@ -83,7 +83,7 @@ obj vector<T> (Object, Serializable) {
size = s size = s
} }
fun operator=(other:ref vector<T>):void { fun operator=(other:ref vec<T>):void {
if (size < other.size) { if (size < other.size) {
destruct() destruct()
copy_construct(&other) copy_construct(&other)
@@ -99,16 +99,16 @@ obj vector<T> (Object, Serializable) {
} }
} }
fun operator+(other: ref vector<T>):vector<T> { fun operator+(other: ref vec<T>):vec<T> {
var newVec.construct(size+other.size):vector<T> var newVec.construct(size+other.size):vec<T>
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
newVec.addEnd(get(i)) newVec.addEnd(get(i))
for (var i = 0; i < other.size; i++;) for (var i = 0; i < other.size; i++;)
newVec.addEnd(other.get(i)) newVec.addEnd(other.get(i))
return newVec return newVec
} }
fun operator+(other: ref T):vector<T> { fun operator+(other: ref T):vec<T> {
var newVec.copy_construct(this):vector<T> var newVec.copy_construct(this):vec<T>
newVec.addEnd(other) newVec.addEnd(other)
return newVec return newVec
} }
@@ -117,19 +117,19 @@ obj vector<T> (Object, Serializable) {
addEnd(other) addEnd(other)
} }
fun operator+=(other: ref vector<T>):void { fun operator+=(other: ref vec<T>):void {
for (var i = 0; i < other.size; i++;) for (var i = 0; i < other.size; i++;)
addEnd(other.get(i)) addEnd(other.get(i))
} }
fun clone(): vector<T> { fun clone(): vec<T> {
var newVec.construct(size): vector<T> var newVec.construct(size): vec<T>
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
newVec.addEnd(data[i]) newVec.addEnd(data[i])
return newVec return newVec
} }
fun reverse(): vector<T> { fun reverse(): vec<T> {
var newVec.construct(size): vector<T> var newVec.construct(size): vec<T>
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
newVec.addEnd(data[(size-i)-1]) newVec.addEnd(data[(size-i)-1])
return newVec return newVec
@@ -148,12 +148,12 @@ obj vector<T> (Object, Serializable) {
return true; return true;
} }
fun slice(start: int, end: int): vector<T> { fun slice(start: int, end: int): vec<T> {
if (start < 0) if (start < 0)
start += size + 1 start += size + 1
if (end < 0) if (end < 0)
end += size + 1 end += size + 1
var new.construct(end-start): vector<T> var new.construct(end-start): vec<T>
for (var i = start; i < end; i++;) for (var i = start; i < end; i++;)
new.add(data[i]) new.add(data[i])
return new return new
@@ -172,7 +172,7 @@ obj vector<T> (Object, Serializable) {
println("Vector access out of bounds! Retuning 0th element as sanest option"); println("Vector access out of bounds! Retuning 0th element as sanest option");
print("Vector tried to access element: "); print("Vector tried to access element: ");
println(index); println(index);
print("Max Index of vector: "); print("Max Index of vec: ");
println(size-1); println(size-1);
while(true) {} while(true) {}
return data[0]; return data[0];
@@ -184,7 +184,7 @@ obj vector<T> (Object, Serializable) {
// This is a template for the interesting reason that structs // 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 // 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 // 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. // do a find index using a different type, which could be fun.
fun find<U>(value: ref U): int { fun find<U>(value: ref U): int {
@@ -199,7 +199,7 @@ obj vector<T> (Object, Serializable) {
} }
// yep // yep
fun operator==<U>(other: ref vector<U>):bool { fun operator==<U>(other: ref vec<U>):bool {
if (size != other.size) if (size != other.size)
return false return false
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
@@ -213,7 +213,7 @@ obj vector<T> (Object, Serializable) {
return; return;
data[index] = dataIn; data[index] = dataIn;
} }
fun add_all(dataIn: ref vector<T>): void { fun add_all(dataIn: ref vec<T>): void {
for (var i = 0; i < dataIn.size; i++;) for (var i = 0; i < dataIn.size; i++;)
addEnd(dataIn[i]); addEnd(dataIn[i]);
} }
@@ -283,20 +283,20 @@ obj vector<T> (Object, Serializable) {
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
data[i] = func(data[i]) data[i] = func(data[i])
} }
fun map<U>(func: fun(ref T):U):vector<U> { fun map<U>(func: fun(ref T):U):vec<U> {
var newVec.construct(size): vector<U> var newVec.construct(size): vec<U>
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
newVec.addEnd(func(data[i])) newVec.addEnd(func(data[i]))
return newVec return newVec
} }
fun map<U>(func: fun(T):U):vector<U> { fun map<U>(func: fun(T):U):vec<U> {
var newVec.construct(size): vector<U> var newVec.construct(size): vec<U>
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
newVec.addEnd(func(data[i])) newVec.addEnd(func(data[i]))
return newVec return newVec
} }
fun flatten_map<U>(func: fun(T):vector<U>):vector<U> { fun flatten_map<U>(func: fun(T):vec<U>):vec<U> {
var newVec.construct(size): vector<U> var newVec.construct(size): vec<U>
for (var i = 0; i < size; i++;) { for (var i = 0; i < size; i++;) {
var to_add = func(data[i]) var to_add = func(data[i])
for (var j = 0; j < to_add.size; j++;) for (var j = 0; j < to_add.size; j++;)
@@ -305,8 +305,8 @@ obj vector<T> (Object, Serializable) {
return newVec return newVec
} }
fun find_first_satisfying(func: fun(T):bool): T return filter(func)[0] fun find_first_satisfying(func: fun(T):bool): T return filter(func)[0]
fun filter(func: fun(T):bool):vector<T> { fun filter(func: fun(T):bool):vec<T> {
var newVec.construct(): vector<T> var newVec.construct(): vec<T>
for (var i = 0; i < size; i++;) for (var i = 0; i < size; i++;)
if (func(data[i])) if (func(data[i]))
newVec.addEnd(data[i]) newVec.addEnd(data[i])

View File

@@ -1,22 +1,22 @@
import vector import vec
fun vector<T>(first:T, second:T):vector::vector<T> { fun vec<T>(first:T, second:T):vec::vec<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
return out return out
} }
fun vector<T>(first:T, second:T, third:T):vector::vector<T> { fun vec<T>(first:T, second:T, third:T):vec::vec<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
out.add(third) out.add(third)
return out return out
} }
fun vector<T>(first:T, second:T, third:T, fourth:T):vector::vector<T> { fun vec<T>(first:T, second:T, third:T, fourth:T):vec::vec<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
out.add(third) out.add(third)
@@ -24,8 +24,8 @@ fun vector<T>(first:T, second:T, third:T, fourth:T):vector::vector<T> {
return out return out
} }
fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T):vector::vector<T> { fun vec<T>(first:T, second:T, third:T, fourth:T, fifth:T):vec::vec<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
out.add(third) out.add(third)
@@ -34,8 +34,8 @@ fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T):vector::vector<T> {
return out return out
} }
fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T):vector::vector<T> { fun vec<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T):vec::vec<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
out.add(third) out.add(third)
@@ -45,8 +45,8 @@ fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T):vector::ve
return out return out
} }
fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T):vector::vector<T> { fun vec<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T):vec::vec<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
out.add(third) out.add(third)
@@ -57,8 +57,8 @@ fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T)
return out return out
} }
fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T):vector::vector<T> { fun vec<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T):vec::vec<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
out.add(third) out.add(third)
@@ -70,8 +70,8 @@ fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T,
return out return out
} }
fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T, ninth:T):vector::vector<T> { fun vec<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T, eighth:T, ninth:T):vec::vec<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
out.add(third) out.add(third)
@@ -84,8 +84,8 @@ fun vector<T>(first:T, second:T, third:T, fourth:T, fifth:T, sixth:T, seventh:T,
return out return out
} }
fun vector<T>(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<T> { fun vec<T>(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<T> {
var out.construct():vector::vector<T> var out.construct():vec::vec<T>
out.add(first) out.add(first)
out.add(second) out.add(second)
out.add(third) out.add(third)