(* Aaron Brown and Bradley Durandetta example.cl Example cool program testing as many aspects of the code generator as possible. *) class A inherits IO { one : Int; two : Int <- 2; three : String; four : Bool <- true; set_one(x : Int) : Int { one <- x }; set_three(x : String) : String { three <- x }; get_one() : Int { one }; get_two() : Int { two }; get_three() : String { three }; get_four() : Bool { four }; add_to_one(x : Int) : Int { one <- one + x }; display_three() : SELF_TYPE { { out_string(three); out_string("\n"); } }; self_type_test() : SELF_TYPE { new SELF_TYPE }; }; class B inherits A { five : String <- "testing "; six : Bool; set_six(x : Bool) : Bool { six <- x }; get_five() : String { five }; get_six() : Bool { six }; five_and_three() : String { five.concat(three) }; add_to_one(x : Int) : Int { one <- one + two + x }; }; class Main inherits IO { a : A <- new A; b : B; c : String; d : Int; a_copy : A; e : A; f : A; g : B; add_2(x : Int) : Int { x + 2 }; main() : Int { { b <- new B; out_string("\nTest integer initialization and dispatch\n"); out_string("Expected output: 2\n"); out_int(b.get_two()); out_string("\n\n"); out_string("Test default integer initialization\n"); out_string("Expected output: 0\n"); out_int(a.get_one()); out_string("\n\n"); out_string("Test default bool initialization, if statement, bool equality\n"); out_string("Expected output: false, good\n"); if(b.get_six() = false) then out_string("false") else out_string("true") fi; if(b.get_six() = true) then out_string(", bad\n") else out_string(", good\n") fi; out_string("\n\n"); out_string("Test default string initialization\n"); out_string("Expected output: \n"); out_string(a.get_three()); out_string("\n\n"); out_string("Test method call and basic math\n"); out_string("Expected output: 3\n"); a.add_to_one(3); out_int(a.get_one()); out_string("\n\n"); out_string("Test overridden method call and basic math\n"); out_string("Expected output: 14\n"); b.set_one(8); out_int(b.add_to_one(4)); out_string("\n\n"); out_string("Test inherited method call and string concatenation\n"); a.set_three("three_in_a"); b.set_three("three_in_b"); out_string("Expected output: three_in_a\n"); a.display_three(); out_string("\nExpected output: three_in_b\n"); b.display_three(); out_string("\nExpected output: testing three_in_b\n"); out_string(b.five_and_three()); out_string("\n\n"); out_string("Test string input, length, and substring\n"); out_string("Input a string: "); c <- "hello there"; out_string("Expected output: \n"); out_string(c); out_string(", string length: "); out_int(c.length()); out_string(", first letter is: "); out_string(c.substr(0,1)); out_string("\n\n"); out_string("Test integer input\n"); out_string("Input an integer: "); d <- 3445; out_string("Expected output: \n"); out_int(d); out_string("\n\n"); out_string("Test object type name\n"); out_string("Expected output: B\n"); out_string(b.type_name()); out_string("\n\n"); out_string("Test object copy\n"); out_string("Expected output: three_in_a\n"); a_copy <- a.copy(); out_string(a_copy.get_three()); out_string("\n\n"); out_string("Test string equality\n"); out_string("Expected output: correct, correct\n"); c <- "Aaron loves monkeys"; if( c = "Aaron loves monkeys" ) then out_string("correct") else out_string("incorrect") fi; if( c = "Aaron does not love monkeys" ) then out_string(", incorrect") else out_string(", correct") fi; out_string("\n\n"); out_string("Test integer equality\n"); out_string("Expected output: correct, correct\n"); d <- 5; if( d = 5 ) then out_string("correct") else out_string("incorrect") fi; if( d = 2 ) then out_string(", incorrect") else out_string(", correct") fi; out_string("\n\n"); out_string("Test integer less_than\n"); out_string("Expected output: correct, correct\n"); d <- 5; if( d < 6 ) then out_string("correct") else out_string("incorrect") fi; if( d < 2 ) then out_string(", incorrect") else out_string(", correct") fi; out_string("\n\n"); out_string("Test integer less_than_or_equal_to\n"); out_string("Expected output: correct, correct, correct\n"); d <- 5; if( d <= 5 ) then out_string("correct") else out_string("incorrect") fi; if( d <= 7 ) then out_string(", correct") else out_string(", incorrect") fi; if( d <= 2 ) then out_string(", incorrect") else out_string(", correct") fi; out_string("\n\n"); out_string("Test case statement\n"); e <- new B; out_string("Expected output: it's a B\n"); case(e) of first : A => first.set_three("it's an A"); second : B => second.set_three("it's a B"); esac; out_string(e.get_three()); out_string("\n\n"); out_string("Test while loop and integer comparison\n"); d <- 0; while( d <= 3 ) loop d <- d + 1 pool; out_string("Expected output: 4\n"); out_int(d); out_string("\n\n"); out_string("Test let statement\n"); out_string("Expected output: right\n"); let a : String <- "wrong", c : String <- "incorrect", a : String <- "right" in let c : String <- "wrong" in if(b.get_two() = 2) then out_string(a) else out_string(c) fi; out_string("\n\n"); out_string("Test new SELF_TYPE\n"); out_string("Expected output: right\n"); f <- e.self_type_test(); case(f) of first : A => out_string("wrong"); second : B => out_string("right"); esac; out_string("\n\n"); out_string("Test short-hand dispatch\n"); out_string("Expected output: 9\n"); out_int(add_2(7)); out_string("\n\n"); out_string("Test isvoid\n"); out_string("Expected output: perfect, perfect\n"); if( isvoid g ) then out_string("perfect") else out_string("imperfect") fi; g <- new B; if( isvoid g ) then out_string(", imperfect") else out_string(", perfect") fi; out_string("\n\n"); out_string("Test static disptach\n"); out_string("Expected output: 12\n"); g.set_one(5); out_int(g@A.add_to_one(7)); out_string("\n\n"); 0; } }; };