Class: Kolom::Interpreter
- Inherits:
-
Object
- Object
- Kolom::Interpreter
- Defined in:
- lib/kolom.rb
Overview
The Interpreter class is responsible for executing the translated Ruby code.
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #evaluate(code) ⇒ Object
-
#initialize ⇒ Interpreter
constructor
A new instance of Interpreter.
- #translate_to_ruby(code) ⇒ Object
Constructor Details
#initialize ⇒ Interpreter
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
#env ⇒ Object (readonly)
Returns the value of attribute env.
25 26 27 |
# File 'lib/kolom.rb', line 25 def env @env end |
#output ⇒ Object (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 |