nand2tetris/assembler/bin/assembler.ml

32 lines
797 B
OCaml
Raw Permalink Normal View History

2022-07-18 19:16:54 +00:00
open Core
open Hack
2022-07-19 17:27:14 +00:00
let read_file file =
let not_empty str = not (String.is_empty str) in
List.filter (In_channel.read_lines file) ~f:not_empty
2022-07-18 19:16:54 +00:00
;;
2022-07-19 17:27:14 +00:00
let outfile file = String.concat [ Filename.chop_extension file; ".hack" ]
2022-07-18 19:16:54 +00:00
let gen_hack file =
let assembly = read_file file in
let binary = Translate.translate assembly in
let outchan = Out_channel.create (outfile file) in
Out_channel.output_lines outchan binary;
2022-07-19 17:27:14 +00:00
Out_channel.close outchan
2022-07-18 19:16:54 +00:00
;;
let param =
let open Command.Param in
2022-07-19 17:27:14 +00:00
anon ("filename" %: string)
2022-07-18 19:16:54 +00:00
;;
let command =
Command.basic
~summary:"Translate <filename>.asm to <filename>.hack"
2022-07-19 17:27:14 +00:00
~readme:(fun () -> "Assembler for project 6 of Nand2Tetris")
(Command.Param.map param ~f:(fun filename () -> gen_hack filename))
2022-07-18 19:16:54 +00:00
;;
2022-07-19 17:27:14 +00:00
let () = Command_unix.run command