fix tests
This commit is contained in:
parent
2f0ae5e2a5
commit
949f354c22
1 changed files with 37 additions and 30 deletions
67
src/test.rs
67
src/test.rs
|
@ -5,30 +5,36 @@ use crate::{
|
|||
#[test]
|
||||
fn test_vm() {
|
||||
let mut vm = VM::new(0x1000);
|
||||
let mut linker = Linker::new(0x100);
|
||||
let mut linker = Linker::new();
|
||||
|
||||
linker.link(&mut vm, "@".into(), crate::Assembler::new()
|
||||
.instruction( crate::VM_Instruction::Fetch )
|
||||
linker.add_procedure("@",
|
||||
crate::Assembler::new()
|
||||
.inst( crate::VM_Instruction::Fetch )
|
||||
.build());
|
||||
linker.link(&mut vm, "!".into(), crate::Assembler::new()
|
||||
.instruction( crate::VM_Instruction::Store )
|
||||
linker.add_procedure("!",
|
||||
crate::Assembler::new()
|
||||
.inst( crate::VM_Instruction::Store )
|
||||
.build());
|
||||
linker.link(&mut vm, "addi".into(), crate::Assembler::new()
|
||||
.instruction( crate::VM_Instruction::Add )
|
||||
|
||||
linker.add_procedure("i+",
|
||||
crate::Assembler::new()
|
||||
.inst( crate::VM_Instruction::Add )
|
||||
.build());
|
||||
linker.link(&mut vm, "subi".into(), crate::Assembler::new()
|
||||
.instruction( crate::VM_Instruction::BitwiseNot )
|
||||
|
||||
linker.add_procedure("i-",
|
||||
crate::Assembler::new()
|
||||
.inst( crate::VM_Instruction::BitwiseNot )
|
||||
.lit(1)
|
||||
.instruction( crate::VM_Instruction::Add )
|
||||
.instruction( crate::VM_Instruction::Add )
|
||||
.inst( crate::VM_Instruction::Add )
|
||||
.inst( crate::VM_Instruction::Add )
|
||||
.build());
|
||||
|
||||
// declare variable 'x' at address 0x86
|
||||
linker.link(&mut vm, "x".into(), crate::Assembler::new()
|
||||
linker.add_procedure("x",
|
||||
crate::Assembler::new()
|
||||
.lit(0x86)
|
||||
.build());
|
||||
|
||||
|
||||
/*
|
||||
* compile & run the following program:
|
||||
*
|
||||
|
@ -41,30 +47,31 @@ fn test_vm() {
|
|||
* 666
|
||||
* }
|
||||
*/
|
||||
linker.link(&mut vm, "main".into(), crate::Assembler::new()
|
||||
linker.add_procedure("main",
|
||||
crate::Assembler::new()
|
||||
// x = 123
|
||||
.lit(100)
|
||||
.lit(23)
|
||||
.call( linker.resolve_symbol(&"addi".into()).expect("unknown symbol 'addi'") )
|
||||
.call( linker.resolve_symbol(&"x".into()).expect("unknown symbol 'x'") )
|
||||
.call( linker.resolve_symbol(&"!".into()).expect("unknown symbol '!'") )
|
||||
.lit(100).lit(23).call("i+")
|
||||
.call("x").call("!")
|
||||
|
||||
// 200 - x
|
||||
// if ( 123 - x ) { emit '*' } else { emit '+' }
|
||||
.lit(123)
|
||||
.call( linker.resolve_symbol(&"x".into()).expect("unknown symbol 'x'") )
|
||||
.call( linker.resolve_symbol(&"@".into()).expect("unknown symbol '@'") )
|
||||
.call( linker.resolve_symbol(&"subi".into()).expect("unknown symbol 'subi'") )
|
||||
.call("x").call("@")
|
||||
.call("i-")
|
||||
.branch(
|
||||
crate::Assembler::new()
|
||||
.lit(42).instruction(crate::VM_Instruction::Emit)
|
||||
.lit(42).inst(crate::VM_Instruction::Emit)
|
||||
.lit(111),
|
||||
crate::Assembler::new()
|
||||
.lit(43).instruction(crate::VM_Instruction::Emit)
|
||||
.lit(222)
|
||||
)
|
||||
.lit(43).inst(crate::VM_Instruction::Emit)
|
||||
.lit(222))
|
||||
.build());
|
||||
|
||||
vm.execute( linker.resolve_symbol(&"main".into()).expect("unknown symbol 'main'") );
|
||||
assert_eq!( vm.data_stack, vec![ 222 ] );
|
||||
println!("\nvm.datastack = {:?}", vm.data_stack);
|
||||
let main_addr = linker.get_link_addr(&"main".into()).expect("main not foudn");
|
||||
|
||||
let bytecode = linker.link_total().expect("link error");
|
||||
vm.load( bytecode );
|
||||
vm.execute( main_addr );
|
||||
|
||||
assert_eq!( vm.data_stack, vec![ 222 ] );
|
||||
println!("\nvm.datastack = {:?}", vm.data_stack);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue