parser.cxx

Tue, 14 Aug 2012 01:54:17 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 14 Aug 2012 01:54:17 +0300
changeset 54
8cc91ef94754
parent 53
9ef7e549391f
child 55
173956c1ac27
permissions
-rw-r--r--

Added floating point support for the most part. However, decimals are somehow lost during a cast..

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) \
37
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
55 ParserError ("%s-statements may only be defined at top level!", token.chars());
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
56
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
57 #define MUST_NOT_TOPLEVEL if (g_CurMode == MODE_TOPLEVEL) \
37
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
58 ParserError ("%s-statements may not be defined at top level!", token.chars());
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
60 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
61 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
62 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
63 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
64 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
65 bool g_GotMainLoop = false;
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
66 unsigned int g_BlockStackCursor = 0;
40
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
67 DataBuffer* g_IfExpression = NULL;
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
68
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
69 // ============================================================================
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
70 // Main parser code. Begins read of the script file, checks the syntax of it
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
71 // and writes the data to the object file via ObjWriter - which also takes care
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
72 // of necessary buffering so stuff is written in the correct order.
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
73 void ScriptReader::ParseBotScript (ObjWriter* w) {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 while (Next()) {
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
75 // ============================================================
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
76 if (!token.compare ("state")) {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 MUST_TOPLEVEL
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
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
79 MustString ();
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 // 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
82 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
83 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
84 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
85
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 // 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
87 // encountered it, then mark down that we have it.
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 31
diff changeset
88 if (!token.icompare ("statespawn"))
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
89 g_stateSpawnDefined = true;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 // Must end in a colon
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 MustNext (":");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93
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
94 // 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
95 // 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
96 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
97 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
98
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
99 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
100 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
101 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
102 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
103
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
104 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
105 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
106 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
107 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
108 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
109
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
110 // ============================================================
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
111 if (!token.compare ("event")) {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 MUST_TOPLEVEL
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 // 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
115 MustString ();
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116
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 EventDef* e = FindEventByName (token);
2
bb2c45522eb6 Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
118 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
119 ParserError ("bad event! got `%s`\n", token.chars());
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 MustNext ("{");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
123 g_CurMode = MODE_EVENT;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 w->Write (DH_EVENT);
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
126 w->Write<word> (e->number);
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
127 g_NumEvents++;
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
128 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
129 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
130
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
131 // ============================================================
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
132 if (!token.compare ("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
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 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
135
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
136 // 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
137 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
138 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
139 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
140 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
141
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
142 // ============================================================
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
143 if (!token.compare ("onenter") || !token.compare ("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
144 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
145 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
146 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
147
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
148 // 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
149 // 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
150 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
151 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
152 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
153 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
154
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
155 // ============================================================
24
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
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
178 // ============================================================
37
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
179 // Goto
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
180 if (!token.compare ("goto")) {
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
181 MUST_NOT_TOPLEVEL
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
182
37
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
183 // Get the name of the label
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
184 MustNext ();
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
185
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
186 // Find the mark this goto statement points to
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
187 // TODO: This should define the mark instead of bombing
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
188 // out if the mark isn't found!
45
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
189 unsigned int m = w->FindMark (token);
37
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
190 if (m == MAX_MARKS)
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
191 ParserError ("unknown label `%s`!", token.chars());
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
192
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
193 // Add a reference to the mark.
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
194 w->Write<word> (DH_GOTO);
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
195 w->AddReference (m);
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
196 MustNext (";");
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
197 continue;
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
198 }
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
199
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
200 // ============================================================
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
201 // If
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
202 if (!token.compare ("if")) {
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
203 MUST_NOT_TOPLEVEL
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
204 PushBlockStack ();
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
205
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
206 // Condition
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
207 MustNext ("(");
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
208
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
209 // Read the expression and write it.
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
210 // TODO: This should be storing it into a variable first, so
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
211 // that else statements would be possible!
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
212 MustNext ();
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
213 DataBuffer* c = ParseExpression (TYPE_INT);
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
214 w->WriteBuffer (c);
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
215
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
216 MustNext (")");
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
217 MustNext ("{");
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
218
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
219 // Add a mark - to here temporarily - and add a reference to it.
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
220 // Upon a closing brace, the mark will be adjusted.
45
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
221 unsigned int marknum = w->AddMark ("");
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
222
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
223 // Use DH_IFNOTGOTO - if the expression is not true, we goto the mark
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
224 // we just defined - and this mark will be at the end of the block.
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
225 w->Write<word> (DH_IFNOTGOTO);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
226 w->AddReference (marknum);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
227
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
228 // Store it in the block stack
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
229 blockstack[g_BlockStackCursor].mark1 = marknum;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
230 blockstack[g_BlockStackCursor].type = BLOCKTYPE_IF;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
231 continue;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
232 }
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
233
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
234 // ============================================================
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
235 // While
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
236 if (!token.compare ("while")) {
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
237 MUST_NOT_TOPLEVEL
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
238 PushBlockStack ();
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
239
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
240 // While loops need two marks - one at the start of the loop and one at the
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
241 // end. The condition is checked at the very start of the loop, if it fails,
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
242 // we use goto to skip to the end of the loop. At the end, we loop back to
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
243 // the beginning with a go-to statement.
45
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
244 unsigned int mark1 = w->AddMark (""); // start
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
245 unsigned int mark2 = w->AddMark (""); // end
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
246
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
247 // Condition
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
248 MustNext ("(");
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
249 MustNext ();
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
250 DataBuffer* expr = ParseExpression (TYPE_INT);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
251 MustNext (")");
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
252 MustNext ("{");
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
253
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
254 // Write condition
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
255 w->WriteBuffer (expr);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
256
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
257 // Instruction to go to the end if it fails
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
258 w->Write<word> (DH_IFNOTGOTO);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
259 w->AddReference (mark2);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
260
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
261 // Store the needed stuff
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
262 blockstack[g_BlockStackCursor].mark1 = mark1;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
263 blockstack[g_BlockStackCursor].mark2 = mark2;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
264 blockstack[g_BlockStackCursor].type = BLOCKTYPE_WHILE;
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
265 continue;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
266 }
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
267
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
268 // ============================================================
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
269 // For loop
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
270 if (!token.compare ("for")) {
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
271 MUST_NOT_TOPLEVEL
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
272 PushBlockStack ();
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
273
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
274 // Initializer
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
275 MustNext ("(");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
276 MustNext ();
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
277 DataBuffer* init = ParseStatement (w);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
278 MustNext (";");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
279
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
280 // Condition
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
281 MustNext ();
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
282 DataBuffer* cond = ParseExpression (TYPE_INT);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
283 MustNext (";");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
284
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
285 // Incrementor
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
286 MustNext ();
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
287 DataBuffer* incr = ParseStatement (w);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
288 MustNext (")");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
289 MustNext ("{");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
290
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
291 // First, write out the initializer
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
292 w->WriteBuffer (init);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
293
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
294 // Init two marks
45
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
295 int mark1 = w->AddMark ("");
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
296 int mark2 = w->AddMark ("");
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
297
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
298 // Add the condition
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
299 w->WriteBuffer (cond);
47
d84d82213137 some cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents: 46
diff changeset
300 w->Write<word> (DH_IFNOTGOTO);
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
301 w->AddReference (mark2);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
302
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
303 // Store the marks and incrementor
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
304 blockstack[g_BlockStackCursor].mark1 = mark1;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
305 blockstack[g_BlockStackCursor].mark2 = mark2;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
306 blockstack[g_BlockStackCursor].buffer1 = incr;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
307 blockstack[g_BlockStackCursor].type = BLOCKTYPE_FOR;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
308 continue;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
309 }
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
310
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
311 // ============================================================
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
312 // Do/while loop
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
313 if (!token.compare ("do")) {
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
314 MUST_NOT_TOPLEVEL
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
315 PushBlockStack ();
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
316 MustNext ("{");
45
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
317 blockstack[g_BlockStackCursor].mark1 = w->AddMark ("");
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
318 blockstack[g_BlockStackCursor].type = BLOCKTYPE_DO;
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
319 continue;
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
320 }
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
321
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
322 // ============================================================
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
323 // Switch
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
324 if (!token.compare ("switch")) {
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
325 /* This goes a bit tricky. switch is structured in the
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
326 * bytecode followingly:
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
327 * (expression)
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
328 * case a: goto casemark1
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
329 * case b: goto casemark2
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
330 * case c: goto casemark3
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
331 * goto mark1 // jump to end if no matches
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
332 * casemark1: ...
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
333 * casemark2: ...
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
334 * casemark3: ...
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
335 * mark1: // end mark
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
336 */
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
337
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
338 MUST_NOT_TOPLEVEL
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
339 PushBlockStack ();
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
340 MustNext ("(");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
341 MustNext ();
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
342 w->WriteBuffer (ParseExpression (TYPE_INT));
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
343 MustNext (")");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
344 MustNext ("{");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
345 blockstack[g_BlockStackCursor].type = BLOCKTYPE_SWITCH;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
346 blockstack[g_BlockStackCursor].mark1 = w->AddMark (""); // end mark
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
347 blockstack[g_BlockStackCursor].buffer1 = NULL; // default header
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
348 continue;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
349 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
350
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
351 // ============================================================
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
352 if (!token.compare ("case")) {
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
353 // case is only allowed inside switch
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
354 BlockInformation* info = &blockstack[g_BlockStackCursor];
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
355 if (info->type != BLOCKTYPE_SWITCH)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
356 ParserError ("case label outside switch");
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
357
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
358 // Get the literal (Zandronum does not support expressions here)
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
359 MustNumber ();
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
360 int num = atoi (token.chars ());
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
361 MustNext (":");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
362
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
363 for (int i = 0; i < MAX_CASE; i++)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
364 if (info->casenumbers[i] == num)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
365 ParserError ("multiple case %d labels in one switch", num);
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
366
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
367 // Write down the expression and case-go-to. This builds
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
368 // the case tree. The closing event will write the actual
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
369 // blocks and move the marks appropriately.
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
370 // AddSwitchCase will add the reference to the mark
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
371 // for the case block that this heralds, and takes care
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
372 // of buffering setup and stuff like that.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
373 // NULL the switch buffer for the case-go-to statement,
49
8e2f7a031410 rename ObjWriter::RecordBuffer to SwitchBuffer, as it is designed specifically for switch and will break if used genericly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 48
diff changeset
374 // we want it all under the switch, not into the case-buffers.
8e2f7a031410 rename ObjWriter::RecordBuffer to SwitchBuffer, as it is designed specifically for switch and will break if used genericly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 48
diff changeset
375 w->SwitchBuffer = NULL;
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
376 w->Write<word> (DH_CASEGOTO);
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
377 w->Write<word> (num);
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
378 AddSwitchCase (w, NULL);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
379 info->casenumbers[info->casecursor] = num;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
380 continue;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
381 }
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
382
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
383 if (!token.compare ("default")) {
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
384 BlockInformation* info = &blockstack[g_BlockStackCursor];
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
385 if (info->type != BLOCKTYPE_SWITCH)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
386 ParserError ("default label outside switch");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
387
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
388 if (info->buffer1)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
389 ParserError ("multiple default labels in one switch");
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
390
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
391 MustNext (":");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
392
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
393 // The default header is buffered into buffer1, since
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
394 // it has to be the last of the case headers
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
395 //
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
396 // Since the expression is pushed into the switch
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
397 // and is only popped when case succeeds, we have
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
398 // to pop it with DH_DROP manually if we end up in
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
399 // a default.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
400 DataBuffer* b = new DataBuffer;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
401 info->buffer1 = b;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
402 b->Write<word> (DH_DROP);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
403 b->Write<word> (DH_GOTO);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
404 AddSwitchCase (w, b);
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
405 continue;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
406 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
407
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
408 // ============================================================
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
409 // Break statement.
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
410 if (!token.compare ("break")) {
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
411 if (!g_BlockStackCursor)
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
412 ParserError ("unexpected `break`");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
413
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
414 BlockInformation* info = &blockstack[g_BlockStackCursor];
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
415
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
416 w->Write<word> (DH_GOTO);
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
417
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
418 // switch and if use mark1 for the closing point,
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
419 // for and while use mark2.
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
420 switch (info->type) {
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
421 case BLOCKTYPE_IF:
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
422 case BLOCKTYPE_SWITCH:
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
423 w->AddReference (info->mark1);
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
424 break;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
425 case BLOCKTYPE_FOR:
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
426 case BLOCKTYPE_WHILE:
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
427 w->AddReference (info->mark2);
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
428 break;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
429 default:
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
430 ParserError ("unexpected `break`");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
431 break;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
432 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
433
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
434 MustNext (";");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
435 continue;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
436 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
437
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
438 // ============================================================
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
439 // Label
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
440 if (!PeekNext().compare (":")) {
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
441 MUST_NOT_TOPLEVEL
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
442
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
443 if (IsKeyword (token))
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
444 ParserError ("label name `%s` conflicts with keyword\n", token.chars());
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
445 if (FindCommand (token))
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
446 ParserError ("label name `%s` conflicts with command name\n", token.chars());
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
447 if (FindGlobalVariable (token))
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
448 ParserError ("label name `%s` conflicts with variable\n", token.chars());
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
449
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
450 w->AddMark (token);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
451 MustNext (":");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
452 continue;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
453 }
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
454
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
455 // ============================================================
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
456 if (!token.compare ("}")) {
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
457 // Closing brace
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
458
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
459 // If we're in the block stack, we're descending down from it now
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
460 if (g_BlockStackCursor > 0) {
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
461 BlockInformation* info = &blockstack[g_BlockStackCursor];
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
462 switch (info->type) {
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
463 case BLOCKTYPE_IF:
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
464 // Adjust the closing mark.
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
465 w->MoveMark (info->mark1);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
466 break;
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
467 case BLOCKTYPE_FOR:
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
468 // Write the incrementor at the end of the loop block
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
469 w->WriteBuffer (info->buffer1);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
470 // fall-thru
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
471 case BLOCKTYPE_WHILE:
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
472 // Write down the instruction to go back to the start of the loop
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
473 w->Write (DH_GOTO);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
474 w->AddReference (info->mark1);
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
475
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
476 // Move the closing mark here since we're at the end of the while loop
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
477 w->MoveMark (info->mark2);
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
478 break;
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
479 case BLOCKTYPE_DO: {
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
480 MustNext ("while");
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
481 MustNext ("(");
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
482 MustNext ();
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
483 DataBuffer* expr = ParseExpression (TYPE_INT);
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
484 MustNext (")");
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
485 MustNext (";");
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
486
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
487 // If the condition runs true, go back to the start.
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
488 w->WriteBuffer (expr);
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
489 w->Write<long> (DH_IFGOTO);
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
490 w->AddReference (info->mark1);
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
491 break;
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
492 }
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
493 case BLOCKTYPE_SWITCH: {
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
494 // Switch closes. Move down to the record buffer of
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
495 // the lower block.
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
496 BlockInformation* previnfo = &blockstack[g_BlockStackCursor - 1];
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
497 if (previnfo->casecursor != -1)
49
8e2f7a031410 rename ObjWriter::RecordBuffer to SwitchBuffer, as it is designed specifically for switch and will break if used genericly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 48
diff changeset
498 w->SwitchBuffer = previnfo->casebuffers[previnfo->casecursor];
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
499 else
49
8e2f7a031410 rename ObjWriter::RecordBuffer to SwitchBuffer, as it is designed specifically for switch and will break if used genericly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 48
diff changeset
500 w->SwitchBuffer = NULL;
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
501
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
502 // If there was a default in the switch, write its header down now.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
503 // If not, write instruction to jump to the end of switch after
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
504 // the headers (thus won't fall-through if no case matched)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
505 if (info->buffer1)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
506 w->WriteBuffer (info->buffer1);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
507 else {
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
508 w->Write<word> (DH_DROP);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
509 w->Write<word> (DH_GOTO);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
510 w->AddReference (info->mark1);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
511 }
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
512
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
513 // Go through all of the buffers we
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
514 // recorded down and write them.
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
515 for (unsigned int u = 0; u < MAX_CASE; u++) {
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
516 if (!info->casebuffers[u])
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
517 continue;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
518
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
519 w->MoveMark (info->casemarks[u]);
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
520 w->WriteBuffer (info->casebuffers[u]);
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
521 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
522
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
523 // Move the closing mark here
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
524 w->MoveMark (info->mark1);
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
525 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
526 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
527
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
528 // Descend down the stack
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
529 g_BlockStackCursor--;
37
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
530 continue;
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
531 }
c349dca807f9 Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 36
diff changeset
532
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
533 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
534 (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
535 (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
536 (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
537
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
538 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
539 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
540
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
541 // Data header must be written before mode is changed because
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
542 // onenter and mainloop go into special buffers, and we want
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
543 // the closing 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
544 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
545 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
546
31
ad027ea58097 Lifted the ';'-after-events rule, though it can still be present. C++ allows it for functions, so this feels like the best solution regarding consistency.. I'm not even sure am I going overboard with this kind of stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
547 if (!PeekNext().compare (";"))
ad027ea58097 Lifted the ';'-after-events rule, though it can still be present. C++ allows it for functions, so this feels like the best solution regarding consistency.. I'm not even sure am I going overboard with this kind of stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
548 MustNext (";");
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
549 continue;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
550 }
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
551
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
552 // ============================================================
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
553 // If nothing else, parse it as a statement (which is either
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
554 // assignment or expression)
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
555 DataBuffer* b = ParseStatement (w);
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
556 w->WriteBuffer (b);
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
557 MustNext (";");
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
558 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
559
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
560 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
561 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
562
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
563 // 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
564 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
565 ParserError ("script must have a state named `stateSpawn`!");
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
566
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
567 // Dump the last state's onenter and mainloop
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
568 w->WriteBuffers ();
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
569
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
570 // String table
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
571 w->WriteStringTable ();
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
572 }
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
573
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
574 // ============================================================================
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
575 // Parses a command call
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
576 DataBuffer* ScriptReader::ParseCommand (CommandDef* comm) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
577 DataBuffer* r = new DataBuffer(64);
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
578 if (g_CurMode == MODE_TOPLEVEL)
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
579 ParserError ("command call at top level");
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
580
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
581 MustNext ("(");
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
582 MustNext ();
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
583
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
584 int curarg = 0;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
585 while (1) {
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
586 if (!token.compare (")")) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
587 if (curarg < comm->numargs - 1)
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
588 ParserError ("too few arguments passed to %s\n", comm->name.chars());
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
589 break;
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
590 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
591 }
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
592
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
593 if (curarg >= comm->maxargs)
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
594 ParserError ("too many arguments passed to %s\n", comm->name.chars());
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
595
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
596 r->Merge (ParseExpression (comm->argtypes[curarg]));
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
597 MustNext ();
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
598
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
599 if (curarg < comm->numargs - 1) {
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
600 MustThis (",");
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
601 MustNext ();
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
602 } else if (curarg < comm->maxargs - 1) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
603 // Can continue, but can terminate as well.
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
604 if (!token.compare (")")) {
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
605 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
606 break;
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
607 } else {
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
608 MustThis (",");
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
609 MustNext ();
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
610 }
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
611 }
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
612
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
613 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
614 }
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
615
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
616 // 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
617 while (curarg < comm->maxargs) {
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
618 r->Write<word> (DH_PUSHNUMBER);
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
619 r->Write<word> (comm->defvals[curarg]);
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
620 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
621 }
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
622
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
623 r->Write<word> (DH_COMMAND);
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
624 r->Write<word> (comm->number);
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
625 r->Write<word> (comm->maxargs);
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
626
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
627 return r;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
628 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
629
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
630 // ============================================================================
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
631 // Is the given operator an assignment operator?
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
632 static bool IsAssignmentOperator (int oper) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
633 switch (oper) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
634 case OPER_ASSIGNADD:
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
635 case OPER_ASSIGNSUB:
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
636 case OPER_ASSIGNMUL:
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
637 case OPER_ASSIGNDIV:
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
638 case OPER_ASSIGNMOD:
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
639 case OPER_ASSIGNLEFTSHIFT:
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
640 case OPER_ASSIGNRIGHTSHIFT:
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
641 case OPER_ASSIGN:
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
642 return true;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
643 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
644 return false;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
645 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
646
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
647 // ============================================================================
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
648 // Finds an operator's corresponding dataheader
52
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
649 static word DataHeaderByOperator (ScriptVar* var, int oper) {
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
650 if (IsAssignmentOperator (oper)) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
651 if (!var)
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
652 error ("operator %d requires left operand to be a variable\n", oper);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
653
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
654 // TODO: At the moment, vars only are global
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
655 // OPER_ASSIGNLEFTSHIFT and OPER_ASSIGNRIGHTSHIFT do not
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
656 // have data headers, instead they are expanded out in
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
657 // the operator parser
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
658 switch (oper) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
659 case OPER_ASSIGNADD: return DH_ADDGLOBALVAR;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
660 case OPER_ASSIGNSUB: return DH_SUBGLOBALVAR;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
661 case OPER_ASSIGNMUL: return DH_MULGLOBALVAR;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
662 case OPER_ASSIGNDIV: return DH_DIVGLOBALVAR;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
663 case OPER_ASSIGNMOD: return DH_MODGLOBALVAR;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
664 case OPER_ASSIGN: return DH_ASSIGNGLOBALVAR;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
665 default: error ("bad assignment operator!!\n");
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
666 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
667 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
668
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
669 switch (oper) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
670 case OPER_ADD: return DH_ADD;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
671 case OPER_SUBTRACT: return DH_SUBTRACT;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
672 case OPER_MULTIPLY: return DH_MULTIPLY;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
673 case OPER_DIVIDE: return DH_DIVIDE;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
674 case OPER_MODULUS: return DH_MODULUS;
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
675 case OPER_EQUALS: return DH_EQUALS;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
676 case OPER_NOTEQUALS: return DH_NOTEQUALS;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
677 case OPER_LESSTHAN: return DH_LESSTHAN;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
678 case OPER_GREATERTHAN: return DH_GREATERTHAN;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
679 case OPER_LESSTHANEQUALS: return DH_LESSTHANEQUALS;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
680 case OPER_GREATERTHANEQUALS: return DH_GREATERTHANEQUALS;
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
681 case OPER_LEFTSHIFT: return DH_LSHIFT;
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
682 case OPER_RIGHTSHIFT: return DH_RSHIFT;
53
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
683 case OPER_OR: return DH_ORLOGICAL;
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
684 case OPER_AND: return DH_ANDLOGICAL;
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
685 case OPER_BITWISEOR: return DH_ORBITWISE;
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
686 case OPER_BITWISEEOR: return DH_EORBITWISE;
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
687 case OPER_BITWISEAND: return DH_ANDBITWISE;
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
688 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
689
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
690 error ("DataHeaderByOperator: couldn't find dataheader for operator %d!\n", oper);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
691 return 0;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
692 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
693
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
694 // ============================================================================
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
695 // Parses an expression, potentially recursively
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
696 DataBuffer* ScriptReader::ParseExpression (int reqtype) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
697 DataBuffer* retbuf = new DataBuffer (64);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
698
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
699 // Parse first operand
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
700 retbuf->Merge (ParseExprValue (reqtype));
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
701
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
702 // Parse any and all operators we get
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
703 int oper;
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
704 while ((oper = ParseOperator (true)) != -1) {
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
705 // We peeked the operator, move forward now
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
706 MustNext();
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
707
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
708 // Can't be an assignement operator, those belong in assignments.
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
709 if (IsAssignmentOperator (oper))
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
710 ParserError ("assignment operator inside expression");
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
711
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
712 // Parse the right operand,
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
713 MustNext ();
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
714 DataBuffer* rb = ParseExprValue (reqtype);
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
715
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
716 // Write to buffer
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
717 retbuf->Merge (rb);
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
718 long dh = DataHeaderByOperator (NULL, oper);
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
719 retbuf->Write<word> (dh);
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
720 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
721
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
722 return retbuf;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
723 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
724
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
725 // ============================================================================
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
726 // Parses an operator string. Returns the operator number code.
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
727 #define ISNEXT(char) !PeekNext (peek ? 1 : 0).compare (char)
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
728 int ScriptReader::ParseOperator (bool peek) {
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
729 str oper;
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
730 if (peek)
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
731 oper += PeekNext ();
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
732 else
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
733 oper += token;
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
734
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
735 // Check one-char operators
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
736 bool equalsnext = ISNEXT ("=");
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
737
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
738 int o = (!oper.compare ("=") && !equalsnext) ? OPER_ASSIGN :
53
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
739 (!oper.compare (">") && !equalsnext && !ISNEXT (">")) ? OPER_GREATERTHAN :
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
740 (!oper.compare ("<") && !equalsnext && !ISNEXT ("<")) ? OPER_LESSTHAN :
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
741 (!oper.compare ("&") && !ISNEXT ("&")) ? OPER_BITWISEAND :
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
742 (!oper.compare ("|") && !ISNEXT ("|")) ? OPER_BITWISEOR :
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
743 !oper.compare ("^") ? OPER_BITWISEEOR :
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
744 !oper.compare ("+") ? OPER_ADD :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
745 !oper.compare ("-") ? OPER_SUBTRACT :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
746 !oper.compare ("*") ? OPER_MULTIPLY :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
747 !oper.compare ("/") ? OPER_DIVIDE :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
748 !oper.compare ("%") ? OPER_MODULUS :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
749 -1;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
750
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
751 if (o != -1) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
752 return o;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
753 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
754
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
755 // Two-char operators
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
756 oper += PeekNext (peek ? 1 : 0);
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
757 equalsnext = !PeekNext (peek ? 2 : 1).compare ("=");
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
758
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
759 o = !oper.compare ("+=") ? OPER_ASSIGNADD :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
760 !oper.compare ("-=") ? OPER_ASSIGNSUB :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
761 !oper.compare ("*=") ? OPER_ASSIGNMUL :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
762 !oper.compare ("/=") ? OPER_ASSIGNDIV :
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
763 !oper.compare ("%=") ? OPER_ASSIGNMOD :
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
764 !oper.compare ("==") ? OPER_EQUALS :
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
765 !oper.compare ("!=") ? OPER_NOTEQUALS :
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
766 !oper.compare (">=") ? OPER_GREATERTHANEQUALS :
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
767 !oper.compare ("<=") ? OPER_LESSTHANEQUALS :
53
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
768 !oper.compare ("&&") ? OPER_AND :
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
769 !oper.compare ("||") ? OPER_OR :
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
770 (!oper.compare ("<<") && !equalsnext) ? OPER_LEFTSHIFT :
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
771 (!oper.compare (">>") && !equalsnext) ? OPER_RIGHTSHIFT :
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
772 -1;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
773
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
774 if (o != -1) {
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
775 MustNext ();
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
776 return o;
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
777 }
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
778
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
779 // Three-char opers
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
780 oper += PeekNext (peek ? 2 : 1);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
781 o = !oper.compare ("<<=") ? OPER_ASSIGNLEFTSHIFT :
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
782 !oper.compare (">>=") ? OPER_ASSIGNRIGHTSHIFT :
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
783 -1;
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
784
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
785 if (o != -1) {
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
786 MustNext ();
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
787 MustNext ();
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
788 }
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
789
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
790 return o;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
791 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
792
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
793 // ============================================================================
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
794 // Parses a value in the expression and returns the data needed to push
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
795 // it, contained in a data buffer. A value can be either a variable, a command,
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
796 // a literal or an expression.
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
797 DataBuffer* ScriptReader::ParseExprValue (int reqtype) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
798 DataBuffer* b = new DataBuffer(16);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
799
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
800 ScriptVar* g;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
801
52
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
802 // Prefixing "!" means negation.
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
803 bool negate = !token.compare ("!");
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
804 if (negate) // Jump past the "!"
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
805 MustNext ();
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
806
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
807 if (!token.compare ("(")) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
808 // Expression
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
809 MustNext ();
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
810 DataBuffer* c = ParseExpression (reqtype);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
811 b->Merge (c);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
812 MustNext (")");
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
813 } else if (CommandDef* comm = FindCommand (token)) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
814 delete b;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
815
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
816 // Command
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
817 if (reqtype && comm->returnvalue != reqtype)
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
818 ParserError ("%s returns an incompatible data type", comm->name.chars());
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
819 b = ParseCommand (comm);
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
820 } else if ((g = FindGlobalVariable (token)) && reqtype != TYPE_STRING) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
821 // Global variable
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
822 b->Write<word> (DH_PUSHGLOBALVAR);
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
823 b->Write<word> (g->index);
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
824 } else {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
825 // If nothing else, check for literal
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
826 switch (reqtype) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
827 case TYPE_VOID:
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
828 ParserError ("unknown identifier `%s` (expected keyword, function or variable)", token.chars());
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
829 break;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
830 case TYPE_INT: {
35
3d3f6ed40171 Negative literal integers work properly now..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 34
diff changeset
831 MustNumber (true);
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
832
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
833 // All values are written unsigned - thus we need to write the value's
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
834 // absolute value, followed by an unary minus if it was negative.
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
835 b->Write<word> (DH_PUSHNUMBER);
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
836
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
837 long v = atoi (token.chars ());
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
838 b->Write<word> (static_cast<word> (abs (v)));
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
839 if (v < 0)
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
840 b->Write<word> (DH_UNARYMINUS);
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
841 break;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
842 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
843 case TYPE_STRING:
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
844 // PushToStringTable either returns the string index of the
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
845 // string if it finds it in the table, or writes it to the
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
846 // table and returns it index if it doesn't find it there.
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
847 MustString (true);
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
848 b->Write<word> (DH_PUSHSTRINGINDEX);
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
849 b->Write<word> (PushToStringTable (token.chars()));
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
850 break;
54
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
851 case TYPE_FLOAT: {
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
852 str floatstring;
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
853
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
854 MustNumber (true);
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
855 floatstring += token;
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
856
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
857 // Go after the decimal point
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
858 if (!PeekNext ().compare(".")) {
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
859 MustNext (".");
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
860 MustNumber (false);
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
861 floatstring += ".";
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
862 floatstring += token;
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
863 }
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
864
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
865 // TODO: Casting float to word causes the decimal to be lost.
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
866 // Find a way to store the number without such loss.
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
867 float val = atof (floatstring);
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
868 b->Write<word> (DH_PUSHNUMBER);
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
869 b->Write<word> (static_cast<word> ((val > 0) ? val : -val));
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
870 if (val < 0)
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
871 b->Write<word> (DH_UNARYMINUS);
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
872
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
873 // TODO: Keep this check after decimal loss is fixed, but make
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
874 // it a real precision loss check. 55.5123 -> 55.512299, this
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
875 // should probably be warned of.
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
876 float check = static_cast<float> (static_cast<word> (val));
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
877 if (val != check)
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
878 ParserWarning ("floating point number %f loses precision (-> %f)", val, check);
8cc91ef94754 Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 53
diff changeset
879 }
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
880 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
881 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
882
52
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
883 // Negate it now if desired
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
884 if (negate)
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
885 b->Write<word> (DH_NEGATELOGICAL);
f2596a239ea1 Added ! operator support. It's not really an operator since it's just checked at the beginning of the expression value but eh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 51
diff changeset
886
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
887 return b;
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
888 }
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
889
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
890 // ============================================================================
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
891 // Parses an assignment. An assignment starts with a variable name, followed
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
892 // by an assignment operator, followed by an expression value. Expects current
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
893 // token to be the name of the variable, and expects the variable to be given.
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
894 DataBuffer* ScriptReader::ParseAssignment (ScriptVar* var) {
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
895 bool global = !var->statename.len ();
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
896
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
897 // Get an operator
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
898 MustNext ();
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
899 int oper = ParseOperator ();
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
900 if (!IsAssignmentOperator (oper))
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
901 ParserError ("expected assignment operator");
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
902
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
903 if (g_CurMode == MODE_TOPLEVEL) // TODO: lift this restriction
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
904 ParserError ("can't alter variables at top level");
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
905
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
906 // Parse the right operand
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
907 MustNext ();
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
908 DataBuffer* retbuf = new DataBuffer;
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
909 DataBuffer* expr = ParseExpression (TYPE_INT);
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
910
51
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
911 // <<= and >>= do not have data headers. Solution: expand them.
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
912 // a <<= b -> a = a << b
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
913 // a >>= b -> a = a >> b
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
914 if (oper == OPER_ASSIGNLEFTSHIFT || oper == OPER_ASSIGNRIGHTSHIFT) {
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
915 retbuf->Write<word> (global ? DH_PUSHGLOBALVAR : DH_PUSHLOCALVAR);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
916 retbuf->Write<word> (var->index);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
917 retbuf->Merge (expr);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
918 retbuf->Write<word> ((oper == OPER_ASSIGNLEFTSHIFT) ? DH_LSHIFT : DH_RSHIFT);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
919 retbuf->Write<word> (global ? DH_ASSIGNGLOBALVAR : DH_ASSIGNLOCALVAR);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
920 retbuf->Write<word> (var->index);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
921 } else {
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
922 retbuf->Merge (expr);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
923 long dh = DataHeaderByOperator (var, oper);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
924 retbuf->Write<word> (dh);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
925 retbuf->Write<word> (var->index);
2cfa6edbf928 Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
926 }
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
927
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 35
diff changeset
928 return retbuf;
40
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
929 }
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
930
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
931 void ScriptReader::PushBlockStack () {
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
932 g_BlockStackCursor++;
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
933 BlockInformation* info = &blockstack[g_BlockStackCursor];
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
934 info->type = 0;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
935 info->mark1 = 0;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
936 info->mark2 = 0;
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
937 info->buffer1 = NULL;
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
938 info->casecursor = -1;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
939 for (int i = 0; i < MAX_CASE; i++) {
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
940 info->casemarks[i] = MAX_MARKS;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
941 info->casebuffers[i] = NULL;
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
942 info->casenumbers[i] = -1;
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
943 }
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
944 }
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
945
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
946 DataBuffer* ScriptReader::ParseStatement (ObjWriter* w) {
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
947 // If it's a variable, expect assignment.
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
948 if (ScriptVar* var = FindGlobalVariable (token)) {
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
949 DataBuffer* b = ParseAssignment (var);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
950 return b;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
951 }
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
952
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
953 // If it's not a keyword, parse it as an expression.
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
954 DataBuffer* b = ParseExpression (TYPE_VOID);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
955 return b;
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
956 }
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
957
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
958 void ScriptReader::AddSwitchCase (ObjWriter* w, DataBuffer* b) {
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
959 BlockInformation* info = &blockstack[g_BlockStackCursor];
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
960
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
961 info->casecursor++;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
962 if (info->casecursor >= MAX_CASE)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
963 ParserError ("too many cases in one switch");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
964
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
965 // Init a mark for the case buffer
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
966 int m = w->AddMark ("");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
967 info->casemarks[info->casecursor] = m;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
968
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
969 // Add a reference to the mark. "case" and "default" both
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
970 // add the necessary bytecode before the reference.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
971 if (b)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
972 b->AddMarkReference (m);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
973 else
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
974 w->AddReference (m);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
975
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
976 // Init a buffer for the case block and tell the object
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
977 // writer to record all written data to it.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
978 info->casebuffers[info->casecursor] = w->SwitchBuffer = new DataBuffer;
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
979 }

mercurial