Skip to content

Commit

Permalink
finish else if and if
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Mar 8, 2024
1 parent ef02f3c commit 52bd97a
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 21 deletions.
124 changes: 105 additions & 19 deletions rust/src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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!(
Expand All @@ -1010,32 +1034,94 @@ if a<8{
} else if a>11 {
} else {
if 8 == 7 {
if 8 == 7 {
} else {
} else {
}
}
}"#,
t
);
t.generate_code().unwrap();
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
}
]
)
}
Expand Down
3 changes: 1 addition & 2 deletions rust/src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 52bd97a

Please sign in to comment.