class Preprocessor

Public Class Methods

new(input, output, filename) click to toggle source
# File rice/rubypp.rb, line 2
def initialize(input, output, filename)
  @input = input
  @output = output
  @filename = filename
  @linenum = 1
end

Public Instance Methods

evaluate(str, linenum) click to toggle source
# File rice/rubypp.rb, line 61
def evaluate(str, linenum)
  result = eval(str, TOPLEVEL_BINDING, @filename, linenum)
  success = true
  return result
end
getline() click to toggle source
# File rice/rubypp.rb, line 9
def getline
  line = @input.gets
  @linenum += 1 if not line.nil?
  return line
end
preprocess() click to toggle source
# File rice/rubypp.rb, line 15
def preprocess
  success = false
  begin
    loop do
      line = getline
      break if line.nil?
      case line
      when /(.*[^\]|^)\#\{(.*?)\}(.*)/
        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)
        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)
        puts result if not result.nil?
      else
        puts line
      end
    end
    success = true
  ensure
    if not success then
      $stderr.puts "Error on line #{@linenum}:"
    end
  end
end
puts(line='') click to toggle source
# File rice/rubypp.rb, line 67
def puts(line='')
  @output.puts(line)
end