test expr parsing

This commit is contained in:
Alain Emilia Anna Zscheile 2024-07-23 13:05:08 +02:00
parent 57803002fc
commit 1ee85e4523

View file

@ -12,7 +12,7 @@ use yz_string_utils::StrLexerBase;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Link(pub u16, pub Box<[u16]>);
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub enum Expr<L> {
Const(f64),
Link(L),
@ -56,7 +56,7 @@ impl<L: Clone + Eq + Ord> Expr<L> {
/// A system of PDEs and monitors (outputs)
///
/// LHS is always the first derivative to time (it is not possible to refer to time derivatives on RHS)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct System {
pub vs: Vec<Expr<Link>>,
@ -97,7 +97,7 @@ impl System {
}
}
#[derive(Clone, Debug, thiserror::Error)]
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
pub enum ParseError {
#[error("unexpected end of expression")]
EmptyExpr,
@ -151,6 +151,9 @@ impl ParseLink for Link {
} else {
let mut new_slb = (*slb).clone();
let var = new_slb.consume_select(|i| i.is_ascii_digit());
if var.is_empty() {
return Ok(None);
}
let var = var.parse::<u16>()?;
if !new_slb.inp.starts_with('_') {
return Ok(None);
@ -254,7 +257,7 @@ fn parse_expr_final<L: Clone + ParseLink>(slb: &mut StrLexerBase) -> Result<Expr
```
%vs
# u_t + 0.11 u_x = 0
0; -0.11 0_0_
0; prod(-0.11, 0_0_)
%mons
# monitor u @ 1 + sin(t)
@ -349,3 +352,31 @@ pub fn parse_system(s: &str) -> Result<System, ((usize, usize), ParseError)> {
Ok(ret)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example0_fail() {
assert_eq!(parse_system("
%vs
# u_t + 0.11 u_x = 0
0; -0.11 0_0_
"), Err(((3, 8), ParseError::InvalidTrail)));
}
#[test]
fn example0() {
println!("{:?}", parse_system("
%vs
# u_t + 0.11 u_x = 0
0; prod(-0.11, 0_0_)
%mons
# monitor u @ 1 + sin(t)
0_; sum(1.0, sin(.))
").unwrap());
}
}