Skip to content

Commit

Permalink
Keep track of mutability in microstatements, afterall (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfellis authored Mar 12, 2024
1 parent 8233d6d commit 520c07a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/lntors/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub fn from_microstatement(
) -> Result<(String, OrderedHashMap<String, String>), Box<dyn std::error::Error>> {
match microstatement {
Microstatement::Arg { .. } => Ok(("".to_string(), out)), // Skip arg microstatements that are just used for discovery during generation
Microstatement::Assignment { name, value } => {
Microstatement::Assignment { name, value, mutable } => {
let (val, o) = from_microstatement(value, scope, program, out)?;
// I wish I didn't have to write the following line because you can't re-assign a
// variable in a let destructuring, afaict
out = o;
Ok((format!("let {} = {}", name, val,).to_string(), out))
Ok((format!("let {} {} = {}", if *mutable { "mut" } else { "" }, name, val,).to_string(), out))
}
Microstatement::Value {
typen,
Expand Down
15 changes: 7 additions & 8 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ impl Const {
#[derive(Debug)]
pub enum Microstatement {
Assignment {
mutable: bool,
name: String,
value: Box<Microstatement>,
},
Expand Down Expand Up @@ -526,6 +527,7 @@ fn baseassignablelist_to_microstatements(
Microstatement::Assignment {
ref name,
ref value,
..
} => {
// If the last microstatement is an assignment, we need to
// reference it as a value and push it back onto the array
Expand Down Expand Up @@ -572,7 +574,7 @@ fn baseassignablelist_to_microstatements(
let mut first_microstatement = None;
for microstatement in microstatements.iter().rev() {
match &microstatement {
Microstatement::Assignment { name, value } if name == &first_var.to_string() => {
Microstatement::Assignment { name, value, .. } if name == &first_var.to_string() => {
first_microstatement = Some(Microstatement::Value {
typen: value.get_type(scope, program)?,
representation: first_var.to_string(),
Expand Down Expand Up @@ -969,12 +971,9 @@ fn declarations_to_microstatements(
program: &Program,
mut microstatements: Vec<Microstatement>,
) -> Result<Vec<Microstatement>, Box<dyn std::error::Error>> {
// We don't care about const vs let in the microstatement world, everything is
// immutable and we just create a crap-ton of variables. TODO: We might have
// user-provided type declaration data we should look into, at least as a sanity check?
let (name, assignables) = match &declarations {
parse::Declarations::Const(c) => (c.variable.clone(), &c.assignables),
parse::Declarations::Let(l) => (l.variable.clone(), &l.assignables),
let (name, assignables, mutable) = match &declarations {
parse::Declarations::Const(c) => (c.variable.clone(), &c.assignables, false),
parse::Declarations::Let(l) => (l.variable.clone(), &l.assignables, true),
};
// Get all of the assignable microstatements generated
microstatements =
Expand All @@ -983,7 +982,7 @@ fn declarations_to_microstatements(
None => Err("An assignment without a value should be impossible."),
Some(v) => Ok(Box::new(v)),
}?;
microstatements.push(Microstatement::Assignment { name, value });
microstatements.push(Microstatement::Assignment { name, value, mutable });
Ok(microstatements)
}

Expand Down

0 comments on commit 520c07a

Please sign in to comment.