Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,198 @@
|
||||
#! @file describe.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the checker writes types and declarations as notes spell them,
|
||||
#! as `&mut list<int>` or `def(u8): bool`.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! The type, as notes spell it.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Its spelling, as `list<i64>`.
|
||||
pub def checker::describe(&this, kind: u32): string {
|
||||
var out := string::new(nothing {});
|
||||
this.describe_into(kind, &mut out, 0);
|
||||
return out;
|
||||
}
|
||||
|
||||
def checker::describe_into(&this, kind: u32, out: &mut string, depth: u32): void {
|
||||
if depth > 16 {
|
||||
out.append("...");
|
||||
return;
|
||||
}
|
||||
|
||||
val first := this.types.first_of(kind);
|
||||
match this.types.kind_of(kind) {
|
||||
ty::error => out.append("?");
|
||||
ty::void_type => out.append("void");
|
||||
ty::null_type => out.append("null");
|
||||
ty::boolean => out.append("bool");
|
||||
ty::untyped_bool => out.append("bool");
|
||||
ty::untyped_int => out.append("integer constant");
|
||||
ty::untyped_uint => out.append("unsigned integer constant");
|
||||
ty::untyped_char => out.append("character constant");
|
||||
ty::untyped_float => out.append("float constant");
|
||||
ty::untyped_float32 => out.append("float constant");
|
||||
ty::untyped_text => out.append("text");
|
||||
ty::empty => out.append("empty array element");
|
||||
ty::signed => out.append(first == 64 ? "int" : first == 32 ? "i32" : first == 16 ? "i16" : "i8");
|
||||
ty::unsigned => out.append(first == 64 ? "uint" : first == 32 ? "u32" : first == 16 ? "u16" : "u8");
|
||||
ty::floating => out.append(first == 64 ? "float" : "f32");
|
||||
ty::reference, ty::pointer => {
|
||||
out.append(this.types.kind_of(kind) == ty::reference ? "&" : "*");
|
||||
if this.types.second_of(kind) != 0 {
|
||||
out.append("mut ");
|
||||
}
|
||||
this.describe_into(first, out, depth + 1);
|
||||
}
|
||||
ty::fixed => {
|
||||
this.describe_into(first, out, depth + 1);
|
||||
if this.types.part_count(kind) != 0 {
|
||||
out.append("[");
|
||||
this.describe_into(this.types.part_at(kind, 0), out, depth + 1);
|
||||
out.append("]");
|
||||
} else {
|
||||
val count := $"[{this.types.second_of(kind)}]";
|
||||
out.append(count.view());
|
||||
}
|
||||
}
|
||||
# A constant is written as its value, as `4` of `buffer<u8, 4>`.
|
||||
ty::value => this.describe_known(this.types.second_of(kind), out, depth + 1);
|
||||
ty::function => {
|
||||
# A pack parameter says `...` of itself.
|
||||
out.append("def(");
|
||||
this.describe_parts(kind, out, depth);
|
||||
val count := this.types.part_count(kind);
|
||||
if this.types.second_of(kind) != 0 && (count == 0 || !this.types.is_open_pack(this.types.part_at(kind, count - 1))) {
|
||||
out.append("...");
|
||||
}
|
||||
out.append("): ");
|
||||
this.describe_into(first, out, depth + 1);
|
||||
}
|
||||
# A pack among the type arguments is written as the types it gives, as
|
||||
# `box<i64, bool, u8>` of `box<A, Ts...>`.
|
||||
ty::named => {
|
||||
if this.qualified_names {
|
||||
this.module_names(first, out);
|
||||
}
|
||||
if this.owned_names || this.qualified_names {
|
||||
this.owner_names(first, out, 0);
|
||||
}
|
||||
out.append(this.names.text(this.decls.names.p[first]));
|
||||
if this.types.part_count(kind) != 0 {
|
||||
out.append("<");
|
||||
if this.has_patterns(first) {
|
||||
this.describe_written(kind, out, depth);
|
||||
} else {
|
||||
this.describe_args(this.types.parts_view(kind), out, depth + 1);
|
||||
}
|
||||
out.append(">");
|
||||
}
|
||||
}
|
||||
ty::param => out.append(this.names.text(this.decls.names.p[first]));
|
||||
ty::pack => {
|
||||
if this.types.part_count(kind) == 0 && first != 0 {
|
||||
out.append(this.names.text(this.decls.names.p[first]));
|
||||
out.append("...");
|
||||
} else {
|
||||
# A variadic function's pack of parameters ends in the one taking the rest.
|
||||
out.append("(");
|
||||
this.describe_parts(kind, out, depth);
|
||||
if this.types.second_of(kind) != 0 {
|
||||
out.append("...");
|
||||
}
|
||||
out.append(")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! The type, with the module of each type it names, as `path_of` spells it:
|
||||
#! `std::containers::dict<core::string, game::item>`, which no two types share.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Its spelling.
|
||||
pub def checker::describe_path(&mut this, kind: u32): string {
|
||||
val saved := this.qualified_names;
|
||||
this.qualified_names = true;
|
||||
var out := this.describe(kind);
|
||||
this.qualified_names = saved;
|
||||
return out;
|
||||
}
|
||||
|
||||
#! Writes the modules a declaration is in, outermost first, as `std::io::`.
|
||||
#! @param this The checker.
|
||||
#! @param decl The declaration.
|
||||
#! @param out Where they are written.
|
||||
def checker::module_names(&this, decl: u32, out: &mut string): void {
|
||||
var modules := vector::<u32>::new();
|
||||
var at: u32 = this.decls.modules.p[decl];
|
||||
while at != 0 && modules.length < 64 {
|
||||
modules.append(at);
|
||||
at = this.modules.parents[at];
|
||||
}
|
||||
var i := modules.length;
|
||||
while i > 0 {
|
||||
i--;
|
||||
out.append(this.names.text(this.modules.names[modules[i]]));
|
||||
out.append("::");
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes the types a type is declared through, as `token::` of `token::kind`.
|
||||
#! @param this The checker.
|
||||
#! @param decl The type's declaration.
|
||||
#! @param out Where they are written.
|
||||
#! @param depth How many have been written.
|
||||
def checker::owner_names(&this, decl: u32, out: &mut string, depth: u32): void {
|
||||
val owner: u32 = this.decls.owners.p[decl];
|
||||
return if owner == 0 || depth > 8 || !this.decls.nests(decl) || this.decls.kinds.p[owner] == dk::builtin;
|
||||
this.owner_names(owner, out, depth + 1);
|
||||
out.append(this.names.text(this.decls.names.p[owner]));
|
||||
out.append("::");
|
||||
}
|
||||
|
||||
def checker::describe_parts(&this, kind: u32, out: &mut string, depth: u32): void {
|
||||
for i in 0..this.types.part_count(kind) {
|
||||
if i != 0 {
|
||||
out.append(", ");
|
||||
}
|
||||
this.describe_into(this.types.part_at(kind, i as u32), out, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#! The name of the declaration, as notes spell it.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The name.
|
||||
pub def checker::decl_text(&this, id: u32): strview {
|
||||
return this.names.text(this.decls.names.p[id]);
|
||||
}
|
||||
|
||||
#! What the declaration is, in a word or two, as "function" or "struct".
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The word.
|
||||
pub def checker::decl_word(&this, id: u32): strview {
|
||||
return match this.decls.kinds.p[id] {
|
||||
dk::builtin => "type",
|
||||
dk::function => "function",
|
||||
dk::variable => this.decls.has(id, df::constant) ? "constant" : "variable",
|
||||
dk::field => "field",
|
||||
dk::struct_type => "struct",
|
||||
dk::union_type => "union",
|
||||
dk::enum_type => "enum",
|
||||
dk::enum_member => "enum member",
|
||||
dk::trait_type => "trait",
|
||||
dk::alias_type => "type alias",
|
||||
dk::type_param => "type parameter",
|
||||
dk::assoc_type => "type",
|
||||
dk::property => "property",
|
||||
dk::attribute => "constant",
|
||||
else => "declaration"
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user