fix tests

This commit is contained in:
Michael Sippel 2024-05-09 15:41:45 +02:00
parent 2f0ae5e2a5
commit 949f354c22
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -5,30 +5,36 @@ use crate::{
#[test] #[test]
fn test_vm() { fn test_vm() {
let mut vm = VM::new(0x1000); 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() linker.add_procedure("@",
.instruction( crate::VM_Instruction::Fetch ) crate::Assembler::new()
.inst( crate::VM_Instruction::Fetch )
.build()); .build());
linker.link(&mut vm, "!".into(), crate::Assembler::new() linker.add_procedure("!",
.instruction( crate::VM_Instruction::Store ) crate::Assembler::new()
.inst( crate::VM_Instruction::Store )
.build()); .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()); .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) .lit(1)
.instruction( crate::VM_Instruction::Add ) .inst( crate::VM_Instruction::Add )
.instruction( crate::VM_Instruction::Add ) .inst( crate::VM_Instruction::Add )
.build()); .build());
// declare variable 'x' at address 0x86 // declare variable 'x' at address 0x86
linker.link(&mut vm, "x".into(), crate::Assembler::new() linker.add_procedure("x",
crate::Assembler::new()
.lit(0x86) .lit(0x86)
.build()); .build());
/* /*
* compile & run the following program: * compile & run the following program:
* *
@ -41,30 +47,31 @@ fn test_vm() {
* 666 * 666
* } * }
*/ */
linker.link(&mut vm, "main".into(), crate::Assembler::new() linker.add_procedure("main",
crate::Assembler::new()
// x = 123 // x = 123
.lit(100) .lit(100).lit(23).call("i+")
.lit(23) .call("x").call("!")
.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 '!'") )
// 200 - x // if ( 123 - x ) { emit '*' } else { emit '+' }
.lit(123) .lit(123)
.call( linker.resolve_symbol(&"x".into()).expect("unknown symbol 'x'") ) .call("x").call("@")
.call( linker.resolve_symbol(&"@".into()).expect("unknown symbol '@'") ) .call("i-")
.call( linker.resolve_symbol(&"subi".into()).expect("unknown symbol 'subi'") )
.branch( .branch(
crate::Assembler::new() crate::Assembler::new()
.lit(42).instruction(crate::VM_Instruction::Emit) .lit(42).inst(crate::VM_Instruction::Emit)
.lit(111), .lit(111),
crate::Assembler::new() crate::Assembler::new()
.lit(43).instruction(crate::VM_Instruction::Emit) .lit(43).inst(crate::VM_Instruction::Emit)
.lit(222) .lit(222))
)
.build()); .build());
vm.execute( linker.resolve_symbol(&"main".into()).expect("unknown symbol 'main'") ); 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 ] ); assert_eq!( vm.data_stack, vec![ 222 ] );
println!("\nvm.datastack = {:?}", vm.data_stack); println!("\nvm.datastack = {:?}", vm.data_stack);
} }