pub mod cmd;
pub mod model;
pub mod resolve;
pub use resolve::{Emerge, Resolve};
use std::ops::Deref;
use crate::{
Map, Name,
aux::NotOrd,
error,
ext::{business::Value, shop::Gtin},
runtime::{
cmd::{Output, Run},
model::{Concept, Entity, Object, Pair},
},
syntax::ast::Script,
};
#[derive(NotOrd!, Default)]
pub struct Runtime {
state: State,
}
impl Runtime {
#[must_use]
pub fn state(&self) -> &State {
&self.state
}
#[must_use]
pub fn to_state(self) -> State {
self.state
}
}
impl Runtime {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn run(&mut self, script: Script) -> Result<(), error::Construction> {
for stmt in script.0 {
let cmd = cmd::construct(stmt, &self.state)?;
if let Some(msg) = self.step(cmd) {
println!("{msg}");
}
}
Ok(())
}
pub fn step(&mut self, cmd: Box<dyn Run>) -> Output {
cmd.run(&mut self.state)
}
}
impl Deref for Runtime {
type Target = State;
fn deref(&self) -> &Self::Target {
&self.state
}
}
#[derive(NotOrd!, Default)]
pub struct State {
pub entities: Map<Name, Entity>,
pub concepts: Map<Name, Concept>,
pub concepts_gtin: Map<Gtin, Concept>,
pub objects: Map<Name, Object>,
pub balances: Map<Pair, Value>,
}