package whilelang; object Programs { /** A program consists of a name and the main statement. */ type Program = Pair[String,Statement] /** Does nothing. */ val skip: Program = ("Skip", Skip) /** Prints out n^2 for n = 1...10. */ val squares: Program = ("Squares", Block( Assign("i", IntLiteral(0)) :: Assign("j", IntLiteral(1)) :: While(LessThan(Var("i"), IntLiteral(10)), Block( Print("", "j") :: Assign("i", Plus(Var("i"), IntLiteral(1))) :: Assign("j", Plus(Var("j"), Plus(Times(IntLiteral(2), Var("i")), IntLiteral(1)))) :: Nil)) :: Nil)) /** Prints out the Collatz sequence starting from 13 */ val collatz: Program = ("Collatz", Block( Assign("x", IntLiteral(13)) :: While(GreaterThan(Var("x"), IntLiteral(1)), Block( Print("x=", "x") :: IfThenElse(Equals(Modulo(Var("x"), IntLiteral(2)), IntLiteral(0)), Assign("x", Division(Var("x"), IntLiteral(2))), Assign("x", Plus(Times(IntLiteral(3), Var("x")), IntLiteral(1)))) :: Nil )) :: Nil ) ) val programs: List[Program] = // skip :: squares :: collatz :: Nil }