Module: Jtor::Expressions

Included in:
JavaParser
Defined in:
lib/jtor/expressions.rb

Constant Summary collapse

ASSINGMENT_OPERATORS =
{
  assign: '=',
  plus: '+=',
  minus: '-=',
  star: '*=',
  slash: '/=',
  and: '&=',
  or: '|=',
  xor: '^=',
  rem: '%=',
  lShift: '<<=',
  rSignedShift: '>>=',
  rUnsignedShift: '>>>='
}
UNARY_OPERATORS =
{
  positive: '+',
  negative: '-',
  preIncrement: '++',
  preDecrement: '--',
  not: '!',
  inverse: '~',
  posIncrement: '++',
  posDecrement: '--'
}
BINARY_OPERATORS =
{
  or: '||',
  and: '&&',
  binOr: '|',
  binAnd: '&',
  xor: '^',
  equals: '==',
  notEquals: '!=',
  less: '<',
  greater: '>',
  lessEquals: '<=',
  greaterEquals: '>=',
  lShift: '<<',
  rSignedShift: '>>',
  rUnsignedShift: '>>>',
  plus: '+',
  minus: '-',
  times: '*',
  divide: '/',
  remainder: '%'
}

Instance Method Summary collapse

Instance Method Details

#translate_arguments(args) ⇒ Object



160
161
162
163
# File 'lib/jtor/expressions.rb', line 160

def translate_arguments(args)
  return '' unless args && args.any?
  "(#{args.map { |arg| translate_expression(arg) }.join(', ')})"
end

#translate_constructor_call_args(params, args) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jtor/expressions.rb', line 165

def translate_constructor_call_args(params, args)
  # These arguments will be `eval`ed in Jtor::StdLib::Base. There the
  # arguments are in an array called `args`. We'll translate any reference
  # to a name defined in `params` to `args[i]`, where `i` is the index of
  # that parameter in `params`
  arguments = translate_arguments(args)
  params.each_with_index do |param, index|
    replacement = "\\1args[#{index}]\\2"
    # Only replace unscoped names matching the param name
    arguments.gsub!(/([ \(])#{param.id.name}([., \)])/, replacement)
  end
  arguments
end

#translate_expression(expr) ⇒ Object



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
# File 'lib/jtor/expressions.rb', line 54

def translate_expression(expr)
  case expr
  when MethodCallExpr
    scoped_expression(expr) do
      "#{expr.name}#{translate_arguments(expr.args)}"
    end
  when AssignExpr
    target = translate_expression(expr.target)
    operator = ASSINGMENT_OPERATORS[expr.operator.to_s.to_sym]
    value = translate_expression(expr.value)
    "#{target} #{operator} #{value}"
  when FieldAccessExpr
    scoped_expression(expr) { expr.field }
  when VariableDeclarationExpr
    expr.vars.map do |var|
      # If only declaring and not assigning, we don't need to translate it
      # (I think)
      next unless var.init
      "#{var.id} = #{translate_expression(var.init)}"
    end.compact.join('; ')
  when QualifiedNameExpr
    "#{translate_expression(expr.qualifier)}.#{expr.name}"
  when NameExpr
    expr.name
  when ArrayAccessExpr
    "#{translate_expression(expr.name)}[#{translate_expression(expr.index)}]"
  when ArrayCreationExpr
    if expr.initializer
      translate_expression(expr.initializer)
    else
      "Array.new(#{expr.array_count})"
    end
  when ArrayInitializerExpr
    values = expr.values.map { |value| translate_expression(value) }
    "[#{values.join(', ')}]"
  when UnaryExpr
    op = expr.operator.to_s.to_sym
    operator = UNARY_OPERATORS[op]
    value = translate_expression(expr.expr)
    case op
    when :posIncrement
      # For preIncrement and preDecrement we record the old value first,
      # then execute the operation and then return the old value
      "(__old = #{value}; #{value} += 1; __old)"
    when :posDecrement
      "(__old = #{value}; #{value} -= 1; __old)"
    when :preIncrement
      # No ++/-- in ruby
      "(#{value} += 1)"
    when :preDecrement
      "(#{value} -= 1)"
    else
      "#{operator}#{value}"
    end
  when BinaryExpr
    left = translate_expression(expr.left)
    operator = BINARY_OPERATORS[expr.operator.to_s.to_sym]
    right = translate_expression(expr.right)
    "#{left} #{operator} #{right}"
  when IntegerLiteralExpr
    # NOTE: `IntegerLiteralExpr` is a subclass of `StringLiteralExpr`, so
    # this case must be before the latter
    expr.value
  when StringLiteralExpr
    expr.value.inspect
  when BooleanLiteralExpr
    expr.value
  when ObjectCreationExpr
    # TODO: Handle object creations with anonymous classes
    scoped_expression(expr) do
      type = translate_type(expr.type)
      "#{type}.new#{translate_arguments(expr.args)}"
    end
  when InstanceOfExpr
    # No `instanceof` operator in ruby, but every expression is an object
    # with the `is_a?` method, which serves the same purpose. We wrap the
    # expression in parenthesis just in case it's not atomic.
    "(#{translate_expression(expr.expr)}).is_a?(#{translate_type(expr.type)})"
  when NullLiteralExpr
    'nil'
  when CastExpr
    # No need to cast anything :P
    translate_expression(expr.expr)
  when ThisExpr
    # TODO: Handle class expressions on `this`
    'self'
  when SuperExpr
    # Return our trusty `sup` helper object which encapsulates super methods
    # TODO: Handle class expressions on `super`
    'sup'
  when ClassExpr
    "#{translate_type(expr.type)}.class"
  when EnclosedExpr
    "(#{translate_expression(expr.inner)})"
  when ConditionalExpr
    condition = translate_expression(expr.condition)
    then_expr = translate_expression(expr.then_expr)
    else_expr = translate_expression(expr.else_expr)
    "#{condition} ? #{then_expr} : #{else_expr}"
  end
end

#translate_expressions(expressions) ⇒ Object



156
157
158
# File 'lib/jtor/expressions.rb', line 156

def translate_expressions(expressions)
  expressions.map { |expr| translate_expression(expr) }.join('; ')
end