//! Texlang variables API //! //! The documentation website contains //! [a tutorial specifically about Texlang's variables API](https://texcraft.dev/texlang/07-variables.html). //! See that documentation for an overview of the API. //! As usual, the documentation here is meant as reference. use crate::command; use crate::error; use crate::parse::OptionalEquals; use crate::prelude as txl; use crate::token; use crate::traits::*; use crate::types; use crate::vm; use common::font; use std::borrow::Cow; use std::collections::HashMap; use std::fmt::Debug; use std::hash::{Hash, Hasher}; use texcraft_stdext::collections::groupingmap; /// Function signature for a variable's immutable getter. /// /// In Texcraft all [Variable]s are built from an immutable getter and a mutable getter. /// This type alias just defines the signature of immutable getters. /// The first argument is the state, and the second argument is the index of the variable. pub type RefFn = fn(state: &S, index: Index) -> &T; /// Function signature for a variable's mutable getters. /// /// In Texcraft all [Variable]s are built from an immutable getter and a mutable getter. /// This type alias just defines the signature of mutable getters. /// The first argument is the state, and the second argument is the index of the variable. pub type MutRefFn = fn(state: &mut S, index: Index) -> &mut T; /// Index of a variable within an array. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)] pub struct Index(pub usize); impl From for Index { fn from(value: usize) -> Self { Index(value) } } /// Specification for how the index of an array variable is determined. /// /// Obtaining a variable from a command involves determining the variable's [Index]. /// This index is ultimately passed into the variable's getters to get a reference or mutable reference /// to the underlying Rust value. pub enum IndexResolver { /// A static index, provided in the enum variant. /// /// This resolver is used for commands that point to a specific entry in a array. /// For example, after executing `\countdef\A 30`, the `\A` control sequence points /// at the count register with index 30. /// The command backing `\A` uses a static resolver with index 30. Static(Index), /// A dynamic index that is determined by reading the input token stream. /// /// For example, in `\count 4` the index of `4` is determined by parsing a number /// from the input token stream. Dynamic(fn(token::Token, &mut vm::ExpandedStream) -> txl::Result), } impl IndexResolver { /// Determine the index of a variable using the input token stream. fn resolve( &self, token: token::Token, input: &mut vm::ExpandedStream, ) -> txl::Result { match self { IndexResolver::Static(addr) => Ok(*addr), // TODO: we should add support for propagation information here. // We need to add to the execution stack IndexResolver::Dynamic(f) => f(token, input), } } } /// A TeX variable command. /// /// Variable commands are _resolved_ to obtain a [Variable]. /// /// A command consists of three parts. /// /// The first two parts are an immutable getter (of type [RefFn]) /// and a mutable getter (of type [MutRefFn]). /// Both of these getters accept a reference to the state (where the variable's value lives) /// and an [Index]. /// The index can be used by the getters to return different values in memory. /// For example, if the getters read from an array, the index may be just the index in the array. /// This mechanism allows users of Texlang to write one pair of getters that can be used in many variables. /// /// The third part of a command is an [IndexResolver] that is used to determine the aforementioned index /// of a variable at runtime. /// The process of resolving a command involves determining the [Index] and returning a [Variable] type, /// which under the hood is just the two getters and this index. pub struct Command { getters: Getters, index_resolver: Option>, use_vm_save_stack: bool, } impl Command { /// Create a new variable command. pub fn new_singleton( ref_fn: RefFn, ref_mut_fn: MutRefFn, ) -> Command { SupportedType::new_command(ref_fn, ref_mut_fn, None) } /// Create a new variable command. pub fn new_array( ref_fn: RefFn, ref_mut_fn: MutRefFn, index_resolver: IndexResolver, ) -> Command { SupportedType::new_command(ref_fn, ref_mut_fn, Some(index_resolver)) } /// Create a singleton variable command whose grouping is managed by the state getters. /// /// The mutable getter must record the previous value and restore it when the corresponding /// group ends. The VM will not add assignments to its own save stack. pub fn new_singleton_with_external_grouping( ref_fn: RefFn, ref_mut_fn: MutRefFn, ) -> Command { let mut command = Self::new_singleton(ref_fn, ref_mut_fn); command.use_vm_save_stack = false; command } /// Create an array variable command whose grouping is managed by the state getters. /// /// The mutable getter must record the previous value and restore it when the corresponding /// group ends. The VM will not add assignments to its own save stack. pub fn new_array_with_external_grouping( ref_fn: RefFn, ref_mut_fn: MutRefFn, index_resolver: IndexResolver, ) -> Command { let mut command = Self::new_array(ref_fn, ref_mut_fn, index_resolver); command.use_vm_save_stack = false; command } /// Create a new getter provider. /// /// A getter provider is a variable command that is not intended to be invoked directly - /// in fact, the variable command will panic the program if it is invoked. /// Instead the provider is included in a VM's initial commands so that /// the VM has a reference to the getters inside the command. /// If a variable with the same getters is subsequently inserted into the commands map /// (for example, by a `\countdef` type command), the VM can still serialize that /// variable using the getters provided here. pub fn new_getter_provider( ref_fn: RefFn, ref_mut_fn: MutRefFn, ) -> Command { SupportedType::new_command( ref_fn, ref_mut_fn, Some(IndexResolver::Dynamic(|_, _| panic!())), ) } /// Create a command that points to a specific element in the array referenced by this command. pub(crate) fn new_array_element(&self, index: Index) -> Self { Self { getters: self.getters.clone(), index_resolver: Some(IndexResolver::Static(index)), use_vm_save_stack: self.use_vm_save_stack, } } /// Returns true if the variable command corresponds to arithmetic variable(s) (e.g. integer or glue). /// /// TODO: we should just expose the type. pub fn is_arithmetic(&self) -> bool { match self.getters { Getters::Int(_, _) | Getters::SmallInt(_, _) | Getters::Dimen(_, _) | Getters::Glue(_, _) | Getters::MuGlue(_, _) => true, Getters::CatCode(_, _) | Getters::MathCode(_, _) | Getters::TokenList(_, _) | Getters::Font(_, _) => false, } } /// Returns the static register index when this command points at one. pub fn static_index(&self) -> Option { match &self.index_resolver { Some(IndexResolver::Static(index)) => Some(index.0), _ => None, } } /// Returns true when this command reads and writes glue. pub fn is_glue(&self) -> bool { matches!(self.getters, Getters::Glue(_, _)) } } impl Command { /// Resolve the command to obtain a [Variable]. pub fn resolve( &self, token: token::Token, input: &mut vm::ExpandedStream, ) -> txl::Result> { let index = match &self.index_resolver { None => Index(0), Some(index_resolver) => { input .vm_mut() .stack_push(token, error::OperationKind::VariableIndex); let err_or = index_resolver.resolve(token, input); input.vm_mut().stack_pop(); err_or? } }; Ok(new_variable(&self.getters, index, self.use_vm_save_stack)) } /// Resolve the command to obtain a [Variable] of a specific type. pub fn resolve_type( &self, token: token::Token, input: &mut vm::ExpandedStream, ) -> txl::Result>> { let Some((ref_fn, ref_mut_fn)) = T::try_cast(self) else { return Ok(None); }; let index = match &self.index_resolver { None => Index(0), Some(index_resolver) => index_resolver.resolve(token, input)?, }; Ok(Some(TypedVariable { ref_fn, ref_mut_fn, index, use_vm_save_stack: self.use_vm_save_stack, })) } } impl Command { pub(crate) fn key(&self) -> CommandKey { let getters_key = self.getters.key(); match &self.index_resolver { None => CommandKey::Singleton(getters_key, self.use_vm_save_stack), Some(index_resolver) => match index_resolver { IndexResolver::Static(a) => { CommandKey::ArrayStatic(getters_key, *a, self.use_vm_save_stack) } IndexResolver::Dynamic(f) => { CommandKey::ArrayDynamic(getters_key, *f as usize, self.use_vm_save_stack) } }, } } } impl Command { /// Resolve the command to a variable and return the value of the variable. pub fn value<'a>( &self, token: token::Token, input: &'a mut vm::ExpandedStream, ) -> txl::Result> { Ok(self.resolve(token, input)?.value(input.state())) } /// Resolve the command to a variable and set the value of the variable using the following tokens in the input stream. /// /// This function is used in TeX code like `\variable = 3`. /// In this case `\variable` is a command which resolves to a variable without consuming any more input. /// The variable is populated using the input `= 3` that follows. pub(crate) fn set_value_using_input( &self, token: token::Token, input: &mut vm::ExecutionInput, scope: groupingmap::Scope, ) -> txl::Result<()> { let variable = self.resolve(token, input.as_mut())?; input .vm_mut() .stack_push(token, error::OperationKind::VariableAssignment); let err_or = variable.set_value_using_input(input, scope); input.vm_mut().stack_pop(); err_or } } /// A key that uniquely identifies commands. If two commands have the same command key, they are the same command. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub(crate) enum CommandKey { Singleton(GettersKey, bool), ArrayStatic(GettersKey, Index, bool), ArrayDynamic(GettersKey, usize, bool), } impl CommandKey { pub(crate) fn getter_key(&self) -> GettersKey { match self { CommandKey::Singleton(k, _) => *k, CommandKey::ArrayStatic(k, _, _) => *k, CommandKey::ArrayDynamic(k, _, _) => *k, } } } /// A key that uniquely identifies the getters ([RefFn] and [MutRefFn]) in a command or variable. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub(crate) struct GettersKey(usize, usize); /// A TeX variable of a specific Rust type `T`. pub struct TypedVariable { ref_fn: RefFn, ref_mut_fn: MutRefFn, index: Index, use_vm_save_stack: bool, } impl Copy for TypedVariable {} impl Clone for TypedVariable { fn clone(&self) -> Self { *self } } impl TypedVariable { /// Returns an immutable reference to the variable's value. pub fn get<'a>(&self, state: &'a S) -> &'a T { (self.ref_fn)(state, self.index) } fn key(&self) -> (usize, usize, Index, bool) { ( self.ref_fn as usize, self.ref_mut_fn as usize, self.index, self.use_vm_save_stack, ) } } impl TypedVariable where S: TexlangState, T: SupportedType, { /// Sets the value of the variable. /// /// The input and scope must be passed because of TeX's grouping semantics. /// When the current group ends, any variable assignments made in the group /// are rolled back. /// Thus this function generally saves the current value of the variable in the VM so that it /// can be restored later. /// This is why the full input must be provided, and not just the state. pub fn set(&self, input: &mut vm::ExecutionInput, scope: groupingmap::Scope, value: T) { let r: &mut T = (self.ref_mut_fn)(input.state_mut(), self.index); let overwritten_value = std::mem::replace(r, value); // We guard the function call behind this conditional to make the current function small // to make it beneficial to inline it. if self.use_vm_save_stack && !input.groups().is_empty() { SupportedType::update_save_stack(input, self, scope, overwritten_value); } else { SupportedType::recycle(input, overwritten_value); } } } impl TypedVariable where S: TexlangState, T: SupportedType, { fn set_using_input( &self, input: &mut vm::ExecutionInput, scope: groupingmap::Scope, ) -> txl::Result<()> { let (_, value) = <(OptionalEquals, T)>::parse(input)?; self.set(input, scope, value); Ok(()) } } impl PartialEq for TypedVariable { fn eq(&self, rhs: &TypedVariable) -> bool { self.key() == rhs.key() } } impl Eq for TypedVariable {} impl Hash for TypedVariable { fn hash(&self, state: &mut H) where H: Hasher, { self.key().hash(state); } } /// Trait satisfied by all Rust types that can be used as TeX variables. /// /// It exists to make the variables API more ergonomic. /// For example, it is used to provide a uniform constructor [Command::new_array] for commands. /// The trait cannot be implemented for new types. pub trait SupportedType: Sized + Debug + Parsable { /// Create a new command of this type with the provided reference functions and index resolver. fn new_command( ref_fn: RefFn, ref_mut_fn: MutRefFn, index_resolver: Option>, ) -> Command; /// Update the VM's save stack after a variable assignment. fn update_save_stack( input: &mut vm::ExecutionInput, variable: &TypedVariable, scope: groupingmap::Scope, overwritten_value: Self, ) { (_, _, _, _) = (input, variable, scope, overwritten_value); } /// Recycle a value that's about to be dropped. /// /// This method is intended for token lists, in which case the vector is returned /// to the buffers pool. fn recycle(input: &mut vm::ExecutionInput, overwritten_value: Self) { (_, _) = (input, overwritten_value) } /// Create a new typed variable of this type from the getters in the command and the provided index. /// /// Return `None` if the command has a different type to `Self`. fn new_typed_variable(command: &Command, index: Index) -> Option>; /// Try to cast a variable command to this type. fn try_cast(command: &Command) -> Option<(RefFn, MutRefFn)>; } /// This function is used to implement the [SupportedType::update_save_stack] method. /// /// It's a bit janky to implement the logic as a free function, rather than, say, as /// a default trait implementation. /// The main problem is that the implementation calls into the save stack, which is an /// internal VM data structure. /// Any way that does this on the trait directly requires making some of the save stack /// type public, because [SupportedType] is public. fn update_save_stack( input: &mut vm::ExecutionInput, variable: &TypedVariable, scope: groupingmap::Scope, overwritten_value: T, map_getter: F, ) where F: Fn(&mut SaveStackElement) -> &mut SaveStackMap, { match scope { groupingmap::Scope::Global => { let n = input.groups().len(); for _ in 0..n { let group = &mut input.groups()[0]; if let Some(stale_value) = map_getter(group).remove(variable) { SupportedType::recycle(input, stale_value); } } } groupingmap::Scope::Local => { if let Some((group, _)) = input.current_group_mut() { if let Some(stale_value) = map_getter(group).save(*variable, overwritten_value) { SupportedType::recycle(input, stale_value); } } } } } macro_rules! supported_type_impl { ( $( { rust_type: $rust_type: path, enum_variant: $enum_variant: ident, $( save_stack_field: $save_stack_field: ident, )? $( recycle_fn: $recycle_fn: ident, )? }, )+ ) => { /// Immutable reference to the value of a variable. pub enum ValueRef<'a> { $( $enum_variant(&'a $rust_type), )+ } /// TeX variable of any type. /// /// A variable uniquely identifies a Rust value in the state, like an `i32`. /// Operations on this value (like reading or setting the value) can be done in two ways: /// /// 1. (Easy, less flexible) Use the methods directly on this type like [Variable::value] /// to read the value. /// These methods are really ergonomic. /// The problem with the value method specifically is that the result /// is a reference which keeps the borrow of the state alive. /// Thus, while holding onto the result of the value, you can't do anything this the /// input stream like reading an argument. /// This is especially a problem when you need to perform a different action depending on the concrete type of the variable. /// /// 2. (Trickier, more flexible) Match on the type's enum variants to determine the /// concrete type of the variable. /// The [TypedVariable] value obtained in this way can be used to perform operations on the value. /// The main benefit of this approach is that after matching on the type, you can still use the input /// stream to do things because there is not borrow alive. /// pub enum Variable { $( $enum_variant(TypedVariable), )+ } fn new_variable(getters: &Getters, index: Index, use_vm_save_stack: bool) -> Variable { match getters { $( Getters::$enum_variant(a, b) => Variable::$enum_variant(TypedVariable { ref_fn: *a, ref_mut_fn: *b, index, use_vm_save_stack, }), )+ } } impl Variable { /// Return a reference to the value of the variable. pub fn value<'a>(&self, state: &'a S) -> ValueRef<'a> { match self { $( Variable::$enum_variant(variable) => ValueRef::$enum_variant(variable.get(state)), )+ } } /// Set the value of a variable using the following tokens in the input stream. fn set_value_using_input( &self, input: &mut vm::ExecutionInput, scope: groupingmap::Scope, ) -> txl::Result<()> { match self { $( Variable::$enum_variant(variable) => variable.set_using_input(input, scope), )+ } } } enum Getters { $( $enum_variant(RefFn, MutRefFn), )+ } impl Clone for Getters { fn clone(&self) -> Self { match self { $( Self::$enum_variant(a, b) => Self::$enum_variant(*a, *b), )+ } } } impl Getters { fn key(&self) -> GettersKey { match self { $( Getters::$enum_variant(a, b) => GettersKey(*a as usize, *b as usize), )+ } } } $( impl SupportedType for $rust_type { fn new_command( ref_fn: RefFn, ref_mut_fn: MutRefFn, index_resolver: Option>, ) -> Command { Command { getters: Getters::$enum_variant(ref_fn, ref_mut_fn), index_resolver, use_vm_save_stack: true, } } $( fn update_save_stack( input: &mut vm::ExecutionInput, variable: &TypedVariable, scope: groupingmap::Scope, overwritten_value: Self, ) { update_save_stack(input, variable, scope, overwritten_value, |element| { &mut element.$save_stack_field }) } )? $( fn recycle(input: &mut vm::ExecutionInput, overwritten_value: Self) { $recycle_fn(input, overwritten_value) } )? fn new_typed_variable( command: &Command, index: Index, ) -> Option> { match command.getters { Getters::$enum_variant(a, b) => Some(TypedVariable { ref_fn: a, ref_mut_fn: b, index, use_vm_save_stack: command.use_vm_save_stack, }), _ => None, } } fn try_cast(command: &Command) -> Option<(RefFn, MutRefFn)> { match command.getters { Getters::$enum_variant(a, b) => Some((a,b)), _ => None, } } } )+ /// Internal VM data structure used to implement TeX's grouping semantics. pub(crate) struct SaveStackElement { $( $( $save_stack_field: SaveStackMap, )? )+ } impl Default for SaveStackElement { fn default() -> Self { Self { $( $( $save_stack_field: Default::default(), )? )+ } } } impl SaveStackElement { pub(crate) fn restore(self, input: &mut vm::ExecutionInput) { $( $( self.$save_stack_field.restore(input); )? )+ } pub(crate) fn serializable<'a>( &'a self, built_ins: &HashMap, ) -> SerializableSaveStackElement<'a> { SerializableSaveStackElement { $( $( $save_stack_field: self.$save_stack_field.serializable(built_ins), )? )+ } } } #[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))] pub(crate) struct SerializableSaveStackElement<'a> { $( $( $save_stack_field: Vec<(token::CsName, usize, Cow<'a, $rust_type>)>, )? )+ } impl<'a> SerializableSaveStackElement<'a> { pub(crate) fn finish_deserialization( self, built_ins: &HashMap>, ) -> SaveStackElement { SaveStackElement { $( $( $save_stack_field: SaveStackMap::from_deserialized(self.$save_stack_field, built_ins), )? )+ } } } }; } supported_type_impl!( { rust_type: i32, enum_variant: Int, save_stack_field: i32, }, { rust_type: u8, enum_variant: SmallInt, save_stack_field: u8, }, { rust_type: common::Scaled, enum_variant: Dimen, save_stack_field: dimen, }, { rust_type: common::Glue, enum_variant: Glue, save_stack_field: glue, }, { rust_type: types::MuGlue, enum_variant: MuGlue, save_stack_field: mu_glue, }, { rust_type: types::CatCode, enum_variant: CatCode, save_stack_field: catcode, }, { rust_type: types::MathCode, enum_variant: MathCode, save_stack_field: math_code, }, { rust_type: Vec, enum_variant: TokenList, save_stack_field: token_list, recycle_fn: recycle_token_list, }, { rust_type: font::Id, enum_variant: Font, save_stack_field: font, }, // { // rust_type: FontParam, // enum_variant: FontParam, // // no save_stack_field because font params are global // } ); fn recycle_token_list(input: &mut vm::ExecutionInput, overwritten_value: Vec) { input.return_token_buffer(overwritten_value); } /// Internal VM data structure used to implement TeX's grouping semantics. struct SaveStackMap(HashMap, T>); impl Default for SaveStackMap { fn default() -> Self { Self(HashMap::new()) } } impl SaveStackMap { fn save(&mut self, variable: TypedVariable, value: T) -> Option { match self.0.entry(variable) { std::collections::hash_map::Entry::Occupied(_) => Some(value), std::collections::hash_map::Entry::Vacant(v) => { v.insert(value); None } } } fn remove(&mut self, variable: &TypedVariable) -> Option { self.0.remove(variable) } fn restore(self, input: &mut vm::ExecutionInput) { for (v, restored_value) in self.0 { let dest = (v.ref_mut_fn)(input.state_mut(), v.index); let overwritten_value = std::mem::replace(dest, restored_value); SupportedType::recycle(input, overwritten_value); } } fn serializable<'a>( &'a self, built_ins: &HashMap, ) -> Vec<(token::CsName, usize, Cow<'a, T>)> { self.0 .iter() .map(|(typed_variable, value): (&TypedVariable, &T)| { let key = GettersKey( typed_variable.ref_fn as usize, typed_variable.ref_mut_fn as usize, ); let cs_name = built_ins.get(&key).unwrap(); (*cs_name, typed_variable.index.0, Cow::Borrowed(value)) }) .collect() } } impl SaveStackMap { fn from_deserialized<'a>( deserialized: Vec<(token::CsName, usize, Cow<'a, T>)>, built_ins: &HashMap>, ) -> Self { let m = deserialized .into_iter() .map( |(cs_name, index, value): (token::CsName, usize, Cow<'a, T>)| { // TODO: surface error here let built_in = built_ins.get(&cs_name).unwrap(); let typed_variable = match built_in.cmd() { command::Command::Variable(variable_command) => { // TODO: error instead of unwrap SupportedType::new_typed_variable(variable_command, Index(index)) .unwrap() } _ => panic!("wrong type of built in TODO return an error here"), }; (typed_variable, value.into_owned()) }, ) .collect(); Self(m) } }