Skip to content

Instantly share code, notes, and snippets.

@IronGremlin
Created January 11, 2022 18:10
Show Gist options
  • Save IronGremlin/b8b10dc0867fe33dd12e2d24815426f6 to your computer and use it in GitHub Desktop.
Save IronGremlin/b8b10dc0867fe33dd12e2d24815426f6 to your computer and use it in GitHub Desktop.
Rust predicate AST
type Input = ();
type Condtion = fn(Input) -> bool;
type Procedure = fn();
#[derive(Clone)]
enum AbstractBoolean {
And(Box<AbstractBoolean>, Box<AbstractBoolean>),
Or(Box<AbstractBoolean>, Box<AbstractBoolean>),
Par(Box<AbstractBoolean>),
Not(Box<AbstractBoolean>),
Cond(Condtion),
}
enum AST {
If(AbstractBoolean, Procedure),
}
fn parseAST(input1: &str) -> Result<Vec<AST>, &str> {
Err("Not implemented")
}
fn interpret(ast: Vec<AST>) -> Box<dyn Fn(Input)> {
Box::new(|s| {})
}
fn example(ast: AbstractBoolean) -> Box<dyn Fn(Input) -> bool> {
Box::new(move |s| match ast.clone() {
AbstractBoolean::And(lcond, rcond) => example(*lcond)(s) && example(*rcond)(s),
AbstractBoolean::Or(lcond,rcond) => example(*lcond)(s) || example(*rcond)(s),
AbstractBoolean::Par(cond) => example(*cond)(s),
AbstractBoolean::Not(cond) => !example(*cond)(s),
AbstractBoolean::Cond(fun) => fun(s),
})
}
@IronGremlin
Copy link
Author

TODO :

  • Learn more about why the borrow checker hates our example function so much (probably this isn't how to do this)
  • Make a better fake input type
  • Go learn nom and write an actual parser
  • Write some tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment