Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Mar 9, 2024
1 parent 52bd97a commit 3769e65
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
13 changes: 13 additions & 0 deletions rust/examples/if.trc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
a:=90
if a==90 {
println("i equal to 90")
}
if a < 89 {
println("i less than 90")
} else {
if a % 2 == 0 {
println("i is even")
} else {
println("i is odd")
}
}
43 changes: 18 additions & 25 deletions rust/src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,18 +680,27 @@ impl<'a> AstBuilder<'a> {
self.staticdata.inst[op_idx].operand = self.staticdata.get_last_opcode_id() + 1;
let t = self.token_lexer.next_token()?;
if t.tp == TokenType::Else {
save_jump_opcode_idx.push(self.staticdata.get_last_opcode_id());
self.add_bycode(Opcode::Jump, 0);
let nxt_tok = self.token_lexer.next_token()?;
if nxt_tok.tp == TokenType::If {
save_jump_opcode_idx.push(self.staticdata.get_last_opcode_id());
self.add_bycode(Opcode::Jump, 0);
// 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(nxt_tok);
self.check_next_token(TokenType::LeftBigBrace)?;
loop {
let t = self.token_lexer.next_token()?;
if t.tp == TokenType::RightBigBrace {
break;
}
self.token_lexer.next_back(t);
self.statement()?;
}
continue;
last_should_be_jumped = self.staticdata.get_last_opcode_id() + 1;
break;
}
self.token_lexer.next_back(t);
last_should_be_jumped = self.staticdata.get_last_opcode_id() + 1;
Expand Down Expand Up @@ -1068,7 +1077,7 @@ if a<8{
},
Inst {
opcode: Opcode::JumpIfFalse,
operand: 19
operand: 15
},
Inst {
opcode: Opcode::Jump,
Expand All @@ -1088,15 +1097,7 @@ if a<8{
},
Inst {
opcode: Opcode::JumpIfFalse,
operand: 19
},
Inst {
opcode: Opcode::Jump,
operand: 0
},
Inst {
opcode: Opcode::JumpIfFalse,
operand: 19
operand: 11
},
Inst {
opcode: Opcode::LoadInt,
Expand All @@ -1112,15 +1113,7 @@ if a<8{
},
Inst {
opcode: Opcode::JumpIfFalse,
operand: 19
},
Inst {
opcode: Opcode::Jump,
operand: 0
},
Inst {
opcode: Opcode::JumpIfFalse,
operand: 19
operand: 15
}
]
)
Expand Down
1 change: 0 additions & 1 deletion rust/src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,6 @@ impl TokenLex<'_> {
pub fn check(&mut self) -> Result<(), RuntimeError> {
if !self.braces_check.is_empty() {
let unmatch_char = self.braces_check.pop().unwrap();
self.clear_error();
return self.report_error_with_context(RuntimeError::new(
Box::new(Context::new_line(
&self.compiler_data.context.module_name,
Expand Down
24 changes: 18 additions & 6 deletions rust/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,10 @@ impl<'a> Vm<'a> {
}
Opcode::MoveStr => {
// todo:inmprove performance
let ptr = self.dynadata.gc.alloc(TrcStr::new(unsafe {
self.dynadata.str_stack.pop().unwrap()
}));
let ptr = self
.dynadata
.gc
.alloc(TrcStr::new(self.dynadata.str_stack.pop().unwrap()));
self.dynadata.obj_stack.push(ptr);
}
Opcode::StoreInt => {
Expand Down Expand Up @@ -590,9 +591,7 @@ impl<'a> Vm<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::compiler::Compiler;
use crate::compiler::InputSource;
use crate::compiler::Option;
use crate::compiler::*;

fn gen_test_env(code: &str) -> Vm {
let mut compiler =
Expand All @@ -615,4 +614,17 @@ mod tests {
);
vm.run().unwrap()
}

#[test]
fn test_if_easy() {
let mut vm = gen_test_env(r#"if 1==1 { print("hello world") }"#);
vm.run().unwrap()
}

#[test]
fn test_if_easy2() {
let mut vm =
gen_test_env(r#"if 1==1 { print("hello world") } else { print("hello world2") }"#);
vm.run().unwrap()
}
}
2 changes: 1 addition & 1 deletion rust/src/tvm/stdlib/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn println(fmt_string: str) -> void {
io::stdout().write_all(b"\n").unwrap();
}

def_module!(module_name = prelude, functions = [print => print, println => print], classes = [
def_module!(module_name = prelude, functions = [print => print, println => println], classes = [
TrcInt => int,
TrcStr => str,
TrcBool => bool,
Expand Down

0 comments on commit 3769e65

Please sign in to comment.