199 lines
8.3 KiB
Plaintext
199 lines
8.3 KiB
Plaintext
import io:*
|
|
import grammer:*
|
|
import lexer:*
|
|
import parser:*
|
|
import string:*
|
|
import util:*
|
|
import symbol:*
|
|
import tree:*
|
|
import serialize:*
|
|
import interpreter:*
|
|
import os:*
|
|
import ast_transformation:*
|
|
import adt_lower:*
|
|
import obj_lower:*
|
|
import defer_lower:*
|
|
import function_value_lower:*
|
|
import ref_lower:*
|
|
import ctce_lower:*
|
|
import address_of_ensure_variable_lower:*
|
|
import c_line_control:*
|
|
import node_counter:*
|
|
import c_generator:*
|
|
import vector:*
|
|
import set:*
|
|
|
|
fun main(argc: int, argv: **char):int {
|
|
// delay construction until we either load it or copy construct it
|
|
var gram: grammer
|
|
var base_dir = string("/").join(string(argv[0]).split('/').slice(0,-2))
|
|
var file_name = base_dir + "/krakenGrammer.kgm"
|
|
var compiled_name = file_name + string(".comp_new")
|
|
var compiled_version = 1
|
|
var file_contents = read_file(file_name)
|
|
var loaded_and_valid = false
|
|
var doing_repl = false
|
|
|
|
if (argc <= 1) {
|
|
println("No input file!\n Call with one argument (the input file), or two arguments (input file and output name)\n Falling into REPL...")
|
|
compiled_name += ".expr"
|
|
file_contents = string("RealGoal = boolean_expression ;\n") + file_contents
|
|
doing_repl = true
|
|
} else if (string(argv[1]) == "-v" || string(argv[1]) == "--version") {
|
|
var version_c_string = #ctce(fun(): *char {
|
|
var version_string = string("Self-hosted Kraken compiler \"Kalypso\" - revision ") + from_system_command(string("git rev-list HEAD | wc -l"), 100) +
|
|
", commit: " + from_system_command(string("git rev-parse HEAD"), 100) +
|
|
", compile date: " + from_system_command(string("date"), 100)
|
|
return version_string.toCharArray()
|
|
}())
|
|
println(version_c_string)
|
|
exit(0)
|
|
}
|
|
var input_file_offset = 1
|
|
var interpret_instead = false
|
|
var opt_str = string("-O2")
|
|
var line_ctrl = false
|
|
var compile_c = true
|
|
var positional_args = vector<string>()
|
|
var flags = set<string>()
|
|
for (var i = 1; i < argc; i++;) {
|
|
var arg_str = string(argv[i])
|
|
if (arg_str == "-i") {
|
|
interpret_instead = true
|
|
} else if (arg_str.length() > 2 && arg_str.slice(0,2) == "-O") {
|
|
opt_str = arg_str
|
|
} else if (arg_str == "-g") {
|
|
line_ctrl = true
|
|
} else if (arg_str == "--no-c-compile") {
|
|
compile_c = false
|
|
} else if (arg_str.length() > 2 && arg_str.first() == '-') {
|
|
flags.add(arg_str.slice(1,-1))
|
|
} else {
|
|
positional_args.add(arg_str)
|
|
}
|
|
}
|
|
/*positional_args.for_each(fun(i:string) println("positional_arg: " + i);)*/
|
|
flags.for_each(fun(i:string) println("flag: " + i);)
|
|
|
|
if (file_exists(compiled_name)) {
|
|
var pos = 0
|
|
var binary = read_file_binary(compiled_name)
|
|
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) {
|
|
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")
|
|
// since we now don't construct before hand
|
|
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 parse1.construct(&gram, &lex): parser
|
|
/*var parse2.construct(&gram): parser*/
|
|
/*var parse3.construct(&gram): parser*/
|
|
/*var parse4.construct(&gram): parser*/
|
|
/*var parse5.construct(&gram): parser*/
|
|
/*var parse6.construct(&gram): parser*/
|
|
/*var parse7.construct(&gram): parser*/
|
|
/*var parse8.construct(&gram): parser*/
|
|
var ast_pass.construct(): ast_transformation
|
|
var parsers = vector(parse1)
|
|
/*var parsers = vector(parse1,parse2,parse3,parse4)*/
|
|
/*var parsers = vector(parse1,parse2,parse3,parse4,parse5,parse6)*/
|
|
/*var parsers = vector(parse1,parse2,parse3,parse4,parse5,parse6,parse7,parse8)*/
|
|
|
|
// This is our REPL loop
|
|
var scope = ast_translation_unit_ptr(string("stdin"))
|
|
if (doing_repl) {
|
|
/*var globals = setup_globals(importer.name_ast_map)*/
|
|
while (doing_repl) {
|
|
var line = get_line(string("> "), 100)
|
|
if (line == "end")
|
|
return 0
|
|
var trimmed_parse = trim(parse1.parse_input(line, string("stdin")))
|
|
var ast_expression = ast_pass.transform_expression(trimmed_parse, scope, map<string, *type>())
|
|
print_value(evaluate_constant_expression(ast_expression))
|
|
/*print_value(evaluate_with_globals(ast_expression, &globals))*/
|
|
}
|
|
}
|
|
|
|
var kraken_file_name = positional_args[0]
|
|
var executable_name = string(".").join(kraken_file_name.split('.').slice(0,-2))
|
|
if (positional_args.size > 1)
|
|
executable_name = positional_args[1]
|
|
var importer.construct(parsers, ast_pass, vector(string(), base_dir + "/stdlib/")): importer
|
|
importer.import(kraken_file_name)
|
|
// Passes
|
|
/*printlnerr("Counting Nodes")*/
|
|
/*node_counter(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)*/
|
|
printlnerr("Lowering ADTs")
|
|
adt_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
|
|
/*printlnerr("Counting Nodes")*/
|
|
/*node_counter(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)*/
|
|
printlnerr("Lowering Objects")
|
|
obj_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
|
|
/*printlnerr("Counting Nodes")*/
|
|
/*node_counter(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)*/
|
|
printlnerr("Lowering Defer")
|
|
defer_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
|
|
/*printlnerr("Counting Nodes")*/
|
|
/*node_counter(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)*/
|
|
// Should come after lowering of ADTs and before lowering of Refs
|
|
printlnerr("Lowering Function Values (Lambdas, etc)")
|
|
function_value_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
|
|
/*printlnerr("Counting Nodes")*/
|
|
/*node_counter(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)*/
|
|
printlnerr("Lowering Ref")
|
|
ref_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
|
|
/*printlnerr("Counting Nodes")*/
|
|
/*node_counter(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)*/
|
|
// Lowers #ctce and the current #ctce_pass
|
|
printlnerr("Lowering CTCE")
|
|
ctce_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
|
|
/*printlnerr("Counting Nodes")*/
|
|
/*node_counter(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)*/
|
|
// Makes sure that & always takes reference to a variable
|
|
printlnerr("Lowering & to always have variable")
|
|
address_of_ensure_variable_lower(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
|
|
if (interpret_instead) {
|
|
printlnerr("Interpreting!")
|
|
call_main(importer.name_ast_map)
|
|
} else {
|
|
if (line_ctrl) {
|
|
printlnerr("running C-specific passes")
|
|
printlnerr("running #line")
|
|
c_line_control(&importer.name_ast_map, &importer.ast_pass.ast_to_syntax)
|
|
}
|
|
printlnerr("Generating C")
|
|
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)
|
|
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_output_pair.second + " " + kraken_c_output_name + " -o " + executable_name
|
|
printlnerr(compile_string)
|
|
system(compile_string)
|
|
}
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|