| 1 |
/* |
| 2 |
* Top-level parsing test - input for test_prs_top |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "tads.h" |
| 6 |
|
| 7 |
function main() |
| 8 |
{ |
| 9 |
local i, j; |
| 10 |
local iter; |
| 11 |
|
| 12 |
for (iter = 0 ; iter < 5 ; ++iter) |
| 13 |
{ |
| 14 |
i = 7; |
| 15 |
j = factorial(i); |
| 16 |
|
| 17 |
tadsSay('iter[' + iter + ']: ' + i + '! = ' + j + '\n'); |
| 18 |
} |
| 19 |
|
| 20 |
test_for_locals(); |
| 21 |
|
| 22 |
test_break_cont(); |
| 23 |
|
| 24 |
test_while(); |
| 25 |
|
| 26 |
test_do_while(); |
| 27 |
|
| 28 |
test_switch(); |
| 29 |
|
| 30 |
// test_catch(); |
| 31 |
} |
| 32 |
|
| 33 |
#if 0 |
| 34 |
function test_catch() |
| 35 |
{ |
| 36 |
try |
| 37 |
{ |
| 38 |
local i, j; |
| 39 |
|
| 40 |
tadsSay('calculating 5 + \'x\'...\n'); |
| 41 |
i = 5; |
| 42 |
j = 'x'; |
| 43 |
tadsSay(i + j); |
| 44 |
} |
| 45 |
catch (RuntimeError exc) |
| 46 |
{ |
| 47 |
tadsSay('caught a run-time error!\n'); |
| 48 |
} |
| 49 |
|
| 50 |
try |
| 51 |
{ |
| 52 |
tadsSay('here\'s a "try" with a "finally" block...\n'); |
| 53 |
} |
| 54 |
finally |
| 55 |
{ |
| 56 |
tadsSay('okay, we\'re in the finally!\n'); |
| 57 |
} |
| 58 |
|
| 59 |
try |
| 60 |
{ |
| 61 |
local i, j; |
| 62 |
|
| 63 |
tadsSay('calculating 5 + \'x\' in "try" with "catch" and "finally"...\n'); |
| 64 |
i = 5; |
| 65 |
j = 'x'; |
| 66 |
tadsSay(i + j); |
| 67 |
} |
| 68 |
catch (RuntimeError exc) |
| 69 |
{ |
| 70 |
tadsSay('caught a RuntimeError\n'); |
| 71 |
} |
| 72 |
finally |
| 73 |
{ |
| 74 |
tadsSay('this is the finally block\n'); |
| 75 |
} |
| 76 |
|
| 77 |
tadsSay('done with exception tests\n'); |
| 78 |
} |
| 79 |
#endif |
| 80 |
|
| 81 |
function factorial(x) |
| 82 |
{ |
| 83 |
if (x > 1) |
| 84 |
return x * factorial(x-1); |
| 85 |
else |
| 86 |
return 1; |
| 87 |
} |
| 88 |
|
| 89 |
function test_for_locals() |
| 90 |
{ |
| 91 |
local x; |
| 92 |
local i = 'outer i'; |
| 93 |
|
| 94 |
for (local i = 1, x = 'bye', local j = 'hello' ; i < 5 ; ++i) |
| 95 |
tadsSay('i = ' + i + ', j = ' + j + ', x = ' + x + '\n'); |
| 96 |
|
| 97 |
tadsSay('at outer scope: i = ' + i + '\n'); |
| 98 |
} |
| 99 |
|
| 100 |
function test_break_cont() |
| 101 |
{ |
| 102 |
for (local i = 1 ; i < 10 ; ++i) |
| 103 |
{ |
| 104 |
tadsSay('test break - i = ' + i + '\n'); |
| 105 |
if (i == 5) |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
for (local i = 1 ; i < 10 ; ++i) |
| 110 |
{ |
| 111 |
tadsSay('test continue - i = ' + i + '\n'); |
| 112 |
if (i >= 5) |
| 113 |
continue; |
| 114 |
|
| 115 |
tadsSay('...not continuing this time!\n'); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
function test_while() |
| 120 |
{ |
| 121 |
local i; |
| 122 |
|
| 123 |
i = 0; |
| 124 |
while (i < 5) |
| 125 |
{ |
| 126 |
tadsSay('this is while loop iteration #' + i + '\n'); |
| 127 |
++i; |
| 128 |
} |
| 129 |
|
| 130 |
while (i > 0) |
| 131 |
{ |
| 132 |
tadsSay('this is while loop 2 - iteration #' + i + '\n'); |
| 133 |
break; |
| 134 |
} |
| 135 |
|
| 136 |
while (i > 0) |
| 137 |
{ |
| 138 |
tadsSay('this is while loop 3 - iteration #' + i + '\n'); |
| 139 |
--i; |
| 140 |
if (i <= 2) |
| 141 |
continue; |
| 142 |
--i; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
function test_do_while() |
| 147 |
{ |
| 148 |
local i; |
| 149 |
|
| 150 |
i = 0; |
| 151 |
do |
| 152 |
{ |
| 153 |
tadsSay('this is do loop - iteration #' + i + '\n'); |
| 154 |
} while (i++ < 5); |
| 155 |
|
| 156 |
do |
| 157 |
{ |
| 158 |
tadsSay('this is do loop 2 - iteration #' + i + '\n'); |
| 159 |
if (i > 6) |
| 160 |
break; |
| 161 |
++i; |
| 162 |
} |
| 163 |
while (i < 10); |
| 164 |
|
| 165 |
do |
| 166 |
{ |
| 167 |
tadsSay('this is do loop 3 - iteration #' + i + '\n'); |
| 168 |
--i; |
| 169 |
if (i < 3) |
| 170 |
continue; |
| 171 |
--i; |
| 172 |
} while (i > 0); |
| 173 |
} |
| 174 |
|
| 175 |
function test_switch() |
| 176 |
{ |
| 177 |
local i; |
| 178 |
|
| 179 |
i = 3; |
| 180 |
tadsSay('no breaks - i = ' + i + '\n'); |
| 181 |
switch(i) |
| 182 |
{ |
| 183 |
case 1: |
| 184 |
tadsSay('case 1\n'); |
| 185 |
|
| 186 |
case 2: |
| 187 |
tadsSay('case 2\n'); |
| 188 |
|
| 189 |
case 3: |
| 190 |
tadsSay('case 3\n'); |
| 191 |
|
| 192 |
case 4: |
| 193 |
tadsSay('case 4\n'); |
| 194 |
|
| 195 |
case 5: |
| 196 |
tadsSay('case 5\n'); |
| 197 |
|
| 198 |
default: |
| 199 |
tadsSay('default 1\n'); |
| 200 |
} |
| 201 |
|
| 202 |
i = 2; |
| 203 |
tadsSay('with breaks - i = ' + i + '\n'); |
| 204 |
switch(i) |
| 205 |
{ |
| 206 |
case 1: |
| 207 |
tadsSay('case 1\n'); |
| 208 |
break; |
| 209 |
|
| 210 |
case 2: |
| 211 |
tadsSay('case 2\n'); |
| 212 |
break; |
| 213 |
|
| 214 |
case 3: |
| 215 |
tadsSay('case 3\n'); |
| 216 |
break; |
| 217 |
|
| 218 |
case 4: |
| 219 |
tadsSay('case 4\n'); |
| 220 |
break; |
| 221 |
|
| 222 |
case 5: |
| 223 |
tadsSay('case 5\n'); |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
function test() |
| 229 |
{ |
| 230 |
/* this loop never terminates... */ |
| 231 |
for (;;) |
| 232 |
{ |
| 233 |
} |
| 234 |
|
| 235 |
/* ...so this statement is unreachable - we should get a warning */ |
| 236 |
test(); |
| 237 |
main(); |
| 238 |
} |
| 239 |
|
| 240 |
function test2() |
| 241 |
{ |
| 242 |
/* |
| 243 |
* This function only returns with a value, because the end of the |
| 244 |
* "for" is unreachable - we should NOT get a warning about |
| 245 |
* returning with and without a value |
| 246 |
*/ |
| 247 |
for (;;) |
| 248 |
{ |
| 249 |
return 1; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
function test2() |
| 254 |
{ |
| 255 |
/* |
| 256 |
* this function implicitly returns both with and without a value - |
| 257 |
* we should get a warning about this |
| 258 |
*/ |
| 259 |
for (local i = 1, local j = 2 ; i < 100 ; ++i) |
| 260 |
{ |
| 261 |
return i; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* required entrypoint function |
| 267 |
*/ |
| 268 |
function _main(args) |
| 269 |
{ |
| 270 |
tadsSay('... this is _main() ...\n'); |
| 271 |
main(); |
| 272 |
tadsSay('... _main() exiting ...\n'); |
| 273 |
} |