parser.cxx

Sat, 14 Jul 2012 18:03:37 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 14 Jul 2012 18:03:37 +0300
changeset 19
66993500719f
parent 18
dbcc3b784234
child 20
d7b13805d1e0
permissions
-rw-r--r--

Commands w/ arguments are now written correctly.

0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * botc source code
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 * All rights reserved.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright notice,
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * this list of conditions and the following disclaimer.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * this list of conditions and the following disclaimer in the documentation
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * and/or other materials provided with the distribution.
3
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
14 * 3. Neither the name of the developer nor the names of its contributors may
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
15 * be used to endorse or promote products derived from this software without
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
16 * specific prior written permission.
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 * 4. Redistributions in any form must be accompanied by information on how to
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 * obtain complete source code for the software and any accompanying
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 * software that uses the software. The source code must either be included
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 * in the distribution or be available for no more than the cost of
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 * distribution plus a nominal fee, and must be freely redistributable
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 * under reasonable conditions. For an executable file, complete source
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 * code means the source code for all modules it contains. It does not
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 * include source code for modules or files that typically accompany the
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 * major components of the operating system on which the executable file
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 * runs.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 */
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 #include <stdio.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 #include <stdlib.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 #include "common.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 #include "str.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 #include "objwriter.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 MUST_TOPLEVEL
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 // Must end in a colon
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 MustNext (":");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 MUST_TOPLEVEL
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 MustNext ("{");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
171 // If the last state did not have a main loop, define a dummy one now.
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
172 if (!gotMainLoop) {
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
173 w->Write (DH_MAINLOOP);
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
174 w->Write (DH_ENDMAINLOOP);
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
175 }
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
176
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 /*
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 // State
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 w->WriteState ("stateSpawn");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 w->Write (DH_ONENTER);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 w->Write (DH_ENDONENTER);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 w->Write (DH_MAINLOOP);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 w->Write (DH_ENDMAINLOOP);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 w->Write (DH_EVENT);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 w->Write (BOTEVENT_PLAYER_FIREDSSG);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 w->Write (DH_COMMAND);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 w->Write (BOTCMD_BEGINJUMPING);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 w->Write (0);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 w->Write (DH_ENDEVENT);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 */
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 }
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
195
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
196 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
197 // 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
198 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
199 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
200
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
201 MustNext ("(");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
202 int curarg = 0;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
203 while (1) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
204 if (curarg >= comm->maxargs) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
205 if (!PeekNext().compare (","))
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
206 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
207 comm->maxargs);
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
208 MustNext (")");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
209 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
210 break;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
211 }
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 (!Next ())
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
214 ParserError ("unexpected end-of-file, unterminated command");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
215
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
216 // 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
217 if (!token.compare (")"))
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
218 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
219
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
220 // For now, it takes in just numbers.
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
221 // Needs to cater for string arguments too...
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
222 if (!token.isnumber())
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
223 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
224
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
225 int i = atoi (token.chars ());
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
226 w->Write<long> (DH_PUSHNUMBER);
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
227 w->Write<long> (i);
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
228
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
229 if (curarg < comm->numargs - 1) {
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 } else if (curarg < comm->maxargs - 1) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
232 // Can continue, but can terminate as well.
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
233 if (!PeekNext ().compare (")")) {
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 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
236 break;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
237 } else
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
238 MustNext (",");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
239 }
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 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
242 }
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
243 MustNext (";");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
244
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
245 // 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
246 while (curarg < comm->maxargs) {
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
247 w->Write<long> (DH_PUSHNUMBER);
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
248 w->Write<long> (comm->defvals[curarg]);
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
249 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
250 }
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
251
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
252 w->Write<long> (DH_COMMAND);
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
253 w->Write<long> (comm->number);
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
254 w->Write<long> (comm->maxargs);
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
255 }

mercurial