diff --git a/examples/expected_result/control_flow.txt b/examples/expected_result/control_flow.txt index 6dae7f0c..b4498763 100644 --- a/examples/expected_result/control_flow.txt +++ b/examples/expected_result/control_flow.txt @@ -5,3 +5,4 @@ 1 2 4 +5 diff --git a/examples/expected_result/func_call.txt b/examples/expected_result/func_call.txt index f5a927c1..92525b62 100644 --- a/examples/expected_result/func_call.txt +++ b/examples/expected_result/func_call.txt @@ -1,3 +1,2 @@ 908977 987 - diff --git a/examples/expected_result/if.txt b/examples/expected_result/if.txt index 8d25caa3..7242fb86 100644 --- a/examples/expected_result/if.txt +++ b/examples/expected_result/if.txt @@ -1,16 +1 @@ -a:=90 -if a==90 { - println("i equal to 90") -} -if a==89 { - println("i equal to 89") -} -if a < 89 { - println("i less than 90") -} else { - if a % 2 == 0 { - println("i is even") - } else { - println("i is odd") - } -} +i equal to 90 diff --git a/examples/expected_result/match.txt b/examples/expected_result/match.txt index 368e44ee..55382c07 100644 --- a/examples/expected_result/match.txt +++ b/examples/expected_result/match.txt @@ -1 +1,2 @@ get 90 +out of range diff --git a/examples/func_call.trc b/examples/func_call.trc index 7c40a526..7a996954 100644 --- a/examples/func_call.trc +++ b/examples/func_call.trc @@ -18,4 +18,4 @@ t1(90, 89, 77) a:=9 b:=8 c:=7 -print("{}{}{}", a, b, c) +println("{}{}{}", a, b, c) diff --git a/examples/helloworld.trc b/examples/helloworld.trc index ad35e5ae..dfe24c9e 100644 --- a/examples/helloworld.trc +++ b/examples/helloworld.trc @@ -1 +1 @@ -print("Hello World") +print("Hello World\n") diff --git a/examples/kmp.trc b/examples/kmp.trc index fb1b1db4..af08ab87 100644 --- a/examples/kmp.trc +++ b/examples/kmp.trc @@ -1,2 +1,2 @@ import "std.algo.string" -print("{}", string::kmp("ababa", "aba")) +println("{}", string::kmp("ababa", "aba")) diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index fc8f30b7..9b06e150 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -1305,11 +1305,18 @@ impl ModuleUnit { fn lex_function(&mut self, funcid: usize, body: &FuncBodyTy) -> RuntimeResult<(usize, usize)> { if !self.first_func { // 如果不是第一个函数,在末尾加上结束主程序的指令 + // println!("run here.Inst num is {}", self.staticdata.inst.len()); self.first_func = true; self.add_bycode(Opcode::Stop, NO_ARG); + // println!( + // "run here.Inst num is {}.Op {}", + // self.staticdata.inst.len(), + // self.staticdata.get_next_opcode_id() + // ); self.staticdata.function_split = Some(self.staticdata.get_last_opcode_id()); } let begin_inst_idx = self.staticdata.get_next_opcode_id(); + // println!("start addr:{}", begin_inst_idx); let func_obj = self .self_scope .borrow() @@ -1361,6 +1368,7 @@ impl ModuleUnit { swap(&mut tmp, &mut self.self_scope.borrow_mut().funcs_temp_store); for i in tmp { let (code_begin, var_mem_sz) = self.lex_function(i.0, &i.1)?; + // println!("{}", code_begin); self.staticdata .funcs_pos .push(FuncStorage::new(code_begin, var_mem_sz)) diff --git a/src/compiler/linker.rs b/src/compiler/linker.rs index 224ff254..7c5ab4f2 100644 --- a/src/compiler/linker.rs +++ b/src/compiler/linker.rs @@ -50,7 +50,8 @@ pub fn link<'a>(data_iter: impl Iterator) -> StaticDa if inst_ori_id == pos.func_addr { function_expected_pos = iter_of_function.next(); let mut added = pos.clone(); - added.func_addr = data.get_last_opcode_id() as usize; + // 该项指令还没有加入,所以是next + added.func_addr = data.get_next_opcode_id() as usize; func_record_tmp.push(added); } } diff --git a/src/tools/dis.rs b/src/tools/dis.rs index b14feab4..74572d76 100644 --- a/src/tools/dis.rs +++ b/src/tools/dis.rs @@ -22,7 +22,7 @@ pub fn dis(opt: crate::compiler::CompileOption, rustcode: bool) -> anyhow::Resul for i in &static_data.dll_module_should_loaded { println!("{}", i); } - println!(); + println!("Inst:"); for i in static_data.inst.iter().enumerate() { if rustcode { if i.1.operand.1 == ARG_WRONG { @@ -64,6 +64,12 @@ pub fn dis(opt: crate::compiler::CompileOption, rustcode: bool) -> anyhow::Resul println!(); } } - + println!("Function Info:"); + for i in &static_data.funcs_pos { + println!( + "Function address:{} | Function Var table size:{}", + i.func_addr, i.var_table_sz + ); + } Ok(()) } diff --git a/src/tvm.rs b/src/tvm.rs index 2c8ed71b..8dd8314f 100644 --- a/src/tvm.rs +++ b/src/tvm.rs @@ -157,6 +157,7 @@ impl<'a> Vm<'a> { #[inline] fn run_opcode(&mut self, pc: &mut usize) -> Result<(), RuntimeError> { + // println!("run opcode:{}", self.static_data.inst[*pc].opcode); match self.static_data.inst[*pc].opcode { Opcode::Add => operator_opcode!(add, self), Opcode::Sub => operator_opcode!(sub, self), @@ -430,6 +431,7 @@ impl<'a> Vm<'a> { } Opcode::Jump => { *pc = self.static_data.inst[*pc].operand.0 as usize; + // *pc += 1; return Ok(()); } Opcode::LoadChar => unsafe { diff --git a/stdlib/src/prelude.rs b/stdlib/src/prelude.rs index a0b2621a..d90455f1 100644 --- a/stdlib/src/prelude.rs +++ b/stdlib/src/prelude.rs @@ -14,6 +14,7 @@ pub fn print(fmt_string: str) -> void { .write_all((**iter.next().unwrap()).to_string().as_bytes()) .unwrap(); } + // print!("{}", unsafe { (**iter.next().unwrap()).to_string() }); if let Some(j) = output_iter.next() { if j != '}' { return Err(ErrorInfo::new(t!(UNCLOSED_FORMAT), t!(FORMAT_STR_ERROR))); @@ -21,6 +22,7 @@ pub fn print(fmt_string: str) -> void { } } else { io::stdout().write_all(&[i as u8]).unwrap(); + // print!("{}", i); } } } diff --git a/tests/test_all_examples.rs b/tests/test_all_examples.rs index 313a6807..883f6b91 100644 --- a/tests/test_all_examples.rs +++ b/tests/test_all_examples.rs @@ -17,7 +17,7 @@ pub fn test_run_examples() { ans_path.push("expected_result"); ans_path.push(path.file_name().expect("not file name")); ans_path.set_extension("txt"); - println!("{}", ans_path.display()); + println!("checking {}", ans_path.display()); let expected_res = read_to_string(ans_path).unwrap(); let assert = cmd.arg("run").arg(path); assert.assert().success().stdout(expected_res);