Skip to content

Commit

Permalink
style: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
BRonen committed Feb 13, 2024
1 parent c5ab422 commit 2d69a7b
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 359 deletions.
30 changes: 15 additions & 15 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
module Main (main) where

import Parser (parse)
import Lexer (lexer)
import Checker (checker)
import Backend.Eval (eval)
import Checker (checker)
import Lexer (lexer)
import Parser (parse)

main :: IO ()
main = do
let tokens = lexer "type A = Int in let a: Bool & A | Bool = if False then 1 else False in a"
let ast = parse tokens
case checker ast of
Right resultT -> do
print resultT
result <- eval ast
case result of
Right v -> print v
Left err -> print err
Left err -> do
print ast
print err
let tokens = lexer "type A = Int in let a: Bool & A | Bool = if False then 1 else False in a"
let ast = parse tokens
case checker ast of
Right resultT -> do
print resultT
result <- eval ast
case result of
Right v -> print v
Left err -> print err
Left err -> do
print ast
print err
168 changes: 79 additions & 89 deletions src/Backend/Eval.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Backend.Eval (eval) where

import Data.Map as Map
import Parser (
SExpr (..)
)
import Parser
( SExpr (..),
)

data Value
= Int Integer
| Str String
| Boolean Bool
| Var String
| Func Context String SExpr
deriving (Show)
= Int Integer
| Str String
| Boolean Bool
| Var String
| Func Context String SExpr
deriving (Show)

type Context = Map String Value

Expand All @@ -20,100 +20,90 @@ eval = evaluate Map.empty

evaluate :: Context -> SExpr -> IO (Either String Value)
evaluate ctx (SLetInfer name value next) = do
value' <- evaluate ctx value
case value' of
Right value'' -> do
let ctx' = Map.insert name value'' ctx
evaluate ctx' next
Left err -> pure $ Left err
value' <- evaluate ctx value
case value' of
Right value'' -> do
let ctx' = Map.insert name value'' ctx
evaluate ctx' next
Left err -> pure $ Left err
evaluate ctx (SLet name _ value next) = do
value' <- evaluate ctx value
case value' of
Right value'' -> do
let ctx' = Map.insert name value'' ctx
evaluate ctx' next
Left err -> pure $ Left err

value' <- evaluate ctx value
case value' of
Right value'' -> do
let ctx' = Map.insert name value'' ctx
evaluate ctx' next
Left err -> pure $ Left err
evaluate ctx (STypeAlias _ _ next) = evaluate ctx next

evaluate ctx (SDefInfer param _ body) = pure $ Right $ Func ctx param body
evaluate ctx (SDef param _ _ body) = pure $ Right $ Func ctx param body

evaluate ctx (SConditional condition cthen celse) = do
conditional <- evaluate ctx condition
case conditional of
Right (Int 0) -> evaluate ctx celse
Right (Str "") -> evaluate ctx celse
Right (Boolean False) -> evaluate ctx celse
Right _ -> evaluate ctx cthen
Left err -> pure $ Left err

