Files
kraken/stdlib/ast.krak

289 lines
12 KiB
Plaintext
Raw Normal View History

2018-06-18 19:04:24 -04:00
import tree:*
import type2:*
2018-06-18 19:04:24 -04:00
import vec:*
import set:*
import util:*
import str:*
import mem:*
2018-10-08 00:28:42 -04:00
import binding:*
2018-06-18 19:04:24 -04:00
adt ast {
_translation_unit: str,
2018-06-22 23:13:08 -04:00
_import: pair<*tree<ast>, set<str>>,
2018-09-22 14:54:52 -04:00
_identifier: pair<str, *binding<type>>,
2018-10-02 23:51:26 -04:00
_binding: triple<str, vec<*binding<type>>, *binding<tree<ast>>>,
2018-06-18 19:04:24 -04:00
_type_def: str,
_adt_def: str,
2018-09-22 14:54:52 -04:00
_function: triple<str, *binding<type>, bool>,
2018-10-08 00:28:42 -04:00
_template: pair<str, map<str, *binding<type>>>,
2018-06-18 19:04:24 -04:00
_declaration,
_block,
_if,
_match,
_case,
_while,
_for,
_return,
_break,
_continue,
_defer,
_call,
_compiler_intrinsic: triple<str, *binding<type>, vec<*binding<type>>>,
2018-09-22 14:54:52 -04:00
_cast: *binding<type>,
_value: pair<str, *binding<type>>
2018-06-18 19:04:24 -04:00
}
2018-10-08 00:28:42 -04:00
fun deref_to_string<T>(in: *T): str
if (in == mem::null<T>())
return str("null")
else
return to_string(in)
fun deref_to_string<T>(in: *T, ts: fun(*T): str): str
if (in == mem::null<T>())
return str("null")
else
return ts(in)
2018-06-18 19:04:24 -04:00
fun to_string(a: ref ast): str {
match(a) {
ast::_translation_unit(b) return str("_translation_unit(") + b + ")"
2018-06-22 23:13:08 -04:00
ast::_import(b) return str("_import(") + to_string(b.first->data) + ")[" + str(",").join(b.second.data) + "]"
2018-09-22 14:54:52 -04:00
ast::_identifier(b) return str("_identifier(") + b.first + ": " + deref_to_string(b.second->bound_to) + ")"
2018-10-08 00:28:42 -04:00
ast::_binding(b) return str("_binding(") + b.first + "[" + str(",").join(b.second.map(fun(x:*binding<type>): str { return deref_to_string(x->bound_to); })) + "]" + "->" + deref_to_string(b.third->bound_to, fun(t: *tree<ast>): str return to_string(t->data);) + ")"
ast::_type_def(b) return str("_type_def(") + b + ")"
ast::_adt_def(b) return str("_adt_def(") + b + ")"
2018-09-22 14:54:52 -04:00
ast::_function(b) return str("_function(") + b.first + ": " + deref_to_string(b.second->bound_to) + ", ext?:" + to_string(b.third) + ")"
2018-10-08 00:28:42 -04:00
ast::_template(b) return str("_template(") + b.first + "[" + str(",").join(b.second.keys) + "])"
2018-06-18 19:04:24 -04:00
ast::_declaration() return str("_declaration")
ast::_block() return str("_block")
ast::_if() return str("_if")
ast::_match() return str("_match")
ast::_case() return str("_case")
ast::_while() return str("_while")
ast::_for() return str("_for")
ast::_return() return str("_return")
ast::_break() return str("_break")
ast::_continue() return str("_continue")
ast::_defer() return str("_defer")
ast::_call() return str("_call")
ast::_compiler_intrinsic(b) return str("_compiler_intrinsic(") + b.first + ": " + deref_to_string(b.second->bound_to) + ")"
ast::_cast(b) return str("_cast")
2018-09-22 14:54:52 -04:00
ast::_value(b) return str("_value(") + b.first + ": " + deref_to_string(b.second->bound_to) + ")"
2018-06-18 19:04:24 -04:00
}
}
fun _translation_unit(p: str): *tree<ast> {
return new<tree<ast>>()->construct(ast::_translation_unit(p))
}
2018-06-22 23:13:08 -04:00
fun _import(p1: *tree<ast>, p2: set<str>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_import(make_pair(p1,p2)))
2018-06-18 19:04:24 -04:00
}
fun _type_def(p: str): *tree<ast> {
return new<tree<ast>>()->construct(ast::_type_def(p))
}
fun _adt_def(p: str): *tree<ast> {
return new<tree<ast>>()->construct(ast::_adt_def(p))
}
2018-09-22 14:54:52 -04:00
fun _cast(p: *binding<type>): *tree<ast> {
2018-06-18 19:04:24 -04:00
return new<tree<ast>>()->construct(ast::_cast(p))
}
2018-09-22 14:54:52 -04:00
fun _identifier(p1: str, p2: *binding<type>): *tree<ast> {
2018-06-18 19:04:24 -04:00
return new<tree<ast>>()->construct(ast::_identifier(make_pair(p1, p2)))
}
2018-10-02 23:51:26 -04:00
fun _binding(p1: str, p2: vec<*binding<type>>, p3: *binding<tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_binding(make_triple(p1, p2, p3)))
2018-06-18 19:04:24 -04:00
}
2018-09-22 14:54:52 -04:00
fun _function(p1: str, p2: *binding<type>, p3: bool): *tree<ast> {
return new<tree<ast>>()->construct(ast::_function(make_triple(p1, p2, p3)))
2018-06-18 19:04:24 -04:00
}
2018-10-08 00:28:42 -04:00
fun _template(p1: str, p2: map<str, *binding<type>>): *tree<ast> {
2018-06-18 19:04:24 -04:00
return new<tree<ast>>()->construct(ast::_template(make_pair(p1, p2)))
}
fun _compiler_intrinsic(p1: str, p2: *binding<type>, p3: vec<*binding<type>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_compiler_intrinsic(make_triple(p1, p2, p3)))
2018-06-18 19:04:24 -04:00
}
2018-09-22 14:54:52 -04:00
fun _value(p1: str, p2: *binding<type>): *tree<ast> {
2018-06-18 19:04:24 -04:00
return new<tree<ast>>()->construct(ast::_value(make_pair(p1, p2)))
}
fun _declaration(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_declaration())
}
fun _block(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_block())
}
fun _if(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_if())
}
fun _match(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_match())
}
fun _case(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_case())
}
fun _while(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_while())
}
fun _for(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_for())
}
fun _return(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_return())
}
fun _break(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_break())
}
fun _continue(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_continue())
}
fun _defer(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_defer())
}
fun _call(): *tree<ast> {
return new<tree<ast>>()->construct(ast::_call())
}
fun _translation_unit(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_translation_unit(p), c)
}
2018-06-22 23:13:08 -04:00
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)
2018-06-18 19:04:24 -04:00
}
fun _type_def(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_type_def(p), c)
}
fun _adt_def(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_adt_def(p), c)
}
2018-09-22 14:54:52 -04:00
fun _cast(p: *binding<type>, c: ref vec<*tree<ast>>): *tree<ast> {
2018-06-18 19:04:24 -04:00
return new<tree<ast>>()->construct(ast::_cast(p), c)
}
2018-09-22 14:54:52 -04:00
fun _identifier(p1: str, p2: *binding<type>, c: ref vec<*tree<ast>>): *tree<ast> {
2018-06-18 19:04:24 -04:00
return new<tree<ast>>()->construct(ast::_identifier(make_pair(p1, p2)), c)
}
2018-10-02 23:51:26 -04:00
fun _binding(p1: str, p2: vec<*binding<type>>, p3: *binding<tree<ast>>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_binding(make_triple(p1, p2, p3)), c)
2018-06-18 19:04:24 -04:00
}
2018-09-22 14:54:52 -04:00
fun _function(p1: str, p2: *binding<type>, p3: bool, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_function(make_triple(p1, p2, p3)), c)
2018-06-18 19:04:24 -04:00
}
2018-10-08 00:28:42 -04:00
fun _template(p1: str, p2: map<str, *binding<type>>, c: ref vec<*tree<ast>>): *tree<ast> {
2018-06-18 19:04:24 -04:00
return new<tree<ast>>()->construct(ast::_template(make_pair(p1, p2)), c)
}
fun _compiler_intrinsic(p1: str, p2: *binding<type>, p3: vec<*binding<type>>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_compiler_intrinsic(make_triple(p1, p2, p3)), c)
2018-06-18 19:04:24 -04:00
}
2018-09-22 14:54:52 -04:00
fun _value(p1: str, p2: *binding<type>, c: ref vec<*tree<ast>>): *tree<ast> {
2018-06-18 19:04:24 -04:00
return new<tree<ast>>()->construct(ast::_value(make_pair(p1, p2)), c)
}
fun _declaration(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_declaration(), c)
}
fun _block(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_block(), c)
}
fun _if(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_if(), c)
}
fun _match(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_match(), c)
}
fun _case(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_case(), c)
}
fun _while(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_while(), c)
}
fun _for(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_for(), c)
}
fun _return(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_return(), c)
}
fun _defer(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_defer(), c)
}
fun _call(c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_call(), c)
}
fun is_translation_unit(i: *tree<ast>): bool { match(i->data) { ast::_translation_unit(b) return true; } return false; }
fun is_import(i: *tree<ast>): bool { match(i->data) { ast::_import(b) return true; } return false; }
fun is_identifier(i: *tree<ast>): bool { match(i->data) { ast::_identifier(b) return true; } return false; }
fun is_binding(i: *tree<ast>): bool { match(i->data) { ast::_binding(b) return true; } return false; }
fun is_type_def(i: *tree<ast>): bool { match(i->data) { ast::_type_def(b) return true; } return false; }
fun is_adt_def(i: *tree<ast>): bool { match(i->data) { ast::_adt_def(b) return true; } return false; }
fun is_function(i: *tree<ast>): bool { match(i->data) { ast::_function(b) return true; } return false; }
fun is_template(i: *tree<ast>): bool { match(i->data) { ast::_template(b) return true; } return false; }
fun is_declaration(i: *tree<ast>): bool { match(i->data) { ast::_declaration() return true; } return false; }
fun is_block(i: *tree<ast>): bool { match(i->data) { ast::_block() return true; } return false; }
fun is_if(i: *tree<ast>): bool { match(i->data) { ast::_if() return true; } return false; }
fun is_match(i: *tree<ast>): bool { match(i->data) { ast::_match() return true; } return false; }
fun is_case(i: *tree<ast>): bool { match(i->data) { ast::_case() return true; } return false; }
fun is_while(i: *tree<ast>): bool { match(i->data) { ast::_while() return true; } return false; }
fun is_for(i: *tree<ast>): bool { match(i->data) { ast::_for() return true; } return false; }
fun is_return(i: *tree<ast>): bool { match(i->data) { ast::_return() return true; } return false; }
fun is_break(i: *tree<ast>): bool { match(i->data) { ast::_break() return true; } return false; }
fun is_continue(i: *tree<ast>): bool { match(i->data) { ast::_continue() return true; } return false; }
fun is_defer(i: *tree<ast>): bool { match(i->data) { ast::_defer() return true; } return false; }
fun is_call(i: *tree<ast>): bool { match(i->data) { ast::_call() return true; } return false; }
fun is_compiler_intrinsic(i: *tree<ast>): bool { match(i->data) { ast::_compiler_intrinsic(b) return true; } return false; }
fun is_cast(i: *tree<ast>): bool { match(i->data) { ast::_cast(b) return true; } return false; }
fun is_value(i: *tree<ast>): bool { match(i->data) { ast::_value(b) return true; } return false; }
fun is_top_level_item(i: *tree<ast>): bool { return i->parent != null<tree<ast>>() && is_translation_unit(i->parent); }
2018-09-22 20:10:56 -04:00
fun get_ancestor_satisfying(t: *tree<ast>, p: fun(*tree<ast>): bool): *tree<ast> {
t = t->parent
while (t != null<tree<ast>>() && !p(t))
t = t->parent
return t
}
fun make_ast_binding(s: *char): *tree<ast> {
return make_ast_binding(str(s))
}
fun make_ast_binding(s: str): *tree<ast> {
2018-10-02 23:51:26 -04:00
return make_ast_binding(s, vec<*binding<type>>())
}
fun make_ast_binding(s: str, v: vec<*binding<type>>): *tree<ast> {
return _binding(s, v, binding<tree<ast>>())
}
fun get_ast_binding_inst_types(binding: *tree<ast>): ref vec<*binding<type>> {
match(binding->data) {
ast::_binding(b) {
return b.second
}
}
error("trying to get binding on not a binding")
}
fun get_ast_binding(binding: *tree<ast>): *tree<ast> {
match(binding->data) {
ast::_binding(b) {
2018-10-02 23:51:26 -04:00
return b.third->bound_to
}
}
error("trying to get binding on not a binding")
}
fun set_ast_binding(binding: *tree<ast>, to: *tree<ast>) {
match(binding->data) {
ast::_binding(b) {
2018-10-02 23:51:26 -04:00
b.third->set(to)
return
}
}
error("trying to set binding on not a binding")
}
fun ast_bound(binding: *tree<ast>): bool {
match(binding->data) {
2018-10-02 23:51:26 -04:00
ast::_binding(b) return b.third->bound()
}
error("Trying to check bound for not a binding")
}
fun ast_binding_str(binding: *tree<ast>): str {
match(binding->data) {
ast::_binding(b) return b.first
}
error("Trying to get name for not a binding")
}