lib/grumlin/translator.rb in grumlin-0.1.3 vs lib/grumlin/translator.rb in grumlin-0.2.0
- old
+ new
@@ -1,41 +1,42 @@
# frozen_string_literal: true
module Grumlin
module Translator
class << self
- def to_string_query(steps) # rubocop:disable Metrics/MethodLength
- counter = 0
- string_steps, bindings = steps.each_with_object([[], {}]) do |step, (acc_g, acc_b)|
- args = step.args.map do |arg|
- binding_name(counter).tap do |b|
- acc_b[b] = arg
- counter += 1
- end
- end.join(", ")
+ def to_bytecode(steps)
+ return arg_to_bytecode(steps) if steps.is_a?(AnonymousStep)
- acc_g << "#{step.name}(#{args})"
+ steps.map do |step|
+ arg_to_bytecode(step)
end
-
- ["g.#{string_steps.join(".")}", bindings]
end
def to_bytecode_query(steps)
steps.map do |step|
- [step.name, *step.args.flatten]
+ arg_to_query_bytecode(step)
end
end
- def to_string(steps)
- "g." + steps.map do |step| # rubocop:disable Style/StringConcatenation
- "#{step.name}(#{step.args.map(&:inspect).join(", ")})"
- end.join(".")
+ private
+
+ def arg_to_bytecode(arg)
+ return arg unless arg.is_a?(AnonymousStep)
+
+ args = arg.args.flatten.map do |a|
+ a.instance_of?(AnonymousStep) ? to_bytecode(a.steps) : arg_to_bytecode(a)
+ end
+ [arg.name, *args]
end
- private
+ def arg_to_query_bytecode(arg)
+ return ["none"] if arg.nil?
+ return arg unless arg.is_a?(AnonymousStep)
- def binding_name(num)
- "b_#{num.to_s(16)}"
+ args = arg.args.flatten.map do |a|
+ a.instance_of?(AnonymousStep) ? Typing.to_bytecode(to_bytecode(a.steps)) : arg_to_query_bytecode(a)
+ end
+ [arg.name, *args]
end
end
end
end