| 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_label_break(); |
| 31 |
} |
| 32 |
|
| 33 |
function test_unref_locals(x, y, z) |
| 34 |
{ |
| 35 |
local i; |
| 36 |
local j; |
| 37 |
|
| 38 |
if (x = y) // note that this should get a warning about the assignment |
| 39 |
{ |
| 40 |
if ((x = z) != 0) // should get no warning here |
| 41 |
{ |
| 42 |
local i; |
| 43 |
local j; |
| 44 |
|
| 45 |
/* the outer 'i' should still be unreferenced */ |
| 46 |
i = 1; |
| 47 |
|
| 48 |
/* the inner 'j' should be unreferenced, too */ |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
function factorial(x) |
| 54 |
{ |
| 55 |
if (x > 1) |
| 56 |
return x * factorial(x-1); |
| 57 |
else |
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
function test_for_locals() |
| 62 |
{ |
| 63 |
local x; |
| 64 |
local i = 'outer i'; |
| 65 |
|
| 66 |
for (local i = 1, x = 'bye', local j = 'hello' ; i < 5 ; ++i, x += '!') |
| 67 |
tadsSay('i = ' + i + ', j = ' + j + ', x = ' + x + '\n'); |
| 68 |
|
| 69 |
tadsSay('at outer scope: i = ' + i + '\n'); |
| 70 |
} |
| 71 |
|
| 72 |
function test_break_cont() |
| 73 |
{ |
| 74 |
for (local i = 1 ; i < 10 ; ++i) |
| 75 |
{ |
| 76 |
tadsSay('test break - i = ' + i + '\n'); |
| 77 |
if (i == 5) |
| 78 |
break; |
| 79 |
} |
| 80 |
|
| 81 |
for (local i = 1 ; i < 10 ; ++i) |
| 82 |
{ |
| 83 |
tadsSay('test continue - i = ' + i + '\n'); |
| 84 |
if (i >= 5) |
| 85 |
continue; |
| 86 |
|
| 87 |
tadsSay('...not continuing this time!\n'); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
function test_while() |
| 92 |
{ |
| 93 |
local i; |
| 94 |
|
| 95 |
i = 0; |
| 96 |
while (i < 5) |
| 97 |
{ |
| 98 |
tadsSay('this is while loop iteration #' + i + '\n'); |
| 99 |
++i; |
| 100 |
} |
| 101 |
|
| 102 |
while (i > 0) |
| 103 |
{ |
| 104 |
tadsSay('this is while loop 2 - iteration #' + i + '\n'); |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
while (i > 0) |
| 109 |
{ |
| 110 |
tadsSay('this is while loop 3 - iteration #' + i + '\n'); |
| 111 |
--i; |
| 112 |
if (i <= 2) |
| 113 |
continue; |
| 114 |
--i; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
function test_do_while() |
| 119 |
{ |
| 120 |
local i; |
| 121 |
|
| 122 |
i = 0; |
| 123 |
do |
| 124 |
{ |
| 125 |
tadsSay('this is do loop - iteration #' + i + '\n'); |
| 126 |
} while (i++ < 5); |
| 127 |
|
| 128 |
do |
| 129 |
{ |
| 130 |
tadsSay('this is do loop 2 - iteration #' + i + '\n'); |
| 131 |
if (i > 6) |
| 132 |
break; |
| 133 |
++i; |
| 134 |
} |
| 135 |
while (i < 10); |
| 136 |
|
| 137 |
do |
| 138 |
{ |
| 139 |
tadsSay('this is do loop 3 - iteration #' + i + '\n'); |
| 140 |
--i; |
| 141 |
if (i < 3) |
| 142 |
continue; |
| 143 |
--i; |
| 144 |
} while (i > 0); |
| 145 |
} |
| 146 |
|
| 147 |
function test_switch() |
| 148 |
{ |
| 149 |
local i; |
| 150 |
|
| 151 |
i = 3; |
| 152 |
tadsSay('no breaks - i = ' + i + '\n'); |
| 153 |
switch(i) |
| 154 |
{ |
| 155 |
case 1: |
| 156 |
tadsSay('case 1\n'); |
| 157 |
|
| 158 |
case 2: |
| 159 |
tadsSay('case 2\n'); |
| 160 |
|
| 161 |
case 3: |
| 162 |
tadsSay('case 3\n'); |
| 163 |
|
| 164 |
case 4: |
| 165 |
tadsSay('case 4\n'); |
| 166 |
|
| 167 |
case 5: |
| 168 |
tadsSay('case 5\n'); |
| 169 |
|
| 170 |
default: |
| 171 |
tadsSay('default 1\n'); |
| 172 |
} |
| 173 |
|
| 174 |
i = 2; |
| 175 |
tadsSay('with breaks - i = ' + i + '\n'); |
| 176 |
switch(i) |
| 177 |
{ |
| 178 |
case 1: |
| 179 |
tadsSay('case 1\n'); |
| 180 |
break; |
| 181 |
|
| 182 |
case 2: |
| 183 |
tadsSay('case 2\n'); |
| 184 |
break; |
| 185 |
|
| 186 |
case 3: |
| 187 |
tadsSay('case 3\n'); |
| 188 |
break; |
| 189 |
|
| 190 |
case 4: |
| 191 |
tadsSay('case 4\n'); |
| 192 |
break; |
| 193 |
|
| 194 |
case 5: |
| 195 |
tadsSay('case 5\n'); |
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
function test() |
| 201 |
{ |
| 202 |
/* this loop never terminates... */ |
| 203 |
for (;;) |
| 204 |
{ |
| 205 |
} |
| 206 |
|
| 207 |
/* ...so this statement is unreachable - we should get a warning */ |
| 208 |
test(); |
| 209 |
main(); |
| 210 |
} |
| 211 |
|
| 212 |
function test2() |
| 213 |
{ |
| 214 |
/* |
| 215 |
* This function only returns with a value, because the end of the |
| 216 |
* "for" is unreachable - we should NOT get a warning about |
| 217 |
* returning with and without a value |
| 218 |
*/ |
| 219 |
for (;;) |
| 220 |
{ |
| 221 |
return 1; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
function test3() |
| 226 |
{ |
| 227 |
/* |
| 228 |
* this function implicitly returns both with and without a value - |
| 229 |
* we should get a warning about this |
| 230 |
*/ |
| 231 |
for (local i = 1, local j = 2 ; i < 100 ; ++i) |
| 232 |
{ |
| 233 |
return i; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* labeled break test |
| 239 |
*/ |
| 240 |
function test_label_break() |
| 241 |
{ |
| 242 |
tadsSay('\bLabeled Break Test\b'); |
| 243 |
|
| 244 |
tadsSay('starting stm1:\n'); |
| 245 |
|
| 246 |
stm1: |
| 247 |
{ |
| 248 |
tadsSay('we\'re now in stm1...\n'); |
| 249 |
tadsSay('preparing to break...\n'); |
| 250 |
break stm1; |
| 251 |
|
| 252 |
tadsSay('after the break???\n'); |
| 253 |
} |
| 254 |
|
| 255 |
tadsSay('here we are after stm1\n'); |
| 256 |
|
| 257 |
tadsSay('starting stm2:\n'); |
| 258 |
stm2: |
| 259 |
{ |
| 260 |
for (local i = 1 ; i < 5 ; ++i) |
| 261 |
{ |
| 262 |
tadsSay('\t- loop: i = ' + i + '\n'); |
| 263 |
if (i == 3) |
| 264 |
break stm2; |
| 265 |
} |
| 266 |
|
| 267 |
/* this label is unreferenced - we should get a warning about it */ |
| 268 |
stm3: |
| 269 |
tadsSay('done with loop\n'); |
| 270 |
} |
| 271 |
tadsSay('after stm2\n'); |
| 272 |
} |
| 273 |
|
| 274 |
/* |
| 275 |
* required entrypoint function |
| 276 |
*/ |
| 277 |
function _main(args) |
| 278 |
{ |
| 279 |
tadsSay('... this is _main() ...\n'); |
| 280 |
main(); |
| 281 |
tadsSay('... _main() exiting ...\n'); |
| 282 |
} |