-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.ml
164 lines (161 loc) · 5.5 KB
/
interpreter.ml
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
open Ast
let rec prefix_plus = function
| List l -> List l
| Operation o -> interpret (Operation o) |> prefix_plus
| _ -> failwith "Invalid application of prefix function" |> (fun _ -> List [])
and prefix_minus = function
| List l -> List (List.map (fun n -> -.n) l)
| Operation o -> interpret (Operation o) |> prefix_minus
| _ -> failwith "Invalid application of prefix function" |> (fun _ -> List [])
and prefix_times = function
| List l -> List (List.map (fun n -> if n > 0.0 then 1.0 else if n < 0.0 then -1.0 else 0.0) l)
| Operation o -> interpret (Operation o) |> prefix_times
| _ -> failwith "Invalid application of prefix function" |> (fun _ -> List [])
and prefix_divide = function
| List l -> List (List.map (fun n -> 1.0 /. n) l)
| Operation o -> interpret (Operation o) |> prefix_divide
| _ -> failwith "Invalid application of prefix function" |> (fun _ -> List [])
and prefix_stretch = function
| List l -> List [float_of_int (List.length l)]
| Operation o -> interpret (Operation o) |> prefix_stretch
| _ -> failwith "Invalid application of prefix function" |> (fun _ -> List [])
and infix_plus a b =
match (a, b) with
| (List a', List b') ->
begin
if List.length a' <> List.length b' then
failwith "Size error" |> (fun _ -> List [])
else
List (List.map2 ( +. ) a' b')
end
| (List _, Operation _) ->
begin
let r = interpret b in
infix_plus a r
end
| _ -> failwith "Invalid application of infix function" |> (fun _ -> List [])
and infix_minus a b =
match (a, b) with
| (List a', List b') ->
begin
if List.length a' <> List.length b' then
failwith "Size error" |> (fun _ -> List [])
else
List (List.map2 ( -. ) a' b')
end
| (List _, Operation _) ->
begin
let r = interpret b in
infix_minus a r
end
| _ -> failwith "Invalid application of infix function" |> (fun _ -> List [])
and infix_times a b =
match (a, b) with
| (List a', List b') ->
begin
if List.length a' <> List.length b' then
failwith "Size error" |> (fun _ -> List [])
else
List (List.map2 ( *. ) a' b')
end
| (List _, Operation _) ->
begin
let r = interpret b in
infix_times a r
end
| _ -> failwith "Invalid application of infix function" |> (fun _ -> List [])
and infix_divide a b =
match (a, b) with
| (List a', List b') ->
begin
if List.length a' <> List.length b' then
failwith "Size error" |> (fun _ -> List [])
else
List (List.map2 ( /. ) a' b')
end
| (List _, Operation _) ->
begin
let r = interpret b in
infix_divide a r
end
| _ -> failwith "Invalid application of infix function" |> (fun _ -> List [])
and infix_reduce a b =
match (a, b) with
| (List a', List b') ->
begin
if List.length a' <> List.length b' then
failwith "Size error" |> (fun _ -> List [])
else
List (Util.multimap2 (fun l r -> if l < 0.0 then Batteries.List.make (-int_of_float l) 0.0 else BatList.make (int_of_float l) r) a' b')
end
| (List _, Operation _) ->
begin
let r = interpret b in
infix_reduce a r
end
| _ -> failwith "Invalid application of infix function" |> (fun _ -> List [])
and infix_stretch a b =
match (a, b) with
| (List a', List b') -> List (a' @ b')
| (List _, Operation _) ->
begin
let r = interpret b in
infix_stretch a r
end
| _ -> failwith "Invalid application of infix function" |> (fun _ -> List [])
and interpret = function
| List l -> List l
| Operation a ->
begin
match a with
| Prefix (op, ast) ->
begin
match op with
| Plus -> prefix_plus ast
| Minus -> prefix_minus ast
| Times -> prefix_times ast
| Divide -> prefix_divide ast
| Stretch -> prefix_stretch ast
| _ -> failwith "Invalid operation" |> (fun _ -> List [])
end
| Infix (op, left, right) ->
begin
match op with
| Plus -> infix_plus left right
| Minus -> infix_minus left right
| Times -> infix_times left right
| Divide -> infix_divide left right
| Reduce -> infix_reduce left right
| Stretch -> infix_stretch left right
| _ -> failwith "Invalid operation" |> (fun _ -> List [])
end
| ReduceOp (op, ast) ->
begin
match ast with
| List l ->
begin
match op with
| Plus -> List [Batteries.List.reduce ( +. ) l]
| Minus -> List [Batteries.List.reduce ( -. ) l]
| Times -> List [Batteries.List.reduce ( *. ) l]
| Divide -> List [Batteries.List.reduce ( /. ) l]
| _ -> failwith "Invalid operation in reduction" |> (fun _ -> List [])
end
| _ -> failwith "Invalid application of reduction" |> (fun _ -> List [])
end
| ExpandOp (op, ast) ->
begin
match ast with
| List l ->
begin
match op with
| Plus -> List (Util.expand ( +. ) l)
| Minus -> List (Util.expand ( -. ) l)
| Times -> List (Util.expand ( *. ) l)
| Divide -> List (Util.expand ( /. ) l)
| _ -> failwith "Invalid operation in reduction" |> (fun _ -> List [])
end
| _ -> failwith "Invalid application of reduction" |> (fun _ -> List [])
end
| _ -> failwith "Invalid usage of operator" |> (fun _ -> List [])
end