Module Simula

Class ArithmeticExpression


public final class ArithmeticExpression extends Expression
Arithmetic expressions
 Simula Standard:  3.5 Arithmetic expressions

    arithmetic-expression
        =  simple-arithmetic-expression
        |  if-clause  simple-arithmetic-expression
           else  arithmetic-expression

    simple-arithmetic-expression
        =  [ + | - ]  term  {  ( + | - )  term }

    term
        =  factor  {  ( * | / | // )  factor }

    factor
        =  primary  { **  primary }

    primary
        =  unsigned-number
        |  variable
        |  function-designator
        |  "("  arithmetic-expression  ")"
 
An arithmetic expression is a rule for computing a numerical value. In the case of simple arithmetic expressions this value is obtained by executing the indicated arithmetic operations on the actual numerical values of the primaries of the expression, as explained in detail in 3.5.1 below. The value of a primary is obvious in the case of numbers. For variables it is the current value (assigned last in the dynamic sense), and for function designators it is the value arising from the computing rules defining the procedure when applied to the current values of the procedure parameters given in the expression. Finally, for arithmetic expressions enclosed by parentheses the value must through a recursive analysis be expressed in terms of the values of primaries of the other three kinds.

In the more general arithmetic expressions, which include if-clauses, one out of several simple arithmetic expressions is selected on the basis of the actual values of the Boolean expressions (see 3.2). This selection is made as follows: The Boolean expressions of the if-clauses are evaluated one by one in sequence from left to right until one having the value true is found. The value of the arithmetic expression is then the value of the first arithmetic expression following this Boolean (the longest arithmetic expression found in this position is understood). If none of the Boolean expressions has the value true, then the value of the arithmetic expression is the value of the expression following the final else.

In evaluating an arithmetic expression, all primaries within that expression are evaluated with the following exceptions:

  - Primaries that occur within any expression governed by an if-clause but not selected by it.
 
  - Primaries that occur within a Boolean expression
    1) after the operator or else when the evaluation of a preceding Boolean tertiary results in false, or
    2) after the operator and then when the evaluation of a preceding equivalence results in false.
  
  - Primaries that occur after a function designator, and the evaluation of
    the function terminates with a goto-statement. In this case the evaluation of
    the arithmetic expression is abandoned.
 
Primaries are always evaluated in strict lexical order. NOTE: The implementation of EXP '**' deviates from the definition in Simula Standard. It is always evaluated in long real and the result is converted to the appropriate type.

Link to GitHub: Source File.

Author:
Simula Standard, Øystein Myhre Andersen
  • Field Details

    • lhs

      private Expression lhs
      The left hand side
    • opr

      private final KeyWord opr
      The arithmetic operation
    • rhs

      private Expression rhs
      The right hand side
  • Constructor Details

    • ArithmeticExpression

      private ArithmeticExpression(Expression lhs, KeyWord opr, Expression rhs)
      Create a new ArithmeticExpression
      Parameters:
      lhs - left hand side
      opr - arithmetic operation
      rhs - right hand side
  • Method Details

    • create

      static Expression create(Expression lhs, KeyWord opr, Expression rhs)
      Create a new ArithmeticExpression.
      Parameters:
      lhs - the left hand side
      opr - the arithmetic operation
      rhs - the right hand side
      Returns:
      the newly created ArithmeticExpression
    • evaluate

      public Expression evaluate()
      Description copied from class: Expression
      Try to Compile-time Evaluate this expression
      Overrides:
      evaluate in class Expression
      Returns:
      the resulting evaluated expression
    • 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
    • toString

      public String toString()
      Overrides:
      toString in class Expression