Table of Contents

Sequence, Yield, and Parallelism

Sequences are implicit in HipHop. That is, two statements separated by a ; are executed sequentially.

yield

Formal syntax

A thread of execution can suspend itself for the current instance using the yield construct. The execution will resume after the yield when the react method of the machine will be called again.

fork { ... } par { ... }

Formal syntax

Run all the bodies in parallel. Complete when all bodies have completed.

Example:

This example uses two nested fork constructs. The second is synchronized with the first as it waits for an event the first branch is to emit.

parallel-unary.hh.js

"use hiphop";
"use hopscript";

var hh = require( "hiphop" );

hiphop module prg( O ) {
   loop {
      signal L;

      fork {
         emit L();
      } par {
         fork {
            if( L.now ) emit O();
         }
      }

      yield;
   }
}

exports.prg = new hh.ReactiveMachine( prg, "parallelunary" );

Exit

exit

Formal syntax

Lexical Escapes

break lbl

Formal syntax

Loops

Loops are so central HipHop program control flow that HipHop proposes several loop constructs.

loop { ... }

Formal syntax

Implements an infinite loop

Example:

sync1.hh.js

"use hiphop";
"use hopscript";

const hh = require( "hiphop" );

hiphop module prg( O ) {
   signal L;

   fork {
      loop {
         emit L();
         yield;
      }
   } par {
      loop {
         await( L.now );
         emit O();
      }
   }
}

const machine = new hh.ReactiveMachine( prg, "sync1" );
machine.debug_emitted_func = console.log;

machine.react()
machine.react()
machine.react()
machine.react()

every( test ) { ... }

Formal syntax

A loop executed each time the test is true:

every1.hh.js

"use hiphop";
"use hopscript";

const hh = require( "hiphop" );

hiphop module prg( in I, O ) {
   every( I.now ) {
      emit O();
   }
}

exports.prg = new hh.ReactiveMachine( prg, "every1" );

do { ... } every( test )

Formal syntax

Execute the do's body and loop when test is true.

loopeach.hh.js

"use hiphop";
"use hopscript";

var hh = require( "hiphop" );

hiphop module prg( in I, O ) {
   do {
      emit O();
   } every( I.now )
}

exports.prg = new hh.ReactiveMachine( prg, "loopeach" );