-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluator.ml
46 lines (39 loc) · 1.22 KB
/
evaluator.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
(*|
| | Apply and eval meme.
|*)
let rec evaluateLoop trees =
if List.length trees = 1 then
evaluate ( List.hd trees )
else (
ignore (evaluate ( List.hd trees )); (* "ignore" function uses the side-effect only. *)
evaluateLoop ( List.tl trees )
)
and evaluate tree = match tree with
| Call (("out" | "o"), value) ->
String ( printLoop value )
(* INFINITE LOOP CATCHING *)
| String value -> String value
| Number value -> Number value
(* ERROR CATCHING *)
| Call (value, listdata) ->
( print_endline ( String.concat "" ["An attempt to call '"; value; "' was made, but it's undefined."] );
Number 0.0)
and printLoop trees =
if List.length trees = 1 then
printTree ( List.hd trees )
else (
ignore ( printTree ( List.hd trees ) );
printLoop ( List.tl trees )
)
and printTree tree = match tree with
| String value ->
( print_string value;
value)
| Number value ->
( print_float value;
string_of_float value)
| Call ( value, listdata ) ->
printTree ( evaluate ( Call ( value, listdata ) ) )
;;
let trees, leftover = parserLoop ( tokenize "o:o:'lol'" );;
evaluateLoop trees;;