-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathValue.hs
More file actions
57 lines (50 loc) · 1.42 KB
/
Value.hs
File metadata and controls
57 lines (50 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Value (Value (..)) where
import Language.ECMAScript3.Syntax
import Data.Int as Int
data Value = Bool Bool
| Int Int
| String String
| Var String
| Error String
| Nil
| Break
| FunVal [Id] [Statement]
| Return Value
| List [Value]
| Pointer String
--
-- Pretty Printer
--
instance Show Value where
show (Bool True) = "true"
show (Bool False) = "false"
show (Int int) = show int
show (String str) = "\"" ++ str ++ "\""
show (Var name) = name
show (Error str) = "Error: " ++ str
show Nil = ""
show Break = ""
show (FunVal b c) = "<<FUNCAO>>"
show (Return a) = "f r " ++ show a
show (List a) = show a
instance Eq Value where
(Int a) == (Int b) = a == b
(List []) == (List []) = True
(List a) == (List b) = compareLists a b
exists a [] = False
exists a (b:c) = if a == b then True else exists a c
extrct a [] = []
extrct a (b:c) = if a == b then extrct a c else b:(extrct a c)
compareLists [] [] = True
compareLists (a:b) c = if (exists a c) then
let
newC = (extrct a c)
in compareLists b newC
else False
-- This function could be replaced by (unwords.map show). The unwords
-- function takes a list of String values and uses them to build a
-- single String where the words are separated by spaces.
showListContents :: [Value] -> String
showListContents [] = ""
showListContents [a] = show a
showListContents (a:as) = show a ++ " " ++ (showListContents as)