-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataTypesFJ.hs
93 lines (83 loc) · 3.78 KB
/
DataTypesFJ.hs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module DataTypesFJ where
-- type for expressions
data Exp =
Variable String
| FieldAccess Exp String
| MethodInv Exp String [Exp]
| New String [Exp]
| Cast String Exp
deriving (Eq)
;;
-- type for methods
data Method = MethodDecl Type String [(Type, String)] Exp deriving (Eq);;
-- type for classes
data Class = ClassDecl String String FDS MDS deriving (Eq);;
-- type for type declaration
data Type = TypeDecl String deriving (Eq);;
-- fields in a class
type FDS = [(Type, String)];;
-- methods in a class
type MDS = [Method];;
-- all classes of the program
type ClassTable = [Class];;
-- all types of the program
type Context = [(String, Type)];;
-- to print better type declarations
showType (TypeDecl s) = s;;
instance Show Type where show = showType;;
-- to print better an expression
showExp (Variable s) = s;;
showExp (FieldAccess e s) = show e ++ "." ++ s;;
showExp (MethodInv e s p) = show e ++ "." ++ s ++ "(" ++ show p ++ ")";;
showExp (New s p) = "new " ++ s ++ "(" ++ show p ++ ")";;
showExp (Cast s e) = "(" ++ s ++ ")" ++ show e;;
instance Show Exp where show = showExp;;
-- to print better a method declaration
showMethodDecl (MethodDecl t s p e) =
show t ++ " " ++ s ++ "(" ++ show p ++ ") { " ++ "return " ++ show e ++ "; }";;
instance Show Method where show = showMethodDecl;;
-- to print better a class declaration
showNewLines [] = "";
showNewLines (x:xs) = show x ++ "\n\t" ++ showNewLines xs;
showClassDecl (ClassDecl s1 s2 f m) =
"\nclass " ++ s1 ++ " extends " ++ s2 ++ " {\n\t" ++ showNewLines f ++ "\n\t" ++ showNewLines m ++ "\n}\n";;
instance Show Class where show = showClassDecl;;
-- static exceptions
data Exception =
VariableNotFoundException String
| MethodNotFoundException String
| FieldNotFoundException String
| ClassNotFoundException String
| DuplicateVariableException String
| DuplicateClassException String
| DuplicateMethodException String
| DuplicateFieldException String
| MismatchParamsException String
| TypeException String
| OverrideException String
| Exception String
deriving (Eq)
;;
-- to print better static exceptions
showException (VariableNotFoundException e) = "*** VariableNotFoundException ==> " ++ e ++ " ***";;
showException (MethodNotFoundException e) = "*** MethodNotFoundException ==> " ++ e ++ " ***";;
showException (FieldNotFoundException e) = "*** FieldNotFoundException ==> " ++ e ++ " ***";;
showException (ClassNotFoundException e) = "*** ClassNotFoundException ==> " ++ e ++ " ***";;
showException (DuplicateVariableException e) = "*** DuplicateVariableException ==> " ++ e ++ " ***";;
showException (DuplicateClassException e) = "*** DuplicateClassException ==> " ++ e ++ " ***";;
showException (DuplicateMethodException e) = "*** DuplicateMethodException ==> " ++ e ++ " ***";;
showException (DuplicateFieldException e) = "*** DuplicateFieldException ==> " ++ e ++ " ***";;
showException (MismatchParamsException e) = "*** MismatchParamsException ==> " ++ e ++ " ***";;
showException (TypeException e) = "*** TypeException ==> " ++ e ++ " ***";;
showException (OverrideException e) = "*** OverrideException ==> " ++ e ++ " ***";;
showException (Exception e) = "*** Exception ==> " ++ e ++ " ***";;
instance Show Exception where show = showException;;
--runtime exceptions
data RuntimeException =
ClassCastException String
| RuntimeException String
;;
-- to print better exceptions at runtime
showRuntimeException (ClassCastException e) = "*** ClassCastException ==> " ++ e ++ " ***";;
showRuntimeException (RuntimeException e) = "*** RuntimeException ==> " ++ e ++ " ***";;
instance Show RuntimeException where show = showRuntimeException;;