nand2tetris/compiler/backend/lib/translate.ml

28 lines
761 B
OCaml
Raw Normal View History

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 _ -> []
)