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.



43
44
45
46
47
48
# File 'lib/milk_tea/core/compile_time.rb', line 43

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



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

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



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

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/milk_tea/core/compile_time.rb', line 122

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/milk_tea/core/compile_time.rb', line 101

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)


204
205
206
# File 'lib/milk_tea/core/compile_time.rb', line 204

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