Expand description
A Rust port of the predicator condition language, which compiles a condition to a small instruction set and evaluates it on a stack machine.
The crate holds its value model: Value and the types it is
built from (Map and its Key, Date, DateTime, Duration), the
Error every entry point answers, and the tagged codec for the
conformance corpus’s tagged-value encoding.
The evaluator runs every tier of the instruction set:
evaluate runs Instructions (read with
Instructions::from_json) over a Context with the Options
given per call, answering a Value. It runs lit, load, compare,
not, unary_bang, unary_minus, jump_if_falsy_or_pop and
jump_if_true_or_pop; add, subtract, multiply, divide and
modulo; access, bracket_access, in, contains and make_list;
object_new, object_set, duration and relative_date, which
reads the current time from the clock the Options name; and call,
which calls a function the Options pass in by name
(Options::function) or one of the reference’s library functions
(the string, list, math, date and JSON functions); store and pop;
cast; jump and pop_jump_if_falsy; jump_backward, each back edge
spending one of the run’s loop budget (Options::loop_budget); and
presence and intersects. Every other opcode is refused as unknown_instruction, and the retired and
and or as retired_opcode.
The compiler turns a source into the same instructions the reference’s
compiler does: compile reads one expression and compile_program
a statement program, each answering Instructions or the
Error::Parse that refused the source, and Instructions::to_json
writes the list back in the tagged encoding. evaluate also takes a
source as its program (a &str), compiled as an expression, and
execute and execute_value one compiled as a statement program.
execute and execute_value run the same instructions as a
statement program: the answer is an Execution, the context the run
leaves (a new value; the one passed in is never changed) with the roots
it stored into, or a Stopped, the error a run stopped on with the
context as of its last completed statement. A store into a root the
Options protect (Options::protected_roots) is refused.
parse_duration reads a duration from its literal spelling ("1h30m"),
and ISA_VERSION is the version of the instruction set the crate runs.
§The public surface
The crate root holds the functions compile, compile_program,
evaluate, execute, execute_value and parse_duration; the
constant ISA_VERSION; the module tagged; and the types Value
(with its parts Map and Key), Context, Instructions,
Duration, Date, DateTime, Options (with its part
OnUnbound), Execution, Stopped, Position, and Error
with the four error types it holds. Every other module is private.
Every entry point answers a Result, and nothing is configured
globally: host functions, the clock and the random source are passed per
call in an Options, and may borrow from the caller for the length of
the call.
No source, instruction list or context makes the crate panic or overflow its stack. A panic inside a function the caller passes in (a host function, a clock or a random source) is not caught: it unwinds through the run to the caller, as any panic in the caller’s own code does.
With the serde feature (off by default), Value implements
Serialize and Deserialize in the tagged encoding, and Date,
DateTime and Duration in the text and the unit map that encoding
carries them in.
Modules§
- tagged
- The conformance corpus’s tagged-value encoding, as text.
Structs§
- Context
- The variables an evaluation reads: each root name bound to a value. A
loadreads a root by its string name; a name the context does not bind is unbound, which is not the same as a name bound toValue::Undefined. - Date
- A civil date: a year, a month and a day on the proleptic Gregorian calendar, with no time of day and no zone. Dates order chronologically.
- Date
Time - An instant in UTC to the microsecond. Its date may be any
Date, so years past 9999 (and before 0) are held without overflow. Instants order chronologically, and two values at the same instant are equal however they were made. - Duration
- A duration: the eight units the reference’s duration carries, every one present and zero unless set. A unit may be negative (the difference of two dates can be). Two durations are equal when every unit is.
- Evaluation
Error - An evaluation failure that is neither a type mismatch nor an unbound variable, and a value refused where it crosses into the crate (a float that is not finite, a value nested too deep).
- Execution
- A statement program that ran to its end (
execute,execute_value): the context it left, the roots it stored into and, fromexecute_value, the value of its last expression statement. - Instructions
- An instruction list: a flat list of instructions, each a list whose first member names the opcode and whose other members are its operands, as the reference’s instruction-set text defines it.
- Map
- A predicator map:
Keys, each with aValue, kept in key order. - Options
- The options an evaluation is run with, built per call: nothing is configured globally.
- Parse
Error - A text that does not parse: a source, or a text handed to
tagged::decode. It knows its position. - Position
- A place in source text: a line and a column, both counted from 1.
- Stopped
- A statement program that stopped before its end: the
Errorit stopped on, the context as of the last statement it completed (every earlier write kept, the failing write not made) and the roots it had stored into by then. The context passed in is never changed; a caller that wants all or nothing keeps the one it has. - Type
Mismatch Error - An operand an operation refused on its type. The reason names the operation.
- Undefined
Variable Error - A variable the context does not bind, reported as an error rather than as undefined.
Enums§
- Error
- Every error the crate answers. The variant is the error’s type, which with the reason is normative where the conformance corpus expects an error (it names the three evaluation types); the message is not.
- Key
- A map key: one of the four kinds the reference’s instruction-set text
lets index a map, a string, an integer, a boolean or undefined. (
nullis not one of the four; a lookup bynullis a miss, not an error.) - OnUnbound
- What a
loadof a variable the context does not bind does. - Value
- A predicator value.
Constants§
- ISA_
VERSION - The version of the reference’s instruction set this crate runs: every opcode of every tier up to it, the version the vendored conformance corpus names.
Functions§
- compile
- Compiles an expression’s source to the instruction list it runs as.
- compile_
program - Compiles a statement program’s source, one statement or more, to the instruction list it runs as.
- evaluate
- Evaluates
programovercontextand answers the value it leaves on top of the stack. - execute
- Runs
programovercontextas a statement program and answers the context it leaves, with the roots it stored into. - execute_
value - Runs
programovercontextasexecutedoes, and also answers the value of its last expression statement: what its lastpopdiscarded, or undefined when it popped nothing (Execution::value). - parse_
duration - Reads a duration from its literal spelling, such as
"3d8h30m"or"1.5s": the spelling a::durationcast reads and a::stringcast writes.