update code format

This commit is contained in:
Konarak 2022-07-19 22:57:14 +05:30
parent f6811aeade
commit d0835a9314
5 changed files with 94 additions and 95 deletions

View File

@ -1,2 +1,2 @@
profile = janestreet profile = janestreet
version = 0.23.0 version = 0.24.1

View File

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

View File

@ -1,44 +1,48 @@
open Base open Base
let symbols = Map.of_alist_exn (module String) [
"R0", 0; let symbols =
"R1", 1; Map.of_alist_exn
"R2", 2; (module String)
"R3", 3; [ "R0", 0
"R4", 4; ; "R1", 1
"R5", 5; ; "R2", 2
"R6", 6; ; "R3", 3
"R7", 7; ; "R4", 4
"R8", 8; ; "R5", 5
"R9", 9; ; "R6", 6
"R10", 10; ; "R7", 7
"R11", 11; ; "R8", 8
"R12", 12; ; "R9", 9
"R13", 13; ; "R10", 10
"R14", 14; ; "R11", 11
"R15", 15; ; "R12", 12
"SP", 0; ; "R13", 13
"LCL", 1; ; "R14", 14
"ARG", 2; ; "R15", 15
"THIS", 3; ; "SP", 0
"THAT", 4; ; "LCL", 1
"SCREEN", 16384; ; "ARG", 2
"KBD", 24576; ; "THIS", 3
] ; "THAT", 4
; "SCREEN", 16384
; "KBD", 24576
]
;;
let comp c = let comp c =
match c with match c with
| "0" -> "0101010" | "0" -> "0101010"
| "1" -> "0111111" | "1" -> "0111111"
| "-1" -> "0111010" | "-1" -> "0111010"
| "D" -> "0001100" | "D" -> "0001100"
| "A" -> "0110000" | "A" -> "0110000"
| "M" -> "1110000" | "M" -> "1110000"
| "!D" -> "0001101" | "!D" -> "0001101"
| "!A" -> "0110001" | "!A" -> "0110001"
| "!M" -> "1110001" | "!M" -> "1110001"
| "-D" -> "0001111" | "-D" -> "0001111"
| "-A" -> "0110011" | "-A" -> "0110011"
| "-M" -> "1110011" | "-M" -> "1110011"
| "D+1" -> "0011111" | "D+1" -> "0011111"
| "A+1" -> "0110111" | "A+1" -> "0110111"
| "M+1" -> "1110111" | "M+1" -> "1110111"
@ -60,20 +64,20 @@ let comp c =
let dest d = let dest d =
match d with match d with
| "" -> "000" | "" -> "000"
| "M" -> "001" | "M" -> "001"
| "D" -> "010" | "D" -> "010"
| "MD" -> "011" | "MD" -> "011"
| "A" -> "100" | "A" -> "100"
| "AM" -> "101" | "AM" -> "101"
| "AD" -> "110" | "AD" -> "110"
| "ADM" -> "111" | "ADM" -> "111"
| _ -> failwith "Unknown DEST Instruction" | _ -> failwith "Unknown DEST Instruction"
;; ;;
let jump j = let jump j =
match j with match j with
| "" -> "000" | "" -> "000"
| "JGT" -> "001" | "JGT" -> "001"
| "JEQ" -> "010" | "JEQ" -> "010"
| "JGE" -> "011" | "JGE" -> "011"

View File

@ -2,62 +2,60 @@ open Base
open Ast open Ast
open Predef open Predef
(* Check if symbol(k) exists in map(map), add it if it does not. *) let add_symbol map k d = if Map.mem map k then map else Map.add_exn map ~key:k ~data:d
let add_symbol map k d =
if Map.mem map k then map
else Map.add_exn map ~key:k ~data:d
;;
(* First Pass. FIXME *)
let rec first_pass exprs st loc = let rec first_pass exprs st loc =
match exprs with match exprs with
| [] -> st | [] -> st
| (Ginstr s)::t -> first_pass t (add_symbol st s loc) loc | Ginstr s :: t -> first_pass t (add_symbol st s loc) loc
| (Ainstr _ | Cinstr _ )::t -> first_pass t st (loc + 1) | (Ainstr _ | Cinstr _) :: t -> first_pass t st (loc + 1)
| _::t -> first_pass t st loc | _ :: t -> first_pass t st loc
;; ;;
(* Second Pass. FIXME *)
let rec second_pass exprs st loc = let rec second_pass exprs st loc =
let is_new s = let is_new s =
try Int.of_string s |> ignore; false try
with Failure _ -> not (Map.mem st s) in Int.of_string s |> ignore;
false
with
| Failure _ -> not (Map.mem st s)
in
match exprs with match exprs with
| [] -> st | [] -> st
| (Ainstr a)::t when (is_new a) -> second_pass t (add_symbol st a loc) (loc + 1) | Ainstr a :: t when is_new a -> second_pass t (add_symbol st a loc) (loc + 1)
| _::t -> second_pass t st loc | _ :: t -> second_pass t st loc
;; ;;
(* Decimal to Binary . FIXME *)
let translate_ainstr addr st = let translate_ainstr addr st =
let to_int addr = let to_int addr =
try Map.find_exn st addr try Map.find_exn st addr with
with Not_found_s _ -> Int.of_string addr in | Not_found_s _ -> Int.of_string addr
in
let pad binary = let pad binary =
let length = 16 - String.length binary in let length = 16 - String.length binary in
let prefix = String.init length ~f:(fun _ -> '0') in let prefix = String.init length ~f:(fun _ -> '0') in
String.concat [prefix; binary] in String.concat [ prefix; binary ]
in
let rec to_binary a = let rec to_binary a =
match a with match a with
| 0 -> "" | 0 -> ""
| _ -> | _ ->
let rem = a % 2 in let rem = a % 2 in
match rem with (match rem with
| 0 -> to_binary (a / 2) ^ "0" | 0 -> to_binary (a / 2) ^ "0"
| _ -> to_binary (a / 2) ^ "1" in | _ -> to_binary (a / 2) ^ "1")
in
pad (to_binary (to_int addr)) pad (to_binary (to_int addr))
;; ;;
let translate_cinstr (d, c, j) = let translate_cinstr (d, c, j) = String.concat [ "111"; comp c; dest d; jump j ]
String.concat ["111"; comp c; dest d; jump j]
;;
let rec _translate exprs st tt = let rec _translate exprs st tt =
match exprs with match exprs with
| [] -> tt | [] -> tt
| (Ainstr a)::t -> translate_ainstr a st :: _translate t st tt | Ainstr a :: t -> translate_ainstr a st :: _translate t st tt
| (Cinstr (d, c, j))::t -> translate_cinstr (d, c, j) :: _translate t st tt | Cinstr (d, c, j) :: t -> translate_cinstr (d, c, j) :: _translate t st tt
| _::t -> _translate t st tt | _ :: t -> _translate t st tt
;; ;;
let parse s = let parse s =
@ -66,9 +64,7 @@ let parse s =
ast ast
;; ;;
let generate_exprs lines = let generate_exprs lines = List.map lines ~f:parse
List.map lines ~f:parse
;;
let generate_st exprs = let generate_st exprs =
let _st = first_pass exprs symbols 0 in let _st = first_pass exprs symbols 0 in

View File

@ -5,4 +5,4 @@
(library (library
(name hack) (name hack)
(libraries base core)) (libraries base))