Skip to content

StepControlStructures

JeanHuguesRobert edited this page Feb 6, 2013 · 1 revision

Mixing statements and steps

Because "steps" and "statements" are not on the same level (steps for tasks, statements for functions), the classical javascript control structures have equivalent structures at the step level.

function xx(){
  ..1..
  try{
    ..2..
  catch( e ){
    ..3..
  finally {
    ..4..
  }
  ..5..
}

becomes:

xx_task = l8.Task( function(){
  this.step( function(){
    ..1..
  }).step( function(){
    this.begin.step( function(){
      ..2..
    }).failure( function(e){
      ..3..
    }).final( function(){
      ..4..
    }).end
  }).step( function(){
    ..5..
  })
})

or

xx_task = l8.compile( function(){
  step; ..1..
  step; begin
    ..2...
    failure;
    ..3..
    final;
    ..4..
  end
  step; ..5..
})
while( condition ){
  ...
  if( extra )break
  ...
  if( other_extra )continue
  ...
}

becomes:

l8.repeat( function(){
  ...
  if( condition ) this.break
  ...
  if( extra ) this.break
  ...
  if( other_extra ) this.continue
  ...
}

or

xx = l8.compile( function(){
  repeat; begin
    ...
    if( condition ) this.break
    ...
    if( extra ) this.break
    ...
    if( other_extra ) this.continue
    ...
  end
})
for( init ; condition ; next ){
  ...
}

becomes:

  init
  this.repeat( function(){
    if( condition ) this.break
    ...
    next
  })
for( init ; condition ; next ){
  ...
  if( extra ) continue
  ...
})

becomes:

  init
  this.repeat( function(){
    if( condition ) this.break
    this.task( function(){
      ...
      this.step( function(){ if( extra ) this.return })
      ...
      this.step( function(){ next })
    })
  })

or

xx = l8.compile( function(){
  init; repeat; begin ; if( condition ) this.break ; begin
    ...
    if( extra ) this.return
    ...
  end; next; end
})
for( var key in object ){
  ...
}

becomes:

  var keys = object.keys(), key
  this.repeat( function(){
    if( !(key = keys.shift()) ) this.break
    ...
  })

or

xx = l8.compile( function(){
  var keys = object.keys(), key
  repeat; begin;
    if( !(key = keys.shift()) ) this.break
    ...
  end
})

Clone this wiki locally