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

Make parser extensible and remove resugaring #497

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ lazy_static = "1.4"
num = "0.4.3"
smallvec = "1.11"

generic_symbolic_expressions = "5.0.4"

egraph-serialize = { version = "0.2.0", default-features = false }

# binary dependencies
Expand Down
30 changes: 17 additions & 13 deletions src/ast/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ pub(crate) fn desugar_program(
program: Vec<Command>,
symbol_gen: &mut SymbolGen,
seminaive_transform: bool,
macros: &Macros,
) -> Result<Vec<NCommand>, Error> {
let mut res = vec![];
for command in program {
let desugared = desugar_command(command, symbol_gen, seminaive_transform)?;
let desugared = desugar_command(command, symbol_gen, seminaive_transform, macros)?;
res.extend(desugared);
}
Ok(res)
Expand All @@ -24,6 +25,7 @@ pub(crate) fn desugar_command(
command: Command,
symbol_gen: &mut SymbolGen,
seminaive_transform: bool,
macros: &Macros,
) -> Result<Vec<NCommand>, Error> {
let res = match command {
Command::SetOption { name, value } => {
Expand Down Expand Up @@ -102,19 +104,23 @@ pub(crate) fn desugar_command(

res
}
Command::Rewrite(ruleset, rewrite, subsume) => {
desugar_rewrite(ruleset, rewrite_name(&rewrite).into(), &rewrite, subsume)
}
Command::Rewrite(ruleset, rewrite, subsume) => desugar_rewrite(
ruleset,
symbol_gen.fresh(&"rewrite".into()),
&rewrite,
subsume,
),
Command::BiRewrite(ruleset, rewrite) => {
desugar_birewrite(ruleset, rewrite_name(&rewrite).into(), &rewrite)
desugar_birewrite(ruleset, symbol_gen.fresh(&"rewrite".into()), &rewrite)
}
Command::Include(span, file) => {
let s = std::fs::read_to_string(&file)
.unwrap_or_else(|_| panic!("{span} Failed to read file {file}"));
return desugar_program(
parse_program(Some(file), &s)?,
parse_program(Some(file), &s, macros)?,
symbol_gen,
seminaive_transform,
macros,
);
}
Command::Rule {
Expand All @@ -123,7 +129,7 @@ pub(crate) fn desugar_command(
rule,
} => {
if name == "".into() {
name = rule.to_string().replace('\"', "'").into();
name = symbol_gen.fresh(&"rule".into());
}

let result = vec![NCommand::NormRule {
Expand All @@ -144,7 +150,7 @@ pub(crate) fn desugar_command(
span,
expr,
schedule,
} => desugar_simplify(&expr, &schedule, span, symbol_gen),
} => desugar_simplify(&expr, &schedule, span, symbol_gen, macros),
Command::RunSchedule(sched) => {
vec![NCommand::RunSchedule(sched.clone())]
}
Expand Down Expand Up @@ -218,7 +224,7 @@ pub(crate) fn desugar_command(
vec![NCommand::Pop(span, num)]
}
Command::Fail(span, cmd) => {
let mut desugared = desugar_command(*cmd, symbol_gen, seminaive_transform)?;
let mut desugared = desugar_command(*cmd, symbol_gen, seminaive_transform, macros)?;

let last = desugared.pop().unwrap();
desugared.push(NCommand::Fail(span, Box::new(last)));
Expand Down Expand Up @@ -323,6 +329,7 @@ fn desugar_simplify(
schedule: &Schedule,
span: Span,
symbol_gen: &mut SymbolGen,
macros: &Macros,
) -> Vec<NCommand> {
let mut res = vec![NCommand::Push(1)];
let lhs = symbol_gen.fresh(&"desugar_simplify".into());
Expand All @@ -341,14 +348,11 @@ fn desugar_simplify(
},
symbol_gen,
false,
macros,
)
.unwrap(),
);

res.push(NCommand::Pop(span, 1));
res
}

pub(crate) fn rewrite_name(rewrite: &Rewrite) -> String {
rewrite.to_string().replace('\"', "'")
}
46 changes: 13 additions & 33 deletions src/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::ToSexp;
use crate::{core::ResolvedCall, *};
use ordered_float::OrderedFloat;
use std::{fmt::Display, hash::Hasher};
Expand Down Expand Up @@ -97,12 +96,6 @@ impl Display for ResolvedVar {
}
}

impl ToSexp for ResolvedVar {
fn to_sexp(&self) -> Sexp {
Sexp::Symbol(self.name.to_string())
}
}

pub type Expr = GenericExpr<Symbol, Symbol>;
/// A generated expression is an expression that is generated by the system
/// and does not have annotations.
Expand Down Expand Up @@ -155,7 +148,7 @@ impl Expr {
}
}

impl<Head: Clone + Display, Leaf: Hash + Clone + Display + Eq> GenericExpr<Head, Leaf> {
impl<Head: HeadBounds, Leaf: LeafBounds> GenericExpr<Head, Leaf> {
pub fn span(&self) -> Span {
match self {
GenericExpr::Lit(span, _) => span.clone(),
Expand Down Expand Up @@ -250,31 +243,18 @@ impl<Head: Clone + Display, Leaf: Hash + Clone + Display + Eq> GenericExpr<Head,
}
}

impl<Head: Display, Leaf: Display> GenericExpr<Head, Leaf> {
/// Converts this expression into a
/// s-expression (symbolic expression).
/// Example: `(Add (Add 2 3) 4)`
pub fn to_sexp(&self) -> Sexp {
let res = match self {
GenericExpr::Lit(_ann, lit) => Sexp::Symbol(lit.to_string()),
GenericExpr::Var(_ann, v) => Sexp::Symbol(v.to_string()),
GenericExpr::Call(_ann, op, children) => Sexp::List(
vec![Sexp::Symbol(op.to_string())]
.into_iter()
.chain(children.iter().map(|c| c.to_sexp()))
.collect(),
),
};
res
}
}

impl<Head, Leaf> Display for GenericExpr<Head, Leaf>
where
Head: Display,
Leaf: Display,
{
impl<Head: Display, Leaf: Display> Display for GenericExpr<Head, Leaf> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_sexp())
match self {
GenericExpr::Lit(_ann, lit) => write!(f, "{lit}"),
GenericExpr::Var(_ann, var) => write!(f, "{var}"),
GenericExpr::Call(_ann, op, children) => {
write!(f, "({op}")?;
for child in children {
write!(f, " {child}")?;
}
write!(f, ")")
}
}
}
}
Loading
Loading