Skip to content

Commit

Permalink
incorporate feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
yihozhang committed Jan 6, 2025
1 parent 731f1da commit 6630e6d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::sync::Arc;
use sugar::{For, WithRuleset};

mod rational;
mod sugar;
pub use rational::*;
mod sugar;

pub fn new_experimental_egraph() -> EGraph {
let mut egraph = EGraph::default();
Expand Down
57 changes: 57 additions & 0 deletions src/sugar/for.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use egglog::{ast::*, util::FreshGen};

pub struct For;

impl Macro<Vec<Command>> for For {
fn get_head(&self) -> String {
"for".to_string()
}

fn parse(
&self,
args: &[Sexp],
span: Span,
parser: &mut Parser,
) -> Result<Vec<Command>, ParseError> {
if args.len() != 2 {
return Err(ParseError::new(span, "expected (for <query> <action>)"));
}

let ruleset = parser.symbol_gen.fresh(&"for_ruleset".into());
let rulename = parser.symbol_gen.fresh(&"for_rule".into());
let query = args[0]
.expect_list("query")?
.iter()
.map(|s| parser.parse_fact(s))
.collect::<Result<Vec<_>, _>>()?;
let action = args[1]
.expect_list("action")?
.iter()
.map(|s| parser.parse_action(s))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect::<Vec<_>>();
let rule = Rule {
span: span.clone(),
head: Actions::new(action),
body: query,
};

Ok(vec![
Command::AddRuleset(ruleset),
Command::Rule {
name: rulename,
ruleset,
rule,
},
Command::RunSchedule(Schedule::Run(
span,
RunConfig {
ruleset,
until: None,
},
)),
])
}
}
4 changes: 4 additions & 0 deletions src/sugar/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod r#for;
pub use r#for::*;
mod with_ruleset;
pub use with_ruleset::*;
56 changes: 1 addition & 55 deletions src/sugar.rs → src/sugar/with_ruleset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egglog::{ast::*, util::FreshGen};
use egglog::ast::*;

pub struct WithRuleset;

Expand Down Expand Up @@ -82,57 +82,3 @@ impl Macro<Vec<Command>> for WithRuleset {
}
}
}

pub struct For;

impl Macro<Vec<Command>> for For {
fn get_head(&self) -> String {
"for".to_string()
}

fn parse(
&self,
args: &[Sexp],
span: Span,
parser: &mut Parser,
) -> Result<Vec<Command>, ParseError> {
if args.len() != 2 {
return Err(ParseError::new(span, "expected (for <query> <action>)"));
}
let ruleset = parser.symbol_gen.fresh(&"for_ruleset".into());
let rulename = parser.symbol_gen.fresh(&"for_rule".into());
let query = args[0]
.expect_list("query")?
.iter()
.map(|s| parser.parse_fact(s))
.collect::<Result<Vec<_>, _>>()?;
let action = args[1]
.expect_list("action")?
.iter()
.map(|s| parser.parse_action(s))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect::<Vec<_>>();
let rule = Rule {
span: span.clone(),
head: Actions::new(action),
body: query,
};
Ok(vec![
Command::AddRuleset(ruleset),
Command::Rule {
name: rulename,
ruleset,
rule,
},
Command::RunSchedule(Schedule::Run(
span,
RunConfig {
ruleset,
until: None,
},
)),
])
}
}

0 comments on commit 6630e6d

Please sign in to comment.