Sha256: 59f923902300dc3b0a7dee578b92f6dfbd0f957404a362a751ac53573311be15
Contents?: true
Size: 1.23 KB
Versions: 26
Compression:
Stored size: 1.23 KB
Contents
# frozen_string_literal: true class Code class Node class While < Node WHILE_KEYWORD = "while" UNTIL_KEYWORD = "until" LOOP_KEYWORD = "loop" def initialize(parsed) return if parsed.blank? @operator = parsed.delete(:operator).presence @statement = Statement.new(parsed.delete(:statement)) if parsed.key?( :statement ) @body = Code.new(parsed.delete(:body).presence) end def evaluate(**args) case @operator when WHILE_KEYWORD last = Object::Nothing.new while (@statement&.evaluate(**args) || Object::Nothing.new).truthy? last = @body&.evaluate(**args) || Object::Nothing.new end last when UNTIL_KEYWORD last = Object::Nothing.new while (@statement&.evaluate(**args) || Object::Nothing.new).falsy? last = @body&.evaluate(**args) || Object::Nothing.new end last when LOOP_KEYWORD loop { @body&.evaluate(**args) || Object::Nothing.new } Object::Nothing.new else Object::Nothing.new end rescue Error::Break => e e.value || Object::Nothing.new end end end end
Version data entries
26 entries across 26 versions & 1 rubygems