finish project eight backend compiler

- modify existing implementation to use variant types for a more
  accurate representation of the vm byte code
- switch to fold from recursion for the main translate function
- use separate modules for translating different vm commands
- move static arithmetic command translations to a map
This commit is contained in:
2024-09-13 16:37:20 -04:00
parent 5973c64e78
commit b7dee58454
35 changed files with 633 additions and 302 deletions

View File

@@ -0,0 +1,3 @@
(tests
(names test_backend)
(libraries backend ounit2))

View File

@@ -0,0 +1,31 @@
open OUnit2
open Backend.Ast
open Backend.Translate
let peq case value = assert_equal value (parse case)
let test_parse_a_command _ =
peq "add" (ArithmeticCommand Add);
peq "sub" (ArithmeticCommand Sub);
peq "neg" (ArithmeticCommand Neg);
peq "eq" (ArithmeticCommand Eq);
peq "gt" (ArithmeticCommand Gt);
peq "lt" (ArithmeticCommand Lt);
peq "and" (ArithmeticCommand And);
peq "or" (ArithmeticCommand Or);
peq "not" (ArithmeticCommand Not)
;;
let test_parse_m_command _ =
peq "pop local 12" (MemoryAccessCommand (Pop, Local, 12));
peq "push argument 4" (MemoryAccessCommand (Push, Argument, 4))
;;
let suite =
"suite"
>::: [ "A Instruction" >:: test_parse_a_command
; "M Instruction" >:: test_parse_m_command
]
;;
let () = run_test_tt_main suite