| 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 | 
|---|
| 30 | fun Main(), static is | 
|---|
| 31 | -- Test addition, multiplication and precedence. | 
|---|
| 32 | Console.Write("2*2 + 2*2 = "); | 
|---|
| 33 | Console.Write(2*2 + 2*2); | 
|---|
| 34 | Console.WriteLine(" (expected: 8)"); | 
|---|
| 35 |  | 
|---|
| 36 | -- Test subtraction, multiplication and precedence. | 
|---|
| 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)"); | 
|---|
| 40 |  | 
|---|
| 41 | -- Test parenthesized sub-expressions. | 
|---|
| 42 | Console.Write("10 * (1 - 1) = "); | 
|---|
| 43 | Console.Write(10 * (1 - 1)); | 
|---|
| 44 | Console.WriteLine(" (expected: 0)"); | 
|---|
| 45 |  | 
|---|
| 46 | -- Test unary plus and minus. | 
|---|
| 47 | Console.Write("(+1) - (-1) - (+(+1)) + (+(-1)) = "); | 
|---|
| 48 | Console.Write((+1) - (-1) - (+(+1)) + (+(-1))); | 
|---|
| 49 | Console.WriteLine(" (expected: 0)"); | 
|---|
| 50 |  | 
|---|
| 51 | -- Test signed multiplication. | 
|---|
| 52 | Console.Write("+1 * +1 = "); | 
|---|
| 53 | Console.Write(+1 * +1); | 
|---|
| 54 | Console.WriteLine(" (expected: 1)"); | 
|---|
| 55 |  | 
|---|
| 56 | Console.Write("-1 * -1 = "); | 
|---|
| 57 | Console.Write(-1 * -1); | 
|---|
| 58 | Console.WriteLine(" (expected: 1)"); | 
|---|
| 59 |  | 
|---|
| 60 | Console.Write("+1 * -1 = "); | 
|---|
| 61 | Console.Write(+1 * -1); | 
|---|
| 62 | Console.WriteLine(" (expected: -1)"); | 
|---|
| 63 |  | 
|---|
| 64 | Console.Write("-1 * +1 = "); | 
|---|
| 65 | Console.Write(-1 * +1); | 
|---|
| 66 | Console.WriteLine(" (expected: -1)"); | 
|---|
| 67 |  | 
|---|
| 68 | -- Test multiplication with large result. | 
|---|
| 69 | Console.Write("1000000 * 1000000 * 1000000 * 1000000 = "); | 
|---|
| 70 | Console.Write(1000000 * 1000000 * 1000000 * 1000000); | 
|---|
| 71 | Console.WriteLine(" (expected: 1000000000000000000000000)"); | 
|---|
| 72 |  | 
|---|
| 73 | -- Test large literals. | 
|---|
| 74 | Console.Write("1000000000000000000000000 = "); | 
|---|
| 75 | Console.Write(1000000000000000000000000); | 
|---|
| 76 | Console.WriteLine(" (expected: 1000000000000000000000000)"); | 
|---|
| 77 |  | 
|---|
| 78 | -- Test large factorials. | 
|---|
| 79 | var n : int; | 
|---|
| 80 |  | 
|---|
| 81 | Console.WriteLine("Factorials:"); | 
|---|
| 82 | n = 1; | 
|---|
| 83 | while n <= 40 do | 
|---|
| 84 | Console.WriteLine(Factorial(n)); | 
|---|
| 85 | n = n + 1; | 
|---|
| 86 | end | 
|---|
| 87 | end | 
|---|
| 88 |  | 
|---|
| 89 | -- Return factorial of n. | 
|---|
| 90 | fun Factorial(n : int) : int, static is | 
|---|
| 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 | 
|---|