Class: QED::Evaluator

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

Overview

Demonstrandum Evaluator

Instance Method Summary collapse

Constructor Details

#initialize(script, *observers) ⇒ Evaluator

Returns a new instance of Evaluator.



9
10
11
12
13
14
15
16
17
18
# File 'lib/qed/evaluator.rb', line 9

def initialize(script, *observers)
  @script  = script
  @file    = script.file
  @ast     = script.parse
  @scope   = script.scope
  @binding = script.binding
  @advice  = script.advice

  @observers = observers
end

Instance Method Details

#advise!(signal, *args) ⇒ Object



105
106
107
108
109
# File 'lib/qed/evaluator.rb', line 105

def advise!(signal, *args)
  @observers.each{ |o| o.update(signal, *args) }
  #@scope.__advice__.call(signal, *args)
  @advice.call(@scope, signal, *args)
end

#error!(section, exception) ⇒ Object



92
93
94
95
# File 'lib/qed/evaluator.rb', line 92

def error!(section, exception)
  advise!(:error, section, exception)
  #raise exception
end

#evaluate_code(section) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/qed/evaluator.rb', line 40

def evaluate_code(section)
  advise!(:before_code, section, @file)
  begin
    advise!(:code, section)
    eval(section.text, @binding, @file, section.line)
    #@scope.module_eval(section.text, @file, section.line)
    pass!(section)
  rescue Assertion => exception
    fail!(section, exception)
  rescue Exception => exception
    error!(section, exception)
  end
  advise!(:after_code, section, @file)
end


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/qed/evaluator.rb', line 63

def evaluate_links(section)
  section.text.scan(/\[qed:\/\/(.*?)\]/) do |match|
    file = $1
    # relative to demo script
    if File.exist?(File.join(@script.directory,file))
      file = File.join(@script.directory,file)
    end
    # ruby or another demo
    case File.extname(file)
    when '.rb'
      import!(file)
    else
      Script.new(@script.applique, file, @script.scope).run
    end
  end
end

#evaluate_text(section) ⇒ Object



56
57
58
59
60
# File 'lib/qed/evaluator.rb', line 56

def evaluate_text(section)
  advise!(:text, section)
  evaluate_links(section)
  advise!(:when, section)
end

#fail!(section, exception) ⇒ Object



86
87
88
89
# File 'lib/qed/evaluator.rb', line 86

def fail!(section, exception)
  advise!(:fail, section, exception)
  #raise exception
end

#import!(file) ⇒ Object



98
99
100
101
102
# File 'lib/qed/evaluator.rb', line 98

def import!(file)
  advise!(:unload)
  eval(File.read(file), @binding, file)
  advise!(:load, file)
end

#pass!(section) ⇒ Object



81
82
83
# File 'lib/qed/evaluator.rb', line 81

def pass!(section)
  advise!(:pass, section)
end

#processObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/qed/evaluator.rb', line 28

def process
  @ast.each do |section|
    case section.type
    when :code
      evaluate_code(section)
    when :text
      evaluate_text(section)
    end
  end
end

#runObject



21
22
23
24
25
# File 'lib/qed/evaluator.rb', line 21

def run
  advise!(:before_document, @script)
  process
  advise!(:after_document, @script)
end