-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.jl
209 lines (166 loc) · 3.64 KB
/
types.jl
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
abstract type LoxExpr end
abstract type Stmt end
## TODO: Add Expr or Stmt to end of all types, for clarity
## expression types ##
mutable struct Assign <: LoxExpr
name::Token
value::LoxExpr
end
mutable struct Binary <: LoxExpr
left::LoxExpr
operator::Token
right::LoxExpr
end
mutable struct Call <: LoxExpr
callee::LoxExpr
paren::Token
arguments::Vector{LoxExpr}
end
mutable struct FnExpr <: LoxExpr
name::Token
params::Vector{Token}
body::Vector{Stmt}
end
# GetExpr gets a field from an instance of a class
mutable struct GetExpr <: LoxExpr
object::LoxExpr
name::Token
end
# SetExpr sets a field on an instance of a class
mutable struct SetExpr <: LoxExpr
object::LoxExpr
name::Token
value::LoxExpr
end
mutable struct Grouping <: LoxExpr
expression::LoxExpr
end
mutable struct Literal <: LoxExpr
value::Any
end
mutable struct ThisExpr <: LoxExpr
keyword::Token
end
mutable struct Unary <: LoxExpr
operator::Token
right::LoxExpr
end
mutable struct Logical <: LoxExpr
left::LoxExpr
operator::Token
right::LoxExpr
end
mutable struct Variable <: LoxExpr
name::Token
end
mutable struct Block <: LoxExpr
statements::Vector{Stmt}
end
mutable struct SuperExpr <: LoxExpr
keyword::Token
method::Token
end
## statement types ##
mutable struct BlockStmt <: Stmt
statements::Vector{Stmt}
end
mutable struct ExpressionStmt <: Stmt
expression::LoxExpr
end
mutable struct FnStmt <: Stmt
name::Token
params::Vector{Token}
body::Vector{Stmt}
end
mutable struct ClassStmt <: Stmt
name::Token
superclass::Union{Variable,Nothing}
methods::Vector{FnStmt}
end
mutable struct IfStmt <: Stmt
condition::LoxExpr
thenBranch::Stmt
elseBranch::Union{Stmt,Nothing}
end
mutable struct PrintStmt <: Stmt
expression::LoxExpr
end
mutable struct ReturnStmt <: Stmt
keyword::Token
value::Union{LoxExpr,Nothing}
end
mutable struct VarStmt <: Stmt
name::Token
initializer::Union{LoxExpr,Nothing}
end
mutable struct WhileStmt <: Stmt
condition::LoxExpr
body::Stmt
end
## Other
struct RuntimeError <: Exception
token::Token
details::String
end
struct Return <: Exception
value::Any
end
struct NativeFunction
arity::Int
callee::Function
end
struct LoxFunction
declaration::FnStmt
closure::Environment
isInitializer::Bool
end
mutable struct LoxClass
name::String
superclass::Union{LoxClass,Nothing}
methods::Dict{String,LoxFunction}
end
mutable struct LoxInstance
klass::LoxClass
fields::Dict{String,Any}
end
function Base.get(instance::LoxInstance, name::Token)
key = name.lexeme
if haskey(instance.fields, key)
return instance.fields[key]
end
method = findMethod(instance.klass, key)
if method !== nothing
boundMethod = bind(method, instance)
return boundMethod
end
throw(RuntimeError(name, "Undefined property '$key'."))
end
function findMethod(klass::LoxClass, name::String)
m = get(klass.methods, name, nothing)
if m !== nothing
return m
end
if klass.superclass !== nothing
return findMethod(klass.superclass, name)
end
return nothing
end
function findMethod(klass::LoxClass, name::Token)
return findMethod(klass, name.lexeme)
end
function bind(f::LoxFunction, instance::LoxInstance)
env = Environment(f.closure)
defineenv(env, "this", instance)
return LoxFunction(f.declaration, env, f.isInitializer)
end
@enum FunctionType begin
NONE
FUNCTION
INITIALIZER
METHOD
end
@enum ClassType begin
CLASSTYPE_NONE
CLASSTYPE_CLASS
CLASSTYPE_SUBCLASS
end