-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.ml
executable file
·73 lines (60 loc) · 2.7 KB
/
utility.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
open Type;;
(***********************************************************************)
(* UTILITY *)
(************************************************************************)
(* VERY USEFUL FOR DEBUGGING - NOT USED IN CODE *)
(************************************************************************)
(* Prints an (int * string) list *)
let print_l (l: (int * string) list) : unit =
List.iter (fun (x) -> print_int (fst x); print_string " "; print_string (snd x); print_string "\n") l
(* Prints a string list *)
let print_str_l (l: string list) : unit =
List.iter (fun (x) -> print_string x; print_string "\n") l
(* Prints an (int*instruction) tuple *)
let print_instr (l:(int * instr)) : unit =
match snd l with
| Set(name, expr) -> print_string "Set"; print_string "\n"
| Read(name) -> print_string "Read : "; print_string name; print_string "\n"
| Print(expr) -> print_string "Print"; print_string "\n"
| If(cond, block1, block2) -> print_string "If"; print_string "\n"
| While (cond, block) -> print_string "While"; print_string "\n"
(* Prints an arithmetic expression *)
let print_expr (ex:expr) : unit =
match ex with
| Num(i) -> print_string "Num : "; print_int i; print_string "\n"
| Var(s) -> print_string "Var : "; print_string s; print_string "\n"
| Op(op, ex1, ex2) -> print_string "op"
(* Prints a list of (int * instr) tuples*)
let print_instr_list (l:(int * instr) list) : unit =
print_string "-------------------\n";
List.iter (fun (x) -> print_string "value: "; print_instr x; print_string "\n") l;
print_string "END\n-------------------\n\n"
(* Get words of a line - string ( a word is a list of characters not containing " " ) *)
let get_words (line: string) : string list = String.split_on_char ' ' line
(* Remove the empty whitespaces from a line *)
let remove_indentation (line: string): string list =
let rec loop l = match l with
| [] -> []
| "" :: tl -> loop tl
| list -> list
in loop (get_words line)
(* Get indentation of a line *)
let get_indentation (line: string) : int =
let rec loop l accu = match l with
| [] -> accu
| x::tl -> if x = "" then loop tl (accu+1) else accu
in loop ( get_words line ) 0
(* Let clean empty word we don't want it *)
let clean_words (line: string list): string list =
let rec loop l = match l with
| [] -> []
| "" :: tl -> loop tl
| hd :: tl -> hd :: loop tl
in loop ( line )
(* Just checks if a string is convertible to int (is an int) *)
let is_int (word: string): bool =
try int_of_string word |> ignore; true
with Failure _ -> false
let is_operator (word: string): bool = match word with
| "+" | "-" | "*" | "/" | "%" -> true
| _ -> false