(* Example cool program testing as many aspects of the code generator as possible. *) class A inherits IO { (* Simple method that returns the second parameter *) method1( p1 : Int, p2: String ) : Object { { out_string(p2); out_string("\n"); out_int(p1); out_string("\n"); }}; }; class B inherits A { method1( p1 : Int, p2: String ) : Object { { out_int(p1); out_string("\n"); out_string(p2); out_string("\n"); }}; }; Class C { x : Int; y : String <- "hello"; z : Bool <- false; getSelf() : SELF_TYPE { self }; multiplyX( a : Int ) : Int { x * a }; getInt() : Int { x }; setInt( newX : Int ) : Int { x <- newX }; getString() : String { y }; getBool() : Bool { z }; }; class C2 inherits C { (* inherited attributes from C *) (* new attribute *) w : C; (* overridden method *) getInt() : Int { x+1 }; (* compute the factorial of a *) factorial( a : Int ) : Int { if a < 1 then 1 else a * factorial( a - 1 ) fi }; (* test more parameters *) multiply3( a : Int, b : Int, c : Int ) : Int { a * b * c }; }; Class Main inherits IO { cvar : C; c2var : C2; i : Int; j : Int; id : B <- new B; main(): Object { { cvar <- new C; c2var <- new C2; cvar.setInt( 1 ); c2var.setInt( 2 ); out_string( cvar.getString() ); out_string("\n"); out_int( cvar.getInt() ); out_string("\n"); out_int( c2var.getInt() ); out_string("\n"); if cvar.getBool() then out_string("true\n") else out_string("false\n") fi; out_string("factorial(4): "); out_int( c2var.factorial( 4 ) ); out_string("\n"); (* Effectively squares the number *) out_int( cvar.multiplyX( cvar.getInt() ) ); out_string("\n"); out_int( cvar.multiplyX( c2var.getInt() ) ); out_string("\n"); i <- 0; j <- 0; while i < 5 loop { out_string("x"); i <- i + 1; while j < 3 loop { out_string("y"); j <- j + 1; } pool; } pool; i <- 5; j <- 13; out_string("\n"); out_int((i*j)/6); out_string("\n"); out_int((i/j)-6); out_string("\n"); out_int(i + (j+j)); out_string("\n"); if 13 < i then out_int((3*4)/6) else if true then out_int(3+i -1) else out_string("You lose!") fi fi; out_string("\n"); if (not false) then out_int(j -1) else out_string("You lose!") fi; out_string("\n"); out_int(2+~1); out_string("\n"); if i<=j then out_int(3+3 -1) else out_string("You lose!") fi; out_string("\n"); id@B.method1( 1, "hello" ); id@A.method1( 5, "Mama" ); --TEST LETS ( let w : Int <- i in { w <- w + 5; out_int(w*i); out_string("\n"); } ); ( let a : Bool <- tRUE in ( let b:Int<-5, c: C <- new C in { c.setInt( b ); if a then { out_string("True\n"); out_int( c.getInt() ); out_string("\n"); } else { out_string("False\n"); out_int( c.getInt() ); out_string("\n"); } fi; } ) ); ( let w : Int <- 3 in { w <- w + 5; out_int(w*j); out_string("\n"); } ); } }; };