compiler/backend/jsflow

  Source   Edit

Implements the translation of CGIR control-flow constructs to JavaScript constructs.

While JavaScript doesn't directly support the kind of control flow that the MIR supports, it does support 'finally' and 'catch', both which are leveraged for the translation. The idea: figure out which statements JavaScript blocks, 'finally's, and 'catch's need to enclose and then place them in a way such that the behaviour is translated correctly.

Types

StructDesc = tuple[structs: seq[Structure], finallys: PackedSet[BlockId], ## labels that denote finally sections
                   inline: Table[BlockId, int]]
Describes how a CGIR statement-list translates to JavaScript code. The focus is on the control-flow constructs, hence the name.   Source   Edit
StructKind = enum
  stkTry,                   ## start of a 'try' statement
  stkBlock,                 ## start of a labeled block
  stkStructStart,           ## start of an 'if' or 'while'
  stkCatch,                 ## a 'catch' clause of a 'try' statement
  stkFinally,               ## a 'finally' clause of a 'try' statement
  stkEnd, ## end of a catch, finally, block, or `stkStructStart`
           ## (if and while)
  stkTerminator,            ## a goto or raise statement. Only relevant during analysis
  stkReturn                  ## JavaScript return
  Source   Edit
Structure = object
  stmt*: int                 ## the associated CGIR statement
  case kind*: StructKind
  of stkStructStart, stkTry, stkBlock, stkCatch, stkFinally, stkEnd:
      label*: BlockId        ## the associated CGIR label
    
  of stkTerminator, stkReturn:
      nil

  
A list of Structure items describes how the JavaScript control-flow statements are laid out.   Source   Edit

Procs

func finalTarget(n: CgNode): CgNode {....raises: [], tags: [].}
Given a label or target list, retrieves the target.   Source   Edit
proc toStructureList(stmts: openArray[CgNode]): StructDesc {....raises: [],
    tags: [].}
Creates and returns the JavaScript control-flow-construct-focused representation for stmts.   Source   Edit