-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.ml
More file actions
74 lines (70 loc) · 2.24 KB
/
Copy pathsyntax.ml
File metadata and controls
74 lines (70 loc) · 2.24 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(* op_t : 2項演算子の型 *)
type op_t = Plus | Minus | Times | Divide | Equal | NotEqual | Less | LessEqual
(* 2項演算子を文字列にする関数 *)
(* op_to_string : op_t -> string *)
let op_to_string op = match op with
Plus -> " + "
| Minus -> " - "
| Times -> " * "
| Divide -> " / "
| Equal -> " = "
| NotEqual -> " <> "
| Less -> " < "
| LessEqual -> " <= "
(* Syntax.t : プログラムを表す型 *)
type t = Number of int
| Bool of bool
| Var of string
| Op of t * op_t * t
| If of t * t * t
| Let of string * t * t
| Letrec of string * string * t * t
| Fun of string * t
| App of t * t
| Try of t * t
| Shift of string * t
| Control of string * t
| Shift0 of string * t
| Control0 of string * t
| Angle_bracket of t
(* プログラムを文字列にする関数 *)
(* Syntax.to_string : Syntax.t -> string *)
let rec to_string exp = match exp with
Number (n) -> string_of_int n
| Bool (b) -> if b then "true" else "false"
| Var (x) -> x
| Op (arg1, op, arg2) ->
"(" ^ to_string arg1
^ op_to_string op
^ to_string arg2 ^ ")"
| If (p, t, e) ->
"(if " ^ to_string p ^
" then " ^ to_string t ^
" else " ^ to_string e ^ ")"
| Let (x, t1, t2) ->
"(let " ^ x ^ " = " ^ to_string t1 ^
" in " ^ to_string t2 ^ ")"
| Letrec (f, x, t1, t2) ->
"(let rec " ^ f ^ " " ^ x ^ " = " ^ to_string t1 ^
" in " ^ to_string t2 ^ ")"
| Fun (x, t) ->
"(fun " ^ x ^ " -> " ^ to_string t ^ ")"
| App (t1, t2) ->
"(" ^ to_string t1 ^ " " ^ to_string t2 ^ ")"
| Try (t1, t2) ->
"(try " ^ to_string t1 ^ " with _ -> " ^ to_string t2 ^ ")"
| Shift (x, t) ->
"(shift " ^ x ^ " -> " ^ to_string t ^ ")"
| Control (x, t) ->
"(control " ^ x ^ " -> " ^ to_string t ^ ")"
| Shift0 (x, t) ->
"(shift0 " ^ x ^ " -> " ^ to_string t ^ ")"
| Control0 (x, t) ->
"(control0 " ^ x ^ " -> " ^ to_string t ^ ")"
| Angle_bracket (t) ->
"< " ^ to_string t ^ ">0"
(* プログラムをプリントする関数 *)
(* Syntax.print : Syntax.t -> unit *)
let print exp =
let str = to_string exp in
print_string str