Class: Kolom::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/kolom.rb

Overview

The Interpreter class is responsible for executing the translated Ruby code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterpreter

Returns a new instance of Interpreter.



27
28
29
30
31
# File 'lib/kolom.rb', line 27

def initialize
  @env = {}
  @output = StringIO.new
  # setup_global_environment
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



25
26
27
# File 'lib/kolom.rb', line 25

def env
  @env
end

#outputObject (readonly)

Returns the value of attribute output.



25
26
27
# File 'lib/kolom.rb', line 25

def output
  @output
end

Instance Method Details

#evaluate(code) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kolom.rb', line 33

def evaluate(code)
  ruby_code = translate_to_ruby(code)

  # Create an instance of DSLContext
  dsl_context = Kolom::MetaProgramming::DSLContext.new

  # Evaluate the translated code in the DSL context
  result = dsl_context.instance_eval(ruby_code)

  # Return the result and any output
  [result, @output.string]
end

#translate_to_ruby(code) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kolom.rb', line 46

def translate_to_ruby(code)
  # This is a simple token-based translation
  # In a real implementation, a proper parser might be used

  # Replace Bengali keywords with Ruby keywords
  KEYWORDS.each do |bengali, ruby|
    # We need to be careful with word boundaries
    code = code.gsub(/(?<![[:alpha:]])#{Regexp.escape(bengali)}(?![[:alpha:]])/, ruby)
  end

  code
end