diff options
author | Akshay <[email protected]> | 2020-10-23 13:34:27 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-10-23 13:34:27 +0100 |
commit | fbdd64d7e0888dd106abe8da7a7c46ec032db1ea (patch) | |
tree | e237a9a151583ccf2daf1f116572d737f1259455 | |
parent | 494078074e5f620f11f72e48d0fc44cff73faaf2 (diff) |
add Base module
-rw-r--r-- | src/Base.hs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/Base.hs b/src/Base.hs new file mode 100644 index 0000000..08131e8 --- /dev/null +++ b/src/Base.hs | |||
@@ -0,0 +1,42 @@ | |||
1 | module Base (Expr (..) | ||
2 | , Env (..) | ||
3 | , Function (..) | ||
4 | ) where | ||
5 | |||
6 | import Data.IORef | ||
7 | |||
8 | -- TODO: use LispNumber (src/Operators.hs) here instead of IntLiteral and FloatLiteral | ||
9 | -- TODO: add character literals: \#a \#b \#c \#space \#newline | ||
10 | -- TODO: add support for complex numbers, oct and hex numbers | ||
11 | data Expr = List [Expr] | ||
12 | | Vector [Expr] | ||
13 | | DottedList [Expr] Expr | ||
14 | | StringLiteral String | ||
15 | | IntLiteral Integer | ||
16 | | FloatLiteral Double | ||
17 | | BoolLiteral Bool | ||
18 | | Id String | ||
19 | deriving (Eq) | ||
20 | |||
21 | data Function = | ||
22 | Function { | ||
23 | params :: [String] | ||
24 | , body :: Expr | ||
25 | , environment :: Env | ||
26 | } | ||
27 | |||
28 | type Env = IORef [(String, IORef Expr)] | ||
29 | |||
30 | showLispList :: [Expr] -> String | ||
31 | showLispList = unwords . map show | ||
32 | |||
33 | instance Show Expr where | ||
34 | show (DottedList xs x) = "(" ++ showLispList xs ++ " . " ++ show x ++ ")" | ||
35 | show (List xs) = "(" ++ showLispList xs ++ ")" | ||
36 | show (Vector xs) = "#(" ++ showLispList xs ++ ")" | ||
37 | show (StringLiteral s) = "\"" ++ s ++ "\"" | ||
38 | show (IntLiteral n) = show n | ||
39 | show (FloatLiteral n) = show n | ||
40 | show (BoolLiteral True) = "#t" | ||
41 | show (BoolLiteral False) = "#f" | ||
42 | show (Id i) = i | ||