Skip to content

Commit

Permalink
in-progress pass
Browse files Browse the repository at this point in the history
  • Loading branch information
oflatt committed Jan 31, 2024
1 parent a49ded1 commit b596230
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
7 changes: 0 additions & 7 deletions src/ast/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
1 change: 1 addition & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions src/ast/remove_globals.rs
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
}
},
}
}

0 comments on commit b596230

Please sign in to comment.