From e898e7b285cb9206e9c9bed1e0ab06605e7b0012 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Thu, 14 Jun 2018 00:08:55 -0400 Subject: [PATCH] Pass system works --- k.krak | 132 ++++++++++++++++++++++++++++++++++++++++++++++ stdlib/map.krak | 3 ++ stdlib/poset.krak | 21 +++++++- 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 k.krak diff --git a/k.krak b/k.krak new file mode 100644 index 0000000..c120d60 --- /dev/null +++ b/k.krak @@ -0,0 +1,132 @@ + +import io:* +import grammer:* +import lexer:* +import parser:* +import str:* +import serialize:* +import os:* +import vec:* +import vec_literals:* +import poset:* +import util:* + +fun main(argc: int, argv: **char):int { + // delay construction until we either load it or copy construct it + var gram: grammer + var base_dir = str("/").join(str(argv[0]).split('/').slice(0,-2)) + var file_name = base_dir + "/krakenGrammer.kgm" + var compiled_name = file_name + str(".comp_new") + var compiled_version = 1 + var file_contents = read_file(file_name) + var loaded_and_valid = false + + if (argc <= 1) { + println("No input file!\n Call with one argument (the input file), or two arguments (input file and output name)") + exit(1) + } else if (str(argv[1]) == "-v" || str(argv[1]) == "--version") { + println("0.0 pre") + exit(0) + } + var opt_str = str("-O2") + var compile_c = true + var positional_args = vec() + for (var i = 1; i < argc; i++;) { + var arg_str = str(argv[i]) + if (arg_str.length() > 2 && arg_str.slice(0,2) == "-O") { + opt_str = arg_str + } else if (arg_str == "--no-c-compile") { + compile_c = false + } else { + positional_args.add(arg_str) + } + } + + if (file_exists(compiled_name)) { + var pos = 0 + var binary = read_file_binary(compiled_name) + var saved_version = 0 + unpack(saved_version, pos) = unserialize(binary, pos) + if (saved_version == compiled_version) { + var cached_contents = str() + unpack(cached_contents, pos) = unserialize(binary, pos) + if (cached_contents == file_contents) { + loaded_and_valid = true + pos = gram.unserialize(binary, pos) + } else println("contents different") + } else println("version number different") + } else { + println("cached file does not exist") + } + if (!loaded_and_valid) { + println("Not loaded_and_valid, re-generating and writing out") + gram.copy_construct(&load_grammer(file_contents)) + println("grammer loaded, calculate_first_set") + gram.calculate_first_set() + println("grammer loaded, calculate_state_automaton") + gram.calculate_state_automaton() + println("calculated, writing out") + write_file_binary(compiled_name, serialize(compiled_version) + serialize(file_contents) + serialize(gram)) + println("done writing") + } + + var lex = lexer(gram.terminals) + var parse.construct(&gram, &lex): parser + + var kraken_file_name = positional_args[0] + var executable_name = str(".").join(kraken_file_name.split('.').slice(0,-2)) + if (positional_args.size > 1) + executable_name = positional_args[1] + + var pass_poset = poset>() + var name_ast_map = map() + var import_paths = vec(str(), base_dir + "/stdlib/") + var passes = vec( + fun(file_name: str): *node { + println("Doing thing 1 to " + file_name) + var file = str() + for (var i = 0; i < import_paths.size; i++;) { + if (file_exists(import_paths[i] + file_name)) { + if (file != "") + error("File: " + file_name + ", found in multiple import paths - at least two of [" + str(",").join(import_paths) + "]") + file = read_file(import_paths[i] + file_name) + } + } + if (file == "") + error("File: " + file_name + ", not found in any import path - none of [" + str(",").join(import_paths) + "]") + printerr(file_name + ", ") + var parse_tree = parse.parse_input(file, file_name) + return new()->copy_construct(&node::placeholder()) + }, + fun(file_name: str): *node { + println("Doing thing 2 to " + file_name) + return new()->copy_construct(&node::placeholder()) + } + ) + for (var i = 0; i < passes.size; i++;) { + if (i == 0) + pass_poset.add_vertex(make_pair(kraken_file_name, i)) + else + pass_poset.add_relationship(make_pair(kraken_file_name, i), make_pair(kraken_file_name, i-1)) + } + while (pass_poset.size() != 0) { + var file_pass = pass_poset.pop() + name_ast_map[file_pass.first] = passes[file_pass.second](file_pass.first) + } + + var kraken_c_output_name = kraken_file_name + ".c" + var c_code = str("//don't you wish this was real kraken\n") + var c_flags = str("") + write_file(kraken_c_output_name, c_code) + + if (compile_c) { + var compile_string = "cc -g " + opt_str + " -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast -Wno-incompatible-pointer-types -std=c99 " + c_flags + " " + kraken_c_output_name + " -o " + executable_name + /*printlnerr(compile_string)*/ + /*system(compile_string)*/ + } + + return 0 +} +adt node { + placeholder +} diff --git a/stdlib/map.krak b/stdlib/map.krak index 9728272..8bb121d 100644 --- a/stdlib/map.krak +++ b/stdlib/map.krak @@ -43,6 +43,9 @@ obj map (Object, Serializable) { pos = values.unserialize(it, pos) return pos } + fun size(): int { + return keys.size + } // the old unnecessary template to prevent generation // if not used trick (in this case, changing out U with V) fun operator==(other: ref map): bool { diff --git a/stdlib/poset.krak b/stdlib/poset.krak index 39c7e8e..486955e 100644 --- a/stdlib/poset.krak +++ b/stdlib/poset.krak @@ -2,6 +2,7 @@ import vec:* import queue:* import map:* import set:* +import util:* fun poset(): poset { var to_ret.construct(): poset @@ -24,6 +25,9 @@ obj poset (Object) { fun destruct() { adj_matrix.destruct() } + fun size(): int { + return adj_matrix.size() + } fun add_relationship(first: T, second: T) { if (!adj_matrix.contains_key(first)) add_vertex(first) @@ -44,6 +48,22 @@ obj poset (Object) { }) return depends_on } + fun pop(): T { + for (var i = 0; i < adj_matrix.keys.size; i++;) { + if (adj_matrix.values[i].size() == 0) { + var to_ret = adj_matrix.keys[i] + for (var j = 0; j < adj_matrix.keys.size; j++;) { + // remove is ok if it doesn't exist + /*if (adj_matrix.values[i].contains(to_ret)) {*/ + adj_matrix.values[j].remove(to_ret) + /*}*/ + } + adj_matrix.remove(to_ret) + return to_ret + } + } + error("Nothing to pop") + } fun get_sorted(): vec { var sorted = vec() var to_do = queue() @@ -56,7 +76,6 @@ obj poset (Object) { sorted.add(current) get_depends_on(current).for_each(fun(vert: T) { temp_adj_matrix[vert].remove(current) - /*print(vert); print(" now has "); print(temp_adj_matrix[vert].size()); println(" dependencies")*/ if (temp_adj_matrix[vert].size() == 0) to_do.push(vert) })