[23de644] | 1 | --
|
---|
| 2 | -- Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | -- All rights reserved.
|
---|
| 4 | --
|
---|
| 5 | -- Redistribution and use in source and binary forms, with or without
|
---|
| 6 | -- modification, are permitted provided that the following conditions
|
---|
| 7 | -- are met:
|
---|
| 8 | --
|
---|
| 9 | -- o Redistributions of source code must retain the above copyright
|
---|
| 10 | -- notice, this list of conditions and the following disclaimer.
|
---|
| 11 | -- o Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | -- notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | -- documentation and/or other materials provided with the distribution.
|
---|
| 14 | -- o The name of the author may not be used to endorse or promote products
|
---|
| 15 | -- derived from this software without specific prior written permission.
|
---|
| 16 | --
|
---|
| 17 | -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | -- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | -- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | -- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | --
|
---|
| 28 |
|
---|
| 29 | class ArithmeticDemo is
|
---|
[c5cb943d] | 30 | fun Main(), static is
|
---|
[23de644] | 31 | -- Test addition, multiplication and precedence.
|
---|
[051b3db8] | 32 | Console.Write("2*2 + 2*2 = ");
|
---|
| 33 | Console.Write(2*2 + 2*2);
|
---|
| 34 | Console.WriteLine(" (expected: 8)");
|
---|
[23de644] | 35 |
|
---|
| 36 | -- Test subtraction, multiplication and precedence.
|
---|
[051b3db8] | 37 | Console.Write("1111 - 1 - 10 - 10*10 - 10*10*10 = ");
|
---|
| 38 | Console.Write(1111 - 1 - 10 - 10*10 - 10*10*10);
|
---|
| 39 | Console.WriteLine(" (expected: 0)");
|
---|
[23de644] | 40 |
|
---|
| 41 | -- Test parenthesized sub-expressions.
|
---|
[051b3db8] | 42 | Console.Write("10 * (1 - 1) = ");
|
---|
| 43 | Console.Write(10 * (1 - 1));
|
---|
| 44 | Console.WriteLine(" (expected: 0)");
|
---|
[23de644] | 45 |
|
---|
| 46 | -- Test unary plus and minus.
|
---|
[051b3db8] | 47 | Console.Write("(+1) - (-1) - (+(+1)) + (+(-1)) = ");
|
---|
| 48 | Console.Write((+1) - (-1) - (+(+1)) + (+(-1)));
|
---|
| 49 | Console.WriteLine(" (expected: 0)");
|
---|
[23de644] | 50 |
|
---|
| 51 | -- Test signed multiplication.
|
---|
[051b3db8] | 52 | Console.Write("+1 * +1 = ");
|
---|
| 53 | Console.Write(+1 * +1);
|
---|
| 54 | Console.WriteLine(" (expected: 1)");
|
---|
[38aaacc2] | 55 |
|
---|
[051b3db8] | 56 | Console.Write("-1 * -1 = ");
|
---|
| 57 | Console.Write(-1 * -1);
|
---|
| 58 | Console.WriteLine(" (expected: 1)");
|
---|
[38aaacc2] | 59 |
|
---|
[051b3db8] | 60 | Console.Write("+1 * -1 = ");
|
---|
| 61 | Console.Write(+1 * -1);
|
---|
| 62 | Console.WriteLine(" (expected: -1)");
|
---|
[38aaacc2] | 63 |
|
---|
[051b3db8] | 64 | Console.Write("-1 * +1 = ");
|
---|
| 65 | Console.Write(-1 * +1);
|
---|
| 66 | Console.WriteLine(" (expected: -1)");
|
---|
[23de644] | 67 |
|
---|
| 68 | -- Test multiplication with large result.
|
---|
[051b3db8] | 69 | Console.Write("1000000 * 1000000 * 1000000 * 1000000 = ");
|
---|
| 70 | Console.Write(1000000 * 1000000 * 1000000 * 1000000);
|
---|
| 71 | Console.WriteLine(" (expected: 1000000000000000000000000)");
|
---|
[23de644] | 72 |
|
---|
| 73 | -- Test large literals.
|
---|
[051b3db8] | 74 | Console.Write("1000000000000000000000000 = ");
|
---|
| 75 | Console.Write(1000000000000000000000000);
|
---|
| 76 | Console.WriteLine(" (expected: 1000000000000000000000000)");
|
---|
[23de644] | 77 |
|
---|
| 78 | -- Test large factorials.
|
---|
| 79 | var n : int;
|
---|
| 80 |
|
---|
[051b3db8] | 81 | Console.WriteLine("Factorials:");
|
---|
[23de644] | 82 | n = 1;
|
---|
| 83 | while n <= 40 do
|
---|
[051b3db8] | 84 | Console.WriteLine(Factorial(n));
|
---|
[23de644] | 85 | n = n + 1;
|
---|
| 86 | end
|
---|
| 87 | end
|
---|
| 88 |
|
---|
| 89 | -- Return factorial of n.
|
---|
[c5cb943d] | 90 | fun Factorial(n : int) : int, static is
|
---|
[23de644] | 91 | var i : int;
|
---|
| 92 | var val : int;
|
---|
| 93 |
|
---|
| 94 | i = 1;
|
---|
| 95 | val = 1;
|
---|
| 96 |
|
---|
| 97 | while i <= n do
|
---|
| 98 | val = val * i;
|
---|
| 99 | i = i + 1;
|
---|
| 100 | end
|
---|
| 101 |
|
---|
| 102 | return val;
|
---|
| 103 | end
|
---|
| 104 | end
|
---|