| 1 |
(* Abstract syntax as produced by the parsed *)
|
| 2 |
|
| 3 |
open Location
|
| 4 |
open Ident
|
| 5 |
|
| 6 |
type pprog = pmodule_item list
|
| 7 |
|
| 8 |
and pmodule_item = pmodule_item' located
|
| 9 |
and pmodule_item' =
|
| 10 |
| TypeDecl of string * ppat
|
| 11 |
| PatDecl of string * ppat
|
| 12 |
| FunDecl of abstr
|
| 13 |
| LetDecl of ppat * pexpr
|
| 14 |
| EvalStatement of pexpr
|
| 15 |
| Debug of debug_directive
|
| 16 |
and debug_directive =
|
| 17 |
[ `Filter of ppat * ppat
|
| 18 |
| `Accept of ppat
|
| 19 |
| `Compile of ppat * ppat list
|
| 20 |
| `Normal_record of ppat
|
| 21 |
| `Compile2 of ppat * ppat list
|
| 22 |
| `Subtype of ppat * ppat
|
| 23 |
]
|
| 24 |
|
| 25 |
|
| 26 |
and pexpr = pexpr' located
|
| 27 |
and pexpr' =
|
| 28 |
| Forget of pexpr * ppat
|
| 29 |
(* CDuce is a Lambda-calculus ... *)
|
| 30 |
| Var of id
|
| 31 |
| Apply of pexpr * pexpr
|
| 32 |
| Abstraction of abstr
|
| 33 |
|
| 34 |
(* Data constructors *)
|
| 35 |
| Cst of Types.const
|
| 36 |
| Pair of pexpr * pexpr
|
| 37 |
| Xml of pexpr * pexpr
|
| 38 |
| RecordLitt of pexpr label_map
|
| 39 |
|
| 40 |
(* Data destructors *)
|
| 41 |
| Op of string * pexpr list
|
| 42 |
| Match of pexpr * branches
|
| 43 |
| Map of pexpr * branches
|
| 44 |
| Ttree of pexpr * branches
|
| 45 |
| Dot of pexpr* label
|
| 46 |
| RemoveField of pexpr * label
|
| 47 |
|
| 48 |
(* Exceptions *)
|
| 49 |
| Try of pexpr * branches
|
| 50 |
|
| 51 |
| MatchFail (* internal usage *)
|
| 52 |
|
| 53 |
and abstr = {
|
| 54 |
fun_name : id option;
|
| 55 |
fun_iface : (ppat * ppat) list;
|
| 56 |
fun_body : branches
|
| 57 |
}
|
| 58 |
|
| 59 |
and branches = (ppat * pexpr) list
|
| 60 |
|
| 61 |
(* A common syntactic class for patterns and types *)
|
| 62 |
|
| 63 |
and ppat = ppat' located
|
| 64 |
and ppat' =
|
| 65 |
| PatVar of string
|
| 66 |
| Recurs of ppat * (string * ppat) list
|
| 67 |
| Internal of Types.descr
|
| 68 |
| Or of ppat * ppat
|
| 69 |
| And of ppat * ppat
|
| 70 |
| Diff of ppat * ppat
|
| 71 |
| Prod of ppat * ppat
|
| 72 |
| XmlT of ppat * ppat
|
| 73 |
| Arrow of ppat * ppat
|
| 74 |
| Optional of ppat
|
| 75 |
| Record of bool * ppat label_map
|
| 76 |
| Capture of id
|
| 77 |
| Constant of id * Types.const
|
| 78 |
| Regexp of regexp * ppat
|
| 79 |
|
| 80 |
and regexp =
|
| 81 |
| Epsilon
|
| 82 |
| Elem of ppat
|
| 83 |
| Seq of regexp * regexp
|
| 84 |
| Alt of regexp * regexp
|
| 85 |
| Star of regexp
|
| 86 |
| WeakStar of regexp
|
| 87 |
| SeqCapture of id * regexp
|
| 88 |
|
| 89 |
|
| 90 |
let rec equal_ppat p1 p2 =
|
| 91 |
let p1 = p1.descr and p2 = p2.descr in
|
| 92 |
(p1 == p2) ||
|
| 93 |
match (p1,p2) with
|
| 94 |
| PatVar x1, PatVar x2 -> x1 = x2
|
| 95 |
| Internal x1, Internal x2 -> Types.equal_descr x1 x2
|
| 96 |
| Or (x1,y1), Or (x2,y2)
|
| 97 |
| And (x1,y1), And (x2,y2)
|
| 98 |
| Diff (x1,y1), Diff (x2,y2)
|
| 99 |
| Prod (x1,y1), Prod (x2,y2)
|
| 100 |
| XmlT (x1,y1), XmlT (x2,y2)
|
| 101 |
| Arrow (x1,y1), Arrow (x2,y2)
|
| 102 |
-> (equal_ppat x1 x2) && (equal_ppat y1 y2)
|
| 103 |
| Optional x1, Optional x2 -> equal_ppat x1 x2
|
| 104 |
| Record (o1,r1), Record (o2,r2) ->
|
| 105 |
(o1 == o2) && (LabelMap.equal equal_ppat r1 r2)
|
| 106 |
| Capture x1, Capture x2 -> x1 == x2
|
| 107 |
| Constant (x1,y1), Constant (x2,y2) ->
|
| 108 |
(x1 == x2) && (Types.equal_const y1 y2)
|
| 109 |
| Regexp (x1,y1), Regexp (x2,y2) ->
|
| 110 |
(equal_regexp x1 x2) && (equal_ppat y1 y2)
|
| 111 |
(* todo: Recurs *)
|
| 112 |
| _ -> false
|
| 113 |
and equal_regexp r1 r2 =
|
| 114 |
(r1 == r2) ||
|
| 115 |
match (r1,r2) with
|
| 116 |
| Elem x1, Elem x2 -> equal_ppat x1 x2
|
| 117 |
| Seq (x1,y1), Seq (x2,y2)
|
| 118 |
| Alt (x1,y1), Alt (x2,y2) -> (equal_regexp x1 x2) && (equal_regexp y1 y2)
|
| 119 |
| Star x1, Star x2
|
| 120 |
| WeakStar x1, WeakStar x2 -> equal_regexp x1 x2
|
| 121 |
| SeqCapture (x1,y1), SeqCapture (x2,y2) ->
|
| 122 |
(x1 == x2) && (equal_regexp y1 y2)
|
| 123 |
| _ -> false
|
| 124 |
|
| 125 |
let rec hash_ppat p =
|
| 126 |
match p.descr with
|
| 127 |
| PatVar x -> 1 + 17 * (Hashtbl.hash x)
|
| 128 |
| Internal x -> 2 + 17 * (Types.hash_descr x)
|
| 129 |
| Or (x,y) -> 3 + 17 * (hash_ppat x) + 257 * (hash_ppat y)
|
| 130 |
| And (x,y) -> 4 + 17 * (hash_ppat x) + 257 * (hash_ppat y)
|
| 131 |
| Diff (x,y) -> 5 + 17 * (hash_ppat x) + 257 * (hash_ppat y)
|
| 132 |
| Prod (x,y) -> 6 + 17 * (hash_ppat x) + 257 * (hash_ppat y)
|
| 133 |
| XmlT (x,y) -> 7 + 17 * (hash_ppat x) + 257 * (hash_ppat y)
|
| 134 |
| Arrow (x,y) -> 8 + 17 * (hash_ppat x) + 257 * (hash_ppat y)
|
| 135 |
| Optional x -> 9 + 17 * (hash_ppat x)
|
| 136 |
| Record (o,r) ->
|
| 137 |
(if o then 10 else 11) + (LabelMap.hash hash_ppat r)
|
| 138 |
| Capture x -> 12 + 17 * (Id.hash x)
|
| 139 |
| Constant (x,y) -> 13 + 17 * (Id.hash x) + 257 * (Types.hash_const y)
|
| 140 |
| Regexp (x,y) ->
|
| 141 |
14 + 17 * (hash_regexp x) + 16637 * (hash_ppat y)
|
| 142 |
| Recurs (x,l) ->
|
| 143 |
15 + 17 * (hash_ppat x) (* todo: hash l *)
|
| 144 |
and hash_regexp = function
|
| 145 |
| Epsilon -> 1
|
| 146 |
| Elem x -> 2 + 17 * (hash_ppat x)
|
| 147 |
| Seq (x,y) -> 3 + 17 * (hash_regexp x) + 257 * (hash_regexp y)
|
| 148 |
| Alt (x,y) -> 4 + 17 * (hash_regexp x) + 257 * (hash_regexp y)
|
| 149 |
| Star x -> 5 + 17 * (hash_regexp x)
|
| 150 |
| WeakStar x -> 6 + 17 * (hash_regexp x)
|
| 151 |
| SeqCapture (x,y) -> 7 + 17 * (Id.hash x) + 257 * (hash_regexp y)
|