diff options
| author | Kleidi Bujari <mail@4kb.net> | 2026-10-13 23:44:16 -0700 |
|---|---|---|
| committer | Kleidi Bujari <mail@4kb.net> | 2026-10-13 23:48:06 -0700 |
| commit | 61a467c1be9895cc8c22b73ac0ff4350047d43e6 (patch) | |
| tree | 563be1036dd60cbf2751e916787cbd3b35e42d2d /src/Hint/Eval.hs | |
| parent | 479a316905ea882375aefe7944f9c02b176a55e2 (diff) | |
| download | hint-master.tar.gz hint-master.tar.bz2 hint-master.zip | |
Diffstat (limited to 'src/Hint/Eval.hs')
| -rw-r--r-- | src/Hint/Eval.hs | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/src/Hint/Eval.hs b/src/Hint/Eval.hs index 48f7b46..36628dc 100644 --- a/src/Hint/Eval.hs +++ b/src/Hint/Eval.hs @@ -2,31 +2,34 @@ module Hint.Eval where import Control.Applicative ((<|>)) import qualified Data.Map as M -import Hint.Parser (Value (..)) +import Data.Maybe (fromJust) +import Hint.Data -type Ident = String -type Env = M.Map Ident Value +type Env = M.Map Identifier Expr data Scope = Scope { env :: [Env] } deriving (Show, Eq) -defaultEnv :: Env -defaultEnv = M.fromList [("pi", VInt 3), ("version", VString "0.1")] - basic :: Scope -basic = Scope (pure defaultEnv) - -next :: Env -next = M.fromList [("ident", VInt 12), ("pi", VInt 4)] +basic = Scope mempty -enter :: Scope -> Env -> Scope -enter (Scope es) e = Scope (e : es) +enter :: Env -> Scope -> Scope +enter e (Scope es) = Scope (e : es) leave :: Scope -> Scope leave (Scope [] ) = error "bad" leave (Scope (_ : es)) = Scope es -scopeLookup :: Ident -> Scope -> Maybe Value +scopeLookup :: Identifier -> Scope -> Maybe Expr scopeLookup _ (Scope []) = Nothing scopeLookup i (Scope (e : es)) = M.lookup i e <|> scopeLookup i (Scope es) + +eval :: Scope -> Expr -> Maybe Expr +eval _ (Literal l) = Literal <$> pure l +eval s (Ident i) = scopeLookup i s +eval scope (Bind name val rest) = + eval (enter (M.fromList [(name, fromJust $ eval scope val)]) scope) rest +eval s (If cond a b) = do + Literal (Boolean c) <- eval s cond + if c then eval s a else eval s b |
