[WIP] initial compiler backend code

This commit is contained in:
2022-08-03 18:34:42 +05:30
parent 1570cf41f4
commit dae97b7a76
14 changed files with 273 additions and 0 deletions

31
compiler/bin/compiler.ml Normal file
View File

@@ -0,0 +1,31 @@
open Core
open Backend
let read_file file =
let not_empty str = not (String.is_empty str) in
List.filter (In_channel.read_lines file) ~f:not_empty
;;
let outfile file = String.concat [ Filename.chop_extension file; ".asm" ]
let gen_hack file =
let ircode = read_file file in
let assembly = Be_translate.translate ircode in
let outchan = Out_channel.create (outfile file) in
Out_channel.output_lines outchan assembly;
Out_channel.close outchan
;;
let param =
let open Command.Param in
anon ("filename" %: string)
;;
let command =
Command.basic
~summary:"Translate <filename>.vm to <filename>.asm"
~readme:(fun () -> "Compiler (backend) for project 7 of Nand2Tetris")
(Command.Param.map param ~f:(fun filename () -> gen_hack filename))
;;
let () = Command_unix.run command

3
compiler/bin/dune Normal file
View File

@@ -0,0 +1,3 @@
(executable
(name compiler)
(libraries backend core core_unix.command_unix))