2022-07-18 19:16:54 +00:00
|
|
|
open Base
|
|
|
|
open Ast
|
|
|
|
open Predef
|
|
|
|
|
2022-07-19 17:27:14 +00:00
|
|
|
let add_symbol map k d = if Map.mem map k then map else Map.add_exn map ~key:k ~data:d
|
2022-07-18 19:16:54 +00:00
|
|
|
|
|
|
|
let rec first_pass exprs st loc =
|
|
|
|
match exprs with
|
|
|
|
| [] -> st
|
2022-07-19 17:27:14 +00:00
|
|
|
| Ginstr s :: t -> first_pass t (add_symbol st s loc) loc
|
2022-07-19 17:41:42 +00:00
|
|
|
| (Ainstr _ | Aconst _ | Cinstr _) :: t -> first_pass t st (loc + 1)
|
2022-07-19 17:27:14 +00:00
|
|
|
| _ :: t -> first_pass t st loc
|
2022-07-18 19:16:54 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
let rec second_pass exprs st loc =
|
2022-07-19 17:41:42 +00:00
|
|
|
let is_new a = not (Map.mem st a) in
|
2022-07-18 19:16:54 +00:00
|
|
|
match exprs with
|
|
|
|
| [] -> st
|
2022-07-19 17:27:14 +00:00
|
|
|
| Ainstr a :: t when is_new a -> second_pass t (add_symbol st a loc) (loc + 1)
|
|
|
|
| _ :: t -> second_pass t st loc
|
|
|
|
;;
|
2022-07-18 19:16:54 +00:00
|
|
|
|
2022-07-19 17:41:42 +00:00
|
|
|
let translate_ainstr addr =
|
2022-07-19 17:27:14 +00:00
|
|
|
let pad binary =
|
2022-07-18 19:16:54 +00:00
|
|
|
let length = 16 - String.length binary in
|
|
|
|
let prefix = String.init length ~f:(fun _ -> '0') in
|
2022-07-19 17:27:14 +00:00
|
|
|
String.concat [ prefix; binary ]
|
|
|
|
in
|
2022-07-18 19:16:54 +00:00
|
|
|
let rec to_binary a =
|
|
|
|
match a with
|
|
|
|
| 0 -> ""
|
|
|
|
| _ ->
|
|
|
|
let rem = a % 2 in
|
2022-07-19 17:27:14 +00:00
|
|
|
(match rem with
|
|
|
|
| 0 -> to_binary (a / 2) ^ "0"
|
|
|
|
| _ -> to_binary (a / 2) ^ "1")
|
|
|
|
in
|
2022-07-19 17:41:42 +00:00
|
|
|
pad (to_binary addr)
|
2022-07-18 19:16:54 +00:00
|
|
|
;;
|
|
|
|
|
2022-07-19 17:27:14 +00:00
|
|
|
let translate_cinstr (d, c, j) = String.concat [ "111"; comp c; dest d; jump j ]
|
2022-07-18 19:16:54 +00:00
|
|
|
|
|
|
|
let parse s =
|
|
|
|
let lexbuf = Lexing.from_string s in
|
2022-07-19 17:27:14 +00:00
|
|
|
let ast = Parser.prog Lexer.read lexbuf in
|
2022-07-19 17:41:42 +00:00
|
|
|
let eval_ainstr str =
|
|
|
|
try Aconst (Int.of_string str) with
|
|
|
|
| Failure _ -> Ainstr str
|
|
|
|
in
|
|
|
|
let _parse expr =
|
|
|
|
match expr with
|
|
|
|
| Ainstr str -> eval_ainstr str
|
|
|
|
| _ -> expr
|
|
|
|
in
|
|
|
|
_parse ast
|
2022-07-18 19:16:54 +00:00
|
|
|
;;
|
|
|
|
|
2022-07-19 17:27:14 +00:00
|
|
|
let generate_exprs lines = List.map lines ~f:parse
|
2022-07-19 17:41:42 +00:00
|
|
|
let generate_st exprs = second_pass exprs (first_pass exprs symbols 0) 16
|
2022-07-18 19:16:54 +00:00
|
|
|
|
2022-07-19 17:41:42 +00:00
|
|
|
let rec _translate exprs st tt =
|
|
|
|
match exprs with
|
|
|
|
| [] -> tt
|
|
|
|
| Aconst a :: t -> translate_ainstr a :: _translate t st tt
|
|
|
|
| Ainstr a :: t -> translate_ainstr (Map.find_exn st a) :: _translate t st tt
|
|
|
|
| Cinstr (d, c, j) :: t -> translate_cinstr (d, c, j) :: _translate t st tt
|
|
|
|
| _ :: t -> _translate t st tt
|
2022-07-18 19:16:54 +00:00
|
|
|
;;
|
|
|
|
|
2022-07-19 17:27:14 +00:00
|
|
|
let translate lines =
|
2022-07-18 19:16:54 +00:00
|
|
|
let exprs = generate_exprs lines in
|
|
|
|
let st = generate_st exprs in
|
|
|
|
_translate exprs st []
|
|
|
|
;;
|