nand2tetris/compiler/lib/be_translate.ml

25 lines
551 B
OCaml

open Base
open Be_ast
open Be_predef
let parse s =
let lexbuf = Lexing.from_string s in
let ast = Be_parser.prog Be_lexer.read lexbuf in
ast
;;
let generate_exprs lines = List.map lines ~f:parse
let rec _translate expr loc tt =
match expr with
| [] -> tt
| Acommand a :: t -> a_command ?loc:(Some loc) a @ _translate t (loc+1) tt
| Mcommand (c, s, a) :: t -> m_command (c, s, a) @ _translate t loc tt
| _ :: t -> _translate t loc tt
;;
let translate lines =
let exprs = generate_exprs lines in
_translate exprs 0 [] @ _end
;;