mirror of
https://github.com/typst/typst
synced 2025-05-15 09:35:28 +08:00
Supports: - Closure syntax: `(x, y) => z` - Shorthand for a single argument: `x => y` - Function syntax: `let f(x) = y` - Capturing of variables from the environment - Error messages for too few / many passed arguments Does not support: - Named arguments - Variadic arguments with `..`
119 lines
3.1 KiB
Rust
119 lines
3.1 KiB
Rust
use std::cell::RefCell;
|
|
use std::collections::HashMap;
|
|
use std::fmt::{self, Debug, Formatter};
|
|
use std::iter;
|
|
use std::rc::Rc;
|
|
|
|
use super::Value;
|
|
|
|
/// A slot where a variable is stored.
|
|
pub type Slot = Rc<RefCell<Value>>;
|
|
|
|
/// A stack of scopes.
|
|
#[derive(Debug, Default, Clone, PartialEq)]
|
|
pub struct Scopes<'a> {
|
|
/// The active scope.
|
|
pub top: Scope,
|
|
/// The stack of lower scopes.
|
|
pub scopes: Vec<Scope>,
|
|
/// The base scope.
|
|
pub base: Option<&'a Scope>,
|
|
}
|
|
|
|
impl<'a> Scopes<'a> {
|
|
/// Create a new, empty hierarchy of scopes.
|
|
pub fn new() -> Self {
|
|
Self {
|
|
top: Scope::new(),
|
|
scopes: vec![],
|
|
base: None,
|
|
}
|
|
}
|
|
|
|
/// Create a new hierarchy of scopes with a base scope.
|
|
pub fn with_base(base: &'a Scope) -> Self {
|
|
Self {
|
|
top: Scope::new(),
|
|
scopes: vec![],
|
|
base: Some(base),
|
|
}
|
|
}
|
|
|
|
/// Enter a new scope.
|
|
pub fn enter(&mut self) {
|
|
self.scopes.push(std::mem::take(&mut self.top));
|
|
}
|
|
|
|
/// Exit the topmost scope.
|
|
///
|
|
/// # Panics
|
|
/// Panics if no scope was entered.
|
|
pub fn exit(&mut self) {
|
|
self.top = self.scopes.pop().expect("no pushed scope");
|
|
}
|
|
|
|
/// Define a constant variable in the active scope.
|
|
pub fn def_const(&mut self, var: impl Into<String>, value: impl Into<Value>) {
|
|
self.top.def_const(var, value);
|
|
}
|
|
|
|
/// Define a mutable variable in the active scope.
|
|
pub fn def_mut(&mut self, var: impl Into<String>, value: impl Into<Value>) {
|
|
self.top.def_mut(var, value);
|
|
}
|
|
|
|
/// Look up the slot of a variable.
|
|
pub fn get(&self, var: &str) -> Option<&Slot> {
|
|
iter::once(&self.top)
|
|
.chain(self.scopes.iter().rev())
|
|
.chain(self.base.into_iter())
|
|
.find_map(|scope| scope.get(var))
|
|
}
|
|
}
|
|
|
|
/// A map from variable names to variable slots.
|
|
#[derive(Default, Clone, PartialEq)]
|
|
pub struct Scope {
|
|
/// The mapping from names to slots.
|
|
values: HashMap<String, Slot>,
|
|
}
|
|
|
|
impl Scope {
|
|
/// Create a new empty scope.
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// Define a constant variable.
|
|
pub fn def_const(&mut self, var: impl Into<String>, value: impl Into<Value>) {
|
|
let cell = RefCell::new(value.into());
|
|
|
|
// Make it impossible to write to this value again.
|
|
// FIXME: Use Ref::leak once stable.
|
|
std::mem::forget(cell.borrow());
|
|
|
|
self.values.insert(var.into(), Rc::new(cell));
|
|
}
|
|
|
|
/// Define a mutable variable.
|
|
pub fn def_mut(&mut self, var: impl Into<String>, value: impl Into<Value>) {
|
|
self.values.insert(var.into(), Rc::new(RefCell::new(value.into())));
|
|
}
|
|
|
|
/// Define a variable with a slot.
|
|
pub fn def_slot(&mut self, var: impl Into<String>, slot: Slot) {
|
|
self.values.insert(var.into(), slot);
|
|
}
|
|
|
|
/// Look up the value of a variable.
|
|
pub fn get(&self, var: &str) -> Option<&Slot> {
|
|
self.values.get(var)
|
|
}
|
|
}
|
|
|
|
impl Debug for Scope {
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
self.values.fmt(f)
|
|
}
|
|
}
|