Module: RBF

Defined in:
lib/rbf/syntax.rb,
lib/rbf.rb,
lib/rbf/jit.rb,
lib/rbf/parser.rb,
lib/rbf/jit/code.rb,
lib/rbf/optimizer.rb,
lib/rbf/interpreter.rb,
lib/rbf/interpreter/storage.rb

Overview

         DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                 Version 2, December 2004

Copyleft meh. [http://meh.paranoid.pk | [email protected]]

         DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

++

Defined Under Namespace

Modules: Syntax Classes: Interpreter, JIT, Optimizer, Parser, Transform

Class Method Summary collapse

Class Method Details

.[](text, syn = nil) ⇒ Object



83
84
85
# File 'lib/rbf.rb', line 83

def self.[] (text, syn=nil)
  evaluate(text, :catch => true, :syntax => syn)
end

.evaluate(text, options = nil) ⇒ Object



73
74
75
76
77
# File 'lib/rbf.rb', line 73

def self.evaluate (text, options=nil)
  options ||= {}

  Interpreter.new(options).evaluate(text)
end

.execute(file, options = {}) ⇒ Object



79
80
81
# File 'lib/rbf.rb', line 79

def self.execute (file, options={})
  evaluate(File.read(file), options)
end

.optimize(text, options = nil) ⇒ Object



67
68
69
70
71
# File 'lib/rbf.rb', line 67

def self.optimize (text, options=nil)
  options ||= {}

  Optimizer.new(options).optimize(text.is_a?(Array) ? text : parse(text.to_s, options[:syntax]))
end

.parse(text, syn = nil) ⇒ Object



62
63
64
65
# File 'lib/rbf.rb', line 62

def self.parse (text, syn=nil)
  Transform.new.apply(Parser.syntax(syntax(syn)).new.parse_with_debug(text)) or
    raise SyntaxError, 'There is a syntax error'
end

.repl(options = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rbf.rb', line 87

def self.repl (options=nil)
  begin
    require 'colorb'
    require 'readline'
  rescue LoadError
    warn 'You need colorb and readline to use the REPL, install them.'

    return false
  end

  interpreter = Interpreter.new(options || {})

  while line = Readline.readline_with_hist_management
    line.rstrip!

    if line.start_with?('!')
      case line[1 .. -1]
        when 'exit', 'quit'
          break

        when 'storage'
          STDOUT.puts interpreter.storage.inspect

        when /^position(?:\s+(\d+))?$/
          if $1
            interpreter.storage.position = $1
          else
            STDOUT.puts interpreter.storage.position.to_s.bold
          end

        when /^get(?:\s+(\d+))?$/
          STDOUT.puts "#{($1 || interpreter.storage.position).to_s.bold}: #{interpreter.storage.get($1)}"

        when /^set(?:\s+(\d+)\s+(\d+))/
          interpreter.storage.set($2.to_i, $1.to_i)

        when 'clear'
          interpreter.storage.clear!

        when 'reset'
          interpreter.storage.reset!

        else
          STDOUT.puts 'Command not found or used improperly'.red
      end

      next
    end

    begin
      interpreter.evaluate(line, interpreter.options.merge(:output => Class.new {
        attr_reader :last

        def print (text)
          STDOUT.print(text)

          @last = text[-1]
        end

        def flush
          STDOUT.sync
        end
      }.new))

      puts if interpreter.output.last && interpreter.output.last != "\n"
    rescue SyntaxError
      next
    rescue
      STDOUT.puts $!.inspect.red, $@.join("\n")
    end
  end
  
  puts "\nExiting REPL."
end

.syntax(name) ⇒ Object



57
58
59
60
# File 'lib/rbf.rb', line 57

def self.syntax (name)
  name.is_a?(Hash) ? name :
    (RBF::Syntax.const_get(name.to_s.capitalize) rescue nil) || Syntax::Default
end