diff --git a/components/lox-compile/src/compile.rs b/components/lox-compile/src/compile.rs index 736b9a6..61ed52d 100644 --- a/components/lox-compile/src/compile.rs +++ b/components/lox-compile/src/compile.rs @@ -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) diff --git a/lox_tests/arithmetic/bytecode b/lox_tests/arithmetic/bytecode index d4c9077..52dacf4 100644 --- a/lox_tests/arithmetic/bytecode +++ b/lox_tests/arithmetic/bytecode @@ -11,6 +11,7 @@ Chunk { ), ), Add, + Pop, Constant( F64( 1.0, @@ -28,6 +29,7 @@ Chunk { ), Multiply, Add, + Pop, Constant( F64( 1.0, @@ -45,6 +47,7 @@ Chunk { ), Divide, Subtract, + Pop, Constant( F64( 1.0, @@ -63,5 +66,6 @@ Chunk { Negate, Multiply, Add, + Pop, ], } \ No newline at end of file diff --git a/lox_tests/arithmetic/execute b/lox_tests/arithmetic/execute index bf47fe6..603ea54 100644 --- a/lox_tests/arithmetic/execute +++ b/lox_tests/arithmetic/execute @@ -30,15 +30,15 @@ stack: [ ), ] +execute: Pop +stack: [] + execute: Constant( F64( 1.0, ), ) stack: [ - Number( - 3.0, - ), Number( 1.0, ), @@ -50,9 +50,6 @@ execute: Constant( ), ) stack: [ - Number( - 3.0, - ), Number( 1.0, ), @@ -67,9 +64,6 @@ execute: Constant( ), ) stack: [ - Number( - 3.0, - ), Number( 1.0, ), @@ -83,9 +77,6 @@ stack: [ execute: Multiply stack: [ - Number( - 3.0, - ), Number( 1.0, ), @@ -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, ), @@ -127,12 +112,6 @@ execute: Constant( ), ) stack: [ - Number( - 3.0, - ), - Number( - 7.0, - ), Number( 1.0, ), @@ -147,12 +126,6 @@ execute: Constant( ), ) stack: [ - Number( - 3.0, - ), - Number( - 7.0, - ), Number( 1.0, ), @@ -166,12 +139,6 @@ stack: [ execute: Divide stack: [ - Number( - 3.0, - ), - Number( - 7.0, - ), Number( 1.0, ), @@ -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, ), @@ -219,15 +174,6 @@ execute: Constant( ), ) stack: [ - Number( - 3.0, - ), - Number( - 7.0, - ), - Number( - 0.33333333333333337, - ), Number( 1.0, ), @@ -242,15 +188,6 @@ execute: Constant( ), ) stack: [ - Number( - 3.0, - ), - Number( - 7.0, - ), - Number( - 0.33333333333333337, - ), Number( 1.0, ), @@ -264,15 +201,6 @@ stack: [ execute: Negate stack: [ - Number( - 3.0, - ), - Number( - 7.0, - ), - Number( - 0.33333333333333337, - ), Number( 1.0, ), @@ -286,15 +214,6 @@ stack: [ execute: Multiply stack: [ - Number( - 3.0, - ), - Number( - 7.0, - ), - Number( - 0.33333333333333337, - ), Number( 1.0, ), @@ -305,17 +224,11 @@ stack: [ execute: Add stack: [ - Number( - 3.0, - ), - Number( - 7.0, - ), - Number( - 0.33333333333333337, - ), Number( -5.0, ), ] +execute: Pop +stack: [] + diff --git a/lox_tests/assignment/bytecode b/lox_tests/assignment/bytecode index 8f2996e..ce4ac43 100644 --- a/lox_tests/assignment/bytecode +++ b/lox_tests/assignment/bytecode @@ -16,6 +16,7 @@ Chunk { Assign( "a", ), + Pop, ReadGlobalVariable { name: "a", }, @@ -45,6 +46,7 @@ Chunk { Assign( "c", ), + Pop, ReadGlobalVariable { name: "c", }, diff --git a/lox_tests/assignment/execute b/lox_tests/assignment/execute index 38de24b..e2c6aa7 100644 --- a/lox_tests/assignment/execute +++ b/lox_tests/assignment/execute @@ -34,6 +34,9 @@ stack: [ ), ] +execute: Pop +stack: [] + execute: ReadGlobalVariable { name: "a", } @@ -41,17 +44,10 @@ stack: [ Number( 2.0, ), - Number( - 2.0, - ), ] execute: Print -stack: [ - Number( - 2.0, - ), -] +stack: [] execute: Constant( F64( @@ -59,9 +55,6 @@ execute: Constant( ), ) stack: [ - Number( - 2.0, - ), Number( 3.0, ), @@ -71,28 +64,18 @@ execute: Assign( "a", ) stack: [ - Number( - 2.0, - ), Number( 3.0, ), ] execute: Print -stack: [ - Number( - 2.0, - ), -] +stack: [] execute: ReadGlobalVariable { name: "a", } stack: [ - Number( - 2.0, - ), Number( 3.0, ), @@ -101,36 +84,22 @@ stack: [ execute: GlobalVarDeclaration { name: "b", } -stack: [ - Number( - 2.0, - ), -] +stack: [] execute: Nil stack: [ - Number( - 2.0, - ), Nil, ] execute: GlobalVarDeclaration { name: "c", } -stack: [ - Number( - 2.0, - ), -] +stack: [] execute: ReadGlobalVariable { name: "b", } stack: [ - Number( - 2.0, - ), Number( 3.0, ), @@ -140,36 +109,23 @@ execute: Assign( "c", ) stack: [ - Number( - 2.0, - ), Number( 3.0, ), ] +execute: Pop +stack: [] + execute: ReadGlobalVariable { name: "c", } stack: [ - Number( - 2.0, - ), - Number( - 3.0, - ), Number( 3.0, ), ] execute: Print -stack: [ - Number( - 2.0, - ), - Number( - 3.0, - ), -] +stack: [] diff --git a/lox_tests/boolean/bytecode b/lox_tests/boolean/bytecode index a15c9df..caf65d0 100644 --- a/lox_tests/boolean/bytecode +++ b/lox_tests/boolean/bytecode @@ -1,10 +1,14 @@ Chunk { code: [ True, + Pop, False, + Pop, True, Not, + Pop, False, Not, + Pop, ], } \ No newline at end of file diff --git a/lox_tests/boolean/execute b/lox_tests/boolean/execute index f5b9ee4..22bc199 100644 --- a/lox_tests/boolean/execute +++ b/lox_tests/boolean/execute @@ -5,53 +5,38 @@ stack: [ ), ] +execute: Pop +stack: [] + execute: False stack: [ - Boolean( - true, - ), Boolean( false, ), ] +execute: Pop +stack: [] + execute: True stack: [ Boolean( true, ), - Boolean( - false, - ), - Boolean( - true, - ), ] execute: Not stack: [ - Boolean( - true, - ), - Boolean( - false, - ), Boolean( false, ), ] +execute: Pop +stack: [] + execute: False stack: [ - Boolean( - true, - ), - Boolean( - false, - ), - Boolean( - false, - ), Boolean( false, ), @@ -62,14 +47,8 @@ stack: [ Boolean( true, ), - Boolean( - false, - ), - Boolean( - false, - ), - Boolean( - true, - ), ] +execute: Pop +stack: [] + diff --git a/lox_tests/comparison/bytecode b/lox_tests/comparison/bytecode index 1b54e58..b308dc1 100644 --- a/lox_tests/comparison/bytecode +++ b/lox_tests/comparison/bytecode @@ -11,6 +11,7 @@ Chunk { ), ), Less, + Pop, Constant( F64( 1.0, @@ -22,6 +23,7 @@ Chunk { ), ), LessEqual, + Pop, Constant( F64( 1.0, @@ -33,6 +35,7 @@ Chunk { ), ), Greater, + Pop, Constant( F64( 1.0, @@ -44,5 +47,6 @@ Chunk { ), ), GreaterEqual, + Pop, ], } \ No newline at end of file diff --git a/lox_tests/comparison/execute b/lox_tests/comparison/execute index 3a8cf5b..1e5ea89 100644 --- a/lox_tests/comparison/execute +++ b/lox_tests/comparison/execute @@ -30,15 +30,15 @@ stack: [ ), ] +execute: Pop +stack: [] + execute: Constant( F64( 1.0, ), ) stack: [ - Boolean( - true, - ), Number( 1.0, ), @@ -50,9 +50,6 @@ execute: Constant( ), ) stack: [ - Boolean( - true, - ), Number( 1.0, ), @@ -66,23 +63,17 @@ stack: [ Boolean( true, ), - Boolean( - true, - ), ] +execute: Pop +stack: [] + execute: Constant( F64( 1.0, ), ) stack: [ - Boolean( - true, - ), - Boolean( - true, - ), Number( 1.0, ), @@ -94,12 +85,6 @@ execute: Constant( ), ) stack: [ - Boolean( - true, - ), - Boolean( - true, - ), Number( 1.0, ), @@ -110,32 +95,20 @@ stack: [ execute: Greater stack: [ - Boolean( - true, - ), - Boolean( - true, - ), Boolean( false, ), ] +execute: Pop +stack: [] + execute: Constant( F64( 1.0, ), ) stack: [ - Boolean( - true, - ), - Boolean( - true, - ), - Boolean( - false, - ), Number( 1.0, ), @@ -147,15 +120,6 @@ execute: Constant( ), ) stack: [ - Boolean( - true, - ), - Boolean( - true, - ), - Boolean( - false, - ), Number( 1.0, ), @@ -169,14 +133,8 @@ stack: [ Boolean( true, ), - Boolean( - true, - ), - Boolean( - false, - ), - Boolean( - true, - ), ] +execute: Pop +stack: [] + diff --git a/lox_tests/equality/bytecode b/lox_tests/equality/bytecode index fa301ec..474e339 100644 --- a/lox_tests/equality/bytecode +++ b/lox_tests/equality/bytecode @@ -11,6 +11,7 @@ Chunk { ), ), Equal, + Pop, Constant( F64( 1.0, @@ -22,5 +23,6 @@ Chunk { ), ), NotEqual, + Pop, ], } \ No newline at end of file diff --git a/lox_tests/equality/execute b/lox_tests/equality/execute index 6fb2421..68dcd87 100644 --- a/lox_tests/equality/execute +++ b/lox_tests/equality/execute @@ -30,15 +30,15 @@ stack: [ ), ] +execute: Pop +stack: [] + execute: Constant( F64( 1.0, ), ) stack: [ - Boolean( - false, - ), Number( 1.0, ), @@ -50,9 +50,6 @@ execute: Constant( ), ) stack: [ - Boolean( - false, - ), Number( 1.0, ), @@ -63,11 +60,11 @@ stack: [ execute: NotEqual stack: [ - Boolean( - false, - ), Boolean( true, ), ] +execute: Pop +stack: [] + diff --git a/lox_tests/string/bytecode b/lox_tests/string/bytecode index d621e91..59b9b91 100644 --- a/lox_tests/string/bytecode +++ b/lox_tests/string/bytecode @@ -7,5 +7,6 @@ Chunk { " world", ), Add, + Pop, ], } \ No newline at end of file diff --git a/lox_tests/string/execute b/lox_tests/string/execute index ce9c41e..8848f4e 100644 --- a/lox_tests/string/execute +++ b/lox_tests/string/execute @@ -26,3 +26,6 @@ stack: [ ), ] +execute: Pop +stack: [] +