nand2tetris/compiler/backend/lib/translate.ml
Konarak b7dee58454
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
2024-09-13 17:03:52 -04:00

28 lines
761 B
OCaml

open! Ast
open! Base
let parse str =
let lexbuf = Lexing.from_string str in
Parser.prog Lexer.read lexbuf
let bootstrap =
[ "@256"; "D=A"; "@SP"; "M=D" ]
@ Functioncall.translate Call "Sys.init" 0
let translate class_name lines : string list =
lines
|> List.map ~f:parse
|> List.bind ~f:(function
| ArithmeticCommand a
-> Arithmetic.translate a
| MemoryAccessCommand (command, segment, offset)
-> Memoryaccess.translate command segment offset class_name
| ProgramFlowCommand (command, name)
-> Programflow.translate command name
| FunctionCallCommand (command, name, arg)
-> Functioncall.translate command name arg
| Return
-> Return.translate
| Comment _ -> []
)