elm/compiler/Transform/Substitute.hs
Evan Czaplicki 9dd5dff279 Make AST more general and try to give its phases better names
Also change the constructors for the Pattern ADT
2014-02-10 00:17:33 +01:00

44 lines
1.6 KiB
Haskell

{-# OPTIONS_GHC -Wall #-}
module Transform.Substitute (subst) where
import Control.Arrow (second, (***))
import qualified Data.Set as Set
import SourceSyntax.Annotation
import SourceSyntax.Expression
import qualified SourceSyntax.Pattern as Pattern
import qualified SourceSyntax.Variable as V
subst :: String -> Expr' -> Expr' -> Expr'
subst old new expr =
let f (A a e) = A a (subst old new e) in
case expr of
Range e1 e2 -> Range (f e1) (f e2)
ExplicitList es -> ExplicitList (map f es)
Binop op e1 e2 -> Binop op (f e1) (f e2)
Lambda p e
| Set.member old (Pattern.boundVars p) -> expr
| otherwise -> Lambda p (f e)
App e1 e2 -> App (f e1) (f e2)
MultiIf ps -> MultiIf (map (f *** f) ps)
Let defs body
| anyShadow -> expr
| otherwise -> Let (map substDef defs) (f body)
where
substDef (Definition p e t) = Definition p (f e) t
anyShadow =
any (Set.member old . Pattern.boundVars) [ p | Definition p _ _ <- defs ]
Var (V.Raw x) -> if x == old then new else expr
Case e cases -> Case (f e) $ map (second f) cases
Data name es -> Data name (map f es)
Access e x -> Access (f e) x
Remove e x -> Remove (f e) x
Insert e x v -> Insert (f e) x (f v)
Modify r fs -> Modify (f r) (map (second f) fs)
Record fs -> Record (map (second f) fs)
Literal _ -> expr
Markdown uid md es -> Markdown uid md (map f es)
PortIn name st -> PortIn name st
PortOut name st signal -> PortOut name st (f signal)