
PROC cgtest1 (CHAN OF BYTE keyb, scr, err)

  #INCLUDE "cgmain.occ"

  --{{{  static chain
  VAL T  IS [1,2,3] :
  VAL BT IS [BYTE 4, BYTE 5, BYTE 6] :
  
  INT X, Y:
  [3]INT  V :
  [3]BYTE BV :
  INT zero, one, two:
  
  --{{{  C1
  PROC C1 ()
    X := 42
  :
  --}}}
  --{{{  C2
  PROC C2 ()
    PROC C1 ()
      X := 36
    :
    C1 ()
  :
  PROC C2 ()        -- descoping the above one
    PROC C1 ()
      X := 69
    :
    C1 ()
  :
  --}}}
  --{{{  C3
  PROC C3 ()
    PROC C4 ()
      PROC C5 ()
        X := 42
      :
      SEQ
        X := 69
        check( X, 69, "C3")
        C5 ()
        check( X, 42, "C4")
    :
    SEQ
      C4 ()
      X := 666
  :
  --}}}
  --{{{  C4
  PROC C4 (VAL INT A, INT B)
    PROC C1 ()
      B := A
    :
    PROC C2 ()
      B := A * A
    :
    PROC C3 (VAL INT X, INT Y)
      Y := X
    :
    SEQ
      C1 ()
      check( A, B, "C5")
      C2 ()
      check( A * A, B, "C6")
      C3 (A, B)
      check( A, B, "C7")
  :
  --}}}
  --{{{  C5
  PROC C5 ()
    PROC C1 (VAL INT X, INT Y)
      Y := X
    :
    SEQ
      C1 (42, X)
      check (X, 42, "C9")
  :
  --}}}
  --{{{  C6
  PROC C6 ()
    CHAN OF INT Sync:
    SEQ
      PAR
        PROC C1 (VAL INT X, INT Y)
          Y := X - 1
        :
        SEQ
          C1 (43, X)
          Sync ! 1
        PROC C1 (VAL INT X, INT Y)
          Y := X + 1
        :
        INT junk:
        SEQ
          Sync ? junk
          C1 (68, Y)
      SEQ
        check( X, 42, "C11")
        check( Y, 69, "C12")
  :
  --}}}
  --{{{  C7
  PROC C7 ()
    --{{{  PROC C1
    PROC C1 (VAL INT J)
      PROC C2 ()
        SEQ
          X := J
          check( X, J, "C15")
      :
      PROC C3 ()
        SEQ
          Y := J
          check( Y, J, "C16")
      :
      SEQ
        C2 ()
        check( X, J, "C17")
        C3 ()
        check( Y, J, "C18")
    :
    --}}}
    --{{{  PROC C2
    PROC C2 ()
      SEQ
        X := 69
        Y := 69
        check( X, 69, "C19")
        check( Y, 69, "C20")
    :
    --}}}
    SEQ
      C1 (3)
      check( X, 3, "C21")
      check( Y, 3, "C22")
      C2 ()
      check( X, 69, "C23")
      check( Y, 69, "C24")
  :
  --}}}
  --{{{  C8
  PROC C8 ()
    --{{{  PROC C1
    PROC C1 (VAL INT A, B)
      PROC C1a ()
        SEQ
          check( A, T[1], "C27")
          check( B, INT BT[1], "C28")
      :
      C1a ()
    :
    --}}}
    --{{{  PROC C2
    PROC C2 (VAL INT A, B)
      PROC C1a ()
        SEQ
          check (A, T[1], "C29")
          check (B, INT BT[1], "C30")
      :
      C1a ()
    :
    --}}}
    SEQ
      C1 (T[1], INT BT[1])
      C2 (T[one], INT BT[one])
      check (one, 1, "C30a")
  :
  --}}}
  --{{{  C9
  PROC C9 ()
    --{{{  C1
    PROC C1 (VAL INT A)
      PROC C1Val (VAL INT X, VAL INT Y)
        SEQ
          check( X, T[1], "C31")
          check( Y, INT BT[1], "C32")
      :
      PROC C1Var (INT X)
        SEQ
          check (X, T[1], "C33")
      :
      SEQ
        V[A] := T[A]
        BV[A] := BT[A]
        C1Val (V[A], INT BV[A])
        C1Var (V[A])
    :
    --}}}
    --{{{  C2
    PROC C2 (VAL INT A)
      PROC C1Val (VAL INT X, VAL INT Y)
        SEQ
          check (X, T[one], "C35")
          check (Y, INT BT[one], "C36")
      :
      PROC C1Var (INT X)
        check( X, T[one], "C37")
      :
      SEQ
        BV[A] := BT[A]
        C1Val (V[A], INT BV[A])
        C1Var (V[A])
    :
    --}}}
    SEQ
      C1 (1)
      C2 (1)
  :
  --}}}
  --{{{  C10
  PROC C10 ()
    INT a, b, c, d, x :
    --{{{  P1
    PROC p1 (VAL INT v2, VAL INT p, VAL INT q, VAL []BYTE s)
      check (p, q, s)
    :
    --}}}
    --{{{  P2
    PROC p2 (VAL INT v2, v3, v4, p, q, VAL []BYTE s)
      check (p, q, s)
    :
    --}}}
    --{{{  P3
    PROC p3 (INT v1, v2, v3, v4, VAL INT c1, c2, c3, c4)
      SEQ
        check (v1,c1, "C43")
        check (v2,c2, "C44")
        check (v3,c3, "C45")
        check (v4,c4, "C46")
    :
    --}}}
    SEQ
      a := 5
      b := 15
      c := 20
      d := 25
      x := 25
      check (x,d,"C38")
      check (x,((((d + a) + (b + c)) - c) - c), "C39")
      check (x,(a * 5) / ((d/5)\4),"C40")
      p1 (x,(d-c),((b-a)/2),"C41")
      p2 (x,x,x,((d+(d+c))/7),((d-c)+(c-b)), "C42")
      --p3 (a,b,c,d,(a+a)-a,(b+b)-b,(c+c)-c,(d+d)-d)
      INT aa, bb, cc, dd :
      SEQ
        aa, bb, cc, dd := a, b, c, d
        p3 (aa,bb,cc,dd,(a+a)-a,(b+b)-b,(c+c)-c,(d+d)-d)
  :
  --}}}
  --{{{  C11   - contains the nested procedures
  PROC C11 ()
    INT a :
    --{{{  PROC one
    PROC one (VAL INT x)
      --static link check
      --{{{  PROC two
      PROC two (VAL INT x)
        --{{{  PROC three
        PROC three (VAL INT x)
          --{{{  PROC four
          PROC four (VAL INT x)
            --{{{  PROC five
            PROC five (VAL INT x)
              --{{{  PROC six
              PROC six (VAL INT final)
                SEQ
                  check(final, 6, "C43")
                  check(a, 42, "C44")
              :
              --}}}
              SEQ
                six (x+1)
                check(x, 5, "C45")
                check(a, 42, "C46")
            :
            --}}}
            SEQ
              five (x+1)
              check(x, 4, "C47")
              check(a, 42, "C48")
          :
          --}}}
          SEQ
            four (x+1)
            check(x, 3, "C49")
            check(a, 42, "C50")
        
        :
        --}}}
        SEQ
          three (x+1)
          check(x, 2, "C51")
          check(a, 42, "C52")
      :
      --}}}
      SEQ
        two (x+1)
        check(x, 1, "C53")
        check(a, 42, "C54")
    :
    --}}}
    SEQ
      a := 42
      one (1)
  :
  --}}}
  --}}}
  INT key :
  SEQ
    init ("CG test 1", "More misc")

    --{{{  static chains
    title ("Static chains")
    --{{{  C1
    X := 0
    C1 ()
    check( X, 42, "C1")
    --}}}
    --{{{  C2
    X := 0
    C2 ()
    check( X, 69, "C2")
    --}}}
    --{{{  C5
    X := 0
    C3 ()
    check( X, 666, "C5")
    --}}}
    --{{{  C8
    X := 0
    C4 (69, X)
    check( 69, X, "C8")
    --}}}
    --{{{  C10
    X := 0
    C5 ()
    check( 42, X, "C10")
    --}}}
    --{{{  C14
    X := 0
    Y := 0
    C6 ()
    check( 42, X, "C13")
    check( 69, Y, "C14")
    --}}}
    --{{{  C26
    X := 0
    Y := 0
    C7 ()
    check( X, 69, "C25")
    check( Y, 69, "C26")
    --}}}
    --}}}
    --{{{  non local vectors
    title ("Non local vectors")
    one := 1
    C8  ()
    C9  ()
    --}}}
    --{{{  bit more complex
    title ("More complex tests")
    C10 ()
    C11 ()
    --}}}
    --{{{  bug 745
    PROC bug.745()
      INT FUNCTION f(VAL INT a0) IS a0 :
      PROC q (VAL INT a0, a1, a2, a3, a4, a5, INT b0, b1, b2, b3, b4, b5)
        b0, b1, b2, b3, b4, b5 := a0, a1, a2, a3, a4, a5
      :
      INT a0, a1, a2, a3, a4, a5, b0, b1, b2, b3, b4, b5 :
      SEQ
        a0, a1, a2, a3, a4, a5 := 100, 101, 102, 103, 104, 105
        q(f(a0), f(a1), f(a2), f(a3), f(a4), f(a5), b0, b1, b2, b3, b4, b5)
        SEQ
          check(b0, 100, "B745a")
          check(b1, 101, "B745b")
          check(b2, 102, "B745c")
          check(b3, 103, "B745d")
          check(b4, 104, "B745e")
          check(b5, 105, "B745f")
    :
    bug.745()
    --}}}
    --{{{  bug 1251
    PROC bug.1251()
      PROC p(VAL []BYTE string, VAL INT x)
        check(x, 99, "B1251a")
      :
      p ("hello there*
         *more string", -- purposely using a continuation line
          99) -- purposely indented less than the asterisk
    :
    bug.1251()
    --}}}
    --{{{  bug 1457
    PROC bug.1457()
      BOOL FUNCTION g(VAL INT b) IS TRUE :
      BOOL bool, b0, b1 :
      SEQ
        b0 := TRUE
        b1 := TRUE
        bool := b0 AND
                 g(77) AND -- this must include a function call
               b1  -- this is purposely indented less than the last line
        check.BOOL(bool, TRUE, "B1457a")
    :
    bug.1457()
    --}}}
    --{{{  bug 1487
    PROC bug.1487()
      INT FUNCTION fracmul(VAL INT x, y) IS x :
      INT FUNCTION refloat(VAL INT x)    IS x :
      INT G :
      INT y :
      SEQ
        G := 88
        -- This is checking an indentation error!
        y := fracmul(fracmul(#33,
                             G),
                 fracmul(#DD, G))
        check(y, #33, "B1487a")
    
        -- This is checking an indentation error!
        y     := (refloat   (fracmul   (fracmul    (G,
                                        fracmul    (G,
                                        fracmul    (G, #9822) +
                                                       #F5C5) +
                                                       #2270),
                                        fracmul    (G,
                                        fracmul    (G,
                                        fracmul    (G, #EBD3) +
                                                       #5F2E) +
                                                       #98BA) +
                                                       #0000)) *
                 G) + G
        check(y, (88*88)+88, "B1487b")
    :
    bug.1487()
    --}}}
    --{{{  bug 1493
    PROC bug.1493()
      [3]BYTE b :
      SEQ
        SEQ
          SEQ
            b := "abc" -- we need a string literal here!
        VAL str IS "*
          *def*
          *" :
        b := str
        check(INT b[1], INT 'e', "B1493a")
    :
    bug.1493()
    --}}}
    --{{{  boolean expressions
    VAL n IS 32 :
    VAL no.of.inputs IS 3 :
    VAL sequences    IS 1 << no.of.inputs :
    
    --{{{  result table
    VAL I IS TRUE :
    VAL O IS FALSE :
    VAL [sequences][n]BOOL bool.result.table IS
    --0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1
    [[O,O,O,O,O,O,O,O,O,O,I,O,O,O,I,I,O,O,O,O,O,O,I,I,O,O,O,O,O,I,I,I],  -- O O O
     [O,O,I,O,O,O,I,I,O,O,O,O,O,O,O,O,O,O,I,O,I,I,I,I,O,O,I,I,I,I,I,I],  -- O O I
     [O,O,O,O,O,O,I,I,O,O,I,O,I,I,I,I,O,O,O,O,O,O,O,O,O,O,I,I,O,I,I,I],  -- O I O
     [O,O,I,O,I,I,I,I,O,O,O,O,O,O,I,I,O,O,I,O,O,O,I,I,I,I,I,I,I,I,I,I],  -- O I I
     [O,O,O,O,O,I,I,I,O,O,I,I,I,I,I,I,O,O,I,I,O,I,I,I,O,O,O,O,O,O,O,O],  -- I O O
     [O,O,I,I,I,I,I,I,O,O,O,O,O,I,I,I,I,I,I,I,I,I,I,I,O,O,I,O,O,O,I,I],  -- I O I
     [O,O,I,I,O,I,I,I,I,I,I,I,I,I,I,I,O,O,O,O,O,I,I,I,O,O,O,O,O,O,I,I],  -- I I O
     [I,I,I,I,I,I,I,I,O,O,I,I,O,I,I,I,O,O,I,I,I,I,I,I,O,O,I,O,I,I,I,I]]: -- I I I
    --}}}
    
    --{{{  evaluate.BOOLs - evaluate lots of different BOOL expressions
    PROC evaluate.BOOLs(VAL BOOL a.in, b.in, c.in, [n]BOOL d)
    
      BOOL a, b, c :
      --{{{  PROC clobber.locals
      PROC clobber.locals()
        -- this is used to prevent an optimiser from commoning up all
        -- accesses to 'a', 'b', and 'c'.
        -- The idea is that this is complicated enough to stop any
        -- knowledge about the values of a, b, and c being preserved.
        a, b, c := id.BOOL(a), id.BOOL(b), id.BOOL(c)
      :
      --}}}
      SEQ
        a, b, c := id.BOOL(a.in), id.BOOL(b.in), id.BOOL(c.in)
        d[00] := (a AND b) AND c
        clobber.locals()
        d[01] := a AND (b AND c)
        clobber.locals()
        d[02] := (a AND b) OR c
        clobber.locals()
        d[03] := a AND (b OR c)
        clobber.locals()
        d[04] := (a OR b) AND c
        clobber.locals()
        d[05] := a OR (b AND c)
        clobber.locals()
        d[06] := (a OR b) OR c
        clobber.locals()
        d[07] := a OR (b OR c)
        clobber.locals()
        d[08] := (a AND b) AND (NOT c)
        clobber.locals()
        d[09] := a AND (b AND (NOT c))
        clobber.locals()
        d[10] := (a AND b) OR (NOT c)
        clobber.locals()
        d[11] := a AND (b OR (NOT c))
        clobber.locals()
        d[12] := (a OR b) AND (NOT c)
        clobber.locals()
        d[13] := a OR (b AND (NOT c))
        clobber.locals()
        d[14] := (a OR b) OR (NOT c)
        clobber.locals()
        d[15] := a OR (b OR (NOT c))
        clobber.locals()
        d[16] := (a AND (NOT b)) AND c
        clobber.locals()
        d[17] := a AND ((NOT b) AND c)
        clobber.locals()
        d[18] := (a AND (NOT b)) OR c
        clobber.locals()
        d[19] := a AND ((NOT b) OR c)
        clobber.locals()
        d[20] := (a OR (NOT b)) AND c
        clobber.locals()
        d[21] := a OR ((NOT b) AND c)
        clobber.locals()
        d[22] := (a OR (NOT b)) OR c
        clobber.locals()
        d[23] := a OR ((NOT b) OR c)
        clobber.locals()
        d[24] := ((NOT a) AND b) AND c
        clobber.locals()
        d[25] := (NOT a) AND (b AND c)
        clobber.locals()
        d[26] := ((NOT a) AND b) OR c
        clobber.locals()
        d[27] := (NOT a) AND (b OR c)
        clobber.locals()
        d[28] := ((NOT a) OR b) AND c
        clobber.locals()
        d[29] := (NOT a) OR (b AND c)
        clobber.locals()
        d[30] := ((NOT a) OR b) OR c
        clobber.locals()
        d[31] := (NOT a) OR (b OR c)
    :
    --}}}
    
    PROC test.BOOL()
      [n]BOOL d :
      -- message format is "BOOL-d[nn]-n"
      [12]BYTE msg :
      VAL j.dig IS 8 :
      VAL j.ten IS 7 :
      VAL i.dig IS (SIZE msg) - 1:
      SEQ
        title("BOOL expressions")
        msg := "BOOL-d[nn]-n"
        SEQ i = 0 FOR sequences
          VAL a IS (i /\ #4) <> 0 :
          VAL b IS (i /\ #2) <> 0 :
          VAL c IS (i /\ #1) <> 0 :
          SEQ
            evaluate.BOOLs(a, b, c, d)
            msg[i.dig] := BYTE (i + (INT '0'))
            SEQ j = 0 FOR n
              SEQ
                msg[j.dig] := BYTE ((j \ 10) + (INT '0'))
                msg[j.ten] := BYTE ((j / 10) + (INT '0'))
                check.BOOL(d[j], bool.result.table[i][j], msg)
    :
    test.BOOL()
    --}}}
    finish ()
:
