1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
// Emacs style mode select -*- C++ -*-
//----------------------------------------------------------------------------
//
// $Id: t_prepro.c,v 1.1 2000/11/02 17:57:28 stroggonmeth Exp $
//
// Copyright(C) 2000 Simon Howard
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// $Log: t_prepro.c,v $
// Revision 1.1 2000/11/02 17:57:28 stroggonmeth
// FraggleScript files...
//
//
//--------------------------------------------------------------------------
//
// Preprocessor.
//
// The preprocessor must be called when the script is first loaded.
// It performs 2 functions:
// 1: blank out comments (which could be misinterpreted)
// 2: makes a list of all the sections held within {} braces
// 3: 'dry' runs the script: goes thru each statement and
// sets the types of all the section_t's in the script
// 4: Saves locations of all goto() labels
//
// the system of section_t's is pretty horrible really, but it works
// and its probably the only way i can think of of saving scripts
// half-way thru running
//
// By Simon Howard
//
//---------------------------------------------------------------------------
/* includes ************************/
#include <stdio.h>
#include <string.h>
#include "command.h"
#include "doomstat.h"
#include "w_wad.h"
#include "z_zone.h"
#include "t_parse.h"
#include "t_spec.h"
#include "t_oper.h"
#include "t_vari.h"
#include "t_func.h"
// clear the script: section and variable slots
void clear_script()
{
int i;
for(i=0; i<SECTIONSLOTS; i++)
current_script->sections[i] = NULL;
for(i=0; i<VARIABLESLOTS; i++)
current_script->variables[i] = NULL;
// clear child scripts
for(i=0; i<MAXSCRIPTS; i++)
current_script->children[i] = NULL;
}
/*********** {} sections *************/
// during preprocessing all of the {} sections
// are found. these are stored in a hash table
// according to their offset in the script.
// functions here deal with creating new section_t's
// and finding them from a given offset.
#define section_hash(b) \
( (int) ( (b) - current_script->data) % SECTIONSLOTS)
section_t *new_section(char *brace)
{
int n;
section_t *newsec;
// create section
// make level so its cleared at start of new level
newsec = Z_Malloc(sizeof(section_t), PU_LEVEL, 0);
newsec->start = brace;
// hook it into the hashchain
n = section_hash(brace);
newsec->next = current_script->sections[n];
current_script->sections[n] = newsec;
return newsec;
}
// find a section_t from the location of the starting { brace
section_t *find_section_start(char *brace)
{
int n = section_hash(brace);
section_t *current;
current = current_script->sections[n];
// use the hash table: check the appropriate hash chain
while(current)
{
if(current->start == brace)
return current;
current = current->next;
}
return NULL; // not found
}
// find a section_t from the location of the ending } brace
section_t *find_section_end(char *brace)
{
int n;
// hash table is no use, they are hashed according to
// the offset of the starting brace
// we have to go through every entry to find from the
// ending brace
for(n=0; n<SECTIONSLOTS; n++) // check all sections in all chains
{
section_t *current = current_script->sections[n];
while(current)
{
if(current->end == brace)
return current; // found it
current = current->next;
}
}
return NULL; // not found
}
/********** labels ****************/
// labels are also found during the
// preprocessing. these are of the form
//
// label_name:
//
// and are used for the goto function.
// goto labels are stored as variables.
// from parse.c
#define isop(c) !( ( (c)<='Z' && (c)>='A') || ( (c)<='z' && (c)>='a') || \
( (c)<='9' && (c)>='0') || ( (c)=='_') )
// create a new label. pass the location inside the script
svariable_t *new_label(char *labelptr)
{
svariable_t *newlabel; // labels are stored as variables
char labelname[256];
char *temp, *temp2;
// copy the label name from the script up to ':'
for(temp=labelptr, temp2 = labelname; *temp!=':'; temp++, temp2++)
*temp2 = *temp;
*temp2 = '\0'; // end string
newlabel = new_variable(current_script, labelname, svt_label);
// put neccesary data in the label
newlabel->value.labelptr = labelptr;
return newlabel;
}
/*********** main loop **************/
// This works by recursion. when a { opening
// brace is found, another instance of the
// function is called for the data inside
// the {} section.
// At the same time, the sections are noted
// down and hashed. Goto() labels are noted
// down, and comments are blanked out
char *process_find_char(char *data, char find)
{
while(*data)
{
if(*data==find) return data;
if(*data=='\"') // found a quote: ignore stuff in it
{
data++;
while(*data && *data != '\"')
{
// escape sequence ?
if(*data=='\\') data++;
data++;
}
// error: end of script in a constant
if(!*data) return NULL;
}
// comments: blank out
if(*data=='/' && *(data+1)=='*') // /* -- */ comment
{
while(*data && (*data != '*' || *(data+1) != '/') )
{
*data=' '; data++;
}
if(*data)
*data = *(data+1) = ' '; // blank the last bit
else
{
rover = data;
// script terminated in comment
script_error("script terminated inside comment\n");
}
}
if(*data=='/' && *(data+1)=='/') // // -- comment
while(*data != '\n')
{
*data=' '; data++; // blank out
}
// labels
if(*data==':' // ':' -- a label
&& current_script->scriptnum != -1) // not levelscript
{
char *labelptr = data-1;
while(!isop(*labelptr)) labelptr--;
new_label(labelptr+1);
}
if(*data=='{') // { -- } sections: add 'em
{
section_t *newsec = new_section(data);
newsec->type = st_empty;
// find the ending } and save
newsec->end = process_find_char(data+1, '}');
if(!newsec->end)
{ // brace not found
rover = data;
script_error("section error: no ending brace\n");
return NULL;
}
// continue from the end of the section
data = newsec->end;
}
data++;
}
return NULL;
}
/*********** second stage parsing ************/
// second stage preprocessing considers the script
// in terms of tokens rather than as plain data.
//
// we 'dry' run the script: go thru each statement and
// collect types for section_t
//
// this is an important thing to do, it cannot be done
// at runtime for 2 reasons:
// 1. gotos() jumping inside loops will pass thru
// the end of the loop
// 2. savegames. loading a script saved inside a
// loop will let it pass thru the loop
//
// this is basically a cut-down version of the normal
// parsing loop.
void get_tokens(char *); // t_parse.c
void dry_run_script()
{
// save some stuff
char *old_rover = rover;
section_t *old_current_section = current_section;
char *end = current_script->data + current_script->len;
char *token_alloc;
killscript = false;
// allocate space for the tokens
token_alloc = Z_Malloc(current_script->len + T_MAXTOKENS, PU_STATIC, 0);
rover = current_script->data;
while(rover < end && *rover)
{
tokens[0] = token_alloc;
get_tokens(rover);
if(killscript) break;
if(!num_tokens) continue;
if(current_section && tokentype[0] == function)
{
if(!strcmp(tokens[0], "if"))
{
current_section->type = st_if;
continue;
}
else if(!strcmp(tokens[0], "elseif"))
{
current_section->type = st_elseif;
continue;
}
else if(!strcmp(tokens[0], "else"))
{
current_section->type = st_else;
continue;
}
else if(!strcmp(tokens[0], "while") ||
!strcmp(tokens[0], "for"))
{
current_section->type = st_loop;
current_section->data.data_loop.loopstart = linestart;
continue;
}
}
}
Z_Free(token_alloc);
// restore stuff
current_section = old_current_section;
rover = old_rover;
}
/***************** main preprocess function ******************/
// set up current_script, script->len
// just call all the other functions
void preprocess(script_t *script)
{
if(debugfile)
fprintf(debugfile," preprocess script %i\n", script->scriptnum);
current_script = script;
script->len = strlen(script->data);
clear_script();
if(debugfile)
fprintf(debugfile, " run thru script\n");
process_find_char(script->data, 0); // fill in everything
if(debugfile)
fprintf(debugfile, " dry run script\n");
dry_run_script();
}
/************ includes ******************/
// FraggleScript allows 'including' of other lumps.
// we divert input from the current_script (normally
// levelscript) to a seperate lump. This of course
// first needs to be preprocessed to remove comments
// etc.
void parse_data(char *data, char *end); // t_parse.c
// parse an 'include' lump
void parse_include(char *lumpname)
{
int lumpnum;
char *temp;
char *lump, *end;
char *saved_rover;
if(-1 == (lumpnum = W_GetNumForName(lumpname)) )
{
script_error("include lump '%s' not found!\n", lumpname);
return;
}
lump = W_CacheLumpNum(lumpnum, PU_STATIC);
// realloc bigger for NULL at end
// SoM: REALLOC does not seem to work! So we alloc here and copy....
//lump = Z_Realloc(lump, W_LumpLength(lumpnum)+10, PU_STATIC, NULL);
temp = lump;
lump = Z_Malloc(W_LumpLength(lumpnum) + 10, PU_STATIC, NULL);
memcpy(lump, temp, W_LumpLength(lumpnum));
saved_rover = rover; // save rover during include
rover = lump; end = lump+W_LumpLength(lumpnum);
*end = 0;
// preprocess the include
// we assume that it does not include sections or labels or
// other nasty things
process_find_char(lump, 0);
// now parse the lump
parse_data(lump, end);
// restore rover
rover = saved_rover;
// free the lump
Z_Free(lump);
Z_Free(temp);
}
//---------------------------------------------------------------------------
//
// $Log: t_prepro.c,v $
// Revision 1.1 2000/11/02 17:57:28 stroggonmeth
// FraggleScript files...
//
// Revision 1.1.1.1 2000/04/30 19:12:08 fraggle
// initial import
//
//
//---------------------------------------------------------------------------
|