1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// This file is deprecated - the real CalcParser is now in src/dna/atp/ops.rs
// Keeping this file for reference but it's no longer used
/*
use pest::Parser;
use pest::iterators::{Pair, Pairs};
use anyhow::{anyhow, Result};
use crate::ops::math::Expr;
#[derive(pest_derive::Parser)]
#[grammar = "ulator.pest"]
pub struct CalcParser;
// Re-export the Rule enum for use by other modules
pub use self::Rule;
pub fn parse_expr(pair: Pair<Rule>) -> Result<Expr> {
match pair.as_rule() {
Rule::number => {
let n: i64 = pair.as_str().parse()?;
Ok(Expr::Number(n))
}
Rule::identifier => Ok(Expr::Var(pair.as_str().to_string())),
Rule::reference => {
let mut inner = pair.into_inner();
let var = inner.next().unwrap().as_str().to_string();
let modifier = inner.next().unwrap().as_str().parse::<i64>()?;
Ok(Expr::Ref { var, modifier })
}
Rule::factor => parse_expr(pair.into_inner().next().unwrap()),
Rule::term => {
let mut inner = pair.into_inner();
let first = parse_expr(inner.next().unwrap())?;
let mut acc = first;
while let Some(op) = inner.next() {
let right = parse_expr(inner.next().unwrap())?;
match op.as_str() {
"x" | "*" => acc = Expr::Mul(Box::new(acc), Box::new(right)),
_ => unreachable!(),
}
}
Ok(acc)
}
Rule::expr => {
let mut inner = pair.into_inner();
let first = parse_expr(inner.next().unwrap())?;
let mut acc = first;
while let Some(op) = inner.next() {
let right = parse_expr(inner.next().unwrap())?;
acc = match op.as_str() {
"+" => Expr::Add(Box::new(acc), Box::new(right)),
"-" => Expr::Sub(Box::new(acc), Box::new(right)),
_ => unreachable!(),
};
}
Ok(acc)
}
_ => Err(anyhow!("unexpected rule: {:?}", pair.as_rule())),
}
}
pub fn parse_program(source: &str) -> Result<Vec<crate::ops::math::Assign>> {
let mut pairs = CalcParser::parse(Rule::program, source)?
.next()
.unwrap()
.into_inner();
let block = pairs.next().ok_or_else(|| anyhow!("missing block"))?;
if block.as_rule() != Rule::reproducibility {
return Err(anyhow!("expected reproducibility block"));
}
let mut assigns = Vec::new();
for stmt in block.into_inner() {
let mut inner = stmt.into_inner();
let name = inner.next().unwrap().as_str().to_string();
let expr_pair = inner.next().unwrap();
let expr = parse_expr(expr_pair)?;
assigns
.push(crate::ops::math::Assign {
name,
value: expr,
});
}
Ok(assigns)
}
*/