summaryrefslogtreecommitdiff
path: root/src/def_rules.rs
blob: a2ba36e866fd4849dc1410dc60c179a30550ae55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use crate::sexpr::{self, Atom, Expr};

#[derive(Debug, Clone)]
pub struct Rule {
    pub lhs: Expr,
    pub rhs: Expr
}

impl TryFrom<Expr> for Rule {
    type Error = ();
    fn try_from(value: Expr) -> Result<Self, Self::Error> {
        if let Expr::Application(v) = value && v[0] == Expr::Atom(Atom::Builtin(String::from("def-rule"))) && v.len() == 3 {
            Ok(Rule {
                lhs: v[1].clone(),
                rhs: v[2].clone()
            })
        } else {Err(())}
    }
}

pub fn parse_def_rules(input: String) -> Vec<Rule> {
    let exprs = sexpr::Parser::new(&input).parse_all();
    exprs.iter().filter_map(|e| e.clone().try_into().ok()).collect()
}