|
7 | 7 | include "mexpr/ast.mc" |
8 | 8 | include "mexpr/eq.mc" |
9 | 9 | include "mexpr/pprint.mc" |
| 10 | +include "mexpr/symbolize.mc" |
10 | 11 |
|
11 | 12 | lang Resymbolize = Ast |
12 | 13 | sem resymbolizeBindings : Expr -> Expr |
@@ -221,3 +222,55 @@ lang MExprResymbolize = |
221 | 222 | sem resymbolizeType nameMap = |
222 | 223 | | ty -> smap_Type_Type (resymbolizeType nameMap) ty |
223 | 224 | end |
| 225 | + |
| 226 | +lang TestLang = MExprResymbolize + MExprEq + MExprPrettyPrint + MExprSym |
| 227 | + sem collectSymVars : Expr -> Map String Name |
| 228 | + sem collectSymVars = |
| 229 | + | e -> collectSymVarsH (mapEmpty cmpString) e |
| 230 | + |
| 231 | + sem collectSymVarsH : Map String Name -> Expr -> Map String Name |
| 232 | + sem collectSymVarsH acc = |
| 233 | + | TmVar t -> |
| 234 | + if nameHasSym t.ident then mapInsert (nameGetStr t.ident) t.ident acc |
| 235 | + else acc |
| 236 | + | t -> sfold_Expr_Expr collectSymVarsH acc t |
| 237 | +end |
| 238 | + |
| 239 | +mexpr |
| 240 | + |
| 241 | +use TestLang in |
| 242 | + |
| 243 | +let optionGet = lam o. optionGetOrElse (lam. never) o in |
| 244 | +let nameNotEq = lam l. lam r. not (nameEq l r) in |
| 245 | + |
| 246 | +-- Unsymbolized variables in the AST are symbolized |
| 247 | +let e = resymbolizeBindings (var_ "x") in |
| 248 | +let syms = collectSymVars e in |
| 249 | +utest mapMem "x" syms with false in |
| 250 | + |
| 251 | +-- Variables bound in the AST are re-symbolized |
| 252 | +let x = nameSym "x" in |
| 253 | +let e = resymbolizeBindings (bind_ (nulet_ x (int_ 2)) (nvar_ x)) in |
| 254 | +let syms = collectSymVars e in |
| 255 | +utest mapMem "x" syms with true in |
| 256 | +utest optionGet (mapLookup "x" syms) with x using nameNotEq in |
| 257 | + |
| 258 | +-- Unsymbolized variables that are bound in the AST are symbolized |
| 259 | +let e = resymbolizeBindings (bind_ (ulet_ "x" (int_ 2)) (var_ "x")) in |
| 260 | +let syms = collectSymVars e in |
| 261 | +utest mapMem "x" syms with true in |
| 262 | + |
| 263 | +-- Symbolized free variables are not re-symbolized |
| 264 | +let y = nameSym "y" in |
| 265 | +let e = resymbolizeBindings |
| 266 | + (bind_ |
| 267 | + (nulet_ x (int_ 2)) |
| 268 | + (addi_ (nvar_ x) (nvar_ y))) |
| 269 | +in |
| 270 | +let syms = collectSymVars e in |
| 271 | +utest mapMem "x" syms with true in |
| 272 | +utest mapMem "y" syms with true in |
| 273 | +utest optionGet (mapLookup "x" syms) with x using nameNotEq in |
| 274 | +utest optionGet (mapLookup "y" syms) with y using nameEq in |
| 275 | + |
| 276 | +() |
0 commit comments