Class: Klue::Langcraft::DSL::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/klue/langcraft/dsl/interpreter.rb

Overview

Interpreter class for processing and interpreting DSL input

This class is responsible for handling method calls, processing arguments, and managing the overall interpretation of the DSL input. It also provides methods for processing input and output, as well as converting the data to hash and JSON formats.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterpreter

Returns a new instance of Interpreter.



19
20
21
# File 'lib/klue/langcraft/dsl/interpreter.rb', line 19

def initialize
  klue_reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/klue/langcraft/dsl/interpreter.rb', line 35

def method_missing(method_name, *args, &block)
  raise "You must call 'process' before using other methods" unless @processed

  key = method_name
  value = klue_process_args(args, block)

  if @data[key]
    @data[key] = [@data[key]] unless @data[key].is_a?(Array)
    @data[key] << value
  else
    @data[key] = value
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/klue/langcraft/dsl/interpreter.rb', line 16

def data
  @data
end

#processedObject

Returns the value of attribute processed.



17
18
19
# File 'lib/klue/langcraft/dsl/interpreter.rb', line 17

def processed
  @processed
end

Instance Method Details

#klue_process_args(args, block) ⇒ Object



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
# File 'lib/klue/langcraft/dsl/interpreter.rb', line 53

def klue_process_args(args, block)
  positional_args = []
  named_args = {}

  # Handling positional and named parameters separately
  args.each do |arg|
    if arg.is_a?(Hash)
      named_args.merge!(arg)
    else
      positional_args << arg
    end
  end

  # Assign positional parameters generically
  data = positional_args.each_with_index.to_h { |arg, index| [:"p#{index + 1}", arg] }

  # Merge named parameters after positional ones
  data.merge!(named_args)

  # Handling a nested block
  if block
    interpreter = Interpreter.new
    interpreter.instance_variable_set(:@processed, true) # Set @processed to true for nested interpreter
    interpreter.instance_eval(&block)
    data.merge!(interpreter.data)
  end

  data.empty? ? nil : data
end

#process(input: nil, input_file: nil, output_file: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/klue/langcraft/dsl/interpreter.rb', line 23

def process(input: nil, input_file: nil, output_file: nil)
  klue_reset
  klue_validate_input_arguments(input, input_file)
  klue_input_content = klue_input_content(input, input_file)

  @processed = true
  instance_eval(klue_input_content)

  klue_write_output(output_file) if output_file
  data
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/klue/langcraft/dsl/interpreter.rb', line 49

def respond_to_missing?(method_name, include_private = false)
  @processed || super
end