add cons patterns
This commit is contained in:
parent
0ee94c3bc2
commit
870aad52cf
1 changed files with 14 additions and 1 deletions
15
interp.hs
15
interp.hs
|
@ -13,6 +13,7 @@ data AST = Add AST AST
|
|||
|
||||
data Pattern = VarP String
|
||||
| IntP Int
|
||||
| ConsP Pattern Pattern
|
||||
deriving (Show, Eq)
|
||||
|
||||
data Value = IntV Int
|
||||
|
@ -68,11 +69,19 @@ eval (Call name args) = get >>= \m ->
|
|||
|
||||
patternBindings :: Pattern -> Value -> Maybe Env
|
||||
patternBindings (VarP n) v = Just $ M.fromList [(n, v)]
|
||||
|
||||
patternBindings (IntP n) (IntV v)
|
||||
| v == n = Just M.empty
|
||||
| otherwise = Nothing
|
||||
patternBindings (IntP n) _ = Nothing
|
||||
|
||||
patternBindings (ConsP _ _) (ListV (_:[])) = Nothing
|
||||
patternBindings (ConsP xp xsp) (ListV (x:xs)) =
|
||||
do
|
||||
xe <- patternBindings xp x
|
||||
xse <- patternBindings xsp $ ListV xs
|
||||
Just $ M.union xe xse
|
||||
|
||||
-- applies many arguments to a function
|
||||
applyMany :: Value -> [Value] -> InterpState Value
|
||||
applyMany fn@(FnV _) (arg:xs) =
|
||||
|
@ -118,4 +127,8 @@ main = do
|
|||
(VarP "x", [IntConst 300])
|
||||
],
|
||||
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 [
|
||||
(ConsP (VarP "x") (ConsP (VarP "y") (VarP "ys")), [Var "ys"])
|
||||
],
|
||||
Call "f" [Var "lst"]]
|
Loading…
Reference in a new issue