Class Constant


public final class Constant extends Expression

Constant.

All constants are treated by the Lexicographical Scanner.


Syntax:

  Constant = unsigned-number | string | character-constant | NONE | NOTEXT
  

Link to GitHub: Source File.

Author:
Øystein Myhre Andersen
See Also:
  • Field Details

    • value

      public Object value
      The constant's value
  • Constructor Details

    • Constant

      public Constant(Type type, Object value)
      Create a new Constant.
      Parameters:
      type - the constant's type
      value - the constant's value
    • Constant

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

    • createRealType

      static Constant createRealType(Object value)
      Create a real type Constant.
      Parameters:
      value - a real type value
      Returns:
      the resulting Constant
    • getType

      private static Type getType(Number n)
      Returns the type of this number.
      Parameters:
      n - the number
      Returns:
      the type of this number.
    • evaluate

      static Constant evaluate(int opr, Number rhn)
      Simplify this Constant.
      Parameters:
      opr - an unary operation
      rhn - a right hand Number
      Returns:
      the resulting Constant
    • evaluate

      static Constant evaluate(Number lhn, int opr, Number rhn)
      Simplify this Constant.
      Parameters:
      lhn - a left hand Number
      opr - an binary operation
      rhn - a right hand Number
      Returns:
      the resulting Constant
    • doChecking

      public void doChecking()
      Description copied from class: SyntaxClass

      Perform semantic checking.

      This must be redefined in every subclass.

      Overrides:
      doChecking in class SyntaxClass
    • maybeStatement

      public boolean maybeStatement()
      Description copied from class: Expression
      Returns true if this expression may be used as a statement.
      Specified by:
      maybeStatement in class Expression
      Returns:
      true if this expression may be used as a statement
    • toJavaCode

      public String toJavaCode()
      Description copied from class: SyntaxClass
      Generate Java code for this Syntax Class.
      Overrides:
      toJavaCode in class SyntaxClass
      Returns:
      Java code
    • encode

      private String encode(String s)
      Encode a string with escape sequences.
      Parameters:
      s - the string
      Returns:
      a string with escape sequences.
    • appendHex

      private void appendHex(StringBuilder sb, char c)
      Java Encoding Utility: Edit hex(c) and append it to the given StringBuilder.
      Parameters:
      sb - the StringBuilder
      c - the input character
    • buildEvaluation

      public void buildEvaluation(Expression rightPart, CodeBuilder codeBuilder)
      Description copied from class: Expression
      ClassFile coding utility: Build Evaluation Code.
      Specified by:
      buildEvaluation in class Expression
      Parameters:
      rightPart - expression
      codeBuilder - the codeBuilder used.
    • buildIntConst

      public static void buildIntConst(CodeBuilder codeBuilder, boolean b)
      ClassFile coding: Build boolean const as an integer 1:true or 0:false.
      Parameters:
      codeBuilder - the codeBuilder to use.
      b - the actual boolean value.
    • buildIntConst

      public static void buildIntConst(CodeBuilder codeBuilder, int i)
      ClassFile coding: Build integer const.
      Parameters:
      codeBuilder - the codeBuilder to use.
      i - the actual integer value.
    • toString

      public String toString()
      Overrides:
      toString in class Expression
    • 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 Constant readObject(AttributeInputStream inpt) throws IOException
      Read and return a Constant.
      Parameters:
      inpt - the AttributeInputStream to read from
      Returns:
      the Constant read from the stream.
      Throws:
      IOException - if something went wrong.