Fri, 13 Jul 2012 20:10:08 +0300
Added event definitions
0 | 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 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
41 | #define __PARSER_CXX__ |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
42 | |
0 | 43 | #include <stdio.h> |
44 | #include <stdlib.h> | |
45 | #include "common.h" | |
46 | #include "str.h" | |
47 | #include "objwriter.h" | |
48 | #include "scriptreader.h" | |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
49 | #include "events.h" |
0 | 50 | |
51 | #define TOKEN_CHARS token.chars() | |
52 | #define TOKEN_IS(s) !token.compare (s) | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
53 | #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \ |
0 | 54 | ParseError ("%ss may only be defined at top level!", token.chars()); |
55 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
56 | int g_NumStates = 0; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
57 | int g_NumEvents = 0; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
58 | int g_CurMode = MODE_TOPLEVEL; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
59 | str g_CurState = ""; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
60 | |
0 | 61 | void ScriptReader::BeginParse (ObjWriter* w) { |
62 | while (Next()) { | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
63 | // printf ("got token %s\n", token.chars()); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
64 | if (TOKEN_IS ("#include")) { |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
65 | MustNext (); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
66 | // First ensure that the file can be opened |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
67 | FILE* newfile = fopen (token.chars(), "r"); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
68 | if (!newfile) |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
69 | ParseError ("couldn't open included file `%s`!", token.chars()); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
70 | fclose (newfile); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
71 | ScriptReader* newreader = new ScriptReader (token.chars()); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
72 | newreader->BeginParse (w); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
73 | } else if (TOKEN_IS ("state")) { |
0 | 74 | MUST_TOPLEVEL |
75 | ||
76 | MustNext (); | |
77 | ||
78 | str statename = token; | |
79 | ||
80 | // State name must be a word. | |
81 | if (statename.first (" ") != statename.len()) | |
82 | ParseError ("state name must be a single word! got `%s`", (char*)statename); | |
83 | ||
84 | // Must end in a colon | |
85 | MustNext (":"); | |
86 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
87 | w->Write (DH_STATENAME); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
88 | w->Write (statename.len()); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
89 | w->WriteString (statename); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
90 | w->Write (DH_STATEIDX); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
91 | w->Write (g_NumStates); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
92 | |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
93 | g_NumStates++; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
94 | g_CurState = statename; |
0 | 95 | } else if (TOKEN_IS ("event")) { |
96 | MUST_TOPLEVEL | |
97 | ||
98 | // Event definition | |
99 | MustNext (); | |
100 | ||
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
101 | EventDef* e = FindEventByName (token); |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
102 | if (!e) |
0 | 103 | ParseError ("bad event! got `%s`\n", token.chars()); |
104 | ||
105 | MustNext ("{"); | |
106 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
107 | g_CurMode = MODE_EVENT; |
0 | 108 | |
109 | w->Write (DH_EVENT); | |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
110 | w->Write<long> (e->number); |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
111 | g_NumEvents++; |
0 | 112 | } else if (TOKEN_IS ("}")) { |
113 | // Closing brace.. | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
114 | switch (g_CurMode) { |
0 | 115 | case MODE_EVENT: |
116 | // Brace closes event. | |
117 | w->Write (DH_ENDEVENT); | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
118 | g_CurMode = MODE_TOPLEVEL; |
0 | 119 | break; |
120 | default: | |
121 | ParseError ("unexpected `}`"); | |
122 | } | |
123 | } else { | |
124 | ParseError ("unknown keyword `%s`!", TOKEN_CHARS); | |
125 | } | |
126 | } | |
127 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
128 | if (g_CurMode != MODE_TOPLEVEL) |
0 | 129 | ParseError ("script did not end at top level! did you forget a `}`?\n"); |
130 | ||
131 | /* | |
132 | // State | |
133 | w->WriteState ("stateSpawn"); | |
134 | ||
135 | w->Write (DH_ONENTER); | |
136 | w->Write (DH_ENDONENTER); | |
137 | ||
138 | w->Write (DH_MAINLOOP); | |
139 | w->Write (DH_ENDMAINLOOP); | |
140 | ||
141 | w->Write (DH_EVENT); | |
142 | w->Write (BOTEVENT_PLAYER_FIREDSSG); | |
143 | w->Write (DH_COMMAND); | |
144 | w->Write (BOTCMD_BEGINJUMPING); | |
145 | w->Write (0); | |
146 | w->Write (DH_ENDEVENT); | |
147 | */ | |
148 | } |