Skip to main content

yomi_core/
lib.rs

1//! The core of the yomi language.
2//!
3//! Has the parser, type checker, VM, saves and the package mechanism. Uses no `unsafe` (`forbid(unsafe_code)`).
4//!
5//! How to run a script (`spec/host/01-execution.md`):
6//! 1. Register stop commands in a [`Registry`] and bind the sugar. Register packages ([`Package`]) with
7//!    [`Registry::package`] (or [`Registry::package_set`] and [`packages!`] to register a set in any order): effect and
8//!    query commands, state slices, and host-declared variables.
9//! 2. Turn the source into a [`Program`] with [`compile()`](compile()) (diagnostics are [`Diagnostic`]). To receive
10//!    warnings as well, use [`compile_with_warnings`]. For multiple modules (files that refer to each other with
11//!    `import`), the host collects the sources and calls [`compile_modules`] (with warnings: [`compile_modules_with_warnings`]).
12//! 3. Create a VM with [`Vm::new`] and repeat [`Vm::run`] and [`Vm::resume`].
13//! 4. At a stop point, make a save with [`Vm::save`] and restore it with [`Vm::load`]. `persistent var`s and passed stop
14//!    points are persistent data in a separate byte string ([`Vm::save_persistent`], [`Vm::load_persistent`]).
15//! 5. For rollback, store [`Vm::snapshot`]s in a [`SnapshotLog`] at each stop point and go back with [`Vm::restore`].
16//! 6. Host `struct`s and `enum`s get `#[derive(YomiType)]` and are registered with [`Registry::ty`]. A registry can be
17//!    written to a declaration file ([`Registry::export_decls`], [`Registry::from_decls`]), so tools can check scripts
18//!    without the host's Rust code.
19//! 7. For localization, pass a [`TextTable`] (a table that replaces texts, keyed by stable IDs) with [`Vm::set_texts`].
20//!    To switch languages while stopped, use [`Vm::refresh_current`]; to render the history in the current language, use
21//!    [`Vm::render`] and [`Vm::history_text`]. Check a table with [`TextTable::check`], and require IDs with
22//!    [`CompileOptions::require_text_ids`] (`spec/host/05-texts.md`).
23
24#![forbid(unsafe_code)]
25
26extern crate self as yomi_core;
27
28mod analysis;
29mod assign_ids;
30mod backend;
31mod codes;
32mod compile;
33mod decls;
34mod diag;
35mod explore;
36mod format;
37mod ident;
38mod ir;
39mod package;
40mod pool;
41mod program;
42mod registry;
43mod repl;
44mod schema_file;
45mod suggest;
46mod syntax;
47mod texts;
48mod type_expand;
49mod value;
50mod vm;
51
52pub use analysis::{Analysis, Reference, Span, Symbol, SymbolKind, analyze, is_name_char};
53pub use assign_ids::{
54    AUTO_ID_LEN, IdRng, Insertion, MissingId, apply as apply_insertions, plan as plan_ids,
55    plan_program as plan_ids_program,
56};
57pub use codes::{CodeGroup, CodeInfo, ErrorCodeInfo, all_codes, core_command_error_codes};
58pub use compile::{
59    CompileOptions, LocalType, compile, compile_modules, compile_modules_with_options,
60    compile_modules_with_warnings, compile_with_warnings, infer_local_types,
61};
62pub use decls::{DeclError, DeclErrorKind, MAX_DECLS_BYTES, MAX_JSON_DEPTH};
63pub use diag::{DiagArg, DiagCode, Diagnostic, Severity};
64pub use explore::{
65    DEFAULT_MAX_STATES, ErrorPath, ExploreConfig, ExploreReport, ExploreStep, StuckPath, TestPath,
66    explore,
67};
68pub use format::{FormatError, format_source};
69pub use package::{
70    CommandError, CommandErrorKind, Ctx, EffectFn, IntoReturn, MigrateError, Package, PackageReg,
71    QueryFn, decode_slice, encode_slice,
72};
73pub use program::{Program, StopKind};
74pub use registry::{
75    CommandDecl, CommandId, CommandKind, FromArgs, FromValue, IntoValue, Kind, PackageDecl,
76    PackageItem, Registry, RegistryError, StopDecl, Str, Sugar, VarDecl, VarHandle, YomiArgs,
77    YomiDecl, YomiMap, YomiType,
78};
79pub use repl::{
80    Applied, ApplyError, ApplyInvalid, Completeness, Declared, Def, InputKind, Name, Repl,
81    ReplYield, Submitted, completeness,
82};
83pub use schema_file::{ExtractError, extract_schema, frozen_schema, migrate_template};
84pub use syntax::keywords::{
85    CONTEXTUAL_KEYWORDS, ContextualKeyword, ContextualPosition, KEYWORDS, Keyword, KeywordKind,
86    is_reserved, keyword,
87};
88pub use texts::{TextError, TextErrorKind, TextIssue, TextIssueKind, TextTable};
89pub use value::{
90    EnumValue, FnValue, Identity, ListData, MAX_TYPE_DEPTH, MapData, MapEntries, StrValue,
91    StructValue, Ty, TypeDecl, TypeKind, Value, VariantDecl, find_type,
92};
93pub use vm::{
94    CueRef, DEFAULT_HISTORY_LIMIT, DEFAULT_SEED, DebugHook, DebugYield, Debugger, FrameInfo,
95    HookAction, Input, InterruptHandle, LineEvent, LoadError, LoadErrorKind, LoadPolicy,
96    LoadReport, PauseHandle, PendingStop, PersistentReport, ReloadError, ResolvedBy, Rng, Snapshot,
97    SnapshotLog, StepMode, StopRecord, UnitKind, Variable, Vm, VmConfig, VmError, VmErrorKind,
98    Yield, pending_stop,
99};
100pub use yomi_derive::YomiType;
101
102/// The version of the core (the `version` in `Cargo.toml`).
103pub fn version() -> &'static str {
104    env!("CARGO_PKG_VERSION")
105}
106
107#[cfg(test)]
108mod tests;