From 52bd97ad0b64fa931550855c5363380e305a25d3 Mon Sep 17 00:00:00 2001 From: limuy Date: Fri, 8 Mar 2024 17:33:21 +0800 Subject: [PATCH] finish else if and if --- rust/src/compiler/ast.rs | 124 +++++++++++++++++++++++++++++++------ rust/src/compiler/token.rs | 3 +- 2 files changed, 106 insertions(+), 21 deletions(-) diff --git a/rust/src/compiler/ast.rs b/rust/src/compiler/ast.rs index d74e8be3..e5aac412 100644 --- a/rust/src/compiler/ast.rs +++ b/rust/src/compiler/ast.rs @@ -682,8 +682,15 @@ impl<'a> AstBuilder<'a> { if t.tp == TokenType::Else { save_jump_opcode_idx.push(self.staticdata.get_last_opcode_id()); self.add_bycode(Opcode::Jump, 0); - self.check_next_token(TokenType::If)?; - self.check_next_token(TokenType::LeftBigBrace)?; + let nxt_tok = self.token_lexer.next_token()?; + if nxt_tok.tp == TokenType::If { + // self.check_next_token(TokenType::If)?; + self.expr(false)?; + self.check_next_token(TokenType::LeftBigBrace)?; + } else { + self.token_lexer.next_back(nxt_tok); + self.check_next_token(TokenType::LeftBigBrace)?; + } continue; } self.token_lexer.next_back(t); @@ -1001,6 +1008,23 @@ mod tests { ) } + #[test] + fn test_if_easy2() { + gen_test_env!( + r#" + if 1==1 { + print("hello world") + } else if 1==2 { + + } else { + print("hello world") + } + "#, + t + ); + t.generate_code().unwrap(); + } + #[test] fn test_if() { gen_test_env!( @@ -1010,11 +1034,11 @@ if a<8{ } else if a>11 { } else { -if 8 == 7 { + if 8 == 7 { -} else { + } else { -} + } }"#, t ); @@ -1022,20 +1046,82 @@ if 8 == 7 { assert_eq!( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 9), - Inst::new(Opcode::LoadInt, 8), - Inst::new(Opcode::LtInt, NO_ARG), - Inst::new(Opcode::JumpIfFalse, 0), - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::Jump, 0), - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::LoadInt, 11), - Inst::new(Opcode::GtInt, NO_ARG), - Inst::new(Opcode::JumpIfFalse, 0), - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::Jump, 0), - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::LoadInt, 7) + Inst { + opcode: Opcode::LoadInt, + operand: 2 + }, + Inst { + opcode: Opcode::StoreInt, + operand: 0 + }, + Inst { + opcode: Opcode::LoadVarInt, + operand: 0 + }, + Inst { + opcode: Opcode::LoadInt, + operand: 3 + }, + Inst { + opcode: Opcode::LtInt, + operand: 0 + }, + Inst { + opcode: Opcode::JumpIfFalse, + operand: 19 + }, + Inst { + opcode: Opcode::Jump, + operand: 0 + }, + Inst { + opcode: Opcode::LoadVarInt, + operand: 0 + }, + Inst { + opcode: Opcode::LoadInt, + operand: 4 + }, + Inst { + opcode: Opcode::GtInt, + operand: 0 + }, + Inst { + opcode: Opcode::JumpIfFalse, + operand: 19 + }, + Inst { + opcode: Opcode::Jump, + operand: 0 + }, + Inst { + opcode: Opcode::JumpIfFalse, + operand: 19 + }, + Inst { + opcode: Opcode::LoadInt, + operand: 3 + }, + Inst { + opcode: Opcode::LoadInt, + operand: 5 + }, + Inst { + opcode: Opcode::EqInt, + operand: 0 + }, + Inst { + opcode: Opcode::JumpIfFalse, + operand: 19 + }, + Inst { + opcode: Opcode::Jump, + operand: 0 + }, + Inst { + opcode: Opcode::JumpIfFalse, + operand: 19 + } ] ) } diff --git a/rust/src/compiler/token.rs b/rust/src/compiler/token.rs index 16a73dfb..258fe7a6 100644 --- a/rust/src/compiler/token.rs +++ b/rust/src/compiler/token.rs @@ -861,8 +861,7 @@ impl Drop for TokenLex<'_> { fn drop(&mut self) { // check the braces stack if let Err(e) = self.check() { - eprintln!("{}", e); - exit(1); + panic!("{}", e); } } }