diff options
| author | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2016-03-11 05:20:55 +0100 |
|---|---|---|
| committer | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2016-03-11 05:21:50 +0100 |
| commit | bf6f745595570a3ebaad34b8c4a8a8c827e15178 (patch) | |
| tree | d7731846dbe42d3bf1e33a56a727904e8abc891b /src/K/Eval.hs | |
| parent | 92c9891946646f0f54a19b7699e128571fbdc4b3 (diff) | |
Adds support for a very simple language (described by K.Ast).
Diffstat (limited to 'src/K/Eval.hs')
| -rw-r--r-- | src/K/Eval.hs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/K/Eval.hs b/src/K/Eval.hs new file mode 100644 index 0000000..a7ebcba --- /dev/null +++ b/src/K/Eval.hs @@ -0,0 +1,40 @@ +module K.Eval where + +import K.Ast +import K.Parsing +import Data.Maybe + +isNum Zero = True +isNum (Succ t) = isNum t +isNum _ = False + +isVal :: Expr -> Bool +isVal Tru = True +isVal Fals = True +isVal t | isNum t = True +isVal _ = False + +eval' x = case x of + IsZero Zero -> Just Tru + IsZero (Succ t) | isNum t -> Just Fals + IsZero t -> IsZero <$> eval' t + + Succ t -> Succ <$> eval' t + + Pred Zero -> Just Zero + Pred (Succ t) | isNum t -> Just t + Pred t -> Pred <$> eval' t + + If Tru t _ -> Just t + If Fals _ f -> Just f + If c t f -> (\c' -> If c' t f) <$> eval' c + + _ -> Nothing + +nf x = fromMaybe x (nf <$> eval' x) + +eval :: Expr -> Maybe Expr +eval t = case nf t of + nft | isVal nft -> Just nft + | otherwise -> Nothing + |
