logo

class

compiler::Block

sys::Obj
  compiler::Node
    compiler::Block
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   19 Jul 06  Brian Frank  Creation
   7  //
   8  
   9  **
  10  ** Block is a list of zero or more Stmts
  11  **
  12  class Block : Node
  13  {
  14  
  15  //////////////////////////////////////////////////////////////////////////
  16  // Construction
  17  //////////////////////////////////////////////////////////////////////////
  18  
  19    new make(Location location)
  20      : super(location)
  21    {
  22      stmts = Stmt[,]
  23    }
  24  
  25  //////////////////////////////////////////////////////////////////////////
  26  // Stmts
  27  //////////////////////////////////////////////////////////////////////////
  28  
  29    **
  30    ** Return is there are no statements
  31    **
  32    Bool isEmpty() { return stmts.isEmpty }
  33  
  34    **
  35    ** Return number of statements
  36    **
  37    Int size() { return stmts.size }
  38  
  39    **
  40    ** Does this block always cause us to exit the method (does the
  41    ** last statement return true for Stmt.isExit)
  42    **
  43    Bool isExit()
  44    {
  45      if (stmts.isEmpty) return false
  46      return stmts.last.isExit
  47    }
  48  
  49    **
  50    ** Append a statement
  51    **
  52    Void add(Stmt stmt)
  53    {
  54      stmts.add(stmt)
  55    }
  56  
  57    **
  58    ** Append a list of statements
  59    **
  60    Void addAll(Stmt[] stmts)
  61    {
  62      this.stmts.addAll(stmts)
  63    }
  64  
  65  //////////////////////////////////////////////////////////////////////////
  66  // Tree
  67  //////////////////////////////////////////////////////////////////////////
  68  
  69    Void walkExpr(|Expr expr->Expr| closure)
  70    {
  71      walk(ExprVisitor.make(closure), VisitDepth.expr)
  72    }
  73  
  74    Void walk(Visitor v, VisitDepth depth)
  75    {
  76      v.enterBlock(this)
  77      stmts.each |Stmt stmt| { stmt.walk(v, depth) }
  78      v.visitBlock(this)
  79      v.exitBlock(this)
  80    }
  81  
  82  //////////////////////////////////////////////////////////////////////////
  83  // Debug
  84  //////////////////////////////////////////////////////////////////////////
  85  
  86    override Void print(AstWriter out) { printOpt(out) }
  87  
  88    Void printOpt(AstWriter out, Bool braces := true)
  89    {
  90      if (braces) out.w("{").nl
  91      out.indent
  92      stmts.each |Stmt stmt| { stmt.print(out) }
  93      out.unindent
  94      if (braces) out.w("}").nl
  95    }
  96  
  97  //////////////////////////////////////////////////////////////////////////
  98  // Fields
  99  //////////////////////////////////////////////////////////////////////////
 100  
 101    Stmt[] stmts
 102  }