Preliminary Posting:

Semantic Interpretations and Restrictions in SRC3

Contents:

0. Declarations: No item can be used until it is declared.
This allows recursion but disallows mutual recursion, even in function definitions.
It allows a constant to be used anywhere once declared, but non recursively
since its value must be set by the declaration.

1. Arithmetic Expressions
2. Arithmetic Expressions with Strings
3. Arithmetic Expressions with bool variables
4. Relational Expressions
5. Logical Expressions
       Rules for assignment to bool
6. Array Expressions

  • Numeric Array Expressions
  • String Array Expressions
  • Mixed StringArray:scaler Expressions
  • bool Array Expressions

  • 7. Assignments
    8. Arguments and Return Values)
    9. Nonuse of the ch type)
    10. Input/Output


    1. Arithmetic Expressions

    For Arithmetic Expression of the form: X <op> Y  The <op> can be: Mulops all bind more tightly than addops.

    Sequences with only Addops are evaluated left to right since all Addops have the same precedence.
    Sequences with only Mulops are also evaluated left to right since all Mulops have the same precedence.

    For A op B: A and B can be either real or int for any operator.
     
     
    A op  B Result
    real  +, -, *  real real
    real  +, -, *  int  real
    int   +, -, *  real  real
    int   +, -, *  int  int
    real   /  real  real
    real    /  int real 
    int  /  real  real
    int  /  int int
    real  %  real  int
    real  %  int  int
    int  %  real  int
    int  %  int  int

    If either argument to the % (mod) operation is real,  it is truncated to an int before applying the mod operation.


    2. Arithmetic Expressions with string

    Some "arithmetic" operations are also defined for the string type. They behave as follows:
    A op B Result Comment
    string   +  string  string Meaning: concatenate:
    "abc" + "de" -> "abcde"
    string   *  int  string Meaning: concatenate repeat:
    "abc" * 3 -> "abcabcabc"
    string   -  int string  Meaning: drop from back:
    "abcde" - 2 -> "abc"
    "abcde" - 10 -> ""
    The int must not be negative
    string  /  int  string Meaning: drop from front:
    "abcde" / 3 -> "de"
    "abcde" / 9 -> ""
    The int must not be negative

    3. Arithmetic Expressions with bool

    None of the "arithmetic" operations are defined for the bool type.

    4. Relational Expressions

    For Relational Expressions of the form X <Relop> Y where X and
    Y are Expressions, the table gives their behavior.
    Relop means: ">", ">=", "<", "<=", "=", "<>"
     
    X op Y Result Comment
    real
    or
    int
    relop real
    or
    int
    bool,
    value
    T or F
    Meaning as usual. Value is type bool
    Both must be cast to real unless both are int
    String
    Expr
    relop string
    Expr
    bool,
    value
    T or F
    Meaning: Ex: A < B iff A comes before B
                in Dictionary listing. **

    ** I.e. the strings are compared, and if
     {the ascii value of the leading character in A not  common to both} 
      < {the ascii value of the leading character not common to both in B }  
    then A < B

    A = B means the strings are identical.

    Expressions such as  A < B < C are not allowed.


    5. Logical Expressions

    A Logical Expression has the form:
    X <Logicop> Y where Logicop means '&&' or '||',
    and X and Y are Expressions;

    Or '!' X where X is an Expression.

    The Expressions permitted for X and Y are coerced to bool values T or F: i. e. Relational  
    Expressions, Logical Expressions, base types.  The result is always of type  bool.
     
     
    LogOp Result Comments
     && bool
    T or F
    X && Y is T iff
    both are T.
     || bool
    T or F
    X || Y is F
    iff both are F.
      ! bool
    T or F
    ! X is T
    iff X is F.

    Note that the bool type, can have only the values T or F, so assignment of a bool to an int, real, or string variable is illegal.  However assignment of an int, real, or string to a variable of type bool is legal. Example:

             C := X < Y is illegal unless C is of type bool.

    Subroutines can return a bool value; they can take a bool valued expression as an argument;
    a return <Expression>  statement can return a bool value;
    a bool can be the argument of a write statement, which writes the character T or F
    but no bool value may be used by arithmetic operators.

    Conditions

    Expressions of any base type can be used in the "condition" part of "if" or "while" statements.
    Before use as a condition, int, real, or string values are converted to bool, following the rules for assignment to bool as described below.

    In an "if", if the condition value is T the "then" statement list is executed;
    if F the "else" statement list, if any, is executed. Thus the statement:
        if a < b then stringV := "T"; numV := 1; else stringV := "F"; numV := 0; end if;
    is legal. The statement:
        if a < b then write "T";   else write "F"; end if;
    is legal. The statement:
        write a < b;
    is legal, and prints the  character T  or F.
    NOTE: These are NOT the same as the strings  "T" or "F" despite similar appearance.

    In a "while", if the condition value is T the "do" statement list is executed;

    Rules for assignment to bool

    If a real or int whose value is 0 is assigned to a bool, the bool has value F.
    If a string which is empty is assigned to a bool, the bool has value F.
    Otherwise the bool has value T.


    6. Array Expressions

    An array has a fixed size and shape, defined by constants when the array is declared.

    6A. Numeric array Expressions

    "Numeric" arrays (of real or int) may be added to or subtracted from other numeric arrays of the same size and shape, element by element. Array elements must be all be int or must all real. Use of mixed variable types are errors.

    Multiplication and division of numeric arrays by numeric arrays are not supported;
    if used, these operations must be defined in SRC3 using subs.
    ArrA op ArrB ArrResult Example
    int  +,-  int  int
    1 2
    3 4
     + 
    8 7
    3 0
     = 
    9 9
    6 4
    real  +,-  int  error  
    int   +,-  real  error  
    real   +,-  real  real 
    1.1 2.2
    3.3 0.1
    2.2 3.1
    1.5 2.2
    3.3 5.3
    4.8 2.3

    6B. String Array Expressions

    The operations
        (array of string) { + } (array of string)
    and
        (array of string) { /, -, * } (array of int)

    are supported,  with operations done element  by element.
     
    ArrA op ArrB ArrResult Examples
    string  +  string  string Meaning: concatenate elements
     "ab"   "cd" 
     "ef"  "gh"
     + 
    "x" "yz"
     ""  "gh"
     = 
    "abx" "cdyz"
    "ef" "ghgh"
    string  * int string Meaning: repeat concatenate:
     
    "ab"  "cd" 
    "ef" "gh"
     * 
    0 1
    2 3
     = 
    "" "cd"
    "efef" "ghghgh"
    the ints must not be negative
    string  -  int  string Meaning: drop elements from back
    "abc" "def"
    "ghi" "jkl"
     - 
    1 0
    3 4
     = 
    "ab" "def"
     ""  ""
    the ints must not be negative
    string  /  int string Meaning: drop elements from front
    "abc" "def"
    "ghi" "jkl"
     / 
    1 0
    3 4
     = 
    "bc" "def"
    "" ""
    the ints must not be negative

    6C. Mixed StringArray:scaler Expressions

    Some binary operators are defined for one base type operand and one array operand.
    The result is an array. Only operations in the order shown is allowed.
    The supported operations and their resulting types are listed below.
     
    ArrA op ScalarB ArrResult Comment
    array of
    string
     +  string  array of
    string
    Meaning: concatenate elements
     "ab"   "cd" 
     "ef"  "gh"
     +  "yz"  = 
    "abyz" "cdyz"
    "efyz" "ghyz"
    array of
    string
     * int array of
    string
    Meaning: repeat concatenate:
    "ab"  "cd" 
    "ef" "gh"
     *   = 
    "abab" "cdcd"
    "efef" "ghgh"
    the ints must not be negative
    array of
    string
     -  int  array of
    string
    Meaning: drop elements from back
    "abcdef" ""
    "ghi" "jkl"
     -   = 
    "abcde" ""
    "gh" "jk"
    the int must not be negative
    array of
    string
     /  int array of
    string
    Meaning: drop elements from front
    "abcdef" ""
    "ghi" "jkl"
     /   = 
    "bcdef" ""
    "hi" "kl"
    the int must not be negative

    6D. bool array Expressions

    Also supported are for arrays of the same size and shape.

    For C := A || B, element C[i,j] is A[i,j] || B[i,j] .
    For C := A && B, element C[i,j] is A[i,j] && B[i,j].

    The results are shown in the table below.
     
    ArrA op ArrB ArrResult Examples
    bool   ||  bool  bool
    a b
    c d
    || 
    e f
    g h
    a || e b || f
    c || g d || h
    bool   &&  bool  bool
    a b
    c d
    && 
    e f
    g h
    a && e b && f
    c && g d && h


    7. Assignments

    An Assignment Statement is of the form: V := X

    Assignment of a Logical Expression or a Relational Expression to a variable is only legal if the variable is type bool. No conversion or coercion method from  bool  to other types is available.

    Assignment of arithmetic expressions, constants,   or returns from arithmetic functions to a real or int variable is legal and the result is converted to the type of that variable. Assignment to a bool variable is also legal;  if the value is 0 then the bool has value F,otherwise T.
    Remember that reals rarely achieve the precise value 0 after a calculation.

    If the RHS is real and the LHS is int, truncation occurs.

    Assignment of string expressions, constants,   or string function returns, to a variable of type string is  legal. Assignment to a bool variable is also legal;
    if the string is empty then the bool has value F , otherwise T.

    Arrays may be assigned to array variables with the same size and shape.

    Arrays of real or of int may be assigned to an array of real, or of int, or of bool .
    The output type is coerced to the declared type of the LHS Array, produced by truncation if needed.
    Arrays of string may only be assigned to arrays of string, or of bool.


    8. Arguments and Return Values

    Arguments may be coerced to the required formal parameter type as follows:
     
    Formal type / Return type Argument type / Expression type Comment
    int real truncate, then coerce to int
    real int  
    bool int, real, string 0, 0.0, and "" are F; all other values are T.

    Expressions in the return statement may be coerced to the declared function return type in the same fashion as argument types above.


    9. Nonuse of a ch type

    No ch type will be implemented, since its functions can be handled as string with a single character. 

    10. I/O

    Since there are no bool literals, it makes no sense, and is an error, to read a bool variable.
    A write of a  bool variable outputs "T" if the value is T and "F" if the value is F.

    A read statement reads in a single variable or array element.

    A write statement writes an expression list, which may be empty.
    A new line is emitted at the end of a write.