Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions libraries/math-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum UnaryOp {
}

/// The tree as written, before each subexpression's sort is read from its spelling.
#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub enum Syntax {
Lit(Literal),
Var(String),
Expand Down Expand Up @@ -111,15 +111,40 @@ pub enum Syntax {
from: Box<Syntax>,
to: Box<Syntax>,
},
/// An expression with the names its `where` clause defines, like `a + f(2) where a = 1, f(t) = t^2`.
Where {
body: Box<Syntax>,
bindings: Vec<Binding>,
},
/// A call whose parentheses end with a `where` clause, boxed so a tree's every node stays small.
CallWhere(Box<CallWhere>),
}

/// A call whose parentheses end with a `where` clause, like `max(a, b where a = 1)`, whose names every argument may read but
/// the function's name, outside the parentheses, can't.
#[derive(Debug, Clone, PartialEq)]
pub struct CallWhere {
pub name: String,
pub arguments: Vec<Syntax>,
pub bindings: Vec<Binding>,
}

/// One case of a piecewise, the value it takes where its condition holds.
#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub struct Case {
pub value: Syntax,
pub condition: Syntax,
}

/// One definition in a `where` clause: a value like `a = 1`, or a function like `f(t) = t^2`.
#[derive(Debug, Clone, PartialEq)]
pub struct Binding {
pub name: String,
/// The function's parameters, of which a value has none.
pub parameters: Vec<String>,
pub value: Syntax,
}

/// A parsed expression, whose every subexpression has the sort its spelling fixes: a value or a matrix.
#[derive(Debug)]
pub enum Node {
Expand Down Expand Up @@ -174,6 +199,18 @@ pub enum ValueNode {
matrices: Vec<MatrixNode>,
distinct: bool,
},
/// A value a `where` clause defines, or a parameter of the function being called.
Local(Local),
/// A call of a function a `where` clause defines.
Call {
function: Local,
arguments: Vec<Node>,
},
/// An expression within the names its `where` clause defines.
Where {
clause: Box<Clause>,
body: Box<ValueNode>,
},
}

/// A subexpression evaluating to a matrix.
Expand Down Expand Up @@ -214,6 +251,18 @@ pub enum MatrixNode {
cases: Vec<SortedCase<MatrixNode>>,
otherwise: Option<Box<MatrixNode>>,
},
/// A matrix a `where` clause defines, or a parameter of the function being called.
Local(Local),
/// A call of a function a `where` clause defines.
Call {
function: Local,
arguments: Vec<Node>,
},
/// An expression within the names its `where` clause defines.
Where {
clause: Box<Clause>,
body: Box<MatrixNode>,
},
}

/// One case of a sorted piecewise, whose condition is a value whatever the sort of its cases.
Expand All @@ -222,3 +271,18 @@ pub struct SortedCase<T> {
pub value: T,
pub condition: ValueNode,
}

/// Where a name a `where` clause or a function's parameters define lives: its scope, counted outward from the innermost, and
/// its position among that scope's values or functions.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Local {
pub depth: usize,
pub index: usize,
}

/// A sorted `where` clause: each value's definition, and each function's body over its parameters.
#[derive(Debug)]
pub struct Clause {
pub values: Vec<Node>,
pub functions: Vec<Node>,
}
Loading
Loading