Class: Skeem::DoExpression

Inherits:
SkmMultiExpression show all
Defined in:
lib/skeem/s_expr_nodes.rb

Overview

Syntax outline: LPAREN DO LPAREN iteration_spec_star RPAREN LPAREN test do_result RPAREN command_star RPAREN Semantics: Initialization: Set of variables is initialized (each with its init expression) Iteration: test expression is evaluation if false: command expressions are executed in order step expressions are executed and bound to the respective variable iteration resumes if true: the do_result expressions are excuted value of last expression is returned ; (do ((vec (make-vector 5)) ; (i 0 (+ i 1))) ; ((= i 5) vec) ; (vector-set! vec i i)) Can be transformed into: (let* ( ;; Variable init (vec (make-vector 5)) (i 0) ;; Utility procedures (update-step (lambda () (begin (set! i (+ i 1)) ))) (do-results (lambda () (begin vec ))) (do-commands (lambda () (begin (vector-set! vec i i) )))) (begin (define do-iteration (lambda () (if (= i 5) (do-results) (begin (do-commands) (update-step) (do-iteration)))))) (do-iteration))

Instance Attribute Summary collapse

Attributes inherited from SkmElement

#position

Instance Method Summary collapse

Methods inherited from SkmMultiExpression

#accept, #associations

Methods inherited from SkmElement

#accept, #boolean?, #bound!, #callable?, #char?, #complex?, #done!, #eqv?, #inspect, #integer?, #list?, #null?, #number?, #pair?, #procedure?, #quasiquote, #quoted!, #rational?, #real?, #skm_eq?, #skm_equal?, #string?, #symbol?, #unquoted!, #vector?, #verbatim?

Constructor Details

#initialize(aTest, doResult, theCommands, theUpdates) ⇒ DoExpression

Returns a new instance of DoExpression.



456
457
458
459
460
461
462
# File 'lib/skeem/s_expr_nodes.rb', line 456

def initialize(aTest, doResult, theCommands, theUpdates)
  super(nil)
  @test = aTest
  @do_result = doResult
  @commands = theCommands
  @update_steps = theUpdates
end

Instance Attribute Details

#commandsObject (readonly)

expression to execute in each iteration



448
449
450
# File 'lib/skeem/s_expr_nodes.rb', line 448

def commands
  @commands
end

#do_resultObject (readonly)

expression to execute before return



451
452
453
# File 'lib/skeem/s_expr_nodes.rb', line 451

def do_result
  @do_result
end

#testObject (readonly)

iterate until the test expression becomes true



445
446
447
# File 'lib/skeem/s_expr_nodes.rb', line 445

def test
  @test
end

#update_stepsObject (readonly)

Update variables



454
455
456
# File 'lib/skeem/s_expr_nodes.rb', line 454

def update_steps
  @update_steps
end

Instance Method Details

#evaluate(aRuntime) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/skeem/s_expr_nodes.rb', line 464

def evaluate(aRuntime)
  # require 'debug'
  loop do
    test_result = test.evaluate(aRuntime)
    if test_result.boolean? && test_result.value == false
      # Only #f is considered as false, everything else is true
      commands&.evaluate(aRuntime)
      if update_steps
        update_steps.evaluate(aRuntime)
        case update_steps
          when SkmEmptyList.instance
            # Do nothing
          when SkmPair
            arr = update_steps.to_a
            arr.each { |delayed_binding| delayed_binding.do_it!(aRuntime) }
          else
            update_steps.do_it!(aRuntime)
        end
      end
    else
      break
    end
  end

  do_result ? do_result.evaluate(aRuntime) : SkmUndefined.instance
end