-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.ml
80 lines (67 loc) · 2.07 KB
/
variables.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
open Types
let var_table : (string, thing) Hashtbl.t = Hashtbl.create 16
let read_var_from_table id =
try
Hashtbl.find var_table id
with Not_found ->
failwith ("No binding for " ^ id)
let write_var id value =
Hashtbl.replace var_table id value
let read_var id =
match String.length id with
| 0 -> failwith "Program error: got empty identifier"
| 1 ->
begin
match id with
| "#" -> Point (0., 0.)
| _ -> read_var_from_table id
end
| 2 ->
begin
match id.[0] with
| 'V' ->
begin
try
let n = int_of_string (String.sub id 1 (String.length id - 1)) in
Loop_variables.get_vertex n
with _ -> read_var_from_table id
end
| 'L' ->
if id.[1] == 'C' then Loop_variables.get_loop_count ()
else failwith "Not implemented"
| _ -> read_var_from_table id
end
| _ -> read_var_from_table id
let get_float id =
match read_var id with
| Number x -> x
| _ -> failwith (id ^ " is not bound to float")
let get_point id =
match read_var id with
| Point p -> p
| _ -> failwith (id ^ " is not bound to point")
let get_figure id =
match read_var id with
| Figure f -> f
| _ -> failwith (id ^ " is not bound to figure")
let print_dictionary () = Debug.print_dictionary var_table
let to_float = function
| Number x -> x
| String s -> get_float s
| Point _ -> failwith "Expected float; got point"
| Figure _ -> failwith "Expected float; got figure"
let to_string = function
| String s -> s
| Number _ -> failwith "Expected string; got float"
| Point _ -> failwith "Expected string; got point"
| Figure _ -> failwith "Expected string; got figure"
let to_point = function
| String s -> get_point s
| Point p -> p
| Number _ -> failwith "Expected point; got float"
| Figure _ -> failwith "Expected point; got figure"
let to_figure = function
| String s -> get_figure s
| Figure f -> f
| Number _ -> failwith "Expected figure; got float"
| Point _ -> failwith "Expected figure; got point"