forked from egraphs-good/egglog
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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,59 @@ | ||
//! Remove global variables from the program by translating | ||
//! them into functions with no arguments. | ||
//! This requires type information, so it is done after type checking. | ||
use crate::{ | ||
desugar::Desugar, Action, GenericAction, GenericActions, GenericExpr, GenericFunctionDecl, | ||
GenericNCommand, NCommand, ResolvedActions, ResolvedFunctionDecl, ResolvedNCommand, Schema, | ||
Symbol, TypeInfo, | ||
}; | ||
use hashbrown::HashSet; | ||
|
||
pub(crate) fn remove_globals( | ||
type_info: &TypeInfo, | ||
prog: &Vec<ResolvedNCommand>, | ||
) -> Vec<ResolvedNCommand> { | ||
let mut res = Vec::new(); | ||
for cmd in prog { | ||
res.extend(remove_globals_cmd(type_info, cmd)); | ||
} | ||
res | ||
} | ||
|
||
/// Removes all globals from a command. | ||
/// Adds new functions for new globals | ||
/// and replaces references to globals with | ||
/// references to the new functions. | ||
/// Also adds the types for these functions to | ||
/// the type info struct. | ||
fn remove_globals_cmd(type_info: &TypeInfo, cmd: &ResolvedNCommand) -> Vec<ResolvedNCommand> { | ||
match cmd { | ||
GenericNCommand::CoreAction(action) => match action { | ||
GenericAction::Let(ann, name, expr) => { | ||
let ty = expr.output_type(type_info); | ||
let func_decl = ResolvedFunctionDecl { | ||
name: name.name, | ||
schema: Schema { | ||
input: vec![], | ||
output: ty.name(), | ||
}, | ||
default: None, | ||
merge: None, | ||
merge_action: GenericActions(vec![]), | ||
cost: None, | ||
unextractable: true, | ||
}; | ||
let mut res = vec![ | ||
GenericNCommand::Function(func_decl), | ||
GenericNCommand::CoreAction(GenericAction::Union( | ||
(), | ||
GenericExpr::Call((), ResolvedCall {}, vec![]), | ||
expr, | ||
)), | ||
]; | ||
|
||
res | ||
} | ||
}, | ||
} | ||
} |