-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMiniRubyAST.hs
97 lines (83 loc) · 2.87 KB
/
MiniRubyAST.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
94
95
96
97
module MiniRubyAST
( ObjectReference
, Value (..)
, Name
, Expr (..)
, Exprs
, Cases
, Case
, Pattern (..)
, ClassDecl (..)
, ConstructorDecl
, NamedMethodDecl (..)
, ReceiveDecl (..)
, MethodDecl (..)
, Prog
)
where
type Name = String
-- | An object reference is an integer uniquely identifying an object.
-- This does not appear in the grammar, but is used in the runtime
-- representation in the interpreter.
type ObjectReference = Int
-- | A value is either a term, an integer, or a string. Expressions
-- are evaluated to values and methods return values.
data Value = IntValue Integer
| StringValue String
| SymbolValue String
| BooleanValue Bool
| ReferenceValue ObjectReference
deriving (Eq, Show)
-- | An expression.
data Expr = IntConst Integer
| StringConst String
| SymbolConst String
| BooleanConst Bool
| Self
| Plus Expr Expr
| Minus Expr Expr
| Times Expr Expr
| LessThan Expr Expr
| EqualTo Expr Expr
| GreaterThan Expr Expr
| DividedBy Expr Expr
| Return Expr
| SetField Name Expr
| SetVar Name Expr
| ReadVar Name
| ReadField Name
| Match Expr Cases
| CallMethod --
Expr -- ^ Receiver --
Name -- ^ Method name --
[Expr] -- ^ Method arguments --
| New Name [Expr] --
deriving (Eq, Show)
type Exprs = [Expr]
type Cases = [Case]
type Case = (Pattern,Exprs)
data Pattern = ConstInt Integer
| ConstString String
| AnyValue Name
deriving (Eq, Show)
data ClassDecl = ClassDecl { className :: Name
, classConstructor :: Maybe ConstructorDecl
, classMethods :: [NamedMethodDecl]
, classReceive :: Maybe ReceiveDecl
}
deriving (Eq, Show)
type ConstructorDecl = MethodDecl
data ReceiveDecl = ReceiveDecl { receiveParameters :: [Name]
, receiveBody :: Exprs
}
deriving (Eq, Show)
data NamedMethodDecl = NamedMethodDecl Name MethodDecl
deriving (Eq, Show)
data MethodDecl = MethodDecl { methodParameters :: [Name]
, methodBody :: Exprs
}
deriving (Eq, Show)
-- | A program is just a list of class declarations. The program
-- is executed by creating an instance of a class named Main. If
-- there is no such class defined, interpretation must fail.
type Prog = [ClassDecl]