Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
script_statement.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * xml Mission Scripting written by Alexander Rawass <alexannika@users.sourceforge.net>
24  */
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <time.h>
30 #include <ctype.h>
31 #include <assert.h>
32 #ifndef WIN32
33 //this file isn't available on my system (all win32 machines?) i dun even know what it has or if we need it as I can compile without it
34 #include <unistd.h>
35 #endif
36 
37 #include <expat.h>
38 #include "xml_support.h"
39 
40 #include "vegastrike.h"
41 
42 #include "cmd/unit_generic.h"
43 #include "mission.h"
44 #include "easydom.h"
45 
46 void Mission::checkStatement( missionNode *node, int mode )
47 {
48  //no difference
49  if (node->tag == DTAG_IF) {
50  doIf( node, mode );
51  } else if (node->tag == DTAG_BLOCK) {
52  doBlock( node, mode );
53  } else if (node->tag == DTAG_SETVAR) {
54  doSetVar( node, mode );
55  } else if (node->tag == DTAG_DEFVAR) {
56  doDefVar( node, mode );
57  } else if (node->tag == DTAG_EXEC) {
58  doExec( node, mode );
59  } else if (node->tag == DTAG_RETURN) {
60  doReturn( node, mode );
61  } else if (node->tag == DTAG_CALL) {
62  varInst *vi = doCall( node, mode );
63  if (vi->type != VAR_VOID) {
64  fatalError( node, mode, "expected void as return from call, got different" );
65  assert( 0 );
66  }
67  deleteVarInst( vi );
68  } else if (node->tag == DTAG_WHILE) {
69  doWhile( node, mode );
70  }
71 }
72 
73 void Mission::doIf( missionNode *node, int mode )
74 {
75  if (mode == SCRIPT_PARSE) {
76  vector< easyDomNode* >::const_iterator siter;
77 
78  int nr_subnodes = node->subnodes.size();
79  if (nr_subnodes != 3) {
80  fatalError( node, mode, "an if-statement needs exact three subnodes, not "+nr_subnodes );
81  printf( "nr_of_subnodes: %d\n", nr_subnodes );
82 
83  assert( 0 );
84  }
85 #if 0
86  int i = 0;
87  for (siter = node->subnodes.begin(); siter != node->subnodes.end() && i < 3; siter++) {
88  missionNode *snode = (missionNode*) *siter;
89  node->script.if_block[i] = snode;
90  }
91 #endif
92 
93  node->script.if_block[0] = (missionNode*) node->subnodes[0];
94  debug( 8, node->script.if_block[0], mode, "if-node" );
95 
96  node->script.if_block[1] = (missionNode*) node->subnodes[1];
97  debug( 8, node->script.if_block[1], mode, "if-node" );
98 
99  node->script.if_block[2] = (missionNode*) node->subnodes[2];
100  debug( 8, node->script.if_block[2], mode, "if-node" );
101  }
102  bool ok = checkBoolExpr( node->script.if_block[0], mode );
103  if (mode == SCRIPT_PARSE) {
104  checkStatement( node->script.if_block[1], mode );
105  checkStatement( node->script.if_block[2], mode );
106  } else {
107  if (ok)
108  checkStatement( node->script.if_block[1], mode );
109  else
110  checkStatement( node->script.if_block[2], mode );
111  }
112 }
113 
114 void Mission::doWhile( missionNode *node, int mode )
115 {
116  if (mode == SCRIPT_PARSE) {
117  int len = node->subnodes.size();
118  if (len != 2) {
119  fatalError( node, mode, "a while-expr needs exact two subnodes" );
120  assert( 0 );
121  }
122  node->script.while_arg[0] = (missionNode*) node->subnodes[0];
123  node->script.while_arg[1] = (missionNode*) node->subnodes[1];
124 
125  bool res = checkBoolExpr( node->script.while_arg[0], mode ); //FIXME unused variable 'res' --chuck_starchaser
126 
127  checkStatement( node->script.while_arg[1], mode );
128  } else {
129  //runtime
130  while ( checkBoolExpr( node->script.while_arg[0], mode ) )
131  checkStatement( node->script.while_arg[1], mode );
132  }
133 }
134