Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for syntax #479

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/ast/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ pub(crate) fn desugar_command(

result
}
Command::For(rule) => {
let ruleset = symbol_gen.fresh(&"implicit_ruleset".into());
let span = rule.span.clone();
vec![
NCommand::AddRuleset(ruleset),
NCommand::NormRule {
ruleset,
name: rule.to_string().replace('\"', "'").into(),
rule,
},
NCommand::RunSchedule(Schedule::Run(
span,
RunConfig {
ruleset,
until: None,
},
)),
]
}
Command::Sort(span, sort, option) => vec![NCommand::Sort(span, sort, option)],
Command::AddRuleset(name) => vec![NCommand::AddRuleset(name)],
Command::UnstableCombinedRuleset(name, subrulesets) => {
Expand Down
11 changes: 11 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ where
ruleset: Symbol,
rule: GenericRule<Head, Leaf>,
},
For(GenericRule<Head, Leaf>),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some docs?

/// `rewrite` is syntactic sugar for a specific form of `rule`
/// which simply unions the left and right hand sides.
///
Expand Down Expand Up @@ -704,6 +705,7 @@ where
ruleset,
rule,
} => rule.to_sexp(*ruleset, *name),
GenericCommand::For(rule) => rule.for_to_sexp(),
GenericCommand::RunSchedule(sched) => list!("run-schedule", sched),
GenericCommand::PrintOverallStatistics => list!("print-stats"),
GenericCommand::QueryExtract {
Expand Down Expand Up @@ -1564,6 +1566,15 @@ where
}
Sexp::List(res)
}

pub fn for_to_sexp(&self) -> Sexp {
let res = vec![
Sexp::Symbol("for".into()),
Sexp::List(self.body.iter().map(|f| f.to_sexp()).collect()),
Sexp::List(self.head.0.iter().map(|a| a.to_sexp()).collect()),
];
Sexp::List(res)
}
}

impl<Head, Leaf> Display for GenericRule<Head, Leaf>
Expand Down
8 changes: 8 additions & 0 deletions src/ast/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ fn command(ctx: &Context) -> Res<Command> {
rule: Rule { span, head, body },
},
)(ctx),
"for" => map(
parens(sequences!(
text("for"),
list(fact),
map(list(action), |x, _| Actions::new(x)),
)),
|((), (body, head)), span| Command::For(Rule { span, head, body }),
)(ctx),
"rewrite" => map(
parens(sequences!(
text("rewrite"),
Expand Down
21 changes: 21 additions & 0 deletions tests/for.egg
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(relation range (i64))
(relation R (i64 i64))

(range 0)
(range 1)
(range 2)

(for ((range x))
((extract x)))

(for ()
((let x1 1)
(let x2 2)
(R x1 x2)))

(check (R 1 2))

(for ((R x y))
((delete (R x y))))

(fail (check (R 1 2)))
Loading