| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/test/test_prs_top.cpp,v 1.1 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: test parsing from the top level |
| 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 | int main(int argc, char **argv) |
| 52 | { |
| 53 | CResLoader *res_loader; |
| 54 | CTcHostIfc *hostifc; |
| 55 | int curarg; |
| 56 | int fatal_error_count = 0; |
| 57 | osfildef *fpout = 0; |
| 58 | CVmFile *imgfile = 0; |
| 59 | CTcUnasSrcCodeStr *unas_in; |
| 60 | CTcUnasOutStdio unas_out; |
| 61 | CTPNStmProg *node; |
| 62 | const char *image_fname; |
| 63 | int success; |
| 64 | int unasm = FALSE; |
| 65 | uchar xor_mask = 0; |
| 66 | char pathbuf[OSFNMAX]; |
| 67 | |
| 68 | /* initialize for testing */ |
| 69 | test_init(); |
| 70 | |
| 71 | /* create the host interface object */ |
| 72 | hostifc = new CTcHostIfcStdio(); |
| 73 | |
| 74 | /* create a resource loader */ |
| 75 | os_get_special_path(pathbuf, sizeof(pathbuf), argv[0], OS_GSP_T3_RES); |
| 76 | res_loader = new CResLoader(pathbuf); |
| 77 | |
| 78 | /* initialize the compiler */ |
| 79 | CTcMain::init(hostifc, res_loader, "us-ascii"); |
| 80 | |
| 81 | /* use test reporting mode */ |
| 82 | G_tok->set_test_report_mode(TRUE); |
| 83 | G_tcmain->set_test_report_mode(TRUE); |
| 84 | |
| 85 | /* create the disassembler input stream */ |
| 86 | unas_in = new CTcUnasSrcCodeStr(G_cs); |
| 87 | |
| 88 | err_try |
| 89 | { |
| 90 | static const char tool_data[4] = { 't', 's', 't', 'P' }; |
| 91 | |
| 92 | /* scan options */ |
| 93 | for (curarg = 1 ; curarg < argc ; ++curarg) |
| 94 | { |
| 95 | char *p; |
| 96 | |
| 97 | /* get the argument string for easy reference */ |
| 98 | p = argv[curarg]; |
| 99 | |
| 100 | /* if it's not an option, we're done */ |
| 101 | if (*p != '-') |
| 102 | break; |
| 103 | |
| 104 | /* if it's a -I argument, use it */ |
| 105 | if (*(p + 1) == 'I') |
| 106 | { |
| 107 | char *path; |
| 108 | |
| 109 | /* |
| 110 | * if it's with this argument, read it, otherwise move |
| 111 | * on to the next argument |
| 112 | */ |
| 113 | if (*(p + 2) == '\0') |
| 114 | path = argv[++curarg]; |
| 115 | else |
| 116 | path = p + 2; |
| 117 | |
| 118 | /* add the directory to the include path list */ |
| 119 | G_tok->add_inc_path(path); |
| 120 | } |
| 121 | else if (*(p + 1) == 'v') |
| 122 | { |
| 123 | /* set verbose mode */ |
| 124 | G_tcmain->set_verbosity(TRUE); |
| 125 | } |
| 126 | else if (*(p + 1) == 'u') |
| 127 | { |
| 128 | /* note unassembly mode */ |
| 129 | unasm = TRUE; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | /* |
| 134 | * invalid usage - consume all the arguments and fall |
| 135 | * through to the usage checker |
| 136 | */ |
| 137 | curarg = argc; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* check arguments */ |
| 143 | if (curarg + 2 != argc) |
| 144 | { |
| 145 | /* terminate the compiler */ |
| 146 | CTcMain::terminate(); |
| 147 | |
| 148 | /* delete our objects */ |
| 149 | delete res_loader; |
| 150 | |
| 151 | /* exit with an error */ |
| 152 | errexit("usage: test_prs [options] <source-file> <image-file>\n" |
| 153 | "options:\n" |
| 154 | " -Idir - add dir to include path\n" |
| 155 | " -v - verbose error messages"); |
| 156 | } |
| 157 | |
| 158 | /* add the default system include directory to the include path */ |
| 159 | os_get_special_path(pathbuf, sizeof(pathbuf), argv[0], OS_GSP_T3_INC); |
| 160 | G_tok->add_inc_path(pathbuf); |
| 161 | |
| 162 | /* set up the tokenizer with the main input file */ |
| 163 | if (G_tok->set_source(argv[curarg], argv[curarg])) |
| 164 | errexit("unable to open source file"); |
| 165 | |
| 166 | /* set up an output file */ |
| 167 | image_fname = argv[curarg+1]; |
| 168 | fpout = osfopwb(image_fname, OSFTT3IMG); |
| 169 | if (fpout == 0) |
| 170 | errexit("unable to open image file"); |
| 171 | imgfile = new CVmFile(); |
| 172 | imgfile->set_file(fpout, 0); |
| 173 | |
| 174 | /* read the first token */ |
| 175 | G_tok->next(); |
| 176 | |
| 177 | /* parse at the top level */ |
| 178 | node = G_prs->parse_top(); |
| 179 | |
| 180 | /* if errors occurred during parsing, stop here */ |
| 181 | if (G_tcmain->get_error_count() != 0 || node == 0) |
| 182 | goto done; |
| 183 | |
| 184 | /* fold symbolic constants for all nodes */ |
| 185 | node->fold_constants(G_prs->get_global_symtab()); |
| 186 | |
| 187 | /* if errors occurred during constant folding, stop now */ |
| 188 | if (G_tcmain->get_error_count() != 0) |
| 189 | goto done; |
| 190 | |
| 191 | /* generate code and write the image file */ |
| 192 | node->build_image(imgfile, xor_mask, tool_data); |
| 193 | |
| 194 | /* if errors occurred during code generation, stop now */ |
| 195 | if (G_tcmain->get_error_count() != 0) |
| 196 | goto done; |
| 197 | |
| 198 | /* disassemble the result if desired */ |
| 199 | if (unasm) |
| 200 | CTcT3Unasm::disasm(unas_in, &unas_out); |
| 201 | |
| 202 | done: ; |
| 203 | } |
| 204 | err_catch(exc) |
| 205 | { |
| 206 | /* |
| 207 | * if it's not a general internal or fatal error, log it; don't |
| 208 | * log general errors, since these will have been logged as |
| 209 | * specific internal errors before being thrown |
| 210 | */ |
| 211 | if (exc->get_error_code() != TCERR_INTERNAL_ERROR |
| 212 | && exc->get_error_code() != TCERR_FATAL_ERROR) |
| 213 | G_tok->log_error(TC_SEV_FATAL, exc->get_error_code()); |
| 214 | |
| 215 | /* count the fatal error */ |
| 216 | ++fatal_error_count; |
| 217 | } |
| 218 | err_end; |
| 219 | |
| 220 | /* report errors */ |
| 221 | fprintf(stderr, |
| 222 | "Warnings: %d\n" |
| 223 | "Errors: %d\n" |
| 224 | "Longest string: %lu, longest list: %lu\n", |
| 225 | G_tcmain->get_warning_count(), |
| 226 | G_tcmain->get_error_count() + fatal_error_count, |
| 227 | (unsigned long)G_cg->get_max_str_len(), |
| 228 | (unsigned long)G_cg->get_max_list_cnt()); |
| 229 | |
| 230 | /* |
| 231 | * note whether or not the compilation was successful - it succeeded |
| 232 | * if we had no errors or fatal errors |
| 233 | */ |
| 234 | success = (G_tcmain->get_error_count() + fatal_error_count == 0); |
| 235 | |
| 236 | /* delete the image file object (this closes the file) */ |
| 237 | delete imgfile; |
| 238 | |
| 239 | /* |
| 240 | * if any errors occurred, delete the image file in the external |
| 241 | * file system - this prevents us from leaving around an incomplete |
| 242 | * or corrupted image file when compilation fails, and helps |
| 243 | * 'make'-type tools realize that they must generate the image file |
| 244 | * target again on the next build, even if source files didn't |
| 245 | * change |
| 246 | */ |
| 247 | if (!success) |
| 248 | osfdel(image_fname); |
| 249 | |
| 250 | /* delete the disassembler input object */ |
| 251 | delete unas_in; |
| 252 | |
| 253 | /* shut down the compiler */ |
| 254 | CTcMain::terminate(); |
| 255 | |
| 256 | /* done with the res loader */ |
| 257 | delete res_loader; |
| 258 | |
| 259 | /* delete the host interface */ |
| 260 | delete hostifc; |
| 261 | |
| 262 | /* show any unfreed memory */ |
| 263 | t3_list_memory_blocks(0); |
| 264 | |
| 265 | /* |
| 266 | * terminate - exit with a success indication if we had no errors |
| 267 | * (other than warnings); exit with an error indication otherwise |
| 268 | */ |
| 269 | return (success ? OSEXSUCC : OSEXFAIL); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * dummy 'make' object implementation |
| 274 | */ |
| 275 | void CTcMake::write_build_config_to_sym_file(class CVmFile *) |
| 276 | { |
| 277 | } |
| 278 | |
| 279 | /* ------------------------------------------------------------------------ */ |
| 280 | /* |
| 281 | * dummy implementation of runtime symbol table |
| 282 | */ |
| 283 | void CVmRuntimeSymbols::add_sym(const char *, size_t, |
| 284 | const vm_val_t *) |
| 285 | { |
| 286 | } |
| 287 | |
| 288 | /* dummy implementation of runtime metaclass table */ |
| 289 | vm_meta_entry_t *CVmMetaTable::get_entry_by_id(const char *id) const |
| 290 | { |
| 291 | return 0; |
| 292 | } |