CSC 330 Lecture Notes Week 6
Operational Semantics of Imperative Programming Languages
Topics from Chapters 4 & 5 of the Book
/*
 * This is the simplest possible executable program.  The result should be a
 * value of 10 in location 0 of static memory.
 */
int i;
void main() {
    i = 10;
}
is that the variable i is assigned the value of 10, which is represented
by the following post-execution memory dump
Static Pool: Location 0: 10 Stack: empty
/*
 * This is a very simple test program for a function call.  The result should
 * be a value of 3 in static memory location 0.  Test the program with
 * EJayInterpreterTest.java.
 */
int i;
void main() {
    f(3);
}
int f(int x) {
    i = x;
}
StaticPool:                     Level 0 symtab:
  0 (i's address):                i, int, mem loc: 0
Stack: empty (main?)              f, int,
                                    Formals: i
                                    Level 1 symtab:
                                      x, int, mem loc: 0
                                    Body: "i = x;"
evalFuncCall            13 lines of code, excluding comments
  evalFuncIdent         13 lines, including 11 lines of error handling
    lookup
    error
  bind                  21 lines, including 8 of error handling
    bind1               11 lines, including 4 of error handling
      evalFormalLValue  4 lines, very similar to evalIdentLValue
        lookup
        LValue
      evalExpr
  evalFuncBody          8 excruciatingly elegant lines of code
    eval                the same 'ol thing
evalReturn              2 stunningly simple lines of code
  eval
  ReturnValue
class ReturnValue ------> RuntimeException
  Value
  ReturnValue(Value)
waldorf% jdb EJayInterpreterTest func-return.ej > stop in EJayInterpreter.evalReturn > run EJayInterpreterTest sample-test-files/func-return.ej Breakpoint hit: EJayInterpreter.evalReturn(), line=452 > where [1] EJayInterpreter.evalReturn (EJayInterpreter.java:452) [2] EJayInterpreter.evalStmt (EJayInterpreter.java:129) [3] EJayInterpreter.eval (EJayInterpreter.java:72) [4] EJayInterpreter.evalStmtList (EJayInterpreter.java:96) [5] EJayInterpreter.eval (EJayInterpreter.java:68) [6] EJayInterpreter.evalStmt (EJayInterpreter.java:135) [7] EJayInterpreter.eval (EJayInterpreter.java:72) [8] EJayInterpreter.evalFuncBody (EJayInterpreter.java:374) [9] EJayInterpreter.evalFuncCall (EJayInterpreter.java:316) [10] EJayInterpreter.evalExpr (EJayInterpreter.java:511) [11] EJayInterpreter.evalAssmnt (EJayInterpreter.java:150) [12] EJayInterpreter.evalStmt (EJayInterpreter.java:117) [13] EJayInterpreter.eval (EJayInterpreter.java:72) [14] EJayInterpreter.evalStmtList (EJayInterpreter.java:96) [15] EJayInterpreter.eval (EJayInterpreter.java:68) [16] EJayInterpreter.evalStmt (EJayInterpreter.java:135) [17] EJayInterpreter.eval (EJayInterpreter.java:72) [18] EJayInterpreter.evalFuncBody (EJayInterpreter.java:374) [19] EJayInterpreter.evalFuncCall (EJayInterpreter.java:316) [20] EJayInterpreter.evalStmt (EJayInterpreter.java:126) [21] EJayInterpreter.eval (EJayInterpreter.java:72) [22] EJayInterpreterTest.main (EJayInterpreterTest.java:35)
int i;
int[10] a;
void main() {
    a[0] = 0;
    a[3] = 3;
    i = 3;
    print "a[i] = ", a[i];
}
i   0:  3
a   1:  -----> 0: undef
               1: undef
               2: undef
               3: 3
               4: undef
                 ...
int[10] a;
int[10][10][10] a3;
void main() {
    a[3] = 3;
    a3[3][3][3] = a[3];
}
a   0:  ----------------------------------------------------> 0: undef
a3  1:  -----> 0: undef                                       1: undef
               1: undef                                       2: undef
               2: undef                                       3: 3
               3: -----------> 0: undef                          ...
               4: undef        1: undef
                 ...           2: undef
                               3: -----------> 0: undef
                                               1: undef
                                               2: undef
                                               3: 3
                                                  ...
struct {int f1; float f2; string f3;} s;
void main() {
    s.f1 = 1;
    s.f2 = 1.5;
    s.f3 = "abc";
}
s  0: ----------> 0: 1
                  1: 1.5
                  2: "abc"
val  ------------> 0: 1
                   1: 1.5
                   2: "abc"
type -------------> TypeNode.child1 -----> field list
                    TypeNode.symtab -----> symtab, field offsets
int i,j;
void main() {
    i = 10; j = 20;
    swap(i,j);
}
void swap(int x, int y) {
    int temp;
    temp = x;
    x = y;
    y = temp;
}
int i,j;
void swap(int* x, int* y) {
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
int main() {
    i = 10;  j = 20;
    swap(&i, &j);
}
int i,j;
void main() {
    i = 10; j = 20;
    swap(i,j);
}
void swap(ref int x, ref int y) {
    int temp;
    temp = x;
    x = y;
    y = temp;
}