-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.ml
66 lines (47 loc) · 1.52 KB
/
Common.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
(* Copyright Per Lindgren 2016-2018, see the file "LICENSE" *)
(* for the full license governing this code. *)
(* characters and strings *)
let tab = "\t"
let tabc = '\t'
let nl = "\n"
let nlc = '\n'
let enl = "\\n"
let op = " {" ^ nl
let cl = "}"
let ec = "\""
let ecit = "\'"
(* Error handling *)
exception TypeError of string
exception CompilerError of string
exception UnMatched
let rec mymap m = function
| [] -> []
| e :: l -> try (m e) :: mymap m l with UnMatched -> mymap m l
let rec myconcat s = function
| [] -> ""
| "":: l -> myconcat s l
| e :: l -> e ^ s ^ myconcat s l
let rec mycon s = function
| [] -> ""
| e :: [] -> e
| "" :: l -> mycon s l
| e :: l -> e ^ s ^ mycon s l
let myass k m = try List.assoc k m with _ -> raise (CompilerError("lookup of " ^ k ^ " failed!"))
let mycompare s1 s2 = (String.compare s1 s2 == 0)
(* Helper functions *)
let p_stdout x = Printf.fprintf stdout "%s\n%!" x
let p_stderr x = Printf.fprintf stderr "%s\n%!" x
let p_oc oc x = Printf.fprintf oc "%s\n%!" x
let rec range i j = if i > j then [] else i :: (range (i+1) j)
let size = 1024 (* Static sized parsing buffer in Parsing.ml (Standard lib) *)
let submatch s i m =
let rec subm s i m r =
try
match String.get s (i mod size) with
| c when c == m -> r
| c when c == tabc -> subm s (i + 1) m (r ^ " ")
| c -> subm s (i + 1) m (r ^ String.make 1 c)
with
_ -> r (* outside string, should never happen *)
in
subm s i m ""