summaryrefslogtreecommitdiff
path: root/src/Hint/Eval.hs
diff options
context:
space:
mode:
authorKleidi Bujari <mail@4kb.net>2026-10-13 23:44:16 -0700
committerKleidi Bujari <mail@4kb.net>2026-10-13 23:48:06 -0700
commit61a467c1be9895cc8c22b73ac0ff4350047d43e6 (patch)
tree563be1036dd60cbf2751e916787cbd3b35e42d2d /src/Hint/Eval.hs
parent479a316905ea882375aefe7944f9c02b176a55e2 (diff)
downloadhint-master.tar.gz
hint-master.tar.bz2
hint-master.zip
baseline parsing/evalHEADmaster
Diffstat (limited to 'src/Hint/Eval.hs')
-rw-r--r--src/Hint/Eval.hs29
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