Class: MilkTea::CompileTime::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/core/compile_time.rb

Instance Method Summary collapse

Constructor Details

#initialize(resolve_identifier:, resolve_member_access:, resolve_type_ref: nil, resolve_call: nil) ⇒ Evaluator

Returns a new instance of Evaluator.



84
85
86
87
88
89
# File 'lib/milk_tea/core/compile_time.rb', line 84

def initialize(resolve_identifier:, resolve_member_access:, resolve_type_ref: nil, resolve_call: nil)
  @resolve_identifier = resolve_identifier
  @resolve_member_access = resolve_member_access
  @resolve_type_ref = resolve_type_ref
  @resolve_call = resolve_call
end

Instance Method Details

#evaluate(expression) ⇒ Object



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
# File 'lib/milk_tea/core/compile_time.rb', line 91

def evaluate(expression)
  case expression
  when AST::ErrorExpr
    nil
  when AST::ExpressionList
    expression.elements.filter_map { |element| evaluate(element) }
  when AST::RangeExpr
    start_val = evaluate(expression.start_expr)
    end_val = evaluate(expression.end_expr)
    start_val.is_a?(Integer) && end_val.is_a?(Integer) ? (start_val...end_val).to_a : nil
  when AST::IntegerLiteral, AST::FloatLiteral, AST::BooleanLiteral, AST::CharLiteral
    expression.value
  when AST::StringLiteral
    expression.value
  when AST::Identifier
    @resolve_identifier&.call(expression)
  when AST::MemberAccess
    @resolve_member_access&.call(expression)
  when AST::Call
    @resolve_call&.call(expression)
  when AST::Specialization
    @resolve_call&.call(expression)
  when AST::SizeofExpr
    type = resolve_layout_type(expression.type)
    type && Layout.size_of(type)
  when AST::AlignofExpr
    type = resolve_layout_type(expression.type)
    type && Layout.alignment_of(type)
  when AST::OffsetofExpr
    type = resolve_layout_type(expression.type)
    if type
      result = Layout.offset_of(type, expression.field)
      return result if result

      id_expr = AST::Identifier.new(name: expression.field)
      value = @resolve_identifier&.call(id_expr)
      if value.is_a?(Types::FieldHandle)
        return Layout.offset_of(type, value.field_name)
      end
    end
    nil
  when AST::UnaryOp
    evaluate_unary(expression)
  when AST::BinaryOp
    evaluate_binary(expression)
  when AST::IfExpr
    condition = evaluate(expression.condition)
    return unless CompileTime.boolean_value?(condition)

    evaluate(condition ? expression.then_expression : expression.else_expression)
  when AST::MatchExpr
    evaluate_match_expression(expression)
  when AST::IndexAccess
    evaluate_index_access(expression)
  when AST::PrefixCast
    evaluate_prefix_cast(expression)
  else
    nil
  end
end

#evaluate_binary(expression) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/milk_tea/core/compile_time.rb', line 269

def evaluate_binary(expression)
  left = evaluate(expression.left)

  case expression.operator
  when "and"
    return unless CompileTime.boolean_value?(left)
    return false if left == false

    right = evaluate(expression.right)
    return right if CompileTime.boolean_value?(right)

    return nil
  when "or"
    return unless CompileTime.boolean_value?(left)
    return true if left == true

    right = evaluate(expression.right)
    return right if CompileTime.boolean_value?(right)

    return nil
  end

  right = evaluate(expression.right)

  case expression.operator
  when ".."
    left.is_a?(Integer) && right.is_a?(Integer) ? (left...right).to_a : nil
  when "=="
    CompileTime.equality_result(left, right)
  when "!="
    result = CompileTime.equality_result(left, right)
    result.nil? ? nil : !result
  when "+"
    if left.is_a?(String) && right.is_a?(String)
      left + right
    elsif left.is_a?(Numeric) && right.is_a?(Numeric)
      left + right
    else
      nil
    end
  when "-"
    left.is_a?(Numeric) && right.is_a?(Numeric) ? left - right : nil
  when "*"
    left.is_a?(Numeric) && right.is_a?(Numeric) ? left * right : nil
  when "/"
    return unless left.is_a?(Numeric) && right.is_a?(Numeric)
    raise Error, "division by zero" if zero_numeric?(right)

    left / right
  when "%"
    return unless left.is_a?(Integer) && right.is_a?(Integer)
    raise Error, "modulo by zero" if right.zero?

    left % right
  when "<<"
    left.is_a?(Integer) && right.is_a?(Integer) ? left << right : nil
  when ">>"
    left.is_a?(Integer) && right.is_a?(Integer) ? left >> right : nil
  when "&"
    left.is_a?(Integer) && right.is_a?(Integer) ? left & right : nil
  when "|"
    left.is_a?(Integer) && right.is_a?(Integer) ? left | right : nil
  when "^"
    left.is_a?(Integer) && right.is_a?(Integer) ? left ^ right : nil
  when "<"
    left.is_a?(Numeric) && right.is_a?(Numeric) ? left < right : nil
  when "<="
    left.is_a?(Numeric) && right.is_a?(Numeric) ? left <= right : nil
  when ">"
    left.is_a?(Numeric) && right.is_a?(Numeric) ? left > right : nil
  when ">="
    left.is_a?(Numeric) && right.is_a?(Numeric) ? left >= right : nil
  end
