2015-12-01 16:19:44 -05:00
|
|
|
import io:*
|
|
|
|
|
import grammer:*
|
|
|
|
|
import parser:*
|
|
|
|
|
import ast_transformation:*
|
|
|
|
|
import string:*
|
|
|
|
|
import util:*
|
|
|
|
|
import symbol:*
|
|
|
|
|
import tree:*
|
|
|
|
|
import serialize:*
|
2016-01-04 00:38:59 -05:00
|
|
|
import c_generator:*
|
2016-05-13 03:10:36 -04:00
|
|
|
import interpreter:*
|
2016-01-04 02:00:06 -05:00
|
|
|
import os:*
|
2016-04-27 15:59:28 -04:00
|
|
|
import compiler_version
|
2015-12-01 16:19:44 -05:00
|
|
|
|
2016-01-02 13:44:31 -05:00
|
|
|
|
2016-04-27 15:59:28 -04:00
|
|
|
fun main(argc: int, argv: **char):int {
|
|
|
|
|
if (argc <= 1) {
|
|
|
|
|
error("No input file!\n Call with one argument (the input file), or two arguments (input file and output name)")
|
|
|
|
|
} else if (string(argv[1]) == "-v" || string(argv[1]) == "--version") {
|
|
|
|
|
println(compiler_version::version_string)
|
|
|
|
|
exit(0)
|
|
|
|
|
}
|
2016-05-12 02:03:20 -04:00
|
|
|
var input_file_offset = 1
|
|
|
|
|
var interpret_instead = false
|
2016-06-07 23:14:52 -07:00
|
|
|
var argv1_str = string(argv[1])
|
|
|
|
|
var opt_str = string("-O3")
|
|
|
|
|
if (argv1_str == "-i") {
|
2016-05-12 02:03:20 -04:00
|
|
|
interpret_instead = true
|
|
|
|
|
input_file_offset++
|
2016-06-07 23:14:52 -07:00
|
|
|
} else if (argv1_str.length() > 2 && argv1_str.slice(0,2) == "-O") {
|
|
|
|
|
opt_str = argv1_str
|
|
|
|
|
input_file_offset++
|
2016-05-12 02:03:20 -04:00
|
|
|
}
|
|
|
|
|
var kraken_file_name = string(argv[input_file_offset])
|
|
|
|
|
var executable_name = string(".").join(kraken_file_name.split('.').slice(0,-2))
|
|
|
|
|
if (argc == input_file_offset+2)
|
|
|
|
|
executable_name = string(argv[input_file_offset+1])
|
2016-01-02 13:44:31 -05:00
|
|
|
// delay construction until we either load it or copy construct it
|
|
|
|
|
var gram: grammer
|
2016-03-28 17:12:53 -04:00
|
|
|
var base_dir = string("/").join(string(argv[0]).split('/').slice(0,-2))
|
|
|
|
|
var file_name = base_dir + "/krakenGrammer.kgm"
|
2015-12-01 16:19:44 -05:00
|
|
|
var compiled_name = file_name + string(".comp_new")
|
2016-04-05 03:14:56 -04:00
|
|
|
var compiled_version = 1
|
2015-12-01 16:19:44 -05:00
|
|
|
var file_contents = read_file(file_name)
|
|
|
|
|
var loaded_and_valid = false
|
|
|
|
|
|
|
|
|
|
if (file_exists(compiled_name)) {
|
2016-04-13 16:25:16 -04:00
|
|
|
/*println("cached file exists")*/
|
2015-12-01 16:19:44 -05:00
|
|
|
var pos = 0
|
|
|
|
|
var binary = read_file_binary(compiled_name)
|
2016-04-13 16:25:16 -04:00
|
|
|
/*println("read file!")*/
|
2016-04-05 03:14:56 -04:00
|
|
|
var saved_version = 0
|
|
|
|
|
unpack(saved_version, pos) = unserialize<int>(binary, pos)
|
|
|
|
|
if (saved_version == compiled_version) {
|
|
|
|
|
var cached_contents = string()
|
|
|
|
|
unpack(cached_contents, pos) = unserialize<string>(binary, pos)
|
|
|
|
|
if (cached_contents == file_contents) {
|
2016-04-13 16:25:16 -04:00
|
|
|
/*println("loaded_and_valid, using cached version!")*/
|
2016-04-05 03:14:56 -04:00
|
|
|
loaded_and_valid = true
|
|
|
|
|
pos = gram.unserialize(binary, pos)
|
|
|
|
|
} else println("contents different")
|
|
|
|
|
} else println("version number different")
|
2015-12-01 16:19:44 -05:00
|
|
|
} else {
|
|
|
|
|
println("cached file does not exist")
|
|
|
|
|
}
|
|
|
|
|
if (!loaded_and_valid) {
|
|
|
|
|
println("Not loaded_and_valid, re-generating and writing out")
|
2016-01-02 13:44:31 -05:00
|
|
|
/*gram = load_grammer(file_contents)*/
|
|
|
|
|
// since we now don't construct before hand
|
|
|
|
|
gram.copy_construct(&load_grammer(file_contents))
|
2015-12-01 16:19:44 -05:00
|
|
|
println("grammer loaded, calculate_first_set")
|
|
|
|
|
gram.calculate_first_set()
|
|
|
|
|
println("grammer loaded, calculate_state_automaton")
|
|
|
|
|
gram.calculate_state_automaton()
|
|
|
|
|
println("calculated, writing out")
|
2016-04-05 03:14:56 -04:00
|
|
|
write_file_binary(compiled_name, serialize(compiled_version) + serialize(file_contents) + serialize(gram))
|
2015-12-01 16:19:44 -05:00
|
|
|
println("done writing")
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-05 07:13:32 -05:00
|
|
|
var parse.construct(gram): parser
|
2015-12-01 16:19:44 -05:00
|
|
|
var ast_pass.construct(): ast_transformation
|
2016-03-28 17:12:53 -04:00
|
|
|
var importer.construct(parse, ast_pass, vector(string(), base_dir + "/stdlib/")): importer
|
2016-01-04 00:38:59 -05:00
|
|
|
importer.import(kraken_file_name)
|
2016-05-12 02:03:20 -04:00
|
|
|
if (interpret_instead) {
|
2016-05-22 14:10:19 -07:00
|
|
|
printlnerr("Interpreting!")
|
2016-05-13 03:10:36 -04:00
|
|
|
var interpret.construct(importer.name_ast_map, importer.ast_pass.ast_to_syntax): interpreter
|
|
|
|
|
interpret.call_main()
|
2016-05-12 02:03:20 -04:00
|
|
|
} else {
|
2016-05-22 14:10:19 -07:00
|
|
|
printlnerr("Generating C")
|
2016-05-12 02:03:20 -04:00
|
|
|
var c_generator.construct(): c_generator
|
|
|
|
|
var c_output_pair = c_generator.generate_c(importer.name_ast_map, importer.ast_pass.ast_to_syntax)
|
|
|
|
|
var kraken_c_output_name = kraken_file_name + ".c"
|
|
|
|
|
write_file(kraken_c_output_name, c_output_pair.first)
|
|
|
|
|
/*println(string("linker string: ") + c_output_pair.second)*/
|
2016-06-07 23:14:52 -07:00
|
|
|
var compile_string = "cc -g " + opt_str + " -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast -std=c99 " + c_output_pair.second + " " + kraken_c_output_name + " -o " + executable_name
|
2016-05-22 14:10:19 -07:00
|
|
|
printlnerr(compile_string)
|
2016-05-12 02:03:20 -04:00
|
|
|
system(compile_string)
|
|
|
|
|
}
|
2015-12-01 16:19:44 -05:00
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|