mirror of https://git.sr.ht/~solsen/rose
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.1 KiB
34 lines
1.1 KiB
module Rose.Data.Let |
|
|
|
import Data.Error |
|
import Data.Atom |
|
import Data.SExpression |
|
import Data.Form |
|
import Util |
|
|
|
export |
|
LetError : SE -> RoseError |
|
LetError se = MkError $ "Invalid let form: " ++ show se |
|
|
|
export |
|
UnevenBindingsError : SE -> RoseError |
|
UnevenBindingsError se = MkError $ "Bindings must be even: " ++ show se |
|
|
|
export |
|
data Let : Type where |
|
MkLet : List SE -> List SE -> SE -> Let |
|
InvalidLet : RoseError -> SE -> Let |
|
|
|
export |
|
Interpreted Let where |
|
interpret (Expression (MkAtom (MkSymbol "let")) (Expression bindings body)) = |
|
if (mod (length (toList bindings)) 2) == 0 |
|
then MkLet (evenPositions bindings) (oddPositions bindings) body |
|
else InvalidLet (UnevenBindingsError bindings) (Expression (MkAtom (MkSymbol "let")) (Expression bindings body)) |
|
interpret se = InvalidLet (LetError se) se |
|
|
|
expression (MkLet variables values body) = |
|
(Expression (MkAtom (MkSymbol "let")) (Expression (fromList bindings) body)) |
|
where bindings : List SE |
|
bindings = foldr (++) [] $ zipWith (\x,y => x::y::[]) variables values |
|
expression (InvalidLet _ se) = se
|
|
|