Sat, 14 Jul 2012 17:39:43 +0300
New rule: a semicolon is now required after events, mainloop, onexit and onenter 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. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
0 | 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" |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
50 | #include "commands.h" |
0 | 51 | |
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) \ |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
53 | ParserError ("%ss may only be defined at top level!", token.chars()); |
0 | 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 = ""; |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
59 | bool g_stateSpawnDefined = false; |
1
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) { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
62 | bool gotMainLoop = false; |
0 | 63 | while (Next()) { |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
64 | // printf ("got token %s\n", token.chars()); |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
65 | if (!token.icompare ("#include")) { |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
66 | MustString (); |
9
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
67 | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
68 | // First ensure that the file can be opened |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
69 | FILE* newfile = fopen (token.chars(), "r"); |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
70 | if (!newfile) |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
71 | ParserError ("couldn't open included file `%s`!", token.chars()); |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
72 | fclose (newfile); |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
73 | ScriptReader* newreader = new ScriptReader (token.chars()); |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
74 | newreader->BeginParse (w); |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
75 | } else if (!token.icompare ("state")) { |
0 | 76 | MUST_TOPLEVEL |
77 | ||
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
78 | MustString (); |
0 | 79 | |
80 | // State name must be a word. | |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
81 | if (token.first (" ") != token.len()) |
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
82 | ParserError ("state name must be a single word! got `%s`", token.chars()); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
83 | str statename = token; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
84 | |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
85 | // stateSpawn is special - it *must* be defined. If we |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
86 | // encountered it, then mark down that we have it. |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
87 | if (!token.icompare ("stateSpawn")) |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
88 | g_stateSpawnDefined = true; |
0 | 89 | |
90 | // Must end in a colon | |
91 | MustNext (":"); | |
92 | ||
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
93 | // If the previous state did not define a mainloop, |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
94 | // define a dummy one now, since one has to be present. |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
95 | if (g_CurState.len() && !gotMainLoop) { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
96 | w->Write (DH_MAINLOOP); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
97 | w->Write (DH_ENDMAINLOOP); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
98 | } |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
99 | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
100 | w->Write (DH_STATENAME); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
101 | w->Write (statename.len()); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
102 | w->WriteString (statename); |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
103 | 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
|
104 | 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
|
105 | |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
106 | g_NumStates++; |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
107 | g_CurState = token; |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
108 | gotMainLoop = false; |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
109 | } else if (!token.icompare ("event")) { |
0 | 110 | MUST_TOPLEVEL |
111 | ||
112 | // Event definition | |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
113 | MustString (); |
0 | 114 | |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
115 | EventDef* e = FindEventByName (token); |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
116 | if (!e) |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
117 | ParserError ("bad event! got `%s`\n", token.chars()); |
0 | 118 | |
119 | MustNext ("{"); | |
120 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
121 | g_CurMode = MODE_EVENT; |
0 | 122 | |
123 | w->Write (DH_EVENT); | |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
124 | 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
|
125 | g_NumEvents++; |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
126 | } else if (!token.icompare ("mainloop")) { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
127 | MUST_TOPLEVEL |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
128 | MustNext ("{"); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
129 | g_CurMode = MODE_MAINLOOP; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
130 | w->Write (DH_MAINLOOP); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
131 | gotMainLoop = true; |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
132 | } else if (!token.icompare ("onenter") || !token.icompare ("onexit")) { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
133 | MUST_TOPLEVEL |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
134 | bool onenter = !token.compare ("onenter"); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
135 | |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
136 | MustNext ("{"); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
137 | g_CurMode = onenter ? MODE_ONENTER : MODE_ONEXIT; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
138 | w->Write (onenter ? DH_ONENTER : DH_ONEXIT); |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
139 | } else if (!token.compare ("}")) { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
140 | // Closing brace |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
141 | int dataheader = (g_CurMode == MODE_EVENT) ? DH_ENDEVENT : |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
142 | (g_CurMode == MODE_MAINLOOP) ? DH_ENDMAINLOOP : |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
143 | (g_CurMode == MODE_ONENTER) ? DH_ENDONENTER : |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
144 | (g_CurMode == MODE_ONEXIT) ? DH_ENDONEXIT : -1; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
145 | |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
146 | if (dataheader == -1) |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
147 | ParserError ("unexpected `}`"); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
148 | |
0 | 149 | // Closing brace.. |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
150 | w->Write (dataheader); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
151 | g_CurMode = MODE_TOPLEVEL; |
18
dbcc3b784234
New rule: a semicolon is now required after events, mainloop, onexit and onenter definitions.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
152 | |
dbcc3b784234
New rule: a semicolon is now required after events, mainloop, onexit and onenter definitions.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
153 | MustNext (";"); |
0 | 154 | } else { |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
155 | // Check if it's a command. |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
156 | CommandDef* comm = GetCommandByName (token); |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
157 | if (comm) |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
158 | ParseCommand (comm, w); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
159 | else |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
160 | ParserError ("unknown keyword `%s`", token.chars()); |
0 | 161 | } |
162 | } | |
163 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
164 | if (g_CurMode != MODE_TOPLEVEL) |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
165 | ParserError ("script did not end at top level; did you forget a `}`?"); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
166 | |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
167 | // stateSpawn must be defined! |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
168 | if (!g_stateSpawnDefined) |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
169 | ParserError ("script must have a state named `stateSpawn`!"); |
0 | 170 | |
171 | /* | |
172 | // State | |
173 | w->WriteState ("stateSpawn"); | |
174 | ||
175 | w->Write (DH_ONENTER); | |
176 | w->Write (DH_ENDONENTER); | |
177 | ||
178 | w->Write (DH_MAINLOOP); | |
179 | w->Write (DH_ENDMAINLOOP); | |
180 | ||
181 | w->Write (DH_EVENT); | |
182 | w->Write (BOTEVENT_PLAYER_FIREDSSG); | |
183 | w->Write (DH_COMMAND); | |
184 | w->Write (BOTCMD_BEGINJUMPING); | |
185 | w->Write (0); | |
186 | w->Write (DH_ENDEVENT); | |
187 | */ | |
188 | } | |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
189 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
190 | void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
191 | // If this was defined at top-level, we stop right at square one! |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
192 | if (g_CurMode == MODE_TOPLEVEL) |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
193 | ParserError ("no commands allowed at top level!"); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
194 | |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
195 | w->Write<long> (DH_COMMAND); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
196 | w->Write<long> (comm->number); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
197 | w->Write<long> (comm->maxargs); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
198 | MustNext ("("); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
199 | int curarg = 0; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
200 | while (1) { |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
201 | if (curarg >= comm->maxargs) { |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
202 | if (!PeekNext().compare (",")) |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
203 | ParserError ("got `,` while expecting command-terminating `)`, are you passing too many parameters? (max %d)", |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
204 | comm->maxargs); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
205 | MustNext (")"); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
206 | curarg++; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
207 | break; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
208 | } |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
209 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
210 | if (!Next ()) |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
211 | ParserError ("unexpected end-of-file, unterminated command"); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
212 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
213 | // If we get a ")" now, the user probably gave too few parameters |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
214 | if (!token.compare (")")) |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
215 | ParserError ("unexpected `)`, did you pass too few parameters? (need %d)", comm->numargs); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
216 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
217 | // For now, it takes in just numbers. |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
218 | // Needs to cater for string arguments too... |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
219 | if (!token.isnumber()) |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
220 | ParserError ("argument %d (`%s`) is not a number", curarg, token.chars()); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
221 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
222 | int i = atoi (token.chars ()); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
223 | w->Write<long> (i); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
224 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
225 | if (curarg < comm->numargs - 1) { |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
226 | MustNext (","); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
227 | } else if (curarg < comm->maxargs - 1) { |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
228 | // Can continue, but can terminate as well. |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
229 | if (!PeekNext ().compare (")")) { |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
230 | MustNext (")"); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
231 | curarg++; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
232 | break; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
233 | } else |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
234 | MustNext (","); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
235 | } |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
236 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
237 | curarg++; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
238 | } |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
239 | MustNext (";"); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
240 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
241 | // If the script skipped any optional arguments, fill in defaults. |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
242 | while (curarg < comm->maxargs) { |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
243 | w->Write<long> (comm->defvals[curarg]); |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
244 | curarg++; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
245 | } |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
246 | } |