Fri, 13 Jul 2012 18:41:40 +0300
Added support for #include directives, added basic header and statistics printing.
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" | |
49 | ||
50 | #define TOKEN_CHARS token.chars() | |
51 | #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
|
52 | #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \ |
0 | 53 | ParseError ("%ss may only be defined at top level!", token.chars()); |
54 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
55 | 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
|
56 | 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
|
57 | 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
|
58 | str g_CurState = ""; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
59 | |
0 | 60 | void ScriptReader::BeginParse (ObjWriter* w) { |
61 | while (Next()) { | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
62 | // 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
|
63 | 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
|
64 | MustNext (); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
65 | // 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
|
66 | 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
|
67 | if (!newfile) |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
68 | 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
|
69 | fclose (newfile); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
70 | 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
|
71 | newreader->BeginParse (w); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
72 | } else if (TOKEN_IS ("state")) { |
0 | 73 | MUST_TOPLEVEL |
74 | ||
75 | MustNext (); | |
76 | ||
77 | str statename = token; | |
78 | ||
79 | // State name must be a word. | |
80 | if (statename.first (" ") != statename.len()) | |
81 | ParseError ("state name must be a single word! got `%s`", (char*)statename); | |
82 | ||
83 | // Must end in a colon | |
84 | MustNext (":"); | |
85 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
86 | 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
|
87 | 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
|
88 | w->WriteString (statename); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
89 | 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
|
90 | 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
|
91 | |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
92 | g_NumStates++; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
93 | g_CurState = statename; |
0 | 94 | } else if (TOKEN_IS ("event")) { |
95 | MUST_TOPLEVEL | |
96 | ||
97 | // Event definition | |
98 | MustNext (); | |
99 | ||
100 | // TODO: make a data file for bot events and read it | |
101 | #if 0 | |
102 | unsigned int u; | |
103 | for (u = 0; u < NUM_BOTEVENTS; u++) { | |
104 | if (!BotEvents[u].name.compare (token)) | |
105 | break; | |
106 | } | |
107 | ||
108 | if (u == NUM_BOTEVENTS) | |
109 | ParseError ("bad event! got `%s`\n", token.chars()); | |
110 | ||
111 | #endif | |
112 | ||
113 | MustNext ("{"); | |
114 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
115 | g_CurMode = MODE_EVENT; |
0 | 116 | |
117 | w->Write (DH_EVENT); | |
118 | // w->Write<long> (u); | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
119 | g_NumEvents++; |
0 | 120 | } else if (TOKEN_IS ("}")) { |
121 | // Closing brace.. | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
122 | switch (g_CurMode) { |
0 | 123 | case MODE_EVENT: |
124 | // Brace closes event. | |
125 | 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
|
126 | g_CurMode = MODE_TOPLEVEL; |
0 | 127 | break; |
128 | default: | |
129 | ParseError ("unexpected `}`"); | |
130 | } | |
131 | } else { | |
132 | ParseError ("unknown keyword `%s`!", TOKEN_CHARS); | |
133 | } | |
134 | } | |
135 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
136 | if (g_CurMode != MODE_TOPLEVEL) |
0 | 137 | ParseError ("script did not end at top level! did you forget a `}`?\n"); |
138 | ||
139 | /* | |
140 | // State | |
141 | w->WriteState ("stateSpawn"); | |
142 | ||
143 | w->Write (DH_ONENTER); | |
144 | w->Write (DH_ENDONENTER); | |
145 | ||
146 | w->Write (DH_MAINLOOP); | |
147 | w->Write (DH_ENDMAINLOOP); | |
148 | ||
149 | w->Write (DH_EVENT); | |
150 | w->Write (BOTEVENT_PLAYER_FIREDSSG); | |
151 | w->Write (DH_COMMAND); | |
152 | w->Write (BOTCMD_BEGINJUMPING); | |
153 | w->Write (0); | |
154 | w->Write (DH_ENDEVENT); | |
155 | */ | |
156 | } |