Clean up for demo
This commit is contained in:
18
kraken.krak
18
kraken.krak
@@ -16,9 +16,9 @@ fun main(argc: int, argv: **char):int {
|
|||||||
// 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 = string("/").join(string(argv[0]).split('/').slice(0,-2))
|
||||||
println(base_dir)
|
/*println(base_dir)*/
|
||||||
var file_name = base_dir + "/krakenGrammer.kgm"
|
var file_name = base_dir + "/krakenGrammer.kgm"
|
||||||
println(file_name)
|
/*println(file_name)*/
|
||||||
|
|
||||||
var compiled_name = file_name + string(".comp_new")
|
var compiled_name = file_name + string(".comp_new")
|
||||||
var compiled_version = 1
|
var compiled_version = 1
|
||||||
@@ -26,22 +26,22 @@ fun main(argc: int, argv: **char):int {
|
|||||||
var loaded_and_valid = false
|
var loaded_and_valid = false
|
||||||
|
|
||||||
if (file_exists(compiled_name)) {
|
if (file_exists(compiled_name)) {
|
||||||
println("cached file exists")
|
/*println("cached file exists")*/
|
||||||
var pos = 0
|
var pos = 0
|
||||||
var binary = read_file_binary(compiled_name)
|
var binary = read_file_binary(compiled_name)
|
||||||
println("read file!")
|
/*println("read file!")*/
|
||||||
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 = string()
|
||||||
unpack(cached_contents, pos) = unserialize<string>(binary, pos)
|
unpack(cached_contents, pos) = unserialize<string>(binary, pos)
|
||||||
if (cached_contents == file_contents) {
|
if (cached_contents == file_contents) {
|
||||||
println("loaded_and_valid, using cached version!")
|
/*println("loaded_and_valid, using cached version!")*/
|
||||||
loaded_and_valid = true
|
loaded_and_valid = true
|
||||||
/*unpack(gram, pos) = unserialize<grammer>(binary, pos)*/
|
/*unpack(gram, pos) = unserialize<grammer>(binary, pos)*/
|
||||||
// skip unnecessary copies this way
|
// skip unnecessary copies this way
|
||||||
pos = gram.unserialize(binary, pos)
|
pos = gram.unserialize(binary, pos)
|
||||||
println("finished unserializeing!!")
|
/*println("finished unserializeing!!")*/
|
||||||
} else println("contents different")
|
} else println("contents different")
|
||||||
} else println("version number different")
|
} else println("version number different")
|
||||||
} else {
|
} else {
|
||||||
@@ -68,13 +68,15 @@ fun main(argc: int, argv: **char):int {
|
|||||||
var parse.construct(gram): parser
|
var parse.construct(gram): parser
|
||||||
var ast_pass.construct(): ast_transformation
|
var ast_pass.construct(): ast_transformation
|
||||||
var importer.construct(parse, ast_pass, vector(string(), base_dir + "/stdlib/")): importer
|
var importer.construct(parse, ast_pass, vector(string(), base_dir + "/stdlib/")): importer
|
||||||
|
/*print("parsing: ")*/
|
||||||
importer.import(kraken_file_name)
|
importer.import(kraken_file_name)
|
||||||
println("NOW DOING C_GENERATOR")
|
println("Generating C")
|
||||||
|
/*println("NOW DOING C_GENERATOR")*/
|
||||||
var c_generator.construct(): c_generator
|
var c_generator.construct(): c_generator
|
||||||
var c_output_pair = c_generator.generate_c(importer.name_ast_map)
|
var c_output_pair = c_generator.generate_c(importer.name_ast_map)
|
||||||
var kraken_c_output_name = kraken_file_name + ".c"
|
var kraken_c_output_name = kraken_file_name + ".c"
|
||||||
write_file(kraken_c_output_name, c_output_pair.first)
|
write_file(kraken_c_output_name, c_output_pair.first)
|
||||||
println(string("linker string: ") + c_output_pair.second)
|
/*println(string("linker string: ") + c_output_pair.second)*/
|
||||||
|
|
||||||
var executable_name = string(".").join(kraken_file_name.split('.').slice(0,-2))
|
var executable_name = string(".").join(kraken_file_name.split('.').slice(0,-2))
|
||||||
if (argc == 3)
|
if (argc == 3)
|
||||||
|
|||||||
37
kraken_minimal.krak
Normal file
37
kraken_minimal.krak
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import io:*
|
||||||
|
import grammer:*
|
||||||
|
import parser:*
|
||||||
|
import ast_transformation:*
|
||||||
|
import string:*
|
||||||
|
import util:*
|
||||||
|
import symbol:*
|
||||||
|
import tree:*
|
||||||
|
import serialize:*
|
||||||
|
import c_generator:*
|
||||||
|
import os:*
|
||||||
|
|
||||||
|
fun main(argc: int, argv: **char):int {
|
||||||
|
var gram: grammer
|
||||||
|
var base_dir = string("/").join(string(argv[0]).split('/').slice(0,-2))
|
||||||
|
var file_name = base_dir + "/krakenGrammer.kgm"
|
||||||
|
var file_contents = read_file(file_name)
|
||||||
|
gram.copy_construct(&load_grammer(file_contents))
|
||||||
|
gram.calculate_first_set()
|
||||||
|
gram.calculate_state_automaton()
|
||||||
|
var kraken_file_name = string(argv[1])
|
||||||
|
var parse.construct(gram): parser
|
||||||
|
var ast_pass.construct(): ast_transformation
|
||||||
|
var importer.construct(parse, ast_pass, vector(string(), base_dir + "/stdlib/")): importer
|
||||||
|
importer.import(kraken_file_name)
|
||||||
|
var c_generator.construct(): c_generator
|
||||||
|
var c_output_pair = c_generator.generate_c(importer.name_ast_map)
|
||||||
|
var kraken_c_output_name = kraken_file_name + ".c"
|
||||||
|
write_file(kraken_c_output_name, c_output_pair.first)
|
||||||
|
var executable_name = string(".").join(kraken_file_name.split('.').slice(0,-2))
|
||||||
|
if (argc == 3)
|
||||||
|
executable_name = string(argv[2])
|
||||||
|
var compile_string = "cc -g -O3 -std=c99 " + c_output_pair.second + " " + kraken_c_output_name + " -o " + executable_name
|
||||||
|
system(compile_string)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ obj ast_transformation (Object) {
|
|||||||
}
|
}
|
||||||
// defines inside of objects + ADTs, outside declaration statements, and function prototypes
|
// defines inside of objects + ADTs, outside declaration statements, and function prototypes
|
||||||
fun second_pass(parse_tree: *tree<symbol>, translation_unit: *ast_node) {
|
fun second_pass(parse_tree: *tree<symbol>, translation_unit: *ast_node) {
|
||||||
println(string("Second Pass for ") + translation_unit->translation_unit.name)
|
/*println(string("Second Pass for ") + translation_unit->translation_unit.name)*/
|
||||||
// we go through the parse tree for getting functions, but we're going through the ast for the things we've already set up and using the ast_to_syntax map
|
// we go through the parse tree for getting functions, but we're going through the ast for the things we've already set up and using the ast_to_syntax map
|
||||||
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") {
|
||||||
@@ -246,7 +246,7 @@ obj ast_transformation (Object) {
|
|||||||
}
|
}
|
||||||
// The third pass finishes up by doing all function bodies (top level and methods in objects)
|
// The third pass finishes up by doing all function bodies (top level and methods in objects)
|
||||||
fun third_pass(parse_tree: *tree<symbol>, translation_unit: *ast_node) {
|
fun third_pass(parse_tree: *tree<symbol>, translation_unit: *ast_node) {
|
||||||
println(string("Third Pass for ") + translation_unit->translation_unit.name)
|
/*println(string("Third Pass for ") + translation_unit->translation_unit.name)*/
|
||||||
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) {
|
ast_node::type_def(backing) {
|
||||||
@@ -270,7 +270,7 @@ obj ast_transformation (Object) {
|
|||||||
}
|
}
|
||||||
// The fourth pass generates the class templates that have not yet been generated in a worklist loop
|
// The fourth pass generates the class templates that have not yet been generated in a worklist loop
|
||||||
fun fourth_pass(parse_tree: *tree<symbol>, translation_unit: *ast_node) {
|
fun fourth_pass(parse_tree: *tree<symbol>, translation_unit: *ast_node) {
|
||||||
println(string("Fourth Pass for ") + translation_unit->translation_unit.name)
|
/*println(string("Fourth Pass for ") + translation_unit->translation_unit.name)*/
|
||||||
while (!fourth_pass_worklist.empty()) {
|
while (!fourth_pass_worklist.empty()) {
|
||||||
var partially_inst_type_def = fourth_pass_worklist.pop()
|
var partially_inst_type_def = fourth_pass_worklist.pop()
|
||||||
partially_inst_type_def->type_def.methods.for_each(fun(method: *ast_node) {
|
partially_inst_type_def->type_def.methods.for_each(fun(method: *ast_node) {
|
||||||
|
|||||||
@@ -41,7 +41,9 @@ obj importer (Object) {
|
|||||||
}
|
}
|
||||||
fun import(file_name: string): *ast_node {
|
fun import(file_name: string): *ast_node {
|
||||||
println("**First Pass**")
|
println("**First Pass**")
|
||||||
|
print("parsing: ")
|
||||||
var to_ret = import_first_pass(file_name)
|
var to_ret = import_first_pass(file_name)
|
||||||
|
println()
|
||||||
println("**Second Pass**")
|
println("**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: string, tree_pair: pair<*tree<symbol>, *ast_node>) ast_pass.second_pass(tree_pair.first, tree_pair.second);)
|
||||||
println("**Third Pass**")
|
println("**Third Pass**")
|
||||||
@@ -66,14 +68,16 @@ obj importer (Object) {
|
|||||||
/*print("pre-parse: "); println(file_name)*/
|
/*print("pre-parse: "); println(file_name)*/
|
||||||
var file = string()
|
var file = string()
|
||||||
import_paths.for_each(fun(path: string) {
|
import_paths.for_each(fun(path: string) {
|
||||||
println(string("Checking ") + path + " for " + file_name)
|
/*println(string("Checking ") + path + " for " + file_name)*/
|
||||||
if (file_exists(path + file_name)) {
|
if (file_exists(path + file_name)) {
|
||||||
println("Found it!")
|
/*println("Found it!")*/
|
||||||
file = read_file(path + file_name)
|
file = read_file(path + file_name)
|
||||||
return
|
return
|
||||||
} else
|
} else {
|
||||||
println("did not find it")
|
/*println("did not find it")*/
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
print(file_name + ", ")
|
||||||
var parse_tree = parse.parse_input(file, file_name)
|
var parse_tree = parse.parse_input(file, file_name)
|
||||||
/*print("post-parse: "); println(file_name)*/
|
/*print("post-parse: "); println(file_name)*/
|
||||||
/*write_file(file_name + ".parse.dot", syntax_tree_to_dot(parse_tree))*/
|
/*write_file(file_name + ".parse.dot", syntax_tree_to_dot(parse_tree))*/
|
||||||
@@ -88,8 +92,8 @@ obj importer (Object) {
|
|||||||
}
|
}
|
||||||
fun register(file_name: string, parse_tree: *tree<symbol>, translation_unit: *ast_node) {
|
fun register(file_name: string, parse_tree: *tree<symbol>, translation_unit: *ast_node) {
|
||||||
name_ast_map.set(file_name, make_pair(parse_tree, translation_unit))
|
name_ast_map.set(file_name, make_pair(parse_tree, translation_unit))
|
||||||
print("Registered parse_tree+translation_unit for ")
|
/*print("Registered parse_tree+translation_unit for ")*/
|
||||||
println(file_name)
|
/*println(file_name)*/
|
||||||
}
|
}
|
||||||
fun trim(parse_tree: *tree<symbol>) {
|
fun trim(parse_tree: *tree<symbol>) {
|
||||||
remove_node(symbol("$NULL$", false), parse_tree)
|
remove_node(symbol("$NULL$", false), parse_tree)
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ obj parser (Object) {
|
|||||||
}
|
}
|
||||||
var acc_state = gss.frontier_get_acc_state(input.size-1)
|
var acc_state = gss.frontier_get_acc_state(input.size-1)
|
||||||
if (acc_state) {
|
if (acc_state) {
|
||||||
println("ACCEPTED!")
|
/*println("ACCEPTED!")*/
|
||||||
return gss.get_edge(acc_state, v0)
|
return gss.get_edge(acc_state, v0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user