| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/test/TEST_PRS.CPP,v 1.4 1999/07/11 00:47:03 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | test_prs.cpp - parser test |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 05/01/99 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | #include "os.h" |
| 27 | #include "t3std.h" |
| 28 | #include "tctok.h" |
| 29 | #include "resload.h" |
| 30 | #include "tcmain.h" |
| 31 | #include "tchostsi.h" |
| 32 | #include "tcglob.h" |
| 33 | #include "tcprs.h" |
| 34 | #include "tctarg.h" |
| 35 | #include "vmfile.h" |
| 36 | #include "tcunas.h" |
| 37 | #include "tct3unas.h" |
| 38 | #include "tcmake.h" |
| 39 | #include "vmimage.h" |
| 40 | #include "vmrunsym.h" |
| 41 | #include "vmmeta.h" |
| 42 | #include "t3test.h" |
| 43 | |
| 44 | |
| 45 | static void errexit(const char *msg) |
| 46 | { |
| 47 | printf("%s\n", msg); |
| 48 | exit(1); |
| 49 | } |
| 50 | |
| 51 | static void show_const(int level, CTcPrsNode *result) |
| 52 | { |
| 53 | CTPNListEle *ele; |
| 54 | int i; |
| 55 | |
| 56 | for (i = level*3 ; i != 0 ; --i) |
| 57 | printf(" "); |
| 58 | |
| 59 | if (result == 0) |
| 60 | { |
| 61 | printf("no value\n"); |
| 62 | } |
| 63 | else if (result->is_const()) |
| 64 | { |
| 65 | switch(result->get_const_val()->get_type()) |
| 66 | { |
| 67 | case TC_CVT_UNK: |
| 68 | printf("unknown\n"); |
| 69 | break; |
| 70 | |
| 71 | case TC_CVT_NIL: |
| 72 | printf("nil\n"); |
| 73 | break; |
| 74 | |
| 75 | case TC_CVT_TRUE: |
| 76 | printf("true\n"); |
| 77 | break; |
| 78 | |
| 79 | case TC_CVT_INT: |
| 80 | printf("int = %ld\n", |
| 81 | result->get_const_val()->get_val_int()); |
| 82 | break; |
| 83 | |
| 84 | case TC_CVT_SSTR: |
| 85 | printf("sstr = %.*s\n", |
| 86 | (int)result->get_const_val()->get_val_str_len(), |
| 87 | result->get_const_val()->get_val_str()); |
| 88 | break; |
| 89 | |
| 90 | case TC_CVT_LIST: |
| 91 | printf("list: %d elements\n", |
| 92 | result->get_const_val()->get_val_list() |
| 93 | ->get_count()); |
| 94 | for (ele = result->get_const_val()->get_val_list()->get_head() ; |
| 95 | ele != 0 ; ele = ele->get_next()) |
| 96 | show_const(level + 1, ele->get_expr()); |
| 97 | break; |
| 98 | |
| 99 | case TC_CVT_OBJ: |
| 100 | printf("object: %ld\n", result->get_const_val()->get_val_obj()); |
| 101 | break; |
| 102 | |
| 103 | case TC_CVT_PROP: |
| 104 | printf("property id: %d\n", |
| 105 | result->get_const_val()->get_val_prop()); |
| 106 | break; |
| 107 | |
| 108 | case TC_CVT_FUNCPTR: |
| 109 | printf("function pointer: %.*s\n", |
| 110 | (int)result->get_const_val()->get_val_funcptr_sym() |
| 111 | ->get_sym_len(), |
| 112 | result->get_const_val()->get_val_funcptr_sym() |
| 113 | ->get_sym()); |
| 114 | break; |
| 115 | |
| 116 | case TC_CVT_ANONFUNCPTR: |
| 117 | printf("anonymous function pointer\n"); |
| 118 | break; |
| 119 | |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | else |
| 125 | printf("Non-constant value\n"); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * node list entry |
| 130 | */ |
| 131 | struct node_entry |
| 132 | { |
| 133 | node_entry(CTcPrsNode *n, CTcTokFileDesc *d, long l) |
| 134 | { |
| 135 | node = n; |
| 136 | desc = d; |
| 137 | linenum = l; |
| 138 | } |
| 139 | |
| 140 | /* top-level parse node for this line */ |
| 141 | CTcPrsNode *node; |
| 142 | |
| 143 | /* file and line of source where node started */ |
| 144 | CTcTokFileDesc *desc; |
| 145 | long linenum; |
| 146 | |
| 147 | /* next entry in list */ |
| 148 | node_entry *nxt; |
| 149 | }; |
| 150 | |
| 151 | int main(int argc, char **argv) |
| 152 | { |
| 153 | CResLoader *res_loader; |
| 154 | CTcHostIfc *hostifc; |
| 155 | int curarg; |
| 156 | int fatal_error_count = 0; |
| 157 | node_entry *node_head = 0; |
| 158 | node_entry *node_tail = 0; |
| 159 | osfildef *fpout = 0; |
| 160 | CVmFile *imgfile = 0; |
| 161 | ulong next_obj_id = 1; |
| 162 | int next_local = 0; |
| 163 | CTcTokFileDesc *desc; |
| 164 | long linenum; |
| 165 | CTcUnasSrcCodeStr *unas_in; |
| 166 | CTcUnasOutStdio unas_out; |
| 167 | char pathbuf[OSFNMAX]; |
| 168 | |
| 169 | /* initialize for testing */ |
| 170 | test_init(); |
| 171 | |
| 172 | /* create the host interface object */ |
| 173 | hostifc = new CTcHostIfcStdio(); |
| 174 | |
| 175 | /* create a resource loader */ |
| 176 | os_get_special_path(pathbuf, sizeof(pathbuf), argv[0], OS_GSP_T3_RES); |
| 177 | res_loader = new CResLoader(pathbuf); |
| 178 | |
| 179 | /* initialize the compiler */ |
| 180 | CTcMain::init(hostifc, res_loader, 0); |
| 181 | |
| 182 | /* create the disassembler input stream */ |
| 183 | unas_in = new CTcUnasSrcCodeStr(G_cs); |
| 184 | |
| 185 | err_try |
| 186 | { |
| 187 | /* scan -I arguments */ |
| 188 | for (curarg = 1 ; curarg < argc ; ++curarg) |
| 189 | { |
| 190 | char *p; |
| 191 | |
| 192 | /* get the argument string for easy reference */ |
| 193 | p = argv[curarg]; |
| 194 | |
| 195 | /* if it's not an option, we're done */ |
| 196 | if (*p != '-') |
| 197 | break; |
| 198 | |
| 199 | /* if it's a -I argument, use it */ |
| 200 | if (*(p + 1) == 'I') |
| 201 | { |
| 202 | char *path; |
| 203 | |
| 204 | /* |
| 205 | * if it's with this argument, read it, otherwise move |
| 206 | * on to the next argument |
| 207 | */ |
| 208 | if (*(p + 2) == '\0') |
| 209 | path = argv[++curarg]; |
| 210 | else |
| 211 | path = p + 2; |
| 212 | |
| 213 | /* add the directory to the include path list */ |
| 214 | G_tok->add_inc_path(path); |
| 215 | } |
| 216 | else if (*(p + 1) == 'v') |
| 217 | { |
| 218 | /* set verbose mode */ |
| 219 | G_tcmain->set_verbosity(TRUE); |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | /* |
| 224 | * invalid usage - consume all the arguments and fall |
| 225 | * through to the usage checker |
| 226 | */ |
| 227 | curarg = argc; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* check arguments */ |
| 233 | if (curarg + 2 != argc) |
| 234 | { |
| 235 | /* terminate the compiler */ |
| 236 | CTcMain::terminate(); |
| 237 | |
| 238 | /* delete our objects */ |
| 239 | delete res_loader; |
| 240 | |
| 241 | /* exit with an error */ |
| 242 | errexit("usage: test_prs [options] <source-file> <image-file>\n" |
| 243 | "options:\n" |
| 244 | " -Idir - add dir to include path\n" |
| 245 | " -v - verbose error messages"); |
| 246 | } |
| 247 | |
| 248 | /* set up the tokenizer with the main input file */ |
| 249 | if (G_tok->set_source(argv[curarg], argv[curarg])) |
| 250 | errexit("unable to open source file"); |
| 251 | |
| 252 | /* set up an output file */ |
| 253 | fpout = osfopwb(argv[curarg+1], OSFTT3IMG); |
| 254 | if (fpout == 0) |
| 255 | errexit("unable to open image file"); |
| 256 | imgfile = new CVmFile(); |
| 257 | imgfile->set_file(fpout, 0); |
| 258 | |
| 259 | /* read the first token */ |
| 260 | G_tok->next(); |
| 261 | |
| 262 | /* parse expressions */ |
| 263 | for (;;) |
| 264 | { |
| 265 | CTcPrsNode *result; |
| 266 | CTcSymbol *entry; |
| 267 | |
| 268 | /* if we're at end of file, we're done */ |
| 269 | if (G_tok->getcur()->gettyp() == TOKT_EOF) |
| 270 | break; |
| 271 | |
| 272 | /* check for our fake declarations */ |
| 273 | switch(G_tok->getcur()->gettyp()) |
| 274 | { |
| 275 | case TOKT_OBJECT: |
| 276 | /* add an object symbol */ |
| 277 | G_tok->next(); |
| 278 | entry = new CTcSymObj(G_tok->getcur()->get_text(), |
| 279 | G_tok->getcur()->get_text_len(), |
| 280 | FALSE, next_obj_id++, FALSE, |
| 281 | TC_META_TADSOBJ, 0); |
| 282 | G_prs->get_global_symtab()->add_entry(entry); |
| 283 | |
| 284 | /* skip the object name */ |
| 285 | G_tok->next(); |
| 286 | break; |
| 287 | |
| 288 | case TOKT_FUNCTION: |
| 289 | /* add a function symbol */ |
| 290 | G_tok->next(); |
| 291 | entry = new CTcSymFunc(G_tok->getcur()->get_text(), |
| 292 | G_tok->getcur()->get_text_len(), |
| 293 | FALSE, 0, 0, FALSE, TRUE, |
| 294 | FALSE, FALSE, FALSE, TRUE); |
| 295 | G_prs->get_global_symtab()->add_entry(entry); |
| 296 | |
| 297 | /* skip the function name */ |
| 298 | G_tok->next(); |
| 299 | break; |
| 300 | |
| 301 | case TOKT_LOCAL: |
| 302 | /* add a local variable symbol */ |
| 303 | G_tok->next(); |
| 304 | entry = new CTcSymLocal(G_tok->getcur()->get_text(), |
| 305 | G_tok->getcur()->get_text_len(), |
| 306 | FALSE, FALSE, next_local++); |
| 307 | G_prs->get_global_symtab()->add_entry(entry); |
| 308 | |
| 309 | /* skip the function name */ |
| 310 | G_tok->next(); |
| 311 | break; |
| 312 | |
| 313 | default: |
| 314 | /* note the starting line */ |
| 315 | desc = G_tok->get_last_desc(); |
| 316 | linenum = G_tok->get_last_linenum(); |
| 317 | |
| 318 | /* parse an expression */ |
| 319 | result = G_prs->parse_expr(); |
| 320 | |
| 321 | /* add it to our list */ |
| 322 | if (result != 0) |
| 323 | { |
| 324 | node_entry *cur; |
| 325 | |
| 326 | /* create a new list entry */ |
| 327 | cur = new node_entry(result, desc, linenum); |
| 328 | |
| 329 | /* link it at the end of our list */ |
| 330 | cur->nxt = 0; |
| 331 | if (node_tail != 0) |
| 332 | node_tail->nxt = cur; |
| 333 | else |
| 334 | node_head = cur; |
| 335 | node_tail = cur; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* parse a semicolon */ |
| 340 | if (G_prs->parse_req_sem()) |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * if there were no parse errors, run through our node list and |
| 346 | * generate code |
| 347 | */ |
| 348 | if (G_tcmain->get_error_count() == 0) |
| 349 | { |
| 350 | /* |
| 351 | * loop through our node list; generate code and then delete |
| 352 | * each list entry |
| 353 | */ |
| 354 | while (node_head != 0) |
| 355 | { |
| 356 | node_entry *nxt; |
| 357 | |
| 358 | /* remember the next entry */ |
| 359 | nxt = node_head->nxt; |
| 360 | |
| 361 | /* |
| 362 | * set this line's descriptor as current, for error |
| 363 | * reporting purposes |
| 364 | */ |
| 365 | G_tok->set_line_info(node_head->desc, node_head->linenum); |
| 366 | |
| 367 | /* fold symbolic constants */ |
| 368 | node_head->node = |
| 369 | node_head->node |
| 370 | ->fold_constants(G_prs->get_global_symtab()); |
| 371 | |
| 372 | /* if it's a constant value, display it */ |
| 373 | show_const(0, node_head->node); |
| 374 | |
| 375 | /* |
| 376 | * generate code; for testing purposes, don't discard |
| 377 | * anything, to ensure we perform all generation |
| 378 | */ |
| 379 | node_head->node->gen_code(FALSE, FALSE); |
| 380 | |
| 381 | /* disassemble this much */ |
| 382 | unas_out.print("// line %lu\n", node_head->linenum); |
| 383 | CTcT3Unasm::disasm(unas_in, &unas_out); |
| 384 | |
| 385 | /* delete this entry */ |
| 386 | delete node_head; |
| 387 | |
| 388 | /* move on to the next entry */ |
| 389 | node_head = nxt; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | err_catch(exc) |
| 394 | { |
| 395 | /* |
| 396 | * if it's not a general internal or fatal error, log it; don't |
| 397 | * log general errors, since these will have been logged as |
| 398 | * specific internal errors before being thrown |
| 399 | */ |
| 400 | if (exc->get_error_code() != TCERR_INTERNAL_ERROR |
| 401 | && exc->get_error_code() != TCERR_FATAL_ERROR) |
| 402 | G_tok->log_error(TC_SEV_FATAL, exc->get_error_code()); |
| 403 | |
| 404 | /* count the fatal error */ |
| 405 | ++fatal_error_count; |
| 406 | } |
| 407 | err_end; |
| 408 | |
| 409 | /* report errors */ |
| 410 | fprintf(stderr, |
| 411 | "Warnings: %d\n" |
| 412 | "Errors: %d\n" |
| 413 | "Longest string: %lu, longest list: %lu\n", |
| 414 | G_tcmain->get_warning_count(), |
| 415 | G_tcmain->get_error_count() + fatal_error_count, |
| 416 | (unsigned long)G_cg->get_max_str_len(), |
| 417 | (unsigned long)G_cg->get_max_list_cnt()); |
| 418 | |
| 419 | /* delete the disassembler input object */ |
| 420 | delete unas_in; |
| 421 | |
| 422 | /* shut down the compiler */ |
| 423 | CTcMain::terminate(); |
| 424 | |
| 425 | /* done with the res loader */ |
| 426 | delete res_loader; |
| 427 | |
| 428 | /* delete the image file */ |
| 429 | delete imgfile; |
| 430 | |
| 431 | /* delete the host interface */ |
| 432 | delete hostifc; |
| 433 | |
| 434 | /* show any unfreed memory */ |
| 435 | t3_list_memory_blocks(0); |
| 436 | |
| 437 | /* success */ |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * dummy 'make' object implementation |
| 443 | */ |
| 444 | void CTcMake::write_build_config_to_sym_file(class CVmFile *) |
| 445 | { |
| 446 | } |
| 447 | |
| 448 | |
| 449 | |
| 450 | /* ------------------------------------------------------------------------ */ |
| 451 | /* |
| 452 | * dummy implementation of runtime symbol table |
| 453 | */ |
| 454 | void CVmRuntimeSymbols::add_sym(const char *, size_t, |
| 455 | const vm_val_t *) |
| 456 | { |
| 457 | } |
| 458 | |
| 459 | /* dummy implementation of runtime metaclass table */ |
| 460 | vm_meta_entry_t *CVmMetaTable::get_entry_by_id(const char *id) const |
| 461 | { |
| 462 | return 0; |
| 463 | } |