-- -- Copyright (c) 2010 Jiri Svoboda -- All rights reserved. -- -- Redistribution and use in source and binary forms, with or without -- modification, are permitted provided that the following conditions -- are met: -- -- o Redistributions of source code must retain the above copyright -- notice, this list of conditions and the following disclaimer. -- o Redistributions in binary form must reproduce the above copyright -- notice, this list of conditions and the following disclaimer in the -- documentation and/or other materials provided with the distribution. -- o The name of the author may not be used to endorse or promote products -- derived from this software without specific prior written permission. -- -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- class ArithmeticDemo is fun Main(), static is -- Test addition, multiplication and precedence. Console.Write("2*2 + 2*2 = "); Console.Write(2*2 + 2*2); Console.WriteLine(" (expected: 8)"); -- Test subtraction, multiplication and precedence. Console.Write("1111 - 1 - 10 - 10*10 - 10*10*10 = "); Console.Write(1111 - 1 - 10 - 10*10 - 10*10*10); Console.WriteLine(" (expected: 0)"); -- Test parenthesized sub-expressions. Console.Write("10 * (1 - 1) = "); Console.Write(10 * (1 - 1)); Console.WriteLine(" (expected: 0)"); -- Test unary plus and minus. Console.Write("(+1) - (-1) - (+(+1)) + (+(-1)) = "); Console.Write((+1) - (-1) - (+(+1)) + (+(-1))); Console.WriteLine(" (expected: 0)"); -- Test signed multiplication. Console.Write("+1 * +1 = "); Console.Write(+1 * +1); Console.WriteLine(" (expected: 1)"); Console.Write("-1 * -1 = "); Console.Write(-1 * -1); Console.WriteLine(" (expected: 1)"); Console.Write("+1 * -1 = "); Console.Write(+1 * -1); Console.WriteLine(" (expected: -1)"); Console.Write("-1 * +1 = "); Console.Write(-1 * +1); Console.WriteLine(" (expected: -1)"); -- Test multiplication with large result. Console.Write("1000000 * 1000000 * 1000000 * 1000000 = "); Console.Write(1000000 * 1000000 * 1000000 * 1000000); Console.WriteLine(" (expected: 1000000000000000000000000)"); -- Test large literals. Console.Write("1000000000000000000000000 = "); Console.Write(1000000000000000000000000); Console.WriteLine(" (expected: 1000000000000000000000000)"); -- Test large factorials. var n : int; Console.WriteLine("Factorials:"); n = 1; while n <= 40 do Console.WriteLine(Factorial(n)); n = n + 1; end end -- Return factorial of n. fun Factorial(n : int) : int, static is var i : int; var val : int; i = 1; val = 1; while i <= n do val = val * i; i = i + 1; end return val; end end