diff --git a/src/ast/desugar.rs b/src/ast/desugar.rs index 70a95c819..daad56803 100644 --- a/src/ast/desugar.rs +++ b/src/ast/desugar.rs @@ -355,13 +355,6 @@ impl Clone for Desugar { } impl Desugar { - pub fn merge_ruleset_name(&self) -> Symbol { - Symbol::from(format!( - "merge_ruleset{}", - "_".repeat(self.number_underscores) - )) - } - pub fn get_fresh(&mut self) -> Symbol { self.next_fresh += 1; format!( diff --git a/src/ast/mod.rs b/src/ast/mod.rs index a3dc3c638..6e5a47017 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -21,6 +21,7 @@ use crate::{ mod expr; pub use expr::*; pub mod desugar; +mod remove_globals; #[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct Id(usize); diff --git a/src/ast/remove_globals.rs b/src/ast/remove_globals.rs new file mode 100644 index 000000000..270fd5961 --- /dev/null +++ b/src/ast/remove_globals.rs @@ -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, +) -> Vec { + 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 { + 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 + } + }, + } +}