parser.cxx

changeset 0
8dce9696d62d
child 1
f0c61c204bc8
equal deleted inserted replaced
-1:000000000000 0:8dce9696d62d
1 /*
2 * botc source code
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of the Skulltag Development Team nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 * 4. Redistributions in any form must be accompanied by information on how to
18 * obtain complete source code for the software and any accompanying
19 * software that uses the software. The source code must either be included
20 * in the distribution or be available for no more than the cost of
21 * distribution plus a nominal fee, and must be freely redistributable
22 * under reasonable conditions. For an executable file, complete source
23 * code means the source code for all modules it contains. It does not
24 * include source code for modules or files that typically accompany the
25 * major components of the operating system on which the executable file
26 * runs.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include "common.h"
44 #include "str.h"
45 #include "objwriter.h"
46 #include "scriptreader.h"
47
48 #define TOKEN_CHARS token.chars()
49 #define TOKEN_IS(s) !token.compare (s)
50 #define MUST_TOPLEVEL if (curmode != MODE_TOPLEVEL) \
51 ParseError ("%ss may only be defined at top level!", token.chars());
52
53 void ScriptReader::BeginParse (ObjWriter* w) {
54 // Script starts at top level
55 curmode = MODE_TOPLEVEL;
56 curstate = "";
57
58 while (Next()) {
59 printf ("got token %s\n", (char*)token);
60 if (TOKEN_IS ("state")) {
61 MUST_TOPLEVEL
62
63 MustNext ();
64
65 str statename = token;
66
67 // State name must be a word.
68 if (statename.first (" ") != statename.len())
69 ParseError ("state name must be a single word! got `%s`", (char*)statename);
70
71
72 // Must end in a colon
73 MustNext (":");
74
75 w->WriteState (statename);
76 curstate = statename;
77 } else if (TOKEN_IS ("event")) {
78 MUST_TOPLEVEL
79
80 // Event definition
81 MustNext ();
82
83 // TODO: make a data file for bot events and read it
84 #if 0
85 unsigned int u;
86 for (u = 0; u < NUM_BOTEVENTS; u++) {
87 if (!BotEvents[u].name.compare (token))
88 break;
89 }
90
91 if (u == NUM_BOTEVENTS)
92 ParseError ("bad event! got `%s`\n", token.chars());
93
94 #endif
95
96 MustNext ("{");
97
98 curmode = MODE_EVENT;
99
100 w->Write (DH_EVENT);
101 // w->Write<long> (u);
102 numevents++;
103 } else if (TOKEN_IS ("}")) {
104 // Closing brace..
105 switch (curmode) {
106 case MODE_EVENT:
107 // Brace closes event.
108 w->Write (DH_ENDEVENT);
109 curmode = MODE_TOPLEVEL;
110 break;
111 default:
112 ParseError ("unexpected `}`");
113 }
114 } else {
115 ParseError ("unknown keyword `%s`!", TOKEN_CHARS);
116 }
117 }
118
119 if (curmode != MODE_TOPLEVEL)
120 ParseError ("script did not end at top level! did you forget a `}`?\n");
121
122 /*
123 // State
124 w->WriteState ("stateSpawn");
125
126 w->Write (DH_ONENTER);
127 w->Write (DH_ENDONENTER);
128
129 w->Write (DH_MAINLOOP);
130 w->Write (DH_ENDMAINLOOP);
131
132 w->Write (DH_EVENT);
133 w->Write (BOTEVENT_PLAYER_FIREDSSG);
134 w->Write (DH_COMMAND);
135 w->Write (BOTCMD_BEGINJUMPING);
136 w->Write (0);
137 w->Write (DH_ENDEVENT);
138 */
139 }

mercurial