parse types into initial ast
This commit is contained in:
147
k.krak
147
k.krak
@@ -83,14 +83,14 @@ fun main(argc: int, argv: **char): int {
|
||||
if (positional_args.size > 1)
|
||||
executable_name = positional_args[1]
|
||||
|
||||
var pass_poset = poset<pair<*ast, str>>()
|
||||
var pass_poset = poset<pair<*tree<ast>, str>>()
|
||||
var name_ast_map = map<str, *tree<ast>>()
|
||||
var passes = map<str, fun(*ast): bool>()
|
||||
var passes = map<str, fun(*tree<ast>): bool>()
|
||||
|
||||
// resolves a single import
|
||||
passes[str("import_resolver")] = fun(import_binding: *ast): bool {
|
||||
passes[str("import_resolver")] = fun(import_binding: *tree<ast>): bool {
|
||||
var file_path = binding_str(import_binding)
|
||||
println("Running import resolver for" + file_path)
|
||||
println("Running import resolver for " + file_path)
|
||||
if (!name_ast_map.contains_key(file_path)) {
|
||||
printerr(file_path + ", ")
|
||||
var parse_tree = parse.parse_input(read_file(file_path), file_path)
|
||||
@@ -103,7 +103,7 @@ fun main(argc: int, argv: **char): int {
|
||||
}
|
||||
|
||||
// ensures that all imports reachable from this one are resolved
|
||||
passes[str("import_checker")] = fun(import_binding: *ast): bool {
|
||||
passes[str("import_checker")] = fun(import_binding: *tree<ast>): bool {
|
||||
var all_resolved = true
|
||||
var file_path = binding_str(import_binding)
|
||||
println("Running import checker for " + file_path)
|
||||
@@ -113,9 +113,9 @@ fun main(argc: int, argv: **char): int {
|
||||
if (!bound(b.first)) {
|
||||
all_resolved = false
|
||||
pass_poset.add_relationship(make_pair(import_binding, str("import_checker")), make_pair(b.first, str("import_resolver")))
|
||||
println(to_string(*b.first) + " is not bound!")
|
||||
println(to_string(b.first->data) + " is not bound!")
|
||||
} else {
|
||||
println(to_string(*b.first) + " is bound!")
|
||||
println(to_string(b.first->data) + " is bound!")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,7 +129,7 @@ fun main(argc: int, argv: **char): int {
|
||||
|
||||
while (pass_poset.size() != 0) {
|
||||
var file_pass = pass_poset.top()
|
||||
printlnerr("doing pass " + file_pass.second + " on " + to_string(*file_pass.first))
|
||||
printlnerr("doing pass " + file_pass.second + " on " + to_string(file_pass.first->data))
|
||||
var done = passes[file_pass.second](file_pass.first)
|
||||
if (done)
|
||||
pass_poset.remove(file_pass)
|
||||
@@ -158,47 +158,97 @@ fun main(argc: int, argv: **char): int {
|
||||
return 0
|
||||
}
|
||||
|
||||
var bindings: *vec<*ast>
|
||||
fun make_binding(s: str): *ast {
|
||||
var binding = new<ast>()->copy_construct(&ast::_binding(make_triple(s, vec<*type>(), null<tree<ast>>())))
|
||||
if (bindings == null<vec<*ast>>())
|
||||
bindings = new<vec<*ast>>()->construct()
|
||||
var bindings: *vec<*tree<ast>>
|
||||
fun make_binding(s: str): *tree<ast> {
|
||||
var binding = _binding(s, vec<*type>(), null<tree<ast>>())
|
||||
if (bindings == null<vec<*tree<ast>>>())
|
||||
bindings = new<vec<*tree<ast>>>()->construct()
|
||||
bindings->add(binding)
|
||||
return binding
|
||||
}
|
||||
fun set_bindings(binding: *tree<ast>, to: *tree<ast>) {
|
||||
set_bindings(&binding->data, to)
|
||||
}
|
||||
fun set_bindings(binding: *ast, to: *tree<ast>) {
|
||||
match(*binding) {
|
||||
match(binding->data) {
|
||||
ast::_binding(b) {
|
||||
var from = binding->_binding.third
|
||||
var from = b.third
|
||||
// don't set null, that will set all unbound ones
|
||||
if (from == null<tree<ast>>()) {
|
||||
binding->_binding.third = to
|
||||
b.third = to
|
||||
return
|
||||
}
|
||||
for (var i = 0; i < bindings->size; i++;)
|
||||
if (bindings->get(i)->_binding.third == from)
|
||||
bindings->get(i)->_binding.third = to
|
||||
if (bindings->get(i)->data._binding.third == from)
|
||||
bindings->get(i)->data._binding.third = to
|
||||
return
|
||||
}
|
||||
}
|
||||
error("trying to set bindings on not a binding")
|
||||
}
|
||||
fun bound(binding: *ast): bool {
|
||||
match(*binding) {
|
||||
fun bound(binding: *tree<ast>): bool {
|
||||
match(binding->data) {
|
||||
ast::_binding(b) return b.third != null<tree<ast>>()
|
||||
}
|
||||
error("Trying to check bound for not a binding")
|
||||
}
|
||||
fun binding_str(binding: *ast): str {
|
||||
match(*binding) {
|
||||
fun binding_str(binding: *tree<ast>): str {
|
||||
match(binding->data) {
|
||||
ast::_binding(b) return b.first
|
||||
}
|
||||
error("Trying to get name for not a binding")
|
||||
}
|
||||
|
||||
fun parse_type(syntax: *tree<symbol>): *type {
|
||||
var is_ref = get_node("\"ref\"", syntax) != null<tree<symbol>>()
|
||||
var indr = 0
|
||||
syntax = get_node("pre_reffed", syntax)
|
||||
var next = get_node("pre_reffed", syntax)
|
||||
while(next != null<tree<symbol>>()) {
|
||||
indr++
|
||||
syntax = next
|
||||
next = get_node("pre_reffed", syntax)
|
||||
}
|
||||
var ident = get_node("scoped_identifier", syntax)
|
||||
var func = get_node("function_type", syntax)
|
||||
var first_child_name = syntax->children[0]->data.name
|
||||
if (ident != null<tree<symbol>>()) {
|
||||
var template_inst = get_node("template_inst", syntax)
|
||||
if (template_inst != null<tree<symbol>>()) {
|
||||
return type(base_type::_obj(make_binding(concat(ident) + "<somin>")), indr, is_ref)
|
||||
} else {
|
||||
return type(base_type::_obj(make_binding(concat(ident))), indr, is_ref)
|
||||
}
|
||||
} else if (func != null<tree<symbol>>()) {
|
||||
var param_types = vec<*type>()
|
||||
var return_type = type(base_type::_void(), 0, false)
|
||||
var variadic = false
|
||||
var raw = false
|
||||
return type(base_type::_fun(make_triple(make_pair(param_types, return_type),
|
||||
variadic, raw)), indr, is_ref)
|
||||
} else if (first_child_name == "\"void\"") {
|
||||
return type(base_type::_void(), indr, is_ref)
|
||||
} else if (first_child_name == "\"char\"") {
|
||||
return type(base_type::_char(), indr, is_ref)
|
||||
} else if (first_child_name == "\"uchar\"") {
|
||||
return type(base_type::_uchar(), indr, is_ref)
|
||||
} else if (first_child_name == "\"short\"") {
|
||||
return type(base_type::_short(), indr, is_ref)
|
||||
} else if (first_child_name == "\"ushort\"") {
|
||||
return type(base_type::_ushort(), indr, is_ref)
|
||||
} else if (first_child_name == "\"int\"") {
|
||||
return type(base_type::_int(), indr, is_ref)
|
||||
} else if (first_child_name == "\"uint\"") {
|
||||
return type(base_type::_uint(), indr, is_ref)
|
||||
} else if (first_child_name == "\"long\"") {
|
||||
return type(base_type::_long(), indr, is_ref)
|
||||
} else if (first_child_name == "\"ulong\"") {
|
||||
return type(base_type::_ulong(), indr, is_ref)
|
||||
} else if (first_child_name == "\"float\"") {
|
||||
return type(base_type::_float(), indr, is_ref)
|
||||
} else if (first_child_name == "\"double\"") {
|
||||
return type(base_type::_double(), indr, is_ref)
|
||||
}
|
||||
error(syntax, "could not parse type " + first_child_name)
|
||||
}
|
||||
|
||||
fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<str>): *tree<ast> {
|
||||
var resolve_import_file = fun(file_name: str): str {
|
||||
var file_path = str()
|
||||
@@ -219,12 +269,25 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
|
||||
return _import(make_binding(resolve_import_file(concat(syntax->children[1]) + ".krak")), from_vector(syntax->children.slice(2,-1).filter(fun(s:*tree<symbol>):bool {
|
||||
return s->data.name == "identifier" || s->data.data == "*"
|
||||
}).map(concat)), vec(syntax_to_ast_helper(syntax->children[1])))
|
||||
} else if (syntax->data.name == "function")
|
||||
return _function(concat(get_node("func_identifier", syntax)), null<type>(),
|
||||
(get_nodes("typed_parameter", syntax) +
|
||||
get_nodes("statement", syntax)).map(syntax_to_ast_helper))
|
||||
else if (syntax->data.name == "typed_parameter")
|
||||
return _identifier(concat(get_node("identifier", syntax)), null<type>())
|
||||
} else if (syntax->data.name == "function") {
|
||||
var parameters = get_nodes("typed_parameter", syntax).map(syntax_to_ast_helper)
|
||||
var body = syntax_to_ast_helper(get_node("statement", syntax))
|
||||
var return_type = null<type>()
|
||||
var return_type_node = get_node("typed_return", syntax)
|
||||
if (return_type_node != null<tree<symbol>>())
|
||||
return_type = parse_type(get_node("type", return_type_node))
|
||||
else
|
||||
return_type = type(base_type::_void(), 0, false)
|
||||
var function_type = type(base_type::_fun(make_triple(make_pair(parameters.map(fun(i: *tree<ast>): *type return i->data._identifier.second;), return_type), false, false)), 0, false)
|
||||
var n = _function(concat(get_node("func_identifier", syntax)), function_type, parameters + body)
|
||||
var template = get_node("template_dec", syntax)
|
||||
if (template == null<tree<symbol>>()) {
|
||||
return n
|
||||
} else {
|
||||
return _template(n->data._function.first, from_vector(get_nodes("template_param", template).map(concat)), vec(n))
|
||||
}
|
||||
} else if (syntax->data.name == "typed_parameter")
|
||||
return _identifier(concat(get_node("identifier", syntax)), parse_type(get_node("type", syntax)))
|
||||
else if (syntax->data.name == "type_def") {
|
||||
var n = _type_def(concat(get_node("identifier", syntax)),
|
||||
get_nodes("declaration_statement", syntax).map(syntax_to_ast_helper))
|
||||
@@ -237,7 +300,11 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
|
||||
} else if (syntax->data.name == "adt_def") {
|
||||
var n = _adt_def(concat(get_node("identifier", syntax)),
|
||||
get_nodes("adt_option", syntax).map(fun(s: *tree<symbol>): *tree<ast> {
|
||||
return _identifier(concat(get_node("identifier", s)), null<type>())
|
||||
var option_type = get_node("type", s)
|
||||
if (option_type != null<tree<symbol>>())
|
||||
return _identifier(concat(get_node("identifier", s)), parse_type(option_type))
|
||||
else
|
||||
return _identifier(concat(get_node("identifier", s)), type(base_type::_void(), 0, false))
|
||||
}))
|
||||
var template = get_node("template_dec", syntax)
|
||||
if (template == null<tree<symbol>>()) {
|
||||
@@ -259,11 +326,11 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
|
||||
return _case(s->children.map(syntax_to_ast_helper))
|
||||
}))
|
||||
} else if (syntax->data.name == "declaration_statement") {
|
||||
var children = vec(_identifier(concat(get_node("identifier", syntax)), null<type>()))
|
||||
var children = vec(_identifier(concat(get_node("identifier", syntax)), parse_type(get_node("type", syntax))))
|
||||
children += get_nodes("boolean_expression", syntax).map(syntax_to_ast_helper)
|
||||
return _declaration(children)
|
||||
} else if (syntax->data.name == "assignment_statement")
|
||||
return _assignment(vec(_binding(concat(syntax->children[1]), null<tree<ast>>()),
|
||||
return _assignment(vec(make_binding(concat(syntax->children[1])),
|
||||
syntax_to_ast_helper(syntax->children[0]),
|
||||
syntax_to_ast_helper(syntax->children[2])))
|
||||
else if (syntax->data.name == "function_call")
|
||||
@@ -289,23 +356,23 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
|
||||
if (template_inst != null<tree<symbol>>()) {
|
||||
if (syntax->children[0]->data.name != "scoped_identifier")
|
||||
error(syntax, "Unexpected template instantiation (not on an identifier)")
|
||||
return _binding(concat(syntax->children[0]) + "<somin>", vec<*type>(), null<tree<ast>>())
|
||||
return make_binding(concat(syntax->children[0]) + "<somin>")
|
||||
} else if (syntax->children[0]->data.terminal) {
|
||||
return _call(vec(_binding(concat(syntax->children[0]), null<tree<ast>>()),
|
||||
return _call(vec(make_binding(concat(syntax->children[0])),
|
||||
syntax_to_ast_helper(syntax->children[1])))
|
||||
} else {
|
||||
return _call(vec(_binding(concat(syntax->children[1]), null<tree<ast>>()),
|
||||
return _call(vec(make_binding(concat(syntax->children[1])),
|
||||
syntax_to_ast_helper(syntax->children[0])))
|
||||
}
|
||||
} else {
|
||||
return _call(vec(_binding(concat(syntax->children[1]), null<tree<ast>>()),
|
||||
return _call(vec(make_binding(concat(syntax->children[1])),
|
||||
syntax_to_ast_helper(syntax->children[0]),
|
||||
syntax_to_ast_helper(syntax->children[2])))
|
||||
}
|
||||
} else if (syntax->data.name == "number")
|
||||
return _value(concat(syntax), null<type>())
|
||||
return _value(concat(syntax), type(base_type::_int(), 0, false))
|
||||
else if (syntax->data.name == "scoped_identifier" || syntax->data.name == "identifier")
|
||||
return _binding(concat(syntax), null<tree<ast>>())
|
||||
return make_binding(concat(syntax))
|
||||
else
|
||||
return null<tree<ast>>()
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import mem:*
|
||||
|
||||
adt ast {
|
||||
_translation_unit: str,
|
||||
_import: pair<*ast, set<str>>,
|
||||
_import: pair<*tree<ast>, set<str>>,
|
||||
_identifier: pair<str, *type>,
|
||||
_binding: triple<str, vec<*type>, *tree<ast>>,
|
||||
_type_def: str,
|
||||
@@ -35,12 +35,12 @@ adt ast {
|
||||
fun to_string(a: ref ast): str {
|
||||
match(a) {
|
||||
ast::_translation_unit(b) return str("_translation_unit(") + b + ")"
|
||||
ast::_import(b) return str("_import(") + to_string(*b.first) + ")[" + str(",").join(b.second.data) + "]"
|
||||
ast::_identifier(b) return str("_identifier(") + b.first + ")"
|
||||
ast::_import(b) return str("_import(") + to_string(b.first->data) + ")[" + str(",").join(b.second.data) + "]"
|
||||
ast::_identifier(b) return str("_identifier(") + b.first + ": " + deref_to_string(b.second) + ")"
|
||||
ast::_binding(b) return str("_binding(") + b.first + "->" + to_string(b.third) + ")"
|
||||
ast::_type_def(b) return str("_type_def(") + b + ")"
|
||||
ast::_adt_def(b) return str("_adt_def(") + b + ")"
|
||||
ast::_function(b) return str("_function(") + b.first + ")"
|
||||
ast::_function(b) return str("_function(") + b.first + ": " + deref_to_string(b.second) + ")"
|
||||
ast::_template(b) return str("_template(") + b.first + "[" + str(",").join(b.second.data) + "])"
|
||||
ast::_declaration() return str("_declaration")
|
||||
ast::_assignment() return str("_assignment")
|
||||
@@ -57,13 +57,13 @@ fun to_string(a: ref ast): str {
|
||||
ast::_call() return str("_call")
|
||||
ast::_compiler_intrinsic(b) return str("_compiler_intrinsic(") + b.first + ")"
|
||||
ast::_cast(b) return str("_cast")
|
||||
ast::_value(b) return str("_value(") + b.first + ")"
|
||||
ast::_value(b) return str("_value(") + b.first + ": " + deref_to_string(b.second) + ")"
|
||||
}
|
||||
}
|
||||
fun _translation_unit(p: str): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_translation_unit(p))
|
||||
}
|
||||
fun _import(p1: *ast, p2: set<str>): *tree<ast> {
|
||||
fun _import(p1: *tree<ast>, p2: set<str>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_import(make_pair(p1,p2)))
|
||||
}
|
||||
fun _type_def(p: str): *tree<ast> {
|
||||
@@ -78,9 +78,6 @@ fun _cast(p: *type): *tree<ast> {
|
||||
fun _identifier(p1: str, p2: *type): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_identifier(make_pair(p1, p2)))
|
||||
}
|
||||
fun _binding(p1: str, p3: *tree<ast>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_binding(make_triple(p1, vec<*type>(), p3)))
|
||||
}
|
||||
fun _binding(p1: str, p2: vec<*type>, p3: *tree<ast>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_binding(make_triple(p1, p2, p3)))
|
||||
}
|
||||
@@ -141,7 +138,7 @@ fun _call(): *tree<ast> {
|
||||
fun _translation_unit(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_translation_unit(p), c)
|
||||
}
|
||||
fun _import(p1: *ast, p2: set<str>, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
fun _import(p1: *tree<ast>, p2: set<str>, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_import(make_pair(p1,p2)), c)
|
||||
}
|
||||
fun _type_def(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
@@ -156,9 +153,6 @@ fun _cast(p: *type, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
fun _identifier(p1: str, p2: *type, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_identifier(make_pair(p1, p2)), c)
|
||||
}
|
||||
fun _binding(p1: str, p3: *tree<ast>, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_binding(make_triple(p1, vec<*type>(), p3)), c)
|
||||
}
|
||||
fun _binding(p1: str, p2: vec<*type>, p3: *tree<ast>, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_binding(make_triple(p1, p2, p3)), c)
|
||||
}
|
||||
|
||||
@@ -37,6 +37,12 @@ fun to_string(in: ulong): str
|
||||
fun to_string<T>(in: *T): str
|
||||
return str("ptr:<") + to_string_num((in) cast ulong) + ">"
|
||||
|
||||
fun deref_to_string<T>(in: *T): str
|
||||
if (in == mem::null<T>())
|
||||
return str("null")
|
||||
else
|
||||
return in->to_string()
|
||||
|
||||
fun string_to_num<T>(it: str): T {
|
||||
var is_negative = false
|
||||
if (it[0] == '-') {
|
||||
|
||||
@@ -2,14 +2,15 @@ import mem:*
|
||||
import str:*
|
||||
import vec:*
|
||||
import util:*
|
||||
import tree:*
|
||||
import ast:*
|
||||
|
||||
adt base_type {
|
||||
_unknown,
|
||||
_void,
|
||||
_object: *ast,
|
||||
_obj: *tree<ast>,
|
||||
// triple<pair<param_types, return_type>, is_variadic, is raw>
|
||||
_function: triple<pair<vec<*type>, *type>, bool, bool>,
|
||||
_fun: triple<pair<vec<*type>, *type>, bool, bool>,
|
||||
_template_placeholder,
|
||||
_bool,
|
||||
_char,
|
||||
@@ -23,7 +24,9 @@ adt base_type {
|
||||
_float,
|
||||
_double
|
||||
}
|
||||
|
||||
fun type(b: base_type, ind: int, ref: bool): *type {
|
||||
return new<type>()->construct(b, ind, ref)
|
||||
}
|
||||
obj type (Object) {
|
||||
var base: base_type
|
||||
var indirection: int
|
||||
@@ -68,12 +71,20 @@ obj type (Object) {
|
||||
match (base) {
|
||||
base_type::_unknown() return indr_string + "_unknown"
|
||||
base_type::_void() return indr_string + "_void"
|
||||
base_type::_object(b) {
|
||||
return indr_string + "_object"
|
||||
base_type::_obj(b) {
|
||||
return indr_string + "_obj(" + to_string(b->data) + ")"
|
||||
}
|
||||
base_type::_function(b) {
|
||||
base_type::_fun(b) {
|
||||
// triple<pair<param_types, return_type>, is_variadic, is raw>
|
||||
return indr_string + "_function()"
|
||||
var to_ret = indr_string
|
||||
if (b.second)
|
||||
to_ret += "_run("
|
||||
else
|
||||
to_ret += "_fun("
|
||||
to_ret += str(", ").join(b.first.first.map(fun(pt: *type): str return pt->to_string();))
|
||||
if (b.third)
|
||||
to_ret += " ..."
|
||||
return to_ret + "): " + b.first.second->to_string()
|
||||
}
|
||||
base_type::_template_placeholder() return indr_string + "_template_placeholder"
|
||||
base_type::_bool() return indr_string + "_bool"
|
||||
@@ -86,7 +97,7 @@ obj type (Object) {
|
||||
base_type::_long() return indr_string + "_long"
|
||||
base_type::_ulong() return indr_string + "_ulong"
|
||||
base_type::_float() return indr_string + "_float"
|
||||
base_type::_double() return indr_string + "_double"
|
||||
base_type::_double() return indr_string + "_double"
|
||||
}
|
||||
return str("impossible type, indirection:") + indirection
|
||||
}
|
||||
@@ -102,15 +113,15 @@ obj type (Object) {
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_object(): bool {
|
||||
fun is_obj(): bool {
|
||||
match (base) {
|
||||
base_type::_object() return true
|
||||
base_type::_obj() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_function(): bool {
|
||||
fun is_fun(): bool {
|
||||
match (base) {
|
||||
base_type::_function() return true
|
||||
base_type::_fun() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user