parser.cxx

Sun, 15 Jul 2012 18:56:26 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 15 Jul 2012 18:56:26 +0300
changeset 26
54eaea6dc27c
parent 25
2a600af97c6b
child 28
fb46d3d40064
permissions
-rw-r--r--

Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)

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"
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
51 #include "stringtable.h"
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
52 #include "variables.h"
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
54 #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
55 ParserError ("%ss may only be defined at top level!", token.chars());
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
57 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
58 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
59 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
60 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
61 bool g_stateSpawnDefined = false;
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
62 bool g_GotMainLoop = false;
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
63
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 void ScriptReader::BeginParse (ObjWriter* w) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 while (Next()) {
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
66 // 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
67 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
68 MustString ();
9
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
69
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
70 // 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
71 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
72 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
73 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
74 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
75 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
76 newreader->BeginParse (w);
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
77 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
78 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
79
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
80 if (!token.icompare ("state")) {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 MUST_TOPLEVEL
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82
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
83 MustString ();
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 // 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
86 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
87 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
88 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
89
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
90 // 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
91 // 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
92 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
93 g_stateSpawnDefined = true;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 // Must end in a colon
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 MustNext (":");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
98 // Write the previous state's onenter and
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
99 // mainloop buffers to file now
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
100 if (g_CurState.len())
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
101 w->WriteBuffers();
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
102
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_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
104 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
105 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
106 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
107
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
108 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
109 g_CurState = token;
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
110 g_GotMainLoop = false;
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
111 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
112 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
113
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
114 if (!token.icompare ("event")) {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 MUST_TOPLEVEL
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 // 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
118 MustString ();
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
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
120 EventDef* e = FindEventByName (token);
2
bb2c45522eb6 Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
121 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
122 ParserError ("bad event! got `%s`\n", token.chars());
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 MustNext ("{");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125
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_EVENT;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 w->Write (DH_EVENT);
2
bb2c45522eb6 Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
129 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
130 g_NumEvents++;
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
131 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
132 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
133
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
134 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
135 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
136 MustNext ("{");
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
137
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
138 // Mode must be set before dataheader is written here!
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
139 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
140 w->Write (DH_MAINLOOP);
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
141 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
142 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
143
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
144 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
145 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
146 bool onenter = !token.compare ("onenter");
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
147 MustNext ("{");
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
148
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
149 // Mode must be set before dataheader is written here,
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
150 // because onenter goes to a separate buffer.
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
151 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
152 w->Write (onenter ? DH_ONENTER : DH_ONEXIT);
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
153 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
154 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
155
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
156 if (!token.compare ("var")) {
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
157 // For now, only globals are supported
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
158 if (g_CurMode != MODE_TOPLEVEL || g_CurState.len())
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
159 ParserError ("variables must only be global for now");
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
160
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
161 MustNext ();
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
162
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
163 // Var name must not be a number
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
164 if (token.isnumber())
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
165 ParserError ("variable name must not be a number");
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
166
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
167 str varname = token;
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
168 ScriptVar* var = DeclareGlobalVariable (this, varname);
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
169
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
170 if (!var)
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
171 ParserError ("declaring %s variable %s failed",
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
172 g_CurState.len() ? "state" : "global", varname.chars());
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
173
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
174 MustNext (";");
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
175 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
176 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
177
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
178 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
179 // 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
180 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
181 (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
182 (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
183 (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
184
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
185 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
186 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
187
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
188 // Data header must be written before mode is changed because
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
189 // onenter and mainloop go into buffers, and we want the closing
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
190 // data headers into said buffers too.
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 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
192 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
193
dbcc3b784234 New rule: a semicolon is now required after events, mainloop, onexit and onenter definitions.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
194 MustNext (";");
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
195 continue;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 }
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
197 // Check global variables
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
198 ScriptVar* g = FindGlobalVariable (token);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
199 if (g) {
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
200 // Not in top level, unfortunately..
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
201 if (g_CurMode == MODE_TOPLEVEL)
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
202 ParserError ("can't alter variables at top level");
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
203
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
204 // Build operator string. Only '=' is one
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
205 // character, others are two.
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
206 MustNext ();
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
207 str oper = token;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
208 if (token.compare ("=") != 0) {
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
209 MustNext ();
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
210 oper += token;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
211 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
212
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
213 // Unary operators
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
214 if (!oper.compare ("++")) {
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
215 w->Write<long> (DH_INCGLOBALVAR);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
216 w->Write<long> (g->index);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
217 } else if (!oper.compare ("--")) {
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
218 w->Write<long> (DH_DECGLOBALVAR);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
219 w->Write<long> (g->index);
25
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
220 } else {
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
221 // Binary operators
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
222 // And only with numbers for now too.
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
223 // TODO: make a proper expression parser!
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
224 MustNumber();
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
225
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
226 int val = atoi (token.chars());
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
227 w->Write<long> (DH_PUSHNUMBER);
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
228 w->Write<long> (val);
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
229
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
230 int h = !oper.compare("=") ? DH_ASSIGNGLOBALVAR :
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
231 !oper.compare("+=") ? DH_ADDGLOBALVAR :
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
232 !oper.compare("-=") ? DH_SUBGLOBALVAR :
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
233 !oper.compare("*=") ? DH_MULGLOBALVAR :
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
234 !oper.compare("/=") ? DH_DIVGLOBALVAR :
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
235 !oper.compare("%=") ? DH_MODGLOBALVAR : -1;
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
236
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
237 if (h == -1)
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
238 ParserError ("bad operator `%s`!", oper.chars());
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
239
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
240 w->Write<long> (h);
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
241 w->Write<long> (g->index);
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
242 }
25
2a600af97c6b Fixed unary operators..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
243
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
244 MustNext (";");
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
245 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
246 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
247
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
248 // Check if it's a command.
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
249 CommandDef* comm = GetCommandByName (token);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
250 if (comm) {
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
251 ParseCommand (comm, w);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
252 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
253 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
254
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
255 ParserError ("unknown keyword `%s`", token.chars());
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
257
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
258 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
259 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
260
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
261 // 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
262 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
263 ParserError ("script must have a state named `stateSpawn`!");
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264
26
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
265
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
266
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
267 // Dump the last state's onenter and mainloop
54eaea6dc27c Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 25
diff changeset
268 w->WriteBuffers();
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
269
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
270 // If we added strings here, we need to write a list of them.
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
271 unsigned int stringcount = CountStringTable ();
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
272 if (stringcount) {
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
273 w->Write<long> (DH_STRINGLIST);
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
274 w->Write<long> (stringcount);
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
275 for (unsigned int a = 0; a < stringcount; a++)
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
276 w->WriteString (g_StringTable[a]);
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
277 }
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
278
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
279 printf ("%u string%s written\n", stringcount, PLURAL (stringcount));
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
280 }
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
281
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
282 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
283 // 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
284 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
285 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
286
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
287 MustNext ("(");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
288 int curarg = 0;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
289 while (1) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
290 if (curarg >= comm->maxargs) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
291 if (!PeekNext().compare (","))
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
292 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
293 comm->maxargs);
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
294 MustNext (")");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
295 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
296 break;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
297 }
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
298
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
299 if (!PeekNext().len())
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
300 ParserError ("unexpected end-of-file, unterminated command");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
301
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
302 // If we get a ")" now, the user probably gave too few parameters
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
303 if (!PeekNext().compare (")"))
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
304 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
305
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
306 // Argument may be using a variable
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
307 ScriptVar* g = FindGlobalVariable (PeekNext ());
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
308 if (g && comm->argtypes[curarg] != RETURNVAL_STRING) {
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
309 // Advance cursor past the var name
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
310 Next();
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
311
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
312 w->Write<long> (DH_PUSHGLOBALVAR);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
313 w->Write<long> (g->index);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
314 } else {
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
315 // Check for raw value
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
316 switch (comm->argtypes[curarg]) {
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
317 case RETURNVAL_INT:
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
318 MustNumber();
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
319 w->Write<long> (DH_PUSHNUMBER);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
320 w->Write<long> (atoi (token.chars ()));
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
321 break;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
322 case RETURNVAL_BOOLEAN:
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
323 MustBool();
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
324 w->Write<long> (DH_PUSHNUMBER);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
325 w->Write<long> (BoolValue ());
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
326 break;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
327 case RETURNVAL_STRING:
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
328 MustString();
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
329 w->Write<long> (DH_PUSHSTRINGINDEX);
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
330 w->Write<long> (PushToStringTable (token.chars()));
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
331 break;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
332 }
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
333 }
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
334
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
335 if (curarg < comm->numargs - 1) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
336 MustNext (",");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
337 } else if (curarg < comm->maxargs - 1) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
338 // Can continue, but can terminate as well.
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
339 if (!PeekNext ().compare (")")) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
340 MustNext (")");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
341 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
342 break;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
343 } else
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
344 MustNext (",");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
345 }
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
346
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
347 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
348 }
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
349 MustNext (";");
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
350
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
351 // 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
352 while (curarg < comm->maxargs) {
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
353 w->Write<long> (DH_PUSHNUMBER);
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
354 w->Write<long> (comm->defvals[curarg]);
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
355 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
356 }
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
357
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
358 w->Write<long> (DH_COMMAND);
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
359 w->Write<long> (comm->number);
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
360 w->Write<long> (comm->maxargs);
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
361 }

mercurial