generated from ElnuDev/rust-project
Add variable-to-variable assignment
This commit is contained in:
parent
01173d2f7d
commit
4bf6e7b820
2 changed files with 55 additions and 50 deletions
|
@ -6,7 +6,9 @@ if True:
|
||||||
"Bob will be here in [x] seconds."
|
"Bob will be here in [x] seconds."
|
||||||
if True:
|
if True:
|
||||||
x = "1"
|
x = "1"
|
||||||
let x = "4"
|
let y = "4"
|
||||||
|
y = "5"
|
||||||
|
x = y
|
||||||
"[x]"
|
"[x]"
|
||||||
"Bob will be here in [x] seconds."
|
"Bob will be here in [x] seconds."
|
||||||
"Bob will be here in [x] seconds."
|
"Bob will be here in [x] seconds."
|
||||||
|
|
|
@ -37,62 +37,65 @@ impl CommandContext {
|
||||||
assign_type,
|
assign_type,
|
||||||
variable,
|
variable,
|
||||||
value,
|
value,
|
||||||
} => match assign_type {
|
} => {
|
||||||
Define => panic!("define command should not be executed at runtime!"),
|
let value = if let Token::Keyword(keyword) = value {
|
||||||
GlobalAssign => {
|
|
||||||
if let Token::Keyword(_) = value {
|
|
||||||
todo!("assignment variable interpolation isn't implemented");
|
|
||||||
}
|
|
||||||
let root = self.context.borrow().get_root(&self.context);
|
|
||||||
// Don't want to re-borrow if root is self
|
|
||||||
let root = if Rc::ptr_eq(&self.context, &root) {
|
|
||||||
&self.context
|
|
||||||
} else {
|
|
||||||
&root
|
|
||||||
};
|
|
||||||
root.borrow_mut()
|
|
||||||
.variables
|
|
||||||
.as_ref()
|
|
||||||
.borrow_mut()
|
|
||||||
.insert(variable.clone(), value.clone());
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
Let => {
|
|
||||||
if let Token::Keyword(_) = value {
|
|
||||||
todo!("assignment variable interpolation isn't implemented");
|
|
||||||
}
|
|
||||||
self.context
|
self.context
|
||||||
.borrow()
|
.borrow()
|
||||||
.variables
|
.get_variables()
|
||||||
.borrow_mut()
|
.get(keyword)
|
||||||
.insert(variable.clone(), value.clone());
|
.unwrap_or_else(|| panic!("undefined variable `{keyword}`"))
|
||||||
return None;
|
.clone()
|
||||||
}
|
} else {
|
||||||
Assign => {
|
value.clone()
|
||||||
if let Token::Keyword(_) = value {
|
};
|
||||||
todo!("assignment variable interpolation isn't implemented");
|
match assign_type {
|
||||||
|
Define => panic!("define command should not be executed at runtime!"),
|
||||||
|
GlobalAssign => {
|
||||||
|
let root = self.context.borrow().get_root(&self.context);
|
||||||
|
// Don't want to re-borrow if root is self
|
||||||
|
let root = if Rc::ptr_eq(&self.context, &root) {
|
||||||
|
&self.context
|
||||||
|
} else {
|
||||||
|
&root
|
||||||
|
};
|
||||||
|
root.borrow_mut()
|
||||||
|
.variables
|
||||||
|
.as_ref()
|
||||||
|
.borrow_mut()
|
||||||
|
.insert(variable.clone(), value.clone());
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
let mut block_option = Some(self.context.clone());
|
Let => {
|
||||||
let mut modified = false;
|
self.context
|
||||||
while let Some(block) = block_option {
|
.borrow()
|
||||||
let block = block.borrow();
|
|
||||||
block
|
|
||||||
.variables
|
.variables
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.entry(variable.clone())
|
.insert(variable.clone(), value.clone());
|
||||||
.and_modify(|e| {
|
return None;
|
||||||
*e = value.clone();
|
}
|
||||||
modified = true;
|
Assign => {
|
||||||
});
|
let mut block_option = Some(self.context.clone());
|
||||||
if modified {
|
let mut modified = false;
|
||||||
break;
|
while let Some(block) = block_option {
|
||||||
|
let block = block.borrow();
|
||||||
|
block
|
||||||
|
.variables
|
||||||
|
.borrow_mut()
|
||||||
|
.entry(variable.clone())
|
||||||
|
.and_modify(|e| {
|
||||||
|
*e = value.clone();
|
||||||
|
modified = true;
|
||||||
|
});
|
||||||
|
if modified {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
block_option = block.parent.clone();
|
||||||
}
|
}
|
||||||
block_option = block.parent.clone();
|
if !modified {
|
||||||
|
panic!("undefined variable `{variable}`");
|
||||||
|
}
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
if !modified {
|
|
||||||
panic!("undefined variable `{variable}`");
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Eat { .. } => return None,
|
Eat { .. } => return None,
|
||||||
|
|
Loading…
Add table
Reference in a new issue