nand2tetris/compiler/backend/lib/translate.ml

27 lines
749 B
OCaml
Raw Permalink 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
2024-09-20 18:20:09 +00:00
| ArithmeticCommand command
-> Arithmetic.translate command
| 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
)