Class: WLang::HostedLanguage::DSL

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

Overview

The hosted language DSL, interpreting expressions

Constant Summary

KEPT_METHODS =

Methods that we keep

[ "__send__", "__id__", "instance_eval", "initialize", "object_id", 
"singleton_method_added", "singleton_method_undefined", "method_missing",
"__evaluate__", "knows?"]

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DSL) initialize(hosted, parser_state)

Creates a DSL instance for a given hosted language and parser state



66
67
68
69
70
71
# File 'lib/wlang/hosted_language.rb', line 66

def initialize(hosted, parser_state)
  @hosted, @parser_state = hosted, parser_state
  class << self
    __clean_scope__
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(name, *args, &block)

Delegates the missing lookup to the current parser scope or raises an UndefinedVariableError (calls @hosted.variable_missing precisely)



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wlang/hosted_language.rb', line 75

def method_missing(name, *args, &block)
  if @parser_state and args.empty? and block.nil?
    if effname = knows?(name)
      @parser_state.scope[effname]
    else
      @hosted.variable_missing(name)
    end
  else
    super(name, *args, &block)
  end
end

Class Method Details

+ (Object) __clean_scope__



55
56
57
58
59
60
61
# File 'lib/wlang/hosted_language.rb', line 55

def __clean_scope__
  # Removes all methods that are not needed to the class
  (instance_methods + private_instance_methods).each do |m|
    m_to_s = m.to_s
    undef_method(m_to_s.to_sym) unless ('__' == m_to_s[0..1]) or KEPT_METHODS.include?(m_to_s)
  end
end

Instance Method Details

- (Object) __evaluate__(__expression__)

Evaluates an expression



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wlang/hosted_language.rb', line 99

def __evaluate__(__expression__)
  __result__ = instance_eval(__expression__)
  
  # backward compatibility with >= 0.8.4 where 'using self'
  # was allowed. This will be removed in wlang 1.0.0
  if __result__.object_id == self.object_id
    Kernel.puts "Warning: using deprecated 'using self' syntax (#{@parser_state.where})\n"\
                "will be removed in wlang 1.0.0. Use 'share all', extends "\
                "::WLang::HostedLanguage::DSL with useful methods or create your own"\
                " hosted language."
    __result__ = @parser_state.scope.to_h
  end
  
  __result__
rescue ::WLang::Error => ex
  ex.parser_state = @parser_state
  ex.expression = __expression__ if ex.respond_to?(:expression=)
  Kernel.raise ex
rescue Exception => ex
  Kernel.raise ::WLang::EvalError.new(ex.message, @parser_state, __expression__, ex)
end

- (Boolean) knows?(name)

Checks if a variable is known in the current parser scope

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/wlang/hosted_language.rb', line 88

def knows?(name)
  if @parser_state.scope.has_key?(name)
    name
  elsif @parser_state.scope.has_key?(name.to_s) 
    name.to_s
  else
    nil
  end
end