Sha256: 1c381beb8ca6ce20cb658f8b476ca5ed59bb0ff2318982f83057d7bf6512a8ae

Contents?: true

Size: 986 Bytes

Versions: 12

Compression:

Stored size: 986 Bytes

Contents

class Code
  class Node
    class While < Node
      WHILE_KEYWORD = :while
      UNTIL_KEYWORD = :until

      def initialize(while_parsed)
        @operator = while_parsed.fetch(:operator)
        @statement = ::Code::Node::Statement.new(while_parsed.fetch(:statement))
        @body = ::Code::Node::Code.new(while_parsed.fetch(:body))
      end

      def evaluate(**args)
        if operator == WHILE_KEYWORD
          object = ::Code::Object::Nothing.new

          while @statement.evaluate(**args).truthy?
            object = @body.evaluate(**args)
          end

          object
        elsif operator == UNTIL_KEYWORD
          object = ::Code::Object::Nothing.new

          until @statement.evaluate(**args).truthy?
            object = @body.evaluate(**args)
          end

          object
        else
          raise NotImplementedError.new(operator.inspect)
        end
      end

      private

      def operator
        @operator.to_sym
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
template-ruby-0.4.0 lib/code/node/while.rb
code-ruby-0.4.0 lib/code/node/while.rb
template-ruby-0.3.1 lib/code/node/while.rb
code-ruby-0.3.1 lib/code/node/while.rb
template-ruby-0.3.0 lib/code/node/while.rb
code-ruby-0.3.0 lib/code/node/while.rb
code-ruby-0.2.4 lib/code/node/while.rb
template-ruby-0.2.4 lib/code/node/while.rb
template-ruby-0.2.3 lib/code/node/while.rb
template-ruby-0.2.2 lib/code/node/while.rb
template-ruby-0.2.1 lib/code/node/while.rb
template-ruby-0.2.0 lib/code/node/while.rb