Class: RubyPP

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

Defined Under Namespace

Classes: Environment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output, filename) ⇒ RubyPP

Returns a new instance of RubyPP.



20
21
22
23
24
25
26
27
# File 'lib/rubypp.rb', line 20

def initialize(input, output, filename)
  @input = input
  @output = output
  @filename = filename
  @linenum = 1
  @environment = Environment.new(self)
  @binding = @environment.binding
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



18
19
20
# File 'lib/rubypp.rb', line 18

def filename
  @filename
end

#inputObject (readonly)

Returns the value of attribute input.



16
17
18
# File 'lib/rubypp.rb', line 16

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



17
18
19
# File 'lib/rubypp.rb', line 17

def output
  @output
end

Instance Method Details

#evaluate(str, linenum) ⇒ Object



81
82
83
84
# File 'lib/rubypp.rb', line 81

def evaluate(str, linenum)
  result = eval(str, @binding, @filename, linenum)
  return result
end

#getlineObject



29
30
31
32
33
# File 'lib/rubypp.rb', line 29

def getline
  line = @input.gets
  @linenum += 1 if not line.nil?
  return line
end

#preprocessObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/rubypp.rb', line 35

def preprocess
  success = false
  begin
    loop do
      line = getline
      break if line.nil?
      case line
      when /(.*[^\\]|^)\#\{(.*?)\}(.*)/
        @output.puts "#{$1}#{evaluate($2, @linenum)}#{$3}"
      when /^\#ruby\s+<<(.*)/
        marker = $1
        str = ''
        evalstart = @linenum
        loop do
          line = getline
          if line.nil? then
            raise "End of input without #{marker}"
          end
          break if line.chomp == marker
          str << line
        end
        result = evaluate(str, evalstart)
        @output.puts result if not result.nil?
      when /^\#ruby\s+(.*)/
        str = line = $1
        while line[-1] == ?\\
          str.chop!
          line = getline
          break if line.nil?
          line.chomp!
          str << line
        end
        result = evaluate(str, @linenum)
        @output.puts result if not result.nil?
      else
        @output.puts line
      end
    end
    success = true
  ensure
    if not success then
      $stderr.puts "Error on line #{@linenum}:"
    end
  end
end