parse types into initial ast

This commit is contained in:
Nathan Braswell
2018-06-22 23:13:08 -04:00
parent e851d0eac5
commit 1cae1b1504
4 changed files with 143 additions and 65 deletions

View File

@@ -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)
}

View File

@@ -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] == '-') {

View File

@@ -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
}