end

#evaluate_index_access(expression) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/milk_tea/core/compile_time.rb', line 197

def evaluate_index_access(expression)
  receiver = evaluate(expression.receiver)
  return nil if receiver.nil?

  if expression.index.is_a?(AST::RangeExpr)
    start_val = evaluate(expression.index.start_expr)
    end_val = evaluate(expression.index.end_expr)
    return nil unless start_val.is_a?(Integer) && end_val.is_a?(Integer)

    return receiver[start_val...end_val] if receiver.is_a?(Array) || receiver.is_a?(String)

    return nil
  end

  index = evaluate(expression.index)
  return nil unless index.is_a?(Integer)
  return receiver[index] if receiver.is_a?(Array)
  return receiver.getbyte(index) if receiver.is_a?(String)

  nil
end

#evaluate_match_expression(expression) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/milk_tea/core/compile_time.rb', line 219

def evaluate_match_expression(expression)
  scrutinee = evaluate(expression.expression)
  return nil unless scrutinee

  expression.arms.each do |arm|
    wildcard = arm.pattern.is_a?(AST::Identifier) && arm.pattern.name == "_"
    if wildcard || CompileTime.equality_result(scrutinee, evaluate(arm.pattern)) == true
      return evaluate(arm.value)
    end
  end

  nil
end

#evaluate_prefix_cast(expression) ⇒ Object



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
# File 'lib/milk_tea/core/compile_time.rb', line 152

def evaluate_prefix_cast(expression)
  operand = evaluate(expression.expression)
  operand = 1 if operand == true
  operand = 0 if operand == false
  return nil unless operand.is_a?(Numeric)

  target_type = begin
    @resolve_type_ref&.call(expression.target_type)
  rescue SemanticError
    nil
  end
  return nil unless target_type.is_a?(Types::Primitive)

  if target_type.boolean?
    return operand != 0
  end

  if target_type.integer?
    return nil unless target_type.integer_width

    value = operand.is_a?(Float) ? operand.truncate : operand
    return nil unless value.is_a?(Integer)

    return wrap_integer(value, target_type.integer_width, target_type.signed_integer?)
  end

  return nil unless target_type.float?

  value = operand.to_f
  target_type.name == "float" ? float32_round(value) : value
end

#evaluate_unary(expression) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/milk_tea/core/compile_time.rb', line 254

def evaluate_unary(expression)
  operand = evaluate(expression.operand)

  case expression.operator
  when "+"
    operand.is_a?(Numeric) ? operand : nil
  when "-"
    operand.is_a?(Numeric) ? -operand : nil
  when "~"
    operand.is_a?(Integer) ? ~operand : nil
  when "not"
    CompileTime.boolean_value?(operand) ? !operand : nil
  end
end

#float32_round(value) ⇒ Object



193
194
195
# File 'lib/milk_tea/core/compile_time.rb', line 193

def float32_round(value)
  [value].pack("f").unpack1("f")
end

#resolve_layout_type(type_ref) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/milk_tea/core/compile_time.rb', line 233

def resolve_layout_type(type_ref)
  result = begin
    @resolve_type_ref&.call(type_ref)
  rescue SemanticError
    nil
  end
  return result if result

  return unless type_ref.respond_to?(:name) && type_ref.name.parts.length >= 1

  expression = ::MilkTea::AST.build_chain_from_parts(type_ref.name.parts)
  return unless expression

  value = evaluate(expression)
  return value if value.is_a?(Types::Struct) || value.is_a?(Types::Primitive) ||
    value.is_a?(Types::Union) || value.is_a?(Types::Nullable) ||
    value.is_a?(Types::StructInstance)

  nil
end

#wrap_integer(value, width, signed) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/milk_tea/core/compile_time.rb', line 184

def wrap_integer(value, width, signed)
  mask = (1 << width) - 1
  wrapped = value & mask
  return wrapped unless signed

  half = 1 << (width - 1)
  wrapped >= half ? wrapped - (1 << width) : wrapped
end

#zero_numeric?(value) ⇒ Boolean

Returns:

  • (Boolean)


344
345
346
# File 'lib/milk_tea/core/compile_time.rb', line 344

def zero_numeric?(value)
  (value.is_a?(Integer) && value.zero?) || (value.is_a?(Float) && value.zero?)
end