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.



68
69
70
71
72
73
# File 'lib/milk_tea/core/compile_time.rb', line 68

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



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

def evaluate(expression)
  case expression
  when AST::ErrorExpr
    nil
  when AST::ExpressionList
    expression.elements.filter_map { |element| evaluate(element) }
  when AST::IntegerLiteral, AST::FloatLiteral, AST::BooleanLiteral
    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)
  else
    nil
  end
end

#evaluate_binary(expression) ⇒ Object



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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/milk_tea/core/compile_time.rb', line 162

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 "=="
    CompileTime.equality_result(left, right)
  when "!="
    result = CompileTime.equality_result(left, right)
    result.nil? ? nil : !result
  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 "/"
    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_unary(expression) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/milk_tea/core/compile_time.rb', line 147

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

#resolve_layout_type(type_ref) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/milk_tea/core/compile_time.rb', line 126

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

#zero_numeric?(value) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/milk_tea/core/compile_time.rb', line 229

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