aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/Main.hs8
-rw-r--r--lisk.cabal1
-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
7 files changed, 13 insertions, 36 deletions
diff --git a/bin/Main.hs b/bin/Main.hs
index 18e1c53..3d9fad4 100644
--- a/bin/Main.hs
+++ b/bin/Main.hs
@@ -1,5 +1,6 @@
1module Main where 1module Main where
2 2
3import Base (Expr (..))
3import Control.Monad (liftM) 4import Control.Monad (liftM)
4import Control.Monad.Except (liftIO, runExceptT, throwError) 5import Control.Monad.Except (liftIO, runExceptT, throwError)
5import Environment 6import Environment
@@ -7,7 +8,7 @@ import Error.Base (LispError (..), LispResult (..),
7 unwrap) 8 unwrap)
8import Error.Pretty (defaults, showError) 9import Error.Pretty (defaults, showError)
9import Evaluator (eval) 10import Evaluator (eval)
10import Parser (Expr (..), parseLispValue) 11import Parser (parseLispValue)
11import System.Console.Readline 12import System.Console.Readline
12import System.Environment (getArgs) 13import System.Environment (getArgs)
13import Text.ParserCombinators.Parsec 14import Text.ParserCombinators.Parsec
@@ -29,7 +30,10 @@ repl env = do
29 case inp of 30 case inp of
30 Nothing -> return () 31 Nothing -> return ()
31 Just ",q" -> return () 32 Just ",q" -> return ()
32 Just i -> evalExpr env i >>= either (putStrLn . pp i) putStrLn >> repl env 33 Just i -> do
34 addHistory i
35 evalExpr env i >>= either (putStrLn . pp i) putStrLn
36 repl env
33 37
34 38
35main :: IO () 39main :: IO ()
diff --git a/lisk.cabal b/lisk.cabal
index 6c2bdd4..47e111b 100644
--- a/lisk.cabal
+++ b/lisk.cabal
@@ -24,6 +24,7 @@ library
24 parsec == 3.*, 24 parsec == 3.*,
25 mtl >= 2.1 25 mtl >= 2.1
26 exposed-modules: 26 exposed-modules:
27 Base,
27 Parser, 28 Parser,
28 Evaluator, 29 Evaluator,
29 Operators, 30 Operators,
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