Sha256: 7572cab3097b18bea0157451ff2d90a7bfc4cc027aa7c5a5cd6c900ff428f74b

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

require File.join(File.dirname(__FILE__),"..","..","lib","babel_bridge")

class TuringParser < BabelBridge::Parser
  ignore_whitespace

  def store
    @store||=[]
  end

  rule :statement, "if", :statement, "then", :statement, :else_clause?, "end" do
    def evaluate
      if statement[0].evaluate
        statement[1].evaluate
      else 
        else_clause.evaluate if else_clause
      end
    end
  end
  rule :else_clause, "else", :statement

  binary_operators_rule :statement, :operand, [[:/, :*], [:+, :-], [:<, :<=, :>, :>=, :==]] do
    def evaluate
      case operator
      when :<, :<=, :>, :>=, :==
        (left.evaluate.send operator, right.evaluate) ? 1 : nil
      else
        left.evaluate.send operator, right.evaluate
      end
    end
  end

  rule :operand, "(", :statement, ")"
  
  rule :operand, "[", :statement, "]", "=", :statement do
    def evaluate
      parser.store[statement[0].evaluate] = statement[1].evaluate
    end
  end

  rule :operand, "[", :statement, "]" do
    def evaluate
      parser.store[statement.evaluate]
    end
  end

  rule :operand, /[-]?[0-9]+/ do
    def evaluate
      to_s.to_i
    end
  end
end

BabelBridge::Shell.new(TuringParser.new).start

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
babel_bridge-0.5.5 examples/turing/turing10.rb
babel_bridge-0.5.4 examples/turing/turing10.rb
babel_bridge-0.5.3 examples/turing/turing10.rb
babel_bridge-0.5.1 examples/turing/turing10.rb
babel_bridge-0.5.0 examples/turing/turing10.rb
babel_bridge-0.4.1 examples/turing/turing10.rb
babel_bridge-0.4.0 examples/turing/turing10.rb
babel_bridge-0.3.1 examples/turing/turing10.rb