diff options
author | elioat <elioat@tilde.institute> | 2025-01-19 10:31:24 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-01-19 10:31:24 -0500 |
commit | 2ff0f0f3a02cc6d921dcb2cc278679306b316a13 (patch) | |
tree | 175ed51b3d0213f940cc84d0b95bc9877c230e60 | |
parent | 0e5af6d78907f8d7e640a76557522d9e9706fdd0 (diff) | |
download | tour-2ff0f0f3a02cc6d921dcb2cc278679306b316a13.tar.gz |
*
-rwxr-xr-x | awk/vm/vm.awk | 17 | ||||
-rwxr-xr-x[-rw-r--r--] | awk/vm/vm_tests.sh | 24 |
2 files changed, 33 insertions, 8 deletions
diff --git a/awk/vm/vm.awk b/awk/vm/vm.awk index e53bf1a..0d7565f 100755 --- a/awk/vm/vm.awk +++ b/awk/vm/vm.awk @@ -162,6 +162,13 @@ function print_stack() { printf "\n" } +function print_state() { + print_stack() + printf "Registers: A=%d B=%d P=%d\n", A, B, P + printf "Memory[P]=%d Memory[B]=%d\n", memory[P], memory[B] + printf "-------------------\n" +} + function execute_instruction(inst) { if (inst ~ /^[0-9]+$/) { # Numbers are pushed onto the stack @@ -198,11 +205,17 @@ function execute_instruction(inst) { # Main execution loop { + # Remove comments (everything after #) + sub(/#.*$/, "") + + # Skip empty lines after comment removal + if (NF == 0) next + # Split the input line into words n = split($0, words) for (i = 1; i <= n; i++) { execute_instruction(words[i]) } - # Print stack after each line of input - print_stack() + # Print state after each line of input + print_state() } diff --git a/awk/vm/vm_tests.sh b/awk/vm/vm_tests.sh index b3bfd6b..8180de9 100644..100755 --- a/awk/vm/vm_tests.sh +++ b/awk/vm/vm_tests.sh @@ -4,27 +4,39 @@ echo "Running VM tests..." echo echo "Test 1: Basic register A operations" -echo "42 A! A A" | awk -f vm.awk +echo "42 A! A A # Store 42 in A, then read A twice" | awk -f vm.awk echo echo "Test 2: Register A and B with memory operations" -echo "100 A! 200 B! 42 @B" | awk -f vm.awk +echo "100 A! 200 B! 42 B !B @B # Store 100 in A, 200 in B, store 42 at B's address" | awk -f vm.awk echo echo "Test 3: Sequential memory operations using P register" -echo "1 2 3 4 5 !+ !+ !+ !+ !+ 0 P! @+ @+ @+ @+ @+" | awk -f vm.awk +echo "0 P! 1 !+ 2 !+ 3 !+ 4 !+ 5 !+ # Store 1-5 sequentially using P" | awk -f vm.awk +echo "0 P! @+ @+ @+ @+ @+ # Read back values using P" | awk -f vm.awk echo echo "Test 4: Complex register manipulation" -echo "42 A! A DUP B! @B" | awk -f vm.awk +echo "42 A! A DUP B! @B # Store 42 in A, copy to B, read via B" | awk -f vm.awk echo echo "Test 5: Register arithmetic" -echo "5 A! 3 B! A B! @B A +" | awk -f vm.awk +echo "5 A! 3 B! A B! @B A + # Store 5 in A, 3 in B, add A + mem[B]" | awk -f vm.awk echo echo "Test 6: Memory pointer operations" -echo "42 0 ! 1 P! @P" | awk -f vm.awk +echo "42 0 ! 1 P! @P # Store 42 at addr 0, point P to 1, read P" | awk -f vm.awk +echo + +echo "Test 7: Register and memory interaction" +echo "10 A! 20 B! A B ! # Store A's value at B's address" | awk -f vm.awk +echo "B @ # Read value at B's address" | awk -f vm.awk +echo + +echo "Test 8: Demonstrating B! vs !B" +echo "100 B! # Set B register to 100" | awk -f vm.awk +echo "42 !B # Store 42 at memory location 100" | awk -f vm.awk +echo "@B # Read from memory location 100" | awk -f vm.awk echo echo "Tests completed." \ No newline at end of file |