-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.ml
502 lines (449 loc) · 22.9 KB
/
backend.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
type index = INDICE of int * int
type range = RANGE of index * index
type value = FLOAT of float | UNDEFINED
(* A sheet can have both float as well as undefined values *)
type sheet = value list list
(* Unary and Binary Operator tokens *)
type tokens = COUNT | ROWCOUNT | COLCOUNT
| SUM | ROWSUM | COLSUM
| AVG | ROWAVG | COLAVG
| MIN | ROWMIN | COLMIN
| MAX | ROWMAX | COLMAX
| ADD | SUBT | MULT | DIV
(* return type of parser *)
type formula =
UNARY of index * tokens * range
| BINARY1 of index * tokens * range * range
| BINARY2 of index * tokens * float * range
| BINARY3 of index * tokens * index * range
(* Exceptions *)
exception NotPossible
exception InvalidRange
exception IncompatibleRange
exception EmptyCell
exception MaxSheetSizeReached
let maxSize = 5000;;
(* Function to check whether given range is valid or not *)
let isInvalidRange (r:range): bool = match r with
RANGE(INDICE(i1, j1), INDICE(i2, j2)) -> (i1 > i2) || (j1 > j2);;
(* Function to check whether two ranges are compatible or not i.e. same number of rows and columns *)
let isIncompatibleRange (r1:range) (r2:range): bool = match r1 with
RANGE(INDICE(a1, a2), INDICE(b1, b2)) -> match r2 with
RANGE(INDICE(c1, c2), INDICE(d1, d2)) -> ((b1-a1) <> (d1-c1)) || ((b2-a2) <> (d2-c2));;
(* Function to evaluate function f with identity e in range r in sheet s *)
let rec full_ans (s:sheet) (r:range) f (e:float): float =
match s with
[] -> (
match r with RANGE(_, INDICE(i2, _)) ->
if i2 < 0 then e else raise EmptyCell
)
| x::xs ->
match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i1 > 0 then full_ans xs (RANGE(INDICE(i1-1, j1), INDICE(i2-1, j2))) f e
else if i2 < 0 then e
else
let rec full_row_ans (v:value list) (i1:int) (i2:int): float =
match v with
[] -> if i2 < 0 then e else raise EmptyCell
| y::ys -> if i1 > 0 then full_row_ans ys (i1-1) (i2-1)
else if i2 < 0 then e
else match y with
FLOAT(c) -> f c (full_row_ans ys 0 (i2-1))
| UNDEFINED -> raise EmptyCell
in f (full_row_ans x j1 j2) (full_ans xs (RANGE(INDICE(0, j1), INDICE(i2-1, j2))) f e);;
(* Function to evaluate function f with identity e on every row of range r in sheet s *)
let rec row_ans (s:sheet) (r:range) f (e:float): float list =
match s with
[] -> (
match r with RANGE(i, INDICE(i2, _)) ->
if i2 < 0 then [] else raise EmptyCell
)
| x::xs ->
match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i1 > 0 then row_ans xs (RANGE(INDICE(i1-1, j1), INDICE(i2-1, j2))) f e
else if i2 < 0 then []
else
let rec full_row_ans (v:value list) (i1:int) (i2:int): float =
match v with
[] -> if i2 < 0 then e else raise EmptyCell
| y::ys -> if i1 > 0 then full_row_ans ys (i1-1) (i2-1)
else if i2 < 0 then e
else match y with
FLOAT(c) -> f c (full_row_ans ys 0 (i2-1))
| UNDEFINED -> raise EmptyCell
in (full_row_ans x j1 j2)::row_ans xs (RANGE(INDICE(0, j1), INDICE(i2-1, j2))) f e;;
(* Function to evaluate function f with identity e on every column of range r in sheet s *)
let rec col_ans (s:sheet) (r:range) f (e:float): float list =
let rec mkzerov (n:int) (e:float): float list = if n = 0 then [] else e::mkzerov (n-1) e
in match s with
[] -> (
match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i2 < 0 then (mkzerov (j2-j1+1) e) else raise EmptyCell
)
| x::xs ->
match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i1 > 0 then col_ans xs (RANGE(INDICE(i1-1, j1), INDICE(i2-1, j2))) f e
else if i2 < 0 then (mkzerov (j2-j1+1) e)
else
let rec merge (v1:value list) (v2:float list) (i1:int) (i2:int): float list =
match v1 with
[] -> if i2 < 0 then [] else raise EmptyCell
| v::vs ->
if i1 > 0 then merge vs v2 (i1-1) (i2-1)
else if i2 < 0 then []
else match v with
FLOAT(c) -> (f c (List.hd v2))::merge vs (List.tl v2) 0 (i2-1)
| UNDEFINED -> raise EmptyCell
in merge x (col_ans xs (RANGE(INDICE(0, j1), INDICE(i2-1, j2))) f e) j1 j2;;
(* Function to count the number of non-empty cells in range r in sheet s *)
let rec full_count_ans (s:sheet) (r:range): float =
match s with
[] -> 0.
| x::xs ->
match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i1 > 0 then full_count_ans xs (RANGE(INDICE(i1-1, j1), INDICE(i2-1, j2)))
else if i2 < 0 then 0.
else
let rec full_row_ans (v:value list) (i1:int) (i2:int): float =
match v with
[] -> 0.
| y::ys -> if i1 > 0 then full_row_ans ys (i1-1) (i2-1)
else if i2 < 0 then 0.
else match y with
FLOAT(c) -> 1. +. (full_row_ans ys 0 (i2-1))
| UNDEFINED -> full_row_ans ys 0 (i2-1)
in (full_row_ans x j1 j2) +. (full_count_ans xs (RANGE(INDICE(0, j1), INDICE(i2-1, j2))));;
(* Function to count the number of non-empty cells in every row of range r in sheet s *)
let rec row_count_ans (s:sheet) (r:range): float list =
let rec mkzerov (n:int): float list = if n = 0 then [] else 0.::mkzerov (n-1)
in match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i2 < 0 then []
else match s with
[] -> mkzerov (i2+1)
| x::xs ->
if i1 > 0 then row_count_ans xs (RANGE(INDICE(i1-1, j1), INDICE(i2-1, j2)))
else
let rec full_row_ans (v:value list) (i1:int) (i2:int): float =
match v with
[] -> 0.
| y::ys -> if i1 > 0 then full_row_ans ys (i1-1) (i2-1)
else if i2 < 0 then 0.
else match y with
FLOAT(c) -> 1. +. (full_row_ans ys 0 (i2-1))
| UNDEFINED -> full_row_ans ys 0 (i2-1)
in (full_row_ans x j1 j2)::row_count_ans xs (RANGE(INDICE(0, j1), INDICE(i2-1, j2)));;
(* Function to count the number of non-empty cells in every column of range r in sheet s *)
let rec col_count_ans (s:sheet) (r:range): float list =
let rec mkzerov (n:int): float list = if n = 0 then [] else 0.::mkzerov (n-1)
in match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
match s with
[] -> mkzerov (j2-j1+1)
| x::xs ->
if i1 > 0 then col_count_ans xs (RANGE(INDICE(i1-1, j1), INDICE(i2-1, j2)))
else if i2 < 0 then mkzerov (j2-j1+1)
else
let rec merge (v1:value list) (v2:float list) (i1:int) (i2:int): float list =
match v1 with
[] -> v2
| v::vs ->
if i1 > 0 then merge vs v2 (i1-1) (i2-1)
else if i2 < 0 then []
else match v with
FLOAT(c) -> (1. +. (List.hd v2))::merge vs (List.tl v2) 0 (i2-1)
| UNDEFINED -> (List.hd v2)::merge vs (List.tl v2) 0 (i2-1)
in merge x (col_count_ans xs (RANGE(INDICE(0, j1), INDICE(i2-1, j2)))) j1 j2;;
(* Function to evaluate function f with one parameter as c on range r in sheet s *)
let rec full_range_ans (s:sheet) (r:range) (c:float) f: float list list =
match s with
[] -> (
match r with RANGE(i, INDICE(i2, _)) ->
if i2 < 0 then [] else raise EmptyCell
)
| x::xs ->
match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i1 > 0 then full_range_ans xs (RANGE(INDICE(i1-1, j1), INDICE(i2-1, j2))) c f
else if i2 < 0 then []
else
let rec full_row_ans (v:value list) (i1:int) (i2:int): float list =
match v with
[] -> if i2 < 0 then [] else raise EmptyCell
| y::ys -> if i1 > 0 then full_row_ans ys (i1-1) (i2-1)
else if i2 < 0 then []
else match y with
FLOAT(x) -> (f x c)::(full_row_ans ys 0 (i2-1))
| UNDEFINED -> raise EmptyCell
in (full_row_ans x j1 j2)::(full_range_ans xs (RANGE(INDICE(0, j1), INDICE(i2-1, j2))) c f);;
(* Function to expand sheet s to dimensions h X l filling new cells with UNDEFINED *)
let rec expandSheet (s:sheet) (h:int) (l:int): sheet =
let rec makeUndefinedRow (n:int): value list = if n = 0 then [] else UNDEFINED::makeUndefinedRow (n-1)
in match s with
[] -> if h = 0 then s else (makeUndefinedRow l)::expandSheet s (h-1) l
| x::xs -> if l = (List.length x) then x::expandSheet xs (h-1) l
else (x @ (makeUndefinedRow (l-(List.length x))))::expandSheet xs (h-1) l;;
(* Function to write float c in index i_ of sheet s *)
let rec writeCell (s:sheet) (i_:index) (c:float): sheet =
match s with
[] -> []
| x::xs ->
match i_ with INDICE(i, j) ->
if i > 0 then x::writeCell xs (INDICE(i-1, j)) c
else
let rec colWriteCell (v:value list) (j:int): value list =
if j = 0 then FLOAT(c)::(List.tl v)
else (List.hd v)::colWriteCell (List.tl v) (j-1)
in (colWriteCell x j)::xs;;
(* Function to write row c in sheet s starting with index i_ *)
let rec writeRow (s:sheet) (i_:index) (c:float list): sheet =
match s with
[] -> []
| x::xs ->
match i_ with INDICE(i, j) ->
if i > 0 then x::writeRow xs (INDICE(i-1, j)) c
else
let rec writeRowUtil (v:value list) (j:int) (c:float list): value list =
if j > 0 then (List.hd v)::writeRowUtil (List.tl v) (j-1) c
else if (List.length c) = 0 then v
else FLOAT((List.hd c))::writeRowUtil (List.tl v) 0 (List.tl c)
in (writeRowUtil x j c)::xs;;
(* Function to write column c in sheet s starting with index i_ *)
let rec writeCol (s:sheet) (i_:index) (c:float list): sheet =
match s with
[] -> []
| x::xs ->
match i_ with INDICE(i, j) ->
if i > 0 then x::writeCol xs (INDICE(i-1, j)) c
else if (List.length c) = 0 then s
else
let rec colWriteCell (v:value list) (j:int) (c:float): value list =
if j = 0 then FLOAT(c)::(List.tl v)
else (List.hd v)::colWriteCell (List.tl v) (j-1) c
in (colWriteCell x j (List.hd c))::writeCol xs i_ (List.tl c);;
(* Function to write range c in sheet s starting with index i_ *)
let rec writeRange (s:sheet) (i_:index) (c:float list list): sheet =
match s with
[] -> []
| x::xs ->
match i_ with INDICE(i, j) ->
if i > 0 then x::writeRange xs (INDICE(i-1, j)) c
else if (List.length c) = 0 then s
else
let rec writeRowUtil (v:value list) (j:int) (c:float list): value list =
if j > 0 then (List.hd v)::writeRowUtil (List.tl v) (j-1) c
else if (List.length c) = 0 then v
else FLOAT((List.hd c))::writeRowUtil (List.tl v) 0 (List.tl c)
in (writeRowUtil x j (List.hd c))::writeRange xs i_ (List.tl c);;
(* Helper function for unary operators of overall kind *)
let unary_full (s:sheet) (i_:index) (ans:float): sheet =
match i_ with INDICE(i, j) ->
if i >= List.length s || j >= List.length (List.hd s) then
let new_h = max (i+1) (List.length s) in
let new_l = max (j+1) (List.length (List.hd s)) in
if new_h > maxSize || new_l > maxSize then raise MaxSheetSizeReached
else writeCell (expandSheet s new_h new_l) i_ ans
else writeCell s i_ ans;;
(* Helper function for unary operators of row-wise kind *)
let unary_row (s:sheet) (r:range) (i_:index) (ans:float list): sheet =
match i_ with INDICE(i, j) ->
match r with RANGE(INDICE(i1, _), INDICE(i2, _)) ->
if i+i2-i1 >= List.length s || j >= List.length (List.hd s) then
let new_h = max (i+i2-i1+1) (List.length s) in
let new_l = max (j+1) (List.length (List.hd s)) in
if new_h > maxSize || new_l > maxSize then raise MaxSheetSizeReached
else writeCol (expandSheet s new_h new_l) i_ ans
else writeCol s i_ ans;;
(* Helper function for unary operators of column-wise kind *)
let unary_col (s:sheet) (r:range) (i_:index) (ans: float list):sheet =
match i_ with INDICE(i, j) ->
match r with RANGE(INDICE(_, j1), INDICE(_, j2)) ->
if i >= List.length s || j+j2-j1 >= List.length (List.hd s) then
let new_h = max (i+1) (List.length s) in
let new_l = max (j+j2-j1+1) (List.length (List.hd s)) in
if new_h > maxSize || new_l > maxSize then raise MaxSheetSizeReached
else writeRow (expandSheet s new_h new_l) i_ ans
else writeRow s i_ ans;;
(* Fills count of valid entries in the given range into the specified cell *)
let full_count (s:sheet) (r:range) (i_:index): sheet =
let ans = full_count_ans s r in unary_full s i_ ans;;
(* Fills count of valid entries per row in the given range into the column starting from the specified cell *)
let row_count (s:sheet) (r:range) (i_:index): sheet =
let ans = row_count_ans s r in unary_row s r i_ ans;;
(* Fills count of valid entries per column in the given range into the row starting from the specified cell *)
let col_count (s:sheet) (r:range) (i_:index): sheet =
let ans = col_count_ans s r in unary_col s r i_ ans;;
(* Fills the sum of entries of cells in the given range into the specified cell *)
let full_sum (s:sheet) (r:range) (i_:index): sheet =
let f a b = a +. b in
let ans = full_ans s r f 0. in unary_full s i_ ans;;
(* Fills the sum of entries of cells per row in the given range into the column starting from the specified cell *)
let row_sum (s:sheet) (r:range) (i_:index): sheet =
let f a b = a +. b in
let ans = row_ans s r f 0. in unary_row s r i_ ans;;
(* Fills the sum of entries of cells per column in the given range into the row starting from the specified cell *)
let col_sum (s:sheet) (r:range) (i_:index): sheet =
let f a b = a +. b in
let ans = col_ans s r f 0. in unary_col s r i_ ans;;
(* Fills the average of entries of cells in the given range into the specified cell *)
let full_avg (s:sheet) (r:range) (i_:index): sheet =
match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
let f a b = a +. b in
let ans = (full_ans s r f 0.) /. ((float_of_int (i2-i1+1)) *. (float_of_int (j2-j1+1))) in
unary_full s i_ ans;;
let rec divideList (v:float list) (c:float): float list = match v with
[] -> []
| x::xs -> (x /. c)::divideList xs c
(* Fills the average of entries of cells per row in the given range into the column starting from the specified cell *)
let row_avg (s:sheet) (r:range) (i_:index): sheet =
match r with RANGE(INDICE(_, j1), INDICE(_, j2)) ->
let f a b = a +. b in
let ans = divideList (row_ans s r f 0.) (float_of_int (j2-j1+1)) in
unary_row s r i_ ans;;
(* Fills the sum of entries of cells per column in the given range into the row starting from the specified cell *)
let col_avg (s:sheet) (r:range) (i_:index): sheet =
match r with RANGE(INDICE(i1, _), INDICE(i2, _)) ->
let f a b = a +. b in
let ans = divideList (col_ans s r f 0.) (float_of_int (i2-i1+1)) in
unary_col s r i_ ans;;
(* Fills the min of entries of cells in the given range into the specified cell *)
let full_min (s:sheet) (r:range) (i_:index): sheet =
let f a b = (min a b) in
let ans = full_ans s r f max_float in unary_full s i_ ans;;
(* Fills the min of entries of cells per row in the given range into the column starting from the specified cell *)
let row_min (s:sheet) (r:range) (i_:index): sheet =
let f a b = (min a b) in
let ans = row_ans s r f max_float in unary_row s r i_ ans;;
(* Fills the min of entries of cells per column in the given range into the row starting from the specified cell *)
let col_min (s:sheet) (r:range) (i_:index): sheet =
let f a b = (min a b) in
let ans = col_ans s r f max_float in unary_col s r i_ ans;;
(* Fills the max of entries of cells in the given range into the specified cell *)
let full_max (s:sheet) (r:range) (i_:index): sheet =
let f a b = (max a b) in
let ans = full_ans s r f min_float in unary_full s i_ ans;;
(* Fills the max of entries of cells per row in the given range into the column starting from the specified cell *)
let row_max (s:sheet) (r:range) (i_:index): sheet =
let f a b = (max a b) in
let ans = row_ans s r f min_float in unary_row s r i_ ans;;
(* Fills the max of entries of cells per column in the given range into the row starting from the specified cell *)
let col_max (s:sheet) (r:range) (i_:index): sheet =
let f a b = (max a b) in
let ans = col_ans s r f min_float in unary_col s r i_ ans;;
(* applies the function f to the contents of each cell in the selected cell range *)
let binary_range_const (s:sheet) (r:range) (c:float) (i_:index) f: sheet =
let ans = full_range_ans s r c f in
match i_ with INDICE(i, j) ->
match r with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i+i2-i1 >= List.length s || j+j2-j1 >= List.length (List.hd s) then
let new_h = max (i+i2-i1+1) (List.length s) in
let new_l = max (j+j2-j1+1) (List.length (List.hd s)) in
if new_h > maxSize || new_l > maxSize then raise MaxSheetSizeReached
else writeRange (expandSheet s new_h new_l) i_ ans
else writeRange s i_ ans;;
(* adds a constant to the contents of each cell in the selected cell range *)
let add_const (s:sheet) (r:range) (c:float) (i_:index): sheet =
let f a b = a +. b in binary_range_const s r c i_ f;;
(* subtracts a constant from the contents of each cell in the selected cell range *)
let subt_const (s:sheet) (r:range) (c:float) (i_:index): sheet =
let f a b = a -. b in binary_range_const s r c i_ f;;
(* multiplies the contents of each cell in the selected cell range by a constant *)
let mult_const (s:sheet) (r:range) (c:float) (i_:index): sheet =
let f a b = a *. b in binary_range_const s r c i_ f;;
(* divides the contents of each cell in the selected cell range by a constant *)
let div_const (s:sheet) (r:range) (c:float) (i_:index): sheet =
let f a b = a /. b in binary_range_const s r c i_ f;;
(* applies the function f element wise on two matrices c1 and c2 *)
let rec applyOperation (c1:float list list) (c2:float list list) f: float list list =
match c1 with
[] -> []
| x::xs ->
let rec rowUtil (c1:float list) (c2:float list): float list =
match c1 with
[] -> []
| y::ys -> (f y (List.hd c2))::rowUtil ys (List.tl c2)
in (rowUtil x (List.hd c2))::applyOperation xs (List.tl c2) f;;
(* performs the function f on the cell contents for each corresponding pair of cells in two selected cell ranges *)
let binary_range_range (s:sheet) (r1:range) (r2:range) (i_:index) f: sheet =
let g a b = a +. b in
let ans = applyOperation (full_range_ans s r1 0. g) (full_range_ans s r2 0. g) f in
match i_ with INDICE(i, j) ->
match r1 with RANGE(INDICE(i1, j1), INDICE(i2, j2)) ->
if i+i2-i1 >= List.length s || j+j2-j1 >= List.length (List.hd s) then
let new_h = max (i+i2-i1+1) (List.length s) in
let new_l = max (j+j2-j1+1) (List.length (List.hd s)) in
if new_h > maxSize || new_l > maxSize then raise MaxSheetSizeReached
else writeRange (expandSheet s new_h new_l) i_ ans
else writeRange s i_ ans;;
(* adds the cell contents for each corresponding pair of cells in two selected cell ranges *)
let add_range (s:sheet) (r1:range) (r2:range) (i_:index): sheet =
let f a b = a +. b in binary_range_range s r1 r2 i_ f;;
(* performs a subtraction on the cell contents for each corresponding pair of cells in two selected cell ranges *)
let subt_range (s:sheet) (r1:range) (r2:range) (i_:index): sheet =
let f a b = a -. b in binary_range_range s r1 r2 i_ f;;
(* multiplies the cell contents for each corresponding pair of cells in two selected cell ranges *)
let mult_range (s:sheet) (r1:range) (r2:range) (i_:index): sheet =
let f a b = a *. b in binary_range_range s r1 r2 i_ f;;
(* performs a division on the cell contents for each corresponding pair of cells in two selected cell ranges *)
let div_range (s:sheet) (r1:range) (r2:range) (i_:index): sheet =
let f a b = a /. b in binary_range_range s r1 r2 i_ f;;
(* Function which returns the value at given index in given sheet *)
let rec getValueAtIndex (s:sheet) (i:index): value =
match s with
[] -> UNDEFINED
| x::xs -> match x with
[] -> UNDEFINED
| y::ys -> match i with INDICE(i, j) ->
if(i = 0 && j = 0) then y
else if(i = 0) then getValueAtIndex (ys::xs) (INDICE(i, j-1))
else getValueAtIndex xs (INDICE(i-1, j));;
(* Interpreter based on formulas returned by parser *)
let eval (s:sheet) (f:formula): sheet = match f with
UNARY(i_, t, r) -> (
if (isInvalidRange r) then raise InvalidRange
else match t with
COUNT -> full_count s r i_
| ROWCOUNT -> row_count s r i_
| COLCOUNT -> col_count s r i_
| SUM -> full_sum s r i_
| ROWSUM -> row_sum s r i_
| COLSUM -> col_sum s r i_
| AVG -> full_avg s r i_
| ROWAVG -> row_avg s r i_
| COLAVG -> col_avg s r i_
| MIN -> full_min s r i_
| ROWMIN -> row_min s r i_
| COLMIN -> col_min s r i_
| MAX -> full_max s r i_
| ROWMAX -> row_max s r i_
| COLMAX -> col_max s r i_
| _ -> raise NotPossible
)
| BINARY1(i_, t, r1, r2) -> (
if (isInvalidRange r1) || (isInvalidRange r2) then raise InvalidRange
else if (isIncompatibleRange r1 r2) then raise IncompatibleRange
else match t with
ADD -> add_range s r1 r2 i_
| SUBT -> subt_range s r1 r2 i_
| MULT -> mult_range s r1 r2 i_
| DIV -> div_range s r1 r2 i_
| _ -> raise NotPossible
)
| BINARY2(i_, t, c, r) -> (
if (isInvalidRange r) then raise InvalidRange
else match t with
ADD -> add_const s r c i_
| SUBT -> subt_const s r c i_
| MULT -> mult_const s r c i_
| DIV -> div_const s r c i_
| _ -> raise NotPossible
)
| BINARY3(i_, t, i, r) -> (
if (isInvalidRange r) then raise InvalidRange
else match (getValueAtIndex s i) with
UNDEFINED -> raise EmptyCell
| FLOAT(c) -> match t with
ADD -> add_const s r c i_
| SUBT -> subt_const s r c i_
| MULT -> mult_const s r c i_
| DIV -> div_const s r c i_
| _ -> raise NotPossible
)
;;