add list pattern
This commit is contained in:
parent
870aad52cf
commit
acce1a15dd
12
interp.hs
12
interp.hs
|
@ -14,6 +14,7 @@ data AST = Add AST AST
|
||||||
data Pattern = VarP String
|
data Pattern = VarP String
|
||||||
| IntP Int
|
| IntP Int
|
||||||
| ConsP Pattern Pattern
|
| ConsP Pattern Pattern
|
||||||
|
| ListP [Pattern]
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
data Value = IntV Int
|
data Value = IntV Int
|
||||||
|
@ -75,6 +76,7 @@ patternBindings (IntP n) (IntV v)
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
patternBindings (IntP n) _ = Nothing
|
patternBindings (IntP n) _ = Nothing
|
||||||
|
|
||||||
|
patternBindings (ConsP x (ListP [])) (ListV (y:[])) = patternBindings x y
|
||||||
patternBindings (ConsP _ _) (ListV (_:[])) = Nothing
|
patternBindings (ConsP _ _) (ListV (_:[])) = Nothing
|
||||||
patternBindings (ConsP xp xsp) (ListV (x:xs)) =
|
patternBindings (ConsP xp xsp) (ListV (x:xs)) =
|
||||||
do
|
do
|
||||||
|
@ -82,6 +84,15 @@ patternBindings (ConsP xp xsp) (ListV (x:xs)) =
|
||||||
xse <- patternBindings xsp $ ListV xs
|
xse <- patternBindings xsp $ ListV xs
|
||||||
Just $ M.union xe xse
|
Just $ M.union xe xse
|
||||||
|
|
||||||
|
patternBindings (ListP []) (ListV (x:xs)) = Nothing -- not enough patterns
|
||||||
|
patternBindings (ListP (_:_)) (ListV []) = Nothing -- not enough values
|
||||||
|
patternBindings (ListP []) (ListV []) = Just M.empty -- base case
|
||||||
|
patternBindings (ListP (x:xs)) (ListV (y:ys)) =
|
||||||
|
do
|
||||||
|
env <- patternBindings x y
|
||||||
|
env' <- patternBindings (ListP xs) (ListV ys)
|
||||||
|
Just $ M.union env' env
|
||||||
|
|
||||||
-- applies many arguments to a function
|
-- applies many arguments to a function
|
||||||
applyMany :: Value -> [Value] -> InterpState Value
|
applyMany :: Value -> [Value] -> InterpState Value
|
||||||
applyMany fn@(FnV _) (arg:xs) =
|
applyMany fn@(FnV _) (arg:xs) =
|
||||||
|
@ -129,6 +140,7 @@ main = do
|
||||||
Call "f" [IntConst 2]]
|
Call "f" [IntConst 2]]
|
||||||
prg4 = [ Def "lst" (ListConst [IntConst 1, IntConst 2, IntConst 3]),
|
prg4 = [ Def "lst" (ListConst [IntConst 1, IntConst 2, IntConst 3]),
|
||||||
Def "f" $ Lambda [
|
Def "f" $ Lambda [
|
||||||
|
(ListP [VarP "x", VarP "y", VarP "z", VarP "w"], [Var "w"]),
|
||||||
(ConsP (VarP "x") (ConsP (VarP "y") (VarP "ys")), [Var "ys"])
|
(ConsP (VarP "x") (ConsP (VarP "y") (VarP "ys")), [Var "ys"])
|
||||||
],
|
],
|
||||||
Call "f" [Var "lst"]]
|
Call "f" [Var "lst"]]
|
Loading…
Reference in New Issue