Files
2026-11-19 04:54:17 -06:00

460 lines
22 KiB
Plaintext

#! @file shapes.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the shapes of the nodes, which say what the slots and records of
#! each kind of node hold, so the tree can be walked and printed without knowing
#! each kind, and the positions of the slots of each record, by name.
module sakura::analyzer;
#! Describes the shape of any given syntax node within the language.
#!
#! All nodes have a distinct `left` and `right`, but they may mean something
#! different. They can point to another node by index, an extra piece of data
#! by index, a run of nodes whose indices are proceeding the length in the
#! extras vector, or simply the flags associated with the node.
#!
#! When the slot, `left` or `right`, is populated by `slot::extra` then the
#! record variable is guaranteed to be populated, as this will describe the
#! full structure of the node. For example, a function node has many additional
#! features to it: receiver, generics, params, return type, body, attributes.
#! All of those cannot be described in simply the `left` and `right` slots. It
#! can be described in `right` if `slot::extra` indicates that `record` further
#! describes it shape and the record tells you how many additional pieces there
#! are and what they represent.
pub struct shape {
#! What a node's first slot holds.
val left: slot;
#! What its second slot holds.
val right: slot;
#! What its record holds, slot by slot, where a slot holds one.
val record: span<slot>;
}
#! Describes what a left hand or right hand side of a node contains.
#!
#! This works in conjunction with the `shape` structure to describe the exact
#! layout of a given node for the purposes of traversing the abstract syntax
#! tree of the language.
pub enum slot : u8 {
none, #! Unused.
node, #! An index into `nodes`.
extra, #! An index into `extras` of one node.
run, #! An index into `extras` where a count is followed by that many nodes.
flags, #! Bits the kind defines, as `mut` or `const`.
bounds #! A run, which only shows where it holds nodes: those a trait's method bounds beside its own.
}
#! The slots the records of declarations share, by their positions, as `record`
#! takes them: each begins with its attributes, then its receiver, where it can
#! be declared through a type, then its type parameters, where it takes them. So
#! what reads one of these of any declaration reads the same slot.
pub enum decl_slot : u32 {
attributes, #! Attributes, of every declaration's record.
receiver, #! Receiver, of all but a type a trait asks for.
type_params, #! Type parameters, of a function's, a struct's, union's, or trait's, and an alias's.
}
#! Describes how the extras of an `if` statement is laid out.
#!
#! An if statement is described by a `left` containing the condition, and
#! `right` containing the `then` and `else` branch of the statement. Thus, this
#! record describes two nodes slots.
pub const if_record: span<slot> = [
slot::node, #! Then body, a `block`.
slot::node #! Else body, a `block` or `no_record`.
];
#! The slots of the record of an `if`, a `when`, `if const`, or `?:`, by their
#! positions, as `record` takes them.
pub enum if_slot : u32 {
then, #! What is taken where the condition holds.
other, #! What is taken where it does not, or 0.
}
#! Describes how the extras of a function are laid out.
#!
#! `left` is this record and `right` its flags. The receiver is the type a
#! method or nested function is declared through, as `box<T>` of `box<T>::get`,
#! and the result and body are 0 where they are left out: a lambda's result, or
#! the body of a function ending at its signature.
pub const function_record: span<slot> = [
slot::run, #! Attributes.
slot::node, #! Receiver, a `type_name`.
slot::run, #! Type parameters, each a `type_param`.
slot::run, #! Parameters, each a `param`.
slot::node, #! Result type.
slot::node, #! Body, a `block`.
slot::bounds, #! Bounds a trait's method puts on the trait's type parameters and `self`, each a `type_param`.
];
#! The slots of a function's record, by their positions, as `record` takes them.
pub enum function_slot : u32 {
attributes, #! Attributes.
receiver, #! Receiver.
type_params, #! Type parameters.
params, #! Parameters.
result, #! Result type.
body, #! Body.
outer, #! Bounds on the trait's type parameters and `self`.
}
#! Describes how the extras of a parameter are laid out: `left` is its type,
#! which a lambda may leave out, and `right` this record.
pub const param_record: span<slot> = [
slot::node, #! Default value.
slot::flags, #! `variadic`, and `mutable` for `&mut this`.
slot::run, #! Attributes.
];
#! The slots of a parameter's record, by their positions, as `record` takes
#! them.
pub enum parameter_slot : u32 {
value, #! Default value.
flags, #! Flags.
attributes, #! Attributes.
}
#! Describes how the extras of a variable, constant, or field are laid out:
#! `left` is this record and `right` its flags. A spread field's type is a
#! `spread`.
pub const variable_record: span<slot> = [
slot::run, #! Attributes.
slot::node, #! Receiver, a `type_name`, as `rect` of `val rect::unit`.
slot::node, #! Type.
slot::node, #! Value.
];
#! The slots of the record of a variable, constant, or field, by their
#! positions, as `record` takes them.
pub enum variable_slot : u32 {
attributes, #! Attributes.
receiver, #! Receiver.
type_expr, #! Type.
value, #! Value.
}
#! Describes how the extras of a property are laid out: `left` is this record
#! and `right` the run of its accessors.
pub const property_record: span<slot> = [
slot::run, #! Attributes.
slot::node, #! Receiver, a `type_name`.
slot::node, #! Type.
slot::flags, #! `mutable` for `var`, and `exported`.
];
#! The slots of a property's record, by their positions, as `record` takes them.
pub enum property_slot : u32 {
attributes, #! Attributes.
receiver, #! Receiver.
type_expr, #! Type.
flags, #! Flags.
}
#! Describes how the extras of an accessor are laid out: `left` is the run of
#! its parameters, `&this` first, and `right` this record. Its token says `get`
#! or `set`.
pub const accessor_record: span<slot> = [
slot::node, #! Body, 0 where it ends at its signature.
slot::flags, #! `exported`, where the accessor is `pub` of its own.
];
#! The slots of an accessor's record, by their positions, as `record` takes
#! them.
pub enum accessor_slot : u32 {
body, #! Body.
flags, #! Flags.
}
#! Describes how the extras of a struct, union, or trait are laid out: `left` is
#! this record and `right` the run of its members. Which it is, is its kind.
pub const aggregate_record: span<slot> = [
slot::run, #! Attributes.
slot::node, #! Receiver, a `type_name`.
slot::run, #! Type parameters, each a `type_param`.
slot::run, #! Traits it implements, or composes, each a `type_name`.
slot::flags, #! `exported`.
];
#! The slots of the record of a struct, union, or trait, by their positions, as
#! `record` takes them.
pub enum aggregate_slot : u32 {
attributes, #! Attributes.
receiver, #! Receiver.
type_params, #! Type parameters.
traits, #! Traits it implements, or composes.
flags, #! Flags.
}
#! Describes how the extras of a type a trait asks for are laid out: `left` is
#! this record and `right` the type standing in where the implementer gives
#! none.
pub const assoc_record: span<slot> = [
slot::run, #! Attributes.
slot::run, #! Bounds, each a `type_name`.
slot::flags, #! `packed`, of a pack, as `type params...;`.
];
#! The slots of the record of a type a trait asks for, by their positions, as
#! `record` takes them.
pub enum assoc_slot : u32 {
attributes, #! Attributes.
bounds, #! Bounds.
flags, #! Flags.
}
#! Describes how the extras of an enum are laid out: `left` is this record and
#! `right` the run of its members.
pub const enumeration_record: span<slot> = [
slot::run, #! Attributes.
slot::node, #! Receiver, a `type_name`.
slot::node, #! The integer type holding it.
slot::flags, #! `exported`.
];
#! The slots of an enum's record, by their positions, as `record` takes them.
pub enum enumeration_slot : u32 {
attributes, #! Attributes.
receiver, #! Receiver.
integer, #! The integer type holding it.
flags, #! Flags.
}
#! Describes how the extras of a type alias are laid out: `left` is this record
#! and `right` the aliased type.
pub const alias_record: span<slot> = [
slot::run, #! Attributes.
slot::node, #! Receiver, a `type_name`.
slot::run, #! Type parameters, each a `type_param`.
slot::flags, #! `exported`.
];
#! The slots of a type alias's record, by their positions, as `record` takes
#! them.
pub enum alias_slot : u32 {
attributes, #! Attributes.
receiver, #! Receiver.
type_params, #! Type parameters.
flags, #! Flags.
}
#! Describes how the extras of a `for` loop are laid out: `left` is this record
#! and `right` the body.
pub const for_record: span<slot> = [
slot::node, #! The index, a `name`, as `i` of `for i, x in xs`.
slot::node, #! The variable, a `name`.
slot::node, #! What is iterated, or where a range begins.
slot::node, #! Where a range ends, as `n` of `0..n`.
slot::flags, #! `reference` for `&x`, with `mutable` for `&mut x`, `constant`, and `inclusive` for `0..=n`.
];
#! The slots of a `for` loop's record, by their positions, as `record` takes
#! them.
pub enum for_slot : u32 {
index, #! The index.
variable, #! The variable.
iterated, #! What is iterated, or where a range begins.
end, #! Where a range ends.
flags, #! Flags.
}
#! Describes how the extras of an `else` fallback are laid out: `left` is the
#! result or maybe it takes from, and `right` this record.
pub const fallback_record: span<slot> = [
slot::node, #! The error it names, a `name`, as `e` of `else (e)`.
slot::node, #! What stands in: a `block` that leaves, or an expression.
];
#! The slots of the record of an `else` fallback, by their positions, as
#! `record` takes them.
pub enum fallback_slot : u32 {
error, #! The error it names.
other, #! What stands in.
}
#! Describes how the extras of a slice are laid out: `left` is what is sliced,
#! and `right` this record, either of whose bounds may be left out, as in
#! `xs[a:]`.
pub const slice_record: span<slot> = [
slot::node, #! Start.
slot::node, #! Count.
];
#! The slots of a slice's record, by their positions, as `record` takes them.
pub enum slice_slot : u32 {
start, #! Start.
count, #! Count.
}
#! Describes how the extras of an `is` test are laid out: `left` is the value
#! tested, and `right` this record.
pub const is_record: span<slot> = [
slot::node, #! The field or trait, a `type_name`, or what names a field, as `d` of `x is (d)`.
slot::node, #! The value the field holds, a `name`, or a `reference` to one.
slot::flags, #! `negated` for `is not`, and `computed` for `x is (d)`.
];
#! The slots of the record of an `is` test, by their positions, as `record`
#! takes them.
pub enum is_slot : u32 {
target, #! The field or trait.
binding, #! The value the field holds.
flags, #! Flags.
}
#! Describes how the extras of a contract are laid out: `left` is its
#! condition, and `right` this record, which is none where it says nothing but
#! its condition. Its token is the word saying which it is.
pub const contract_record: span<slot> = [
slot::node, #! The name an `ensures` gives what the function returns, a `name`.
slot::node, #! What it says failed, in place of the condition as written.
];
#! The slots of the record of a contract, by their positions, as `record` takes
#! them.
pub enum contract_slot : u32 {
bound, #! The name given what is returned.
text, #! What it says failed.
}
#! Which contract a `contract` node is, as its token says.
pub enum clause : u8 {
expects, #! What a function needs of what it is given, checked where it is written.
asserts, #! What must hold as the work goes on, checked where it is written.
ensures, #! What holds where the block it is written in is left, checked there.
}
#! Describes how the extras of a lambda are laid out: `left` is the run of its
#! parameters, and `right` this record.
pub const lambda_record: span<slot> = [
slot::node, #! Result type, 0 where it is left out.
slot::node, #! Body: a `block`, or the expression after `=>`.
];
#! The slots of a lambda's record, by their positions, as `record` takes them.
pub enum lambda_slot : u32 {
result, #! Result type.
body, #! Body.
}
#! No record, for the kinds whose slots hold everything they have.
pub const no_record: span<slot> = [];
#! The shape of each kind of node, in the order `nk` declares them.
#!
#! Index 0 stands for "absent" in both vectors: node 0 is an `error` node
#! holding nothing, a run at 0 is empty, and a record at 0 holds nothing in each
#! of its slots, which the zeros `extras` begins with make true. So a slot left
#! out holds 0 whatever it is, and a record of nothing takes no room.
#!
#! A `type_name` of one segment without type arguments, as `u8`, holds no run,
#! its token being the name, and likewise a `path` of one segment.
#!
#! Where a node's token alone says what it is, as a `name` or a `number`, both
#! of its slots are unused. An operator's token is the operator.
pub const shapes: shape[nk::count as u64] = [
# Structure
shape { left = slot::run, right = slot::none, record = no_record }, # error: what it did parse
shape { left = slot::run, right = slot::none, record = no_record }, # document: declarations
shape { left = slot::run, right = slot::none, record = no_record }, # block: statements
# Declarations
shape { left = slot::node, right = slot::none, record = no_record }, # import_decl: `path`
shape { left = slot::node, right = slot::node, record = no_record }, # module_decl: `path`, `when` condition
shape { left = slot::node, right = slot::node, record = no_record }, # extern_import: `string`, `when` condition
shape { left = slot::node, right = slot::node, record = no_record }, # extern_module: `path`, `when` condition
shape { left = slot::node, right = slot::extra, record = if_record }, # when_block: condition, [then, else]
shape { left = slot::extra, right = slot::flags, record = function_record }, # function
shape { left = slot::node, right = slot::extra, record = param_record }, # param: type, [default, flags]
shape { left = slot::extra, right = slot::flags, record = variable_record }, # variable
shape { left = slot::extra, right = slot::flags, record = variable_record }, # field
shape { left = slot::node, right = slot::run, record = no_record }, # spread: `type_name`, renames
shape { left = slot::node, right = slot::none, record = no_record }, # rename: new `name`; token is the old
shape { left = slot::extra, right = slot::run, record = property_record }, # property: [...], accessors
shape { left = slot::run, right = slot::extra, record = accessor_record }, # accessor: params, [body, flags]
shape { left = slot::extra, right = slot::run, record = aggregate_record }, # struct_decl: [...], fields
shape { left = slot::extra, right = slot::run, record = aggregate_record }, # union_decl: [...], fields
shape { left = slot::extra, right = slot::run, record = aggregate_record }, # trait_decl: [...], members
shape { left = slot::extra, right = slot::node, record = assoc_record }, # assoc_type: [...], default
shape { left = slot::extra, right = slot::run, record = enumeration_record }, # enumeration: [...], members
shape { left = slot::run, right = slot::node, record = no_record }, # enumerator: attributes, value
shape { left = slot::extra, right = slot::node, record = alias_record }, # alias: [...], aliased type
shape { left = slot::node, right = slot::run, record = no_record }, # attribute: `type_name`, field inits
shape { left = slot::run, right = slot::flags, record = no_record }, # type_param: bounds, `packed`
shape { left = slot::run, right = slot::node, record = no_record }, # const_param: bounds, type
shape { left = slot::run, right = slot::node, record = no_record }, # type_pattern: parameters' type parameters, result's
shape { left = slot::node, right = slot::node, record = no_record }, # array_pattern: element's type parameter, length's
# Types
shape { left = slot::run, right = slot::none, record = no_record }, # segment: type arguments or parameters
shape { left = slot::run, right = slot::none, record = no_record }, # type_name: segments
shape { left = slot::node, right = slot::flags, record = no_record }, # type_ref: referent, `mutable`
shape { left = slot::node, right = slot::flags, record = no_record }, # type_ptr: pointee, `mutable`
shape { left = slot::node, right = slot::node, record = no_record }, # type_fixed: element, length
shape { left = slot::run, right = slot::node, record = no_record }, # type_func: parameter types, result
shape { left = slot::node, right = slot::none, record = no_record }, # type_spread: the type spread
shape { left = slot::node, right = slot::none, record = no_record }, # type_of: declaration
shape { left = slot::node, right = slot::run, record = no_record }, # type_match: subject, arms
shape { left = slot::node, right = slot::extra, record = if_record }, # type_cond: condition, [then, else]
# Statements
shape { left = slot::node, right = slot::extra, record = if_record }, # if_stmt: condition, [then, else]
shape { left = slot::node, right = slot::extra, record = if_record }, # if_const: condition, [then, else]
shape { left = slot::node, right = slot::node, record = no_record }, # while_stmt: condition, body
shape { left = slot::extra, right = slot::node, record = for_record }, # for_stmt: [...], body
shape { left = slot::node, right = slot::run, record = no_record }, # match_stmt: subject, arms
shape { left = slot::run, right = slot::node, record = no_record }, # arm: patterns (empty for `else`), body
shape { left = slot::node, right = slot::none, record = no_record }, # return_stmt: value
shape { left = slot::none, right = slot::none, record = no_record }, # break_stmt
shape { left = slot::none, right = slot::none, record = no_record }, # continue_stmt
shape { left = slot::node, right = slot::none, record = no_record }, # expr_stmt: expression
shape { left = slot::node, right = slot::extra, record = contract_record }, # contract: condition, [name, text]; token is the word
# Expressions
shape { left = slot::none, right = slot::none, record = no_record }, # name
shape { left = slot::run, right = slot::none, record = no_record }, # path: segments
shape { left = slot::none, right = slot::none, record = no_record }, # number
shape { left = slot::none, right = slot::none, record = no_record }, # character
shape { left = slot::none, right = slot::none, record = no_record }, # string
shape { left = slot::run, right = slot::none, record = no_record }, # istring: text pieces and holes
shape { left = slot::none, right = slot::none, record = no_record }, # boolean
shape { left = slot::none, right = slot::none, record = no_record }, # null_lit
shape { left = slot::node, right = slot::none, record = no_record }, # unary: operand; also `*p` and `...xs`
shape { left = slot::node, right = slot::none, record = no_record }, # postfix: operand of `++` or `--`
shape { left = slot::node, right = slot::flags, record = no_record }, # reference: operand, `mutable`
shape { left = slot::node, right = slot::node, record = no_record }, # binary: left, right
shape { left = slot::node, right = slot::node, record = no_record }, # assign: target, value
shape { left = slot::node, right = slot::extra, record = if_record }, # ternary: condition, [then, else]
shape { left = slot::node, right = slot::extra, record = fallback_record }, # fallback: value, [error, fallback]
shape { left = slot::node, right = slot::run, record = no_record }, # call: callee, arguments
shape { left = slot::node, right = slot::run, record = no_record }, # type_args: callee, types, as `value_of(d)::<i64>`
shape { left = slot::node, right = slot::node, record = no_record }, # index: object, index
shape { left = slot::node, right = slot::extra, record = slice_record }, # slice: object, [start, count]
shape { left = slot::node, right = slot::run, record = no_record }, # member: object, type arguments
shape { left = slot::node, right = slot::node, record = no_record }, # member_of: object, declaration, as `x.(d)`
shape { left = slot::node, right = slot::node, record = no_record }, # cast: value, type
shape { left = slot::node, right = slot::extra, record = is_record }, # is_test: value, [target, binding, flags]
shape { left = slot::node, right = slot::run, record = no_record }, # construct: `type_name`, field inits
shape { left = slot::node, right = slot::flags, record = no_record }, # field_init: value, `named`; token is the field where named
shape { left = slot::run, right = slot::none, record = no_record }, # array: elements
shape { left = slot::run, right = slot::extra, record = lambda_record }, # lambda: params, [result, body]
shape { left = slot::node, right = slot::run, record = no_record }, # match_value: subject, arms
shape { left = slot::node, right = slot::node, record = no_record }, # range: start, end; token is `..` or `..=`
shape { left = slot::node, right = slot::none, record = no_record }, # old_value: the expression, within an `ensures`
shape { left = slot::none, right = slot::none, record = no_record }, # undefined
];
#! The shape of the node, which says what its `left` and `right` hold.
#! @param this The node.
#! @returns The shape.
pub val node::shape: shape { get => shapes[this.kind as u64]; }
#! The shape of nodes of the kind.
#! @param this The kind of node.
#! @returns The shape.
pub val nk::shape: shape { get => shapes[this as u64]; }