-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.ml
More file actions
42 lines (37 loc) · 1.09 KB
/
print.ml
File metadata and controls
42 lines (37 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Ast = struct
open Ast
open Token
let rec expression = function
| Binary b ->
let left = expression b.left and right = expression b.right in
Printf.sprintf "(%s %s %s)" (to_string b.operator) left right
| Grouping g ->
let expr = expression g in
Printf.sprintf "(group %s)" expr
| Literal Nil -> "nil"
| Literal (Bool b) -> Bool.to_string b
| Literal (Number f) -> Float.to_string f
| Literal (String s) -> s
| Unary u ->
let str = to_string u.operator in
Printf.sprintf "(%s %s)" str @@ expression u.expr
| _ -> failwith "not implemented"
let literal = function
| Bool b -> Bool.to_string b
| Number f -> begin
let s = Float.to_string f in
let l = String.length s in
match s.[l - 1] = '.' with
| true -> String.sub s 0 (l - 1)
| false -> s
end
| String a -> a
| Nil -> "nil"
end
module Token = struct
open Lexer
let print xs =
let f token = Token.to_string token.t in
List.map f xs
let print_out xs = print xs |> List.iter print_endline
end