From 03ff05a99314f88403a89e7512206ff6a38eef72 Mon Sep 17 00:00:00 2001 From: xffxff <1247714429@qq.com> Date: Mon, 25 Sep 2023 09:09:26 +0800 Subject: [PATCH] supports assignemtn --- components/lox-compile/src/compile.rs | 6 +++++- components/lox-execute/src/vm.rs | 4 ++++ components/lox-ir/src/bytecode.rs | 1 + lox_tests/assignment.lox | 3 ++- lox_tests/assignment/bytecode | 9 +++++++++ lox_tests/assignment/execute | 21 ++++++++++++++++++++- lox_tests/assignment/syntax | 3 +++ lox_tests/assignment/token | 7 ++++++- 8 files changed, 50 insertions(+), 4 deletions(-) diff --git a/components/lox-compile/src/compile.rs b/components/lox-compile/src/compile.rs index 91e0e52..aaa4a19 100644 --- a/components/lox-compile/src/compile.rs +++ b/components/lox-compile/src/compile.rs @@ -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())) + } } } diff --git a/components/lox-execute/src/vm.rs b/components/lox-execute/src/vm.rs index 58ca10c..7ce1a6c 100644 --- a/components/lox-execute/src/vm.rs +++ b/components/lox-execute/src/vm.rs @@ -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); diff --git a/components/lox-ir/src/bytecode.rs b/components/lox-ir/src/bytecode.rs index bc1cc96..a3860f3 100644 --- a/components/lox-ir/src/bytecode.rs +++ b/components/lox-ir/src/bytecode.rs @@ -21,6 +21,7 @@ pub enum Code { VarDeclaration(String), Variable(String), Nil, + Assign(String), } #[derive(PartialEq, Eq, Debug, Clone, Default)] diff --git a/lox_tests/assignment.lox b/lox_tests/assignment.lox index 09b000e..b084feb 100644 --- a/lox_tests/assignment.lox +++ b/lox_tests/assignment.lox @@ -1,2 +1,3 @@ var a = 1; -a = 2; \ No newline at end of file +a = 2; +print a; \ No newline at end of file diff --git a/lox_tests/assignment/bytecode b/lox_tests/assignment/bytecode index 2076a9f..39edff4 100644 --- a/lox_tests/assignment/bytecode +++ b/lox_tests/assignment/bytecode @@ -8,8 +8,17 @@ Chunk { VarDeclaration( "a", ), + Constant( + F64( + 2.0, + ), + ), + Assign( + "a", + ), Variable( "a", ), + Print, ], } \ No newline at end of file diff --git a/lox_tests/assignment/execute b/lox_tests/assignment/execute index ee4d08e..d00dec7 100644 --- a/lox_tests/assignment/execute +++ b/lox_tests/assignment/execute @@ -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: [] + diff --git a/lox_tests/assignment/syntax b/lox_tests/assignment/syntax index 9becf31..40510c2 100644 --- a/lox_tests/assignment/syntax +++ b/lox_tests/assignment/syntax @@ -10,3 +10,6 @@ Expr { value: NumberLiteral(2), }, } +Print { + expr: Variable(a), +} diff --git a/lox_tests/assignment/token b/lox_tests/assignment/token index 0742858..25cf12c 100644 --- a/lox_tests/assignment/token +++ b/lox_tests/assignment/token @@ -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(' '), @@ -16,5 +16,10 @@ TokenTree { Whitespace(' '), Number(2), Semicolon, + Whitespace('\n'), + Alphabetic(print), + Whitespace(' '), + Alphabetic(a), + Semicolon, ], } \ No newline at end of file