Started work on interpreter, commit to fix issues it will depend on
This commit is contained in:
31
kraken.krak
31
kraken.krak
@@ -8,6 +8,7 @@ import symbol:*
|
||||
import tree:*
|
||||
import serialize:*
|
||||
import c_generator:*
|
||||
/*import interpreter:**/
|
||||
import os:*
|
||||
import compiler_version
|
||||
|
||||
@@ -19,14 +20,20 @@ fun main(argc: int, argv: **char):int {
|
||||
println(compiler_version::version_string)
|
||||
exit(0)
|
||||
}
|
||||
/*var gram.construct(): grammer*/
|
||||
var input_file_offset = 1
|
||||
var interpret_instead = false
|
||||
if (string(argv[1]) == "-i") {
|
||||
interpret_instead = true
|
||||
input_file_offset++
|
||||
}
|
||||
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])
|
||||
// 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))
|
||||
/*println(base_dir)*/
|
||||
var file_name = base_dir + "/krakenGrammer.kgm"
|
||||
/*println(file_name)*/
|
||||
|
||||
var compiled_name = file_name + string(".comp_new")
|
||||
var compiled_version = 1
|
||||
var file_contents = read_file(file_name)
|
||||
@@ -45,10 +52,7 @@ fun main(argc: int, argv: **char):int {
|
||||
if (cached_contents == file_contents) {
|
||||
/*println("loaded_and_valid, using cached version!")*/
|
||||
loaded_and_valid = true
|
||||
/*unpack(gram, pos) = unserialize<grammer>(binary, pos)*/
|
||||
// skip unnecessary copies this way
|
||||
pos = gram.unserialize(binary, pos)
|
||||
/*println("finished unserializeing!!")*/
|
||||
} else println("contents different")
|
||||
} else println("version number different")
|
||||
} else {
|
||||
@@ -68,26 +72,25 @@ fun main(argc: int, argv: **char):int {
|
||||
println("done writing")
|
||||
}
|
||||
|
||||
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
|
||||
/*print("parsing: ")*/
|
||||
importer.import(kraken_file_name)
|
||||
if (interpret_instead) {
|
||||
println("Interpreting!")
|
||||
/*var interpret.construct(importer.name_ast_map, importer.ast_pass.ast_to_syntax): interpreter*/
|
||||
/*interpret.call_main()*/
|
||||
} else {
|
||||
println("Generating C")
|
||||
/*println("NOW DOING C_GENERATOR")*/
|
||||
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)*/
|
||||
|
||||
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
|
||||
println(compile_string)
|
||||
system(compile_string)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -188,6 +188,7 @@ obj ast_transformation (Object) {
|
||||
vector(
|
||||
make_pair("operator==", ast_function_ptr(string("operator=="), type_ptr(vector(equals_param->identifier.type), type_ptr(base_type::boolean())), vector(equals_param), false)),
|
||||
make_pair("operator!=", ast_function_ptr(string("operator!="), type_ptr(vector(nequals_param->identifier.type), type_ptr(base_type::boolean())), vector(nequals_param), false)),
|
||||
make_pair("construct", ast_function_ptr(string("construct"), type_ptr(vector<*type>(), node->adt_def.self_type->clone_with_increased_indirection()), vector<*ast_node>(), false)),
|
||||
make_pair("copy_construct", ast_function_ptr(string("copy_construct"), type_ptr(vector(copy_construct_param->identifier.type), type_ptr(base_type::void_return())), vector(copy_construct_param), false)),
|
||||
make_pair("operator=", ast_function_ptr(string("operator="), type_ptr(vector(assign_param->identifier.type), type_ptr(base_type::void_return())), vector(assign_param), false)),
|
||||
make_pair("destruct", ast_function_ptr(string("destruct"), type_ptr(vector<*type>(), type_ptr(base_type::void_return())), vector<*ast_node>(), false))
|
||||
@@ -740,7 +741,7 @@ obj ast_transformation (Object) {
|
||||
}
|
||||
fun transform_case_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node {
|
||||
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)
|
||||
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)
|
||||
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)))
|
||||
var the_adt = the_adts[0]
|
||||
|
||||
@@ -303,6 +303,10 @@ obj c_generator (Object) {
|
||||
equals_res.post += generate_from_defer_stack(&defer_stack, -1, enclosing_object, child).one_string()
|
||||
defer_stack.pop()
|
||||
function_definitions += equals_res.one_string() + "return result;\n"
|
||||
} else if (backing.name == "construct") {
|
||||
function_definitions += "/*construct*/"
|
||||
function_definitions += "this->flag = -1;\n"
|
||||
function_definitions += "return this;\n"
|
||||
} else if (backing.name == "copy_construct") {
|
||||
var param = backing.parameters[0]
|
||||
function_definitions += "/*copy_construct*/"
|
||||
|
||||
55
stdlib/interpreter.krak
Normal file
55
stdlib/interpreter.krak
Normal file
@@ -0,0 +1,55 @@
|
||||
import io:*
|
||||
import map:*
|
||||
import string:*
|
||||
import util:*
|
||||
import tree:*
|
||||
import symbol:*
|
||||
import ast_nodes:*
|
||||
import ast_transformation:*
|
||||
|
||||
adt value {
|
||||
integer: int,
|
||||
floating: float
|
||||
}
|
||||
fun print_value(v: ref value) {
|
||||
match (v) {
|
||||
value::integer(data) println(data)
|
||||
value::floating(data) println(data)
|
||||
}
|
||||
}
|
||||
|
||||
obj interpreter (Object) {
|
||||
var ast_to_syntax: map<*ast_node, *tree<symbol>>
|
||||
var name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>
|
||||
fun construct(name_ast_map_in: map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax_in: map<*ast_node, *tree<symbol>>): *interpreter {
|
||||
name_ast_map.copy_construct(&name_ast_map_in)
|
||||
ast_to_syntax.copy_construct(&ast_to_syntax_in)
|
||||
return this
|
||||
}
|
||||
fun operator=(old: ref interpreter) {
|
||||
destruct()
|
||||
copy_construct(&old)
|
||||
}
|
||||
fun copy_construct(old: *interpreter) {
|
||||
name_ast_map.copy_construct(&old->name_ast_map)
|
||||
ast_to_syntax.copy_construct(&old->ast_to_syntax)
|
||||
}
|
||||
fun destruct() {
|
||||
name_ast_map.destruct()
|
||||
ast_to_syntax.destruct()
|
||||
}
|
||||
fun call_main() {
|
||||
var results = vector<*ast_node>()
|
||||
name_ast_map.for_each(fun(key: string, value: pair<*tree<symbol>,*ast_node>) {
|
||||
results += scope_lookup(string("main"), value.second)
|
||||
})
|
||||
if (results.size != 1)
|
||||
error(string("wrong number of mains to call: ") + results.size)
|
||||
println("calling main!")
|
||||
print_value(interpret_function_call(results[0], vector<value>()))
|
||||
}
|
||||
fun interpret_function_call(func: *ast_node, params: vector<value>): value {
|
||||
return value::integer(0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user