Class SwitchStatement


public final class SwitchStatement extends Statement

Switch Statement.

This is a S-PORT extension to the language.


Syntax:

switch-statement = SWITCH ( lowKey : hiKey ) switchKey
                   BEGIN { switch-case } [ none-case ] END [ otherwise-case ]

     switch-case = WHEN caseKey-list do statement ;
     
     none-case   = WHEN NONE do statement ;
     
     otherwise-case = OTHERWISE statement ;
     
        caseKey-list = caseKey { , caseKey }
     
           caseKey = caseConstant  | caseConstant : caseConstant
           
              caseConstant = integer-or-character-constant
     
     lowKey = integer-or-character-expression
     hiKey = integer-or-character-expression
     switchKey = integer-or-character-expression

Translated into a Java Switch Statement with break after each statement.

Example:

  switch(lowkey:hikey) key
  begin
     when 0 do statement-0 ;
     ...
     when none do statement-e ;
  end
  
  Is compiled into Java-code:
  
  if(key < lowkey || key > hikey) throw new RTS_SimulaRuntimeError("Switch key outside key interval");
  switch(key) {
      case 0: statement-0; break;
      ...
      default: statement-e; break;
  }
  

Link to GitHub: Source File.

Author:
Øystein Myhre Andersen
  • Field Details

    • lowKey

      private Expression lowKey
      The low key.
    • hiKey

      private Expression hiKey
      The high key
    • switchKey

      private Expression switchKey
      The switchKey
    • switchCases

      private final Vector<SwitchStatement.SwitchWhenPart> switchCases
      The list of When parts
    • lookupSwitchCases

      private List<SwitchCase> lookupSwitchCases
      The lookupSwitc cases.
    • has_NONE_case

      private boolean has_NONE_case
      Indicator: har NONE case.
    • defaultTarget

      private Label defaultTarget
      The beginning of the default handler block.
    • endLabel

      private Label endLabel
      The end label.
  • Constructor Details

    • SwitchStatement

      SwitchStatement(int line)
      Create a new SwitchStatement.
      Parameters:
      line - the source line number
    • SwitchStatement

      private SwitchStatement()
      Default constructor used by Attribute File I/O
  • Method Details

    • expectCasePair

      private SwitchStatement.SwitchInterval expectCasePair()
      Parse Utility: Expect case pair.
      Returns:
      the resulting SwitchInterval
    • doChecking

      public void doChecking()
      Description copied from class: SyntaxClass

      Perform semantic checking.

      This must be redefined in every subclass.

      Overrides:
      doChecking in class SyntaxClass
    • doJavaCoding

      public void doJavaCoding()
      Description copied from class: SyntaxClass
      Output Java code.
      Overrides:
      doJavaCoding in class Statement
    • buildByteCode

      public void buildByteCode(CodeBuilder codeBuilder)
      Description copied from class: Statement
      Build Java ByteCode.
      Overrides:
      buildByteCode in class Statement
      Parameters:
      codeBuilder - the codeBuilder to use.
    • buildSwitchKeyTest

      private void buildSwitchKeyTest(CodeBuilder codeBuilder)
      ClassFile coding utility: buildSwitchKeyTest.
      Parameters:
      codeBuilder - the codeBuilder to use.
    • print

      public void print(int indent)
      Description copied from class: SyntaxClass
      Utility print method.
      Overrides:
      print in class SyntaxClass
      Parameters:
      indent - number of spaces leading the line
    • printTree

      public void printTree(int indent, Object head)
      Description copied from class: SyntaxClass
      Utility print syntax tree method.
      Overrides:
      printTree in class SyntaxClass
      Parameters:
      indent - number of spaces leading the lines
      head - the head of the tree.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • writeObject

      public void writeObject(AttributeOutputStream oupt) throws IOException
      Description copied from class: SyntaxClass
      Write a SyntaxClass object to a AttributeOutputStream.
      Overrides:
      writeObject in class SyntaxClass
      Parameters:
      oupt - the AttributeOutputStream to write to.
      Throws:
      IOException - if something went wrong.
    • readObject

      public static SwitchStatement readObject(AttributeInputStream inpt) throws IOException
      Read and return an object.
      Parameters:
      inpt - the AttributeInputStream to read from
      Returns:
      the object read from the stream.
      Throws:
      IOException - if something went wrong.