67 lines
2.4 KiB
Plaintext
67 lines
2.4 KiB
Plaintext
#! @file scopes.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the bindings of names to the locals, parameters, and type
|
||
|
|
#! parameters they refer to. They are one stack, and each name keeps the
|
||
|
|
#! binding of it nearest the top, so finding what a name refers to takes one
|
||
|
|
#! read, and a scope ends by cutting the stack back to where it began.
|
||
|
|
|
||
|
|
module sakura::analyzer;
|
||
|
|
|
||
|
|
|
||
|
|
#! Where the bindings are now, which a scope ending cuts them back to.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @returns The mark.
|
||
|
|
pub def checker::scope_mark(&this): u32 {
|
||
|
|
return this.bind_names.length as u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Binds the name to what it refers to, hiding what it referred to before
|
||
|
|
#! until the scope ends. `_` is never bound, since nothing reads it.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param name The ID of the name.
|
||
|
|
#! @param value What it refers to: a local or type parameter with its bit of
|
||
|
|
#! `ref_local` or `ref_type`.
|
||
|
|
pub def checker::bind(&mut this, name: u32, value: u32): void {
|
||
|
|
return if name == names::underscore_id || name == 0;
|
||
|
|
|
||
|
|
if this.innermost.length <= name as u64 {
|
||
|
|
this.innermost.resize(name as u64 + 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
this.bind_names.append(name);
|
||
|
|
this.bind_values.append(value);
|
||
|
|
this.bind_prev.append(this.innermost[name]);
|
||
|
|
this.innermost[name] = this.bind_names.length as u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Ends the scopes begun since the mark.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param mark Where the bindings were, as `scope_mark` gave it.
|
||
|
|
pub def checker::unbind_to(&mut this, mark: u32): void {
|
||
|
|
# Each name is given back what it hid from the top down, so a name bound more
|
||
|
|
# than once since the mark is given back what it was bound to before.
|
||
|
|
var top := this.bind_names.length;
|
||
|
|
return if top <= mark as u64;
|
||
|
|
while top > mark as u64 {
|
||
|
|
top--;
|
||
|
|
val name: u32 = this.bind_names[top];
|
||
|
|
this.innermost[name] = this.bind_prev[top];
|
||
|
|
}
|
||
|
|
this.bind_names.resize(mark as u64);
|
||
|
|
this.bind_values.resize(mark as u64);
|
||
|
|
this.bind_prev.resize(mark as u64);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What the name is bound to, or 0 where it is bound to nothing.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param name The ID of the name.
|
||
|
|
#! @returns What it is bound to, or 0.
|
||
|
|
pub def checker::bound(&this, name: u32): u32 {
|
||
|
|
return 0 if name as u64 >= this.innermost.length;
|
||
|
|
|
||
|
|
val at: u32 = this.innermost[name];
|
||
|
|
return at == 0 ? 0 : this.bind_values[at - 1];
|
||
|
|
}
|