-
Notifications
You must be signed in to change notification settings - Fork 55
Lean backend [M2] - 3/3 - Resugarings #1591
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,4 +15,5 @@ pub mod names; | |
| pub mod ocaml_engine; | ||
| pub mod phase; | ||
| pub mod printer; | ||
| pub mod resugarings; | ||
| pub mod symbol; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //! The "resugaring" phases used by printers. | ||
|
|
||
| //! This module defines resugarings instances (see | ||
| //! [`hax_rust_engine::ast::Resugaring`] for the definition of a | ||
| //! resugaring). Each backend defines its own set of resugaring phases. | ||
|
|
||
| use crate::ast::identifiers::global_id::DefId; | ||
| use crate::ast::resugared::*; | ||
| use crate::ast::visitors::*; | ||
| use crate::ast::*; | ||
| use crate::printer::*; | ||
| use std::collections::HashSet; | ||
|
|
||
| /// Binop resugaring. Used to identify expressions of the form `(f e1 e2)` where | ||
| /// `f` is a known identifier. | ||
| pub struct BinOp { | ||
| /// Stores a set of identifiers that should be resugared as binary | ||
| /// operations. Usually, those identifiers come from the hax encoding. Each | ||
| /// backend can select its own set of identifiers Typically, if the backend | ||
| /// has a special support for addition, `known_ops` will contain | ||
| /// `hax::machine::int::add` | ||
| pub known_ops: HashSet<DefId>, | ||
| } | ||
|
|
||
| impl BinOp { | ||
| /// Adds a new binary operation from a list of (hax-introduced) names | ||
| pub fn new(known_ops: &[DefId]) -> Self { | ||
| Self { | ||
| known_ops: HashSet::from_iter(known_ops.iter().cloned()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl AstVisitorMut for BinOp { | ||
| fn enter_expr_kind(&mut self, x: &mut ExprKind) { | ||
| let ExprKind::App { | ||
| head, | ||
| args, | ||
| generic_args, | ||
| bounds_impls, | ||
| trait_, | ||
| }: &mut ExprKind = x | ||
| else { | ||
| return; | ||
| }; | ||
| let ExprKind::GlobalId(id) = &*head.kind else { | ||
| return; | ||
| }; | ||
| let [lhs, rhs] = &args[..] else { return }; | ||
| if self.known_ops.iter().any(|defid| id == defid) { | ||
| *x = ExprKind::Resugared(ResugaredExprKind::BinOp { | ||
| op: id.clone(), | ||
| lhs: lhs.clone(), | ||
| rhs: rhs.clone(), | ||
| generic_args: generic_args.clone(), | ||
| bounds_impls: bounds_impls.clone(), | ||
| trait_: trait_.clone(), | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Resugaring for BinOp { | ||
| fn name(&self) -> String { | ||
| "binop".to_string() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.