Skip to content

Commit

Permalink
supports assignemtn
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 25, 2023
1 parent 73607af commit 03ff05a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 4 deletions.
6 changes: 5 additions & 1 deletion components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ fn compile_expr(db: &dyn crate::Db, expr: &syntax::Expr, chunk: &mut Chunk) {
let word_str = word.as_str(db);
chunk.emit_byte(Code::Variable(word_str.to_string()))
}
syntax::Expr::Assign { name, value } => todo!(),
syntax::Expr::Assign { name, value } => {
compile_expr(db, value, chunk);
let name_str = name.as_str(db);
chunk.emit_byte(Code::Assign(name_str.to_string()))
}
}
}
4 changes: 4 additions & 0 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ impl VM {
let value = self.globals.get(&name).expect("variable not found");
self.push(value.clone());
}
bytecode::Code::Assign(name) => {
let value = self.pop();
self.globals.insert(name, value);
}
}
if let Some(step_inspect) = &mut step_inspect {
step_inspect(instruction, self);
Expand Down
1 change: 1 addition & 0 deletions components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum Code {
VarDeclaration(String),
Variable(String),
Nil,
Assign(String),
}

#[derive(PartialEq, Eq, Debug, Clone, Default)]
Expand Down
3 changes: 2 additions & 1 deletion lox_tests/assignment.lox
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
var a = 1;
a = 2;
a = 2;
print a;
9 changes: 9 additions & 0 deletions lox_tests/assignment/bytecode
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ Chunk {
VarDeclaration(
"a",
),
Constant(
F64(
2.0,
),
),
Assign(
"a",
),
Variable(
"a",
),
Print,
],
}
21 changes: 20 additions & 1 deletion lox_tests/assignment/execute
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,31 @@ execute: VarDeclaration(
)
stack: []

execute: Constant(
F64(
2.0,
),
)
stack: [
Number(
2.0,
),
]

execute: Assign(
"a",
)
stack: []

execute: Variable(
"a",
)
stack: [
Number(
1.0,
2.0,
),
]

execute: Print
stack: []

3 changes: 3 additions & 0 deletions lox_tests/assignment/syntax
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ Expr {
value: NumberLiteral(2),
},
}
Print {
expr: Variable(a),
}
7 changes: 6 additions & 1 deletion lox_tests/assignment/token
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TokenTree {
source text: "var a = 1;\na = 2;",
source text: "var a = 1;\na = 2;\nprint a;",
tokens: [
Alphabetic(var),
Whitespace(' '),
Expand All @@ -16,5 +16,10 @@ TokenTree {
Whitespace(' '),
Number(2),
Semicolon,
Whitespace('\n'),
Alphabetic(print),
Whitespace(' '),
Alphabetic(a),
Semicolon,
],
}

0 comments on commit 03ff05a

Please sign in to comment.