| 1 |
(* |
| 2 |
* A simple pattern matching compiler. |
| 3 |
* This one uses Mikael Pettersson's algorithm. |
| 4 |
* |
| 5 |
* NOTE: This module is complete detached from the rest of the |
| 6 |
* infrastructure so that it can be reused. |
| 7 |
*) |
| 8 |
|
| 9 |
signature MATCH_COMPILER = |
| 10 |
sig |
| 11 |
(* These are client defined types *) |
| 12 |
structure Guard : sig type guard |
| 13 |
val toString : guard -> string |
| 14 |
end |
| 15 |
structure Exp : sig type exp |
| 16 |
val toString : exp -> string |
| 17 |
end |
| 18 |
structure Action : sig type action end |
| 19 |
structure Con : sig type con val compare : con * con -> order end |
| 20 |
structure Literal : sig type literal |
| 21 |
val compare : literal * literal -> order |
| 22 |
end |
| 23 |
structure Var : sig type var end |
| 24 |
|
| 25 |
(* These are new types *) |
| 26 |
datatype index = INT of int | LABEL of Var.var |
| 27 |
datatype path = PATH of index list |
| 28 |
|
| 29 |
structure Path : |
| 30 |
sig val compare : path * path -> order |
| 31 |
val toString : path -> string |
| 32 |
val toIdent : path -> string |
| 33 |
val dot : path * index -> path |
| 34 |
structure Map : ORD_MAP where type Key.ord_key = path |
| 35 |
end |
| 36 |
|
| 37 |
datatype name = VAR of Var.var | PVAR of path |
| 38 |
|
| 39 |
structure Subst : ORD_MAP where type Key.ord_key = Var.var |
| 40 |
|
| 41 |
type pat |
| 42 |
type subst = name Subst.map |
| 43 |
|
| 44 |
datatype decon = |
| 45 |
CON of Con.con (* match a user constructor *) |
| 46 |
| LIT of Literal.literal (* match a user literal *) |
| 47 |
|
| 48 |
exception MatchCompiler of string |
| 49 |
|
| 50 |
type compiled_dfa (* compiled pattern matching dfa *) |
| 51 |
type compiled_rule |
| 52 |
type rule_no = int |
| 53 |
type compiled_pat |
| 54 |
|
| 55 |
(* Compile a user pattern into internal pattern form; |
| 56 |
* This function abstracts out the computation of paths and bindings. |
| 57 |
*) |
| 58 |
val rename : |
| 59 |
( { idPat : Var.var -> compiled_pat, |
| 60 |
asPat : Var.var * 'pat -> compiled_pat, |
| 61 |
wildPat : unit -> compiled_pat, |
| 62 |
consPat : Con.con * 'pat list -> compiled_pat, |
| 63 |
tuplePat : 'pat list -> compiled_pat, |
| 64 |
recordPat : (Var.var * 'pat) list -> compiled_pat, |
| 65 |
litPat : Literal.literal -> compiled_pat, |
| 66 |
|
| 67 |
(* logical connectives and other extensions to the standard *) |
| 68 |
orPat : 'pat list -> compiled_pat, |
| 69 |
andPat : 'pat list -> compiled_pat, |
| 70 |
notPat : 'pat -> compiled_pat, |
| 71 |
wherePat : 'pat * Guard.guard -> compiled_pat, |
| 72 |
nestedPat : 'pat * (int * Exp.exp) * 'pat -> compiled_pat |
| 73 |
} -> 'pat -> compiled_pat |
| 74 |
) |
| 75 |
-> {number: rule_no, (* rule number *) |
| 76 |
pats: 'pat list, (* the pattern *) |
| 77 |
guard: Guard.guard option, (* optional guard *) |
| 78 |
cont: Var.var option, (* optional continuation *) |
| 79 |
action: Action.action (* action *) |
| 80 |
} |
| 81 |
-> compiled_rule |
| 82 |
|
| 83 |
(* Compile a set of canonical rules into a dfa *) |
| 84 |
val compile : {compiled_rules:compiled_rule list, |
| 85 |
compress: bool |
| 86 |
} -> compiled_dfa |
| 87 |
|
| 88 |
val exhaustive : compiled_dfa -> bool |
| 89 |
val redundant : compiled_dfa -> IntListSet.set |
| 90 |
|
| 91 |
(* For debugging *) |
| 92 |
val toString : compiled_dfa -> string |
| 93 |
|
| 94 |
(* Generate code for a compiled dfa. |
| 95 |
* Assuming an ML-like language. |
| 96 |
*) |
| 97 |
val codeGen : |
| 98 |
{ genFail : unit -> Exp.exp, |
| 99 |
genOk : Action.action -> Exp.exp, |
| 100 |
genPath : path -> Exp.exp, |
| 101 |
genBind : (Var.var * Exp.exp) list -> 'decl list, |
| 102 |
genCase : Var.var * (decon * path option list * Exp.exp) list * |
| 103 |
Exp.exp option -> Exp.exp, |
| 104 |
genIf : Guard.guard * Exp.exp * Exp.exp -> Exp.exp, |
| 105 |
genGoto : int * Var.var list -> Exp.exp, (* call a function *) |
| 106 |
genFun : int * Var.var list * Exp.exp -> 'decl, (* function def *) |
| 107 |
genCont : Var.var * int * Var.var list -> 'decl, |
| 108 |
genLet : 'decl list * Exp.exp -> Exp.exp, |
| 109 |
genVar : path -> Var.var, |
| 110 |
genVal : Var.var * Exp.exp -> 'decl, |
| 111 |
genProj : path * (path option * index) list -> 'decl |
| 112 |
} -> (Exp.exp * compiled_dfa) |
| 113 |
-> Exp.exp |
| 114 |
end |