Skip to content

Commit

Permalink
clear temp variables in stack
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 25, 2023
1 parent 3029d38 commit 20fa3d5
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 251 deletions.
5 changes: 4 additions & 1 deletion components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ struct Compiler {
impl Compiler {
fn compile_stmt(&mut self, db: &dyn crate::Db, stmt: &syntax::Stmt, chunk: &mut Chunk) {
match stmt {
syntax::Stmt::Expr(expr) => self.compile_expr(db, expr, chunk),
syntax::Stmt::Expr(expr) => {
self.compile_expr(db, expr, chunk);
chunk.emit_byte(Code::Pop);
}
syntax::Stmt::Print(expr) => {
self.compile_expr(db, expr, chunk);
chunk.emit_byte(Code::Print)
Expand Down
4 changes: 4 additions & 0 deletions lox_tests/arithmetic/bytecode
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Chunk {
),
),
Add,
Pop,
Constant(
F64(
1.0,
Expand All @@ -28,6 +29,7 @@ Chunk {
),
Multiply,
Add,
Pop,
Constant(
F64(
1.0,
Expand All @@ -45,6 +47,7 @@ Chunk {
),
Divide,
Subtract,
Pop,
Constant(
F64(
1.0,
Expand All @@ -63,5 +66,6 @@ Chunk {
Negate,
Multiply,
Add,
Pop,
],
}
111 changes: 12 additions & 99 deletions lox_tests/arithmetic/execute
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ stack: [
),
]

execute: Pop
stack: []

execute: Constant(
F64(
1.0,
),
)
stack: [
Number(
3.0,
),
Number(
1.0,
),
Expand All @@ -50,9 +50,6 @@ execute: Constant(
),
)
stack: [
Number(
3.0,
),
Number(
1.0,
),
Expand All @@ -67,9 +64,6 @@ execute: Constant(
),
)
stack: [
Number(
3.0,
),
Number(
1.0,
),
Expand All @@ -83,9 +77,6 @@ stack: [

execute: Multiply
stack: [
Number(
3.0,
),
Number(
1.0,
),
Expand All @@ -96,26 +87,20 @@ stack: [

execute: Add
stack: [
Number(
3.0,
),
Number(
7.0,
),
]

execute: Pop
stack: []

execute: Constant(
F64(
1.0,
),
)
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
1.0,
),
Expand All @@ -127,12 +112,6 @@ execute: Constant(
),
)
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
1.0,
),
Expand All @@ -147,12 +126,6 @@ execute: Constant(
),
)
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
1.0,
),
Expand All @@ -166,12 +139,6 @@ stack: [

execute: Divide
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
1.0,
),
Expand All @@ -182,32 +149,20 @@ stack: [

execute: Subtract
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
0.33333333333333337,
),
]

execute: Pop
stack: []

execute: Constant(
F64(
1.0,
),
)
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
0.33333333333333337,
),
Number(
1.0,
),
Expand All @@ -219,15 +174,6 @@ execute: Constant(
),
)
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
0.33333333333333337,
),
Number(
1.0,
),
Expand All @@ -242,15 +188,6 @@ execute: Constant(
),
)
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
0.33333333333333337,
),
Number(
1.0,
),
Expand All @@ -264,15 +201,6 @@ stack: [

execute: Negate
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
0.33333333333333337,
),
Number(
1.0,
),
Expand All @@ -286,15 +214,6 @@ stack: [

execute: Multiply
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
0.33333333333333337,
),
Number(
1.0,
),
Expand All @@ -305,17 +224,11 @@ stack: [

execute: Add
stack: [
Number(
3.0,
),
Number(
7.0,
),
Number(
0.33333333333333337,
),
Number(
-5.0,
),
]

execute: Pop
stack: []

2 changes: 2 additions & 0 deletions lox_tests/assignment/bytecode
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Chunk {
Assign(
"a",
),
Pop,
ReadGlobalVariable {
name: "a",
},
Expand Down Expand Up @@ -45,6 +46,7 @@ Chunk {
Assign(
"c",
),
Pop,
ReadGlobalVariable {
name: "c",
},
Expand Down
Loading

0 comments on commit 20fa3d5

Please sign in to comment.