Initial revision
[bpt/mlt.git] / src / tree.sml
1 (*
2 * Dynamic web page generation with Standard ML
3 * Copyright (C) 2003 Adam Chlipala
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *)
19
20 (* Template language abstract syntax trees *)
21
22 structure Tree =
23 struct
24 open Common
25
26 type ident = string
27 type path = ident list
28
29 type 'a withext = 'a * (int * int)
30
31 (* Pattern *)
32 datatype pat' =
33 Wild_p | Int_p of int | String_p of string
34 | Ident_p of path
35 | Record_p of bool * (ident * pat) list
36 | FlexRecord_p of (ident * pat) list
37 | App_p of path * pat | Cons_p of pat * pat
38 | As_p of ident * pat
39 and pat = PAT of pat' withext
40
41 (* Expression *)
42 and exp' =
43 Int_e of int | String_e of string | Char_e of string | Ident_e of path
44 | Plus_e of exp * exp | Minus_e of exp * exp | Times_e of exp * exp
45 | Divide_e of exp * exp | Mod_e of exp * exp | Neg_e | Param_e
46 | Template_e of ident
47 | Orelse_e of exp * exp | Andalso_e of exp * exp
48 | StrCat_e of exp * exp
49 | Eq_e of exp * exp | Neq_e of exp * exp
50 | Gt_e of exp * exp | Gte_e of exp * exp
51 | Lt_e of exp * exp | Lte_e of exp * exp
52 | Cons_e of exp * exp
53 | Record_e of bool * (ident * exp) list
54 | RecordUpd_e of exp * (ident * exp) list
55 | Proj_e of ident | App_e of exp * exp
56 and exp = EXP of exp' withext
57
58 and blockItem' =
59 Html_i of string (* literal HTML code to be sent *)
60 | Ref_i of (ident * exp) list (* define some new refs *)
61 | Val_i of pat * exp (* val binding *)
62 | Assn_i of ident * exp (* assignment to ref *)
63 | Exp_i of exp (* expression to be evaluated *)
64 | Open_i of path list (* imports to top level *)
65 | Ifthenelse_i of (exp * block) list * block option (* if statement *)
66 | Case_i of exp * (pat * block) list (* case statement *)
67 | Foreach_i of ident * exp * block (* foreach statement with list *)
68 | For_i of ident * exp * exp * block (* foreach statement with integer range *)
69 | TryCatch_i of block * (pat * block) list (* try...catch exception handlers w/ pattern matching *)
70 and blockItem = BITEM of blockItem' withext
71 (* Block of thingers to twiddle *)
72 and block = BLOCK of block' withext
73 withtype block' = blockItem list
74 and path = ident list
75
76 fun unblock (BLOCK (x, _)) = x
77 fun unpat (PAT (x, _)) = x
78 fun unexp (EXP (x, _)) = x
79
80
81 fun expString (EXP (e, _)) =
82 (case e of
83 Int_e n => Int.toString n
84 | String_e s => "\"" ^ s ^ "\""
85 | Ident_e [id] => id
86 | Ident_e [sn, id] => sn ^ "." ^ id
87 | Plus_e (e1, e2) => expString e1 ^ " + " ^ expString e2
88 | Minus_e (e1, e2) => expString e1 ^ " - " ^ expString e2
89 | Orelse_e (e1, e2) => expString e1 ^ " || " ^ expString e2
90 | Andalso_e (e1, e2) => expString e1 ^ " && " ^ expString e2
91 | Times_e (e1, e2) => expString e1 ^ " * " ^ expString e2
92 | Divide_e (e1, e2) => expString e1 ^ " / " ^ expString e2
93 | Mod_e (e1, e2) => expString e1 ^ " % " ^ expString e2
94 | Eq_e (e1, e2) => expString e1 ^ " = " ^ expString e2
95 | Neq_e (e1, e2) => expString e1 ^ " <> " ^ expString e2
96 | Gt_e (e1, e2) => expString e1 ^ " > " ^ expString e2
97 | Gte_e (e1, e2) => expString e1 ^ " >= " ^ expString e2
98 | Lt_e (e1, e2) => expString e1 ^ " < " ^ expString e2
99 | Lte_e (e1, e2) => expString e1 ^ " <= " ^ expString e2
100 | Neg_e => "-"
101 | Param_e => "$"
102 | Template_e id => "@" ^ id
103 | Cons_e (e1, e2) => expString e1 ^ " :: " ^ expString e2
104 | Record_e (true, elms) => foldl (fn ((_, e), s) => s ^ ", " ^ expString e) "(" elms ^ ")"
105 | Record_e (false, elms) => foldl (fn ((lab, e), s) => s ^ ", " ^ lab ^ " = " ^ expString e) "(" elms ^ ")"
106 | RecordUpd_e _ => "<RECORDUPDATE>"
107 | Proj_e id => "#" ^ id
108 | App_e (e1, e2) => expString e1 ^ "(" ^ expString e2 ^ ")"
109 | _ => "<NUTTY>")
110
111 fun pathString [] = "<ERROR>"
112 | pathString [id] = id
113 | pathString (h::t) = h ^ "." ^ pathString t
114 end