conditional <- evaluate ctx condition
case conditional of
Right (Int 0) -> evaluate ctx celse
Right (Str "") -> evaluate ctx celse
Right (Boolean False) -> evaluate ctx celse
Right _ -> evaluate ctx cthen
Left err -> pure $ Left err
evaluate ctx (SApp func arg) = do
func' <- evaluate ctx func
case func' of
Right (Func ctx' param body) -> do
arg' <- evaluate ctx arg
case arg' of
Right arg'' -> evaluate (Map.insert param arg'' ctx') body
Left err -> pure $ Left err
Left _ -> pure func'
_ -> pure $ Left $ "Trying to call a non-callable value: " ++ show func

func' <- evaluate ctx func
case func' of
Right (Func ctx' param body) -> do
arg' <- evaluate ctx arg
case arg' of
Right arg'' -> evaluate (Map.insert param arg'' ctx') body
Left err -> pure $ Left err
Left _ -> pure func'
_ -> pure $ Left $ "Trying to call a non-callable value: " ++ show func
evaluate ctx (SPlus x y) = do
x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int v), Right (Int v')) -> pure $ Right $ Int $ v + v'
_ -> pure $ Left $ "Calling sum with invalid params: [ " ++ show x' ++ " - " ++ show y ++ " ]"
x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int v), Right (Int v')) -> pure $ Right $ Int $ v + v'
_ -> pure $ Left $ "Calling sum with invalid params: [ " ++ show x' ++ " - " ++ show y ++ " ]"
evaluate ctx (SMinus x y) = do
x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int v), Right (Int v')) -> pure $ Right $ Int $ v - v'
_ -> pure $ Left $ "Calling subtraction with invalid params: [ " ++ show x' ++ " - " ++ show y' ++ " ]"

x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int v), Right (Int v')) -> pure $ Right $ Int $ v - v'
_ -> pure $ Left $ "Calling subtraction with invalid params: [ " ++ show x' ++ " - " ++ show y' ++ " ]"
evaluate ctx (SDiv x y) = do
x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int v), Right (Int v')) -> pure $ Right $ Int $ v `div` v'
_ -> pure $ Left $ "Calling division with invalid params: [ " ++ show x' ++ " - " ++ show y ++ " ]"
x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int v), Right (Int v')) -> pure $ Right $ Int $ v `div` v'
_ -> pure $ Left $ "Calling division with invalid params: [ " ++ show x' ++ " - " ++ show y ++ " ]"
evaluate ctx (STimes x y) = do
x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int v), Right (Int v')) -> pure $ Right $ Int $ v * v'
_ -> pure $ Left $ "Calling multiplication with invalid params: [ " ++ show x' ++ " - " ++ show y' ++ " ]"

x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int v), Right (Int v')) -> pure $ Right $ Int $ v * v'
_ -> pure $ Left $ "Calling multiplication with invalid params: [ " ++ show x' ++ " - " ++ show y' ++ " ]"
evaluate ctx (SAnd x y) = do
x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int 0), Right _) -> pure x'
(Right (Str ""), Right _) -> pure x'
(Right (Boolean False), Right _) -> pure x'
(Right _, Right _) -> pure y'
(Left err, _) -> pure $ Left err
(_, Left err) -> pure $ Left err

x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int 0), Right _) -> pure x'
(Right (Str ""), Right _) -> pure x'
(Right (Boolean False), Right _) -> pure x'
(Right _, Right _) -> pure y'
(Left err, _) -> pure $ Left err
(_, Left err) -> pure $ Left err
evaluate ctx (SOr x y) = do
x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int 0), Right _) -> pure y'
(Right (Str ""), Right _) -> pure y'
(Right (Boolean False), Right _) -> pure y'
(Right _, Right _) -> pure x'
(Left err, _) -> pure $ Left err
(_, Left err) -> pure $ Left err

x' <- evaluate ctx x
y' <- evaluate ctx y
case (x', y') of
(Right (Int 0), Right _) -> pure y'
(Right (Str ""), Right _) -> pure y'
(Right (Boolean False), Right _) -> pure y'
(Right _, Right _) -> pure x'
(Left err, _) -> pure $ Left err
(_, Left err) -> pure $ Left err
evaluate ctx (SBrack expr) = evaluate ctx expr
evaluate ctx (SName name) = case Map.lookup name ctx of
Just expr -> pure $ Right expr
Nothing -> pure $ Left $ "Variable not initialized: " ++ name
Just expr -> pure $ Right expr
Nothing -> pure $ Left $ "Variable not initialized: " ++ name
evaluate _ (SInt v) = pure $ Right $ Int v
evaluate _ (SBool v) = pure $ Right $ Boolean v
evaluate _ (SString v) = pure $ Right $ Str v

evaluate _ (SString v) = pure $ Right $ Str v
evaluate _ s = do
pure $ Left $ "Evaluating invalid node: " ++ show s
pure $ Left $ "Evaluating invalid node: " ++ show s
Loading

0 comments on commit 2d69a7b

Please sign in to comment.