148 lines
6.2 KiB
Plaintext
148 lines
6.2 KiB
Plaintext
#! @file evaluating.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains what the checker asks of the program's evaluator, which runs code
|
|
#! while checking, where a value must be known that folding does not give: an
|
|
#! array's length a function gives, the condition of an `if const`, a local
|
|
#! constant's value, a range's end for `for const`, a constant type argument,
|
|
#! or the text of `compile_error`. The analyzer has no evaluator of its own: the
|
|
#! program's machine is one, which whoever checks a program to run or build it
|
|
#! gives the checker before checking.
|
|
#!
|
|
#! What is asked for is lowered with what it calls, each checked as lowering
|
|
#! reaches it, and run, unless what it calls had an error checking it, which is
|
|
#! never run. A checker given no evaluator, as one answering an editor's
|
|
#! queries, knows what folding gives and nothing more.
|
|
|
|
module sakura::analyzer;
|
|
|
|
|
|
#! What the checker asks the evaluator for.
|
|
pub enum evaluation_kind : u8 {
|
|
value, #! The value of an expression: `table` and the node.
|
|
local, #! A local constant's value: `table` and its `variable` node.
|
|
condition, #! Whether the condition of an `if const` holds: `table` and the `if_const` node.
|
|
text, #! The text `compile_error` reports: `table` and the `call` node.
|
|
}
|
|
|
|
#! What the checker asks the evaluator to work out, and where.
|
|
pub struct evaluation_ask {
|
|
pub var what: evaluation_kind; #! What it is.
|
|
pub var table: u32; #! The result table the node was checked in.
|
|
pub var node: u32; #! The node.
|
|
}
|
|
|
|
#! What the evaluator worked out of what was asked.
|
|
pub struct evaluation_answer {
|
|
pub var worked: bool; #! Whether it was worked out.
|
|
pub var noted: bool; #! Where it was not, whether the evaluator noted why, so the checker notes nothing more.
|
|
pub var scalar: bool; #! Whether the value is a number, `bool`, character, or member of an enum, which `value` holds.
|
|
pub var value: constant; #! The value, where it is one of those, or whether a condition holds.
|
|
pub var text: string; #! The text `compile_error` reports, where that was asked for.
|
|
}
|
|
|
|
#! What the checker keeps of where checking is while the evaluator runs, which
|
|
#! checks what checking reached first: what one body's checking holds that
|
|
#! another's must begin without.
|
|
struct checker::outside {
|
|
var at: checker::frame; #! Where checking was.
|
|
var quiet: u32; #! Whether notes were left out.
|
|
var undecided: u32; #! How many undecided `if const` and `for const` were around it.
|
|
var waits_for: u32; #! The call that might wait for its parameter's type.
|
|
var free_result: bool; #! Whether a lambda takes its result from its body.
|
|
var walked: u64; #! How many places loops walked.
|
|
}
|
|
|
|
#! Asks the program's evaluator for what only running gives, once what checking
|
|
#! reached is checked.
|
|
#! @param this The checker.
|
|
#! @param what What is asked for.
|
|
#! @param node The node it is asked of, in the table being written.
|
|
#! @param out Where the answer is written.
|
|
#! @returns Whether it was worked out.
|
|
pub def checker::ask_evaluator(&mut this, what: evaluation_kind, node: u32, out: &mut evaluation_answer): bool {
|
|
out.worked = false;
|
|
out.noted = false;
|
|
return false if this.quiet != 0 || this.waits_for_instance();
|
|
# An expression checking found an error in is never run.
|
|
return false if what == evaluation_kind::value && this.type_at(node) == bt::error;
|
|
val run := this.evaluator else { return false; };
|
|
val ask := evaluation_ask { what = what, table = this.table_node_of(node).table, node = node };
|
|
|
|
val held := this.step_outside();
|
|
run(&ask, out);
|
|
this.step_back(&held);
|
|
return out.worked;
|
|
}
|
|
|
|
#! Keeps where checking is and begins as checking a declaration of its own does,
|
|
#! so what is checked meanwhile, as what an evaluation lowers, sees nothing of
|
|
#! the body being checked.
|
|
#! @param this The checker.
|
|
#! @returns What was kept, which `step_back` puts back.
|
|
def checker::step_outside(&mut this): checker::outside {
|
|
val made := checker::outside {
|
|
at = this.at,
|
|
quiet = this.quiet,
|
|
undecided = this.undecided,
|
|
waits_for = this.waits_for,
|
|
free_result = this.free_result,
|
|
walked = this.walked.length
|
|
};
|
|
this.quiet = 0;
|
|
this.undecided = 0;
|
|
this.waits_for = 0;
|
|
this.free_result = false;
|
|
return made;
|
|
}
|
|
|
|
#! Puts back where checking was, as `step_outside` kept it.
|
|
#! @param this The checker.
|
|
#! @param held What was kept.
|
|
def checker::step_back(&mut this, held: &checker::outside): void {
|
|
this.at = held.at;
|
|
this.quiet = held.quiet;
|
|
this.undecided = held.undecided;
|
|
this.waits_for = held.waits_for;
|
|
this.free_result = held.free_result;
|
|
this.walked.resize(held.walked);
|
|
}
|
|
|
|
#! Asks the evaluator for the value of an expression of a number, `bool`,
|
|
#! character, or enum, and records it at the node where it was worked out. One
|
|
#! reading a local only the program running gives a value is not asked for.
|
|
#! @param this The checker.
|
|
#! @param node The expression.
|
|
#! @param out Where the answer is written.
|
|
#! @returns Whether its value is known now.
|
|
pub def checker::run_value(&mut this, node: u32, out: &mut evaluation_answer): bool {
|
|
return false if this.runtime_local_read(node) != 0;
|
|
if this.ask_evaluator(evaluation_kind::value, node, out) && out.scalar {
|
|
this.set_value(node, out.value);
|
|
}
|
|
return this.is_known(node);
|
|
}
|
|
|
|
#! The constant made to fit an integer type of its bits, wrapping as the
|
|
#! program running wraps.
|
|
#! @param this The checker.
|
|
#! @param value The constant.
|
|
#! @param kind The integer type.
|
|
#! @returns The constant wrapped to its bits.
|
|
def checker::wrap_constant(&mut this, value: constant, kind: u32): constant {
|
|
val plain := this.unalias(kind);
|
|
val tkd := this.types.kind_of(plain);
|
|
return value if value.floating || (tkd != ty::signed && tkd != ty::unsigned);
|
|
val bits := this.types.first_of(plain);
|
|
var raw := value.bits();
|
|
if bits < 64 {
|
|
raw &= (1u << bits as u64) - 1;
|
|
if tkd == ty::signed && (raw >> (bits - 1) as u64) != 0 {
|
|
raw |= ~((1u << bits as u64) - 1);
|
|
}
|
|
}
|
|
return constant::from_bits(raw, tkd == ty::signed);
|
|
}
|