aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-10-23 13:33:31 +0100
committerAkshay <[email protected]>2020-10-23 13:33:31 +0100
commit494078074e5f620f11f72e48d0fc44cff73faaf2 (patch)
tree25aea03ceaae5fc3010e1894ae74d6eee0259cb5 /src
parentf68c7f7a140127320f9dadc57a3d2a485b86d3df (diff)
refactor internal datatypes to Base
- avoids cyclic module dependencies - cleaner exports to Main module
Diffstat (limited to 'src')
-rw-r--r--src/Environment.hs4
-rw-r--r--src/Error/Base.hs2
-rw-r--r--src/Evaluator.hs3
-rw-r--r--src/Operators.hs2
-rw-r--r--src/Parser.hs29
5 files changed, 6 insertions, 34 deletions
diff --git a/src/Environment.hs b/src/Environment.hs
index 4c444b6..b7b7ee6 100644
--- a/src/Environment.hs
+++ b/src/Environment.hs
@@ -8,15 +8,13 @@ module Environment ( Env
8 , IOResult 8 , IOResult
9 ) where 9 ) where
10 10
11import Base (Env (..), Expr (..))
11import Control.Applicative ((<$>)) 12import Control.Applicative ((<$>))
12import Control.Monad (mapM) 13import Control.Monad (mapM)
13import Control.Monad.Except 14import Control.Monad.Except
14import Data.IORef 15import Data.IORef
15import Data.Maybe (isJust) 16import Data.Maybe (isJust)
16import Error.Base (LispError (..), LispResult (..), unwrap) 17import Error.Base (LispError (..), LispResult (..), unwrap)
17import Parser (Expr (..))
18
19type Env = IORef [(String, IORef Expr)]
20 18
21newEnv :: IO Env 19newEnv :: IO Env
22newEnv = newIORef [] 20newEnv = newIORef []
diff --git a/src/Error/Base.hs b/src/Error/Base.hs
index 747904a..008a2fc 100644
--- a/src/Error/Base.hs
+++ b/src/Error/Base.hs
@@ -3,8 +3,8 @@ module Error.Base ( LispError (..)
3 , unwrap 3 , unwrap
4 ) where 4 ) where
5 5
6import Base (Expr (..))
6import Control.Monad.Except 7import Control.Monad.Except
7import Parser
8import Text.Parsec 8import Text.Parsec
9import Text.Parsec.Error 9import Text.Parsec.Error
10import Text.ParserCombinators.Parsec 10import Text.ParserCombinators.Parsec
diff --git a/src/Evaluator.hs b/src/Evaluator.hs
index 9e6632e..3bc1e09 100644
--- a/src/Evaluator.hs
+++ b/src/Evaluator.hs
@@ -1,5 +1,6 @@
1module Evaluator (eval) where 1module Evaluator (eval) where
2 2
3import Base
3import Control.Applicative ((*>)) 4import Control.Applicative ((*>))
4import Control.Arrow ((&&&)) 5import Control.Arrow ((&&&))
5import Control.Monad.Except 6import Control.Monad.Except
@@ -7,7 +8,6 @@ import Environment
7import Error.Base (LispError (..), LispResult (..), 8import Error.Base (LispError (..), LispResult (..),
8 unwrap) 9 unwrap)
9import Operators 10import Operators
10import Parser
11import Text.ParserCombinators.Parsec 11import Text.ParserCombinators.Parsec
12 12
13apply :: String -> [Expr] -> LispResult Expr 13apply :: String -> [Expr] -> LispResult Expr
@@ -20,6 +20,7 @@ evalUnquoteSplicing :: Env -> Expr -> IOResult Expr
20evalUnquoteSplicing env (List xs) = List <$> mapM (eval env) xs 20evalUnquoteSplicing env (List xs) = List <$> mapM (eval env) xs
21evalUnquoteSplicing env literal = return literal 21evalUnquoteSplicing env literal = return literal
22 22
23-- might be worth including unquote and unquote-splicing in lisk's prelude
23evalUnquote :: Env -> Expr -> IOResult Expr 24evalUnquote :: Env -> Expr -> IOResult Expr
24evalUnquote env (DottedList h t) = List . (:[]) <$> liftM2 DottedList (mapM (evalUnquote env) h) (evalUnquote env t) 25evalUnquote env (DottedList h t) = List . (:[]) <$> liftM2 DottedList (mapM (evalUnquote env) h) (evalUnquote env t)
25evalUnquote env (Vector vs) = List . (:[]) . Vector <$> mapM (evalUnquote env) vs 26evalUnquote env (Vector vs) = List . (:[]) . Vector <$> mapM (evalUnquote env) vs
diff --git a/src/Operators.hs b/src/Operators.hs
index fa39e23..bab888d 100644
--- a/src/Operators.hs
+++ b/src/Operators.hs
@@ -1,8 +1,8 @@
1module Operators (primitives) where 1module Operators (primitives) where
2 2
3import Base
3import Control.Monad.Except 4import Control.Monad.Except
4import Error.Base (LispError (..), LispResult (..)) 5import Error.Base (LispError (..), LispResult (..))
5import Parser
6 6
7primitives :: [(String, [Expr] -> LispResult Expr)] 7primitives :: [(String, [Expr] -> LispResult Expr)]
8primitives = map (\(n, f) -> (n, f n)) 8primitives = map (\(n, f) -> (n, f n))
diff --git a/src/Parser.hs b/src/Parser.hs
index f83f4cc..94de680 100644
--- a/src/Parser.hs
+++ b/src/Parser.hs
@@ -1,5 +1,4 @@
1module Parser ( parseLispValue 1module Parser ( parseLispValue
2 , Expr(..)
3 , parseString 2 , parseString
4 , parseInt 3 , parseInt
5 , parseFloat 4 , parseFloat
@@ -7,22 +6,10 @@ module Parser ( parseLispValue
7 , parseQuote 6 , parseQuote
8 ) where 7 ) where
9 8
9import Base (Expr (..), Function)
10import Control.Applicative ((<$>)) 10import Control.Applicative ((<$>))
11import Text.ParserCombinators.Parsec 11import Text.ParserCombinators.Parsec
12 12
13-- TODO: use LispNumber (src/Operators.hs) here instead of IntLiteral and FloatLiteral
14-- TODO: add character literals: \#a \#b \#c \#space \#newline
15-- TODO: add support for complex numbers, oct and hex numbers
16data Expr = List [Expr]
17 | Vector [Expr]
18 | DottedList [Expr] Expr
19 | StringLiteral String
20 | IntLiteral Integer
21 | FloatLiteral Double
22 | BoolLiteral Bool
23 | Id String
24 deriving (Eq)
25
26-- backslash double quote escapes a quote inside strings 13-- backslash double quote escapes a quote inside strings
27quotedChar = noneOf ['\"'] <|> try (string "\\\"" >> return '"') 14quotedChar = noneOf ['\"'] <|> try (string "\\\"" >> return '"')
28 15
@@ -92,7 +79,6 @@ parseQuote = parseModifier "'" "quote"
92parseQuasiquote = parseModifier "`" "quasiquote" 79parseQuasiquote = parseModifier "`" "quasiquote"
93parseUnquote = parseModifier "," "unquote" 80parseUnquote = parseModifier "," "unquote"
94parseUnquoteSplicing = parseModifier ",@" "unquote-splicing" 81parseUnquoteSplicing = parseModifier ",@" "unquote-splicing"
95-- TODO: add modifier for unquote splicing: ,@
96 82
97parseLispValue :: Parser Expr 83parseLispValue :: Parser Expr
98parseLispValue = 84parseLispValue =
@@ -114,16 +100,3 @@ parseLispValue =
114 return $ maybe (List x) (DottedList x) t 100 return $ maybe (List x) (DottedList x) t
115 <?> "lisp value" 101 <?> "lisp value"
116 102
117showLispList :: [Expr] -> String
118showLispList = unwords . map show
119
120instance Show Expr where
121 show (DottedList xs x) = "(" ++ showLispList xs ++ " . " ++ show x ++ ")"
122 show (List xs) = "(" ++ showLispList xs ++ ")"
123 show (Vector xs) = "#(" ++ showLispList xs ++ ")"
124 show (StringLiteral s) = "\"" ++ s ++ "\""
125 show (IntLiteral n) = show n
126 show (FloatLiteral n) = show n
127 show (BoolLiteral True) = "#t"
128 show (BoolLiteral False) = "#f"
129 show (Id i) = i