Module: RLSL::Prism::Builtins::OperatorRules

Defined in:
lib/rlsl/prism/builtins/operator_rules.rb

Constant Summary collapse

BINARY_OPERATORS =
{
  "+" => :arithmetic,
  "-" => :arithmetic,
  "*" => :arithmetic,
  "/" => :arithmetic,
  "%" => :arithmetic,

  "==" => :comparison,
  "!=" => :comparison,
  "<" => :comparison,
  ">" => :comparison,
  "<=" => :comparison,
  ">=" => :comparison,

  "&&" => :logical,
  "||" => :logical
}.freeze
UNARY_OPERATORS =
{
  "-" => :negate,
  "!" => :not
}.freeze

Class Method Summary collapse

Class Method Details

.binary_op_result_type(op, left_type, right_type) ⇒ Object



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
# File 'lib/rlsl/prism/builtins/operator_rules.rb', line 40

def binary_op_result_type(op, left_type, right_type)
  op_kind = BINARY_OPERATORS[op.to_s]

  case op_kind
  when :comparison, :logical
    :bool
  when :arithmetic
    if matrix_type?(left_type) && vector_type?(right_type)
      matrix_vector_result(left_type)
    elsif vector_type?(left_type) && matrix_type?(right_type)
      matrix_vector_result(right_type)
    elsif matrix_type?(left_type) && matrix_type?(right_type)
      left_type
    elsif matrix_type?(left_type) && scalar_type?(right_type)
      left_type
    elsif scalar_type?(left_type) && matrix_type?(right_type)
      right_type
    elsif vector_type?(left_type) && vector_type?(right_type)
      left_type
    elsif vector_type?(left_type) && scalar_type?(right_type)
      left_type
    elsif scalar_type?(left_type) && vector_type?(right_type)
      right_type
    else
      scalar_arithmetic_result_type(op, left_type, right_type)
    end
  end
end

.binary_operator?(op) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rlsl/prism/builtins/operator_rules.rb', line 32

def binary_operator?(op)
  BINARY_OPERATORS.key?(op.to_s)
end

.matrix_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/rlsl/prism/builtins/operator_rules.rb', line 73

def matrix_type?(type)
  i[mat2 mat3 mat4].include?(type)
end

.matrix_vector_result(matrix_type) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/rlsl/prism/builtins/operator_rules.rb', line 89

def matrix_vector_result(matrix_type)
  case matrix_type
  when :mat2 then :vec2
  when :mat3 then :vec3
  when :mat4 then :vec4
  end
end

.scalar_arithmetic_result_type(op, left_type, right_type) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/rlsl/prism/builtins/operator_rules.rb', line 81

def scalar_arithmetic_result_type(op, left_type, right_type)
  return :float unless scalar_type?(left_type) && scalar_type?(right_type)
  return :float if op.to_s == "/"
  return :int if left_type == :int && right_type == :int

  :float
end

.scalar_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rlsl/prism/builtins/operator_rules.rb', line 77

def scalar_type?(type)
  i[float int].include?(type)
end

.unary_operator?(op) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rlsl/prism/builtins/operator_rules.rb', line 36

def unary_operator?(op)
  UNARY_OPERATORS.key?(op.to_s)
end

.vector_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/rlsl/prism/builtins/operator_rules.rb', line 69

def vector_type?(type)
  i[vec2 vec3 vec4].include?(type)
end