| | 1 | #ifdef RCSID |
| | 2 | static char RCSid[] = |
| | 3 | "$Header: d:/cvsroot/tads/tads3/test/TEST_TOK.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_tok.cpp - tokenizer test |
| | 15 | Function |
| | 16 | |
| | 17 | Notes |
| | 18 | |
| | 19 | Modified |
| | 20 | 04/16/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 "vmimage.h" |
| | 35 | #include "vmrunsym.h" |
| | 36 | #include "t3test.h" |
| | 37 | |
| | 38 | |
| | 39 | static void errexit(const char *msg) |
| | 40 | { |
| | 41 | printf("%s\n", msg); |
| | 42 | exit(1); |
| | 43 | } |
| | 44 | |
| | 45 | int main(int argc, char **argv) |
| | 46 | { |
| | 47 | CResLoader *res_loader; |
| | 48 | CTcHostIfc *hostifc; |
| | 49 | int curarg; |
| | 50 | CTcTokFileDesc *desc; |
| | 51 | long linenum; |
| | 52 | int pp_mode = FALSE; |
| | 53 | char pathbuf[OSFNMAX]; |
| | 54 | |
| | 55 | /* initialize for testing */ |
| | 56 | test_init(); |
| | 57 | |
| | 58 | /* create the host interface object */ |
| | 59 | hostifc = new CTcHostIfcStdio(); |
| | 60 | |
| | 61 | /* create a resource loader */ |
| | 62 | os_get_special_path(pathbuf, sizeof(pathbuf), argv[0], OS_GSP_T3_RES); |
| | 63 | res_loader = new CResLoader(pathbuf); |
| | 64 | |
| | 65 | /* initialize the compiler */ |
| | 66 | CTcMain::init(hostifc, res_loader, "us-ascii"); |
| | 67 | |
| | 68 | /* set test reporting mode */ |
| | 69 | G_tok->set_test_report_mode(TRUE); |
| | 70 | G_tcmain->set_test_report_mode(TRUE); |
| | 71 | |
| | 72 | err_try |
| | 73 | { |
| | 74 | /* add some pre-defined symbols for testing */ |
| | 75 | G_tok->add_define("_MSC_VER", "1100"); |
| | 76 | G_tok->add_define("_WIN32", "1"); |
| | 77 | G_tok->add_define("_M_IX86", "500"); |
| | 78 | G_tok->add_define("__STDC__", "0"); |
| | 79 | G_tok->add_define("_INTEGRAL_MAX_BITS", "64"); |
| | 80 | G_tok->add_define("__cplusplus", "1"); |
| | 81 | |
| | 82 | /* scan -I arguments */ |
| | 83 | for (curarg = 1 ; curarg < argc ; ++curarg) |
| | 84 | { |
| | 85 | char *p; |
| | 86 | |
| | 87 | /* get the argument string for easy reference */ |
| | 88 | p = argv[curarg]; |
| | 89 | |
| | 90 | /* if it's not an option, we're done */ |
| | 91 | if (*p != '-') |
| | 92 | break; |
| | 93 | |
| | 94 | /* if it's a -I argument, use it */ |
| | 95 | if (*(p + 1) == 'I') |
| | 96 | { |
| | 97 | char *path; |
| | 98 | |
| | 99 | /* |
| | 100 | * if it's with this argument, read it, otherwise move |
| | 101 | * on to the next argument |
| | 102 | */ |
| | 103 | if (*(p + 2) == '\0') |
| | 104 | path = argv[++curarg]; |
| | 105 | else |
| | 106 | path = p + 2; |
| | 107 | |
| | 108 | /* add the directory to the include path list */ |
| | 109 | G_tok->add_inc_path(path); |
| | 110 | } |
| | 111 | else if (*(p + 1) == 'P') |
| | 112 | { |
| | 113 | /* set preprocess-only mode */ |
| | 114 | G_tok->set_mode_pp_only(TRUE); |
| | 115 | pp_mode = TRUE; |
| | 116 | } |
| | 117 | else if (*(p + 1) == 'v') |
| | 118 | { |
| | 119 | /* set verbose mode */ |
| | 120 | G_tcmain->set_verbosity(TRUE); |
| | 121 | } |
| | 122 | else |
| | 123 | { |
| | 124 | /* |
| | 125 | * invalid usage - consume all the arguments and fall |
| | 126 | * through to the usage checker |
| | 127 | */ |
| | 128 | curarg = argc; |
| | 129 | break; |
| | 130 | } |
| | 131 | } |
| | 132 | |
| | 133 | /* check arguments */ |
| | 134 | if (curarg + 1 != argc) |
| | 135 | { |
| | 136 | /* terminate the compiler */ |
| | 137 | CTcMain::terminate(); |
| | 138 | |
| | 139 | /* delete our objects */ |
| | 140 | delete res_loader; |
| | 141 | |
| | 142 | /* exit with an error */ |
| | 143 | errexit("usage: test_tok [options] <source-file>\n" |
| | 144 | "options:\n" |
| | 145 | " -Idir - add dir to include path\n" |
| | 146 | " -P - preprocess to standard output\n" |
| | 147 | " -v - verbose error messages"); |
| | 148 | } |
| | 149 | |
| | 150 | /* set up the tokenizer with the main input file */ |
| | 151 | if (G_tok->set_source(argv[curarg], argv[curarg])) |
| | 152 | errexit("unable to open source file"); |
| | 153 | |
| | 154 | /* start out with no stream */ |
| | 155 | desc = 0; |
| | 156 | linenum = 0; |
| | 157 | |
| | 158 | /* read lines of input */ |
| | 159 | for (;;) |
| | 160 | { |
| | 161 | /* read the next line, and stop if we've reached end of file */ |
| | 162 | if (G_tok->read_line_pp()) |
| | 163 | break; |
| | 164 | |
| | 165 | /* |
| | 166 | * If we're in a different stream than for the last line, or |
| | 167 | * the new line number is more than the last line number plus |
| | 168 | * 1, add a #line directive to the output stream. |
| | 169 | * |
| | 170 | * In order to make test log output independent of local path |
| | 171 | * naming conventions and the local directory structure, use |
| | 172 | * only the root filename in the #line directive. |
| | 173 | */ |
| | 174 | if (pp_mode |
| | 175 | && (G_tok->get_last_desc() != desc |
| | 176 | || G_tok->get_last_linenum() != linenum + 1)) |
| | 177 | printf("#line %ld %s\n", G_tok->get_last_linenum(), |
| | 178 | G_tok->get_last_desc()->get_dquoted_rootname()); |
| | 179 | |
| | 180 | /* remember the last line we read */ |
| | 181 | desc = G_tok->get_last_desc(); |
| | 182 | linenum = G_tok->get_last_linenum(); |
| | 183 | |
| | 184 | /* show this line */ |
| | 185 | printf("%s\n", G_tok->get_cur_line()); |
| | 186 | } |
| | 187 | |
| | 188 | /* dump the hash table, to see what it looks like */ |
| | 189 | G_tok->get_defines_table()->debug_dump(); |
| | 190 | } |
| | 191 | err_catch(exc) |
| | 192 | { |
| | 193 | /* |
| | 194 | * if it's not the general internal error, log it; don't log |
| | 195 | * general internal errors, since these will have been logged as |
| | 196 | * specific internal errors before being thrown |
| | 197 | */ |
| | 198 | if (exc->get_error_code() != TCERR_INTERNAL_ERROR) |
| | 199 | printf("exception caught: %d\n", exc->get_error_code()); |
| | 200 | } |
| | 201 | err_end; |
| | 202 | |
| | 203 | /* shut down the compiler */ |
| | 204 | CTcMain::terminate(); |
| | 205 | |
| | 206 | /* done with the res loader */ |
| | 207 | delete res_loader; |
| | 208 | |
| | 209 | /* delete host interface */ |
| | 210 | delete hostifc; |
| | 211 | |
| | 212 | /* show any unfreed memory */ |
| | 213 | t3_list_memory_blocks(0); |
| | 214 | |
| | 215 | /* success */ |
| | 216 | return 0; |
| | 217 | } |
| | 218 | |
| | 219 | /* ------------------------------------------------------------------------ */ |
| | 220 | /* |
| | 221 | * dummy implementation of runtime symbol table |
| | 222 | */ |
| | 223 | void CVmRuntimeSymbols::add_sym(const char *, size_t, |
| | 224 | const vm_val_t *) |
| | 225 | { |
| | 226 | } |
| | 227 | |