Add reassignment
This commit is contained in:
parent
982bd48d64
commit
88d09abcce
@ -9,4 +9,7 @@ function test()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local a, b, c = add(10)(15)
|
local a, b, c = add(10)(15)
|
||||||
|
|
||||||
|
b = 10
|
||||||
|
|
||||||
local pr = print(a, b, c)
|
local pr = print(a, b, c)
|
||||||
@ -206,7 +206,7 @@ impl Parse for Block {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Assignment(AccessModifier, Vec<Node<String>>, ExpressionList),
|
Assignment(Option<AccessModifier>, Vec<Node<String>>, ExpressionList),
|
||||||
Return(ExpressionList),
|
Return(ExpressionList),
|
||||||
If(Node<Expression>, Block),
|
If(Node<Expression>, Block),
|
||||||
}
|
}
|
||||||
@ -218,7 +218,7 @@ impl Parse for Statement {
|
|||||||
let function = stream.parse::<Node<Function>>()?;
|
let function = stream.parse::<Node<Function>>()?;
|
||||||
if let Some(name) = function.kind.name {
|
if let Some(name) = function.kind.name {
|
||||||
Ok(Self::Assignment(
|
Ok(Self::Assignment(
|
||||||
AccessModifier::Global,
|
None,
|
||||||
vec![name],
|
vec![name],
|
||||||
ExpressionList(vec![Node {
|
ExpressionList(vec![Node {
|
||||||
kind: Expression::FunctionDefinition(
|
kind: Expression::FunctionDefinition(
|
||||||
@ -255,14 +255,14 @@ impl Parse for Statement {
|
|||||||
}
|
}
|
||||||
stream.expect(Token::Symbol('='))?;
|
stream.expect(Token::Symbol('='))?;
|
||||||
let expr = stream.parse()?;
|
let expr = stream.parse()?;
|
||||||
Ok(Statement::Assignment(access_modifier, names, expr))
|
Ok(Statement::Assignment(Some(access_modifier), names, expr))
|
||||||
} else if let Some(Token::Word(_)) = peeked
|
} else if let Some(Token::Word(_)) = peeked
|
||||||
&& stream.peek2() == Some(Token::Symbol('='))
|
&& stream.peek2() == Some(Token::Symbol('='))
|
||||||
{
|
{
|
||||||
let name = stream.parse()?;
|
let name = stream.parse()?;
|
||||||
stream.expect(Token::Symbol('='))?;
|
stream.expect(Token::Symbol('='))?;
|
||||||
Ok(Self::Assignment(
|
Ok(Self::Assignment(
|
||||||
AccessModifier::Global,
|
None,
|
||||||
vec![name],
|
vec![name],
|
||||||
ExpressionList(vec![stream.parse()?]),
|
ExpressionList(vec![stream.parse()?]),
|
||||||
))
|
))
|
||||||
|
|||||||
@ -70,10 +70,16 @@ impl Statement {
|
|||||||
match self {
|
match self {
|
||||||
Statement::Assignment(access, names, expr_list) => {
|
Statement::Assignment(access, names, expr_list) => {
|
||||||
let mut constants = HashSet::new();
|
let mut constants = HashSet::new();
|
||||||
if *access == AccessModifier::Global {
|
if *access == Some(AccessModifier::Global) {
|
||||||
for name in names {
|
for name in names {
|
||||||
constants.insert(Constant::String(name.kind.clone()));
|
constants.insert(Constant::String(name.kind.clone()));
|
||||||
}
|
}
|
||||||
|
} else if *access == None {
|
||||||
|
for name in names {
|
||||||
|
if !scope.locals.contains_key(&name.kind) {
|
||||||
|
constants.insert(Constant::String(name.kind.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
for name in names {
|
for name in names {
|
||||||
scope.locals.insert(name.kind.clone(), 0);
|
scope.locals.insert(name.kind.clone(), 0);
|
||||||
@ -125,7 +131,7 @@ impl Statement {
|
|||||||
expr_regs.extend(regs);
|
expr_regs.extend(regs);
|
||||||
}
|
}
|
||||||
match access_modifier {
|
match access_modifier {
|
||||||
AccessModifier::Local => {
|
Some(AccessModifier::Local) => {
|
||||||
for (i, name) in names.iter().enumerate() {
|
for (i, name) in names.iter().enumerate() {
|
||||||
scope.locals.insert(
|
scope.locals.insert(
|
||||||
name.kind.clone(),
|
name.kind.clone(),
|
||||||
@ -137,10 +143,39 @@ impl Statement {
|
|||||||
}
|
}
|
||||||
dbg!(&scope.locals);
|
dbg!(&scope.locals);
|
||||||
}
|
}
|
||||||
AccessModifier::Global => {
|
Some(AccessModifier::Global) => {
|
||||||
for (name, reg) in names.iter().zip(expr_regs) {
|
for (i, name) in names.iter().enumerate() {
|
||||||
let global = state.get_constant(&Constant::String(name.kind.clone()));
|
let global = state.get_constant(&Constant::String(name.kind.clone()));
|
||||||
instructions.push(Instruction::SetGlobal(reg, global));
|
instructions.push(Instruction::SetGlobal(
|
||||||
|
expr_regs
|
||||||
|
.get(i)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(scope.register_counter.0 + i as u16),
|
||||||
|
global,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
for (i, name) in names.iter().enumerate() {
|
||||||
|
if scope.locals.contains_key(&name.kind) {
|
||||||
|
scope.locals.insert(
|
||||||
|
name.kind.clone(),
|
||||||
|
expr_regs
|
||||||
|
.get(i)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(scope.register_counter.0 + i as u16),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
let global =
|
||||||
|
state.get_constant(&Constant::String(name.kind.clone()));
|
||||||
|
instructions.push(Instruction::SetGlobal(
|
||||||
|
expr_regs
|
||||||
|
.get(i)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(scope.register_counter.0 + i as u16),
|
||||||
|
global,
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user