parser.cxx

Wed, 19 Dec 2012 22:01:42 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Wed, 19 Dec 2012 22:01:42 +0200
changeset 69
29a3e669d648
parent 68
588cc27e84bb
permissions
-rw-r--r--

So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.

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"
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
53 #include "array.h"
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
55 #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
56 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
57
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
58 #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
59 ParserError ("%s-statements may not be defined at top level!", token.chars());
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
61 #define SCOPE(n) scopestack[g_ScopeCursor - n]
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
62
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
63 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
64 int g_NumEvents = 0;
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
65 parsermode_e g_CurMode = MODE_TOPLEVEL;
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
66 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
67 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
68 bool g_GotMainLoop = false;
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
69 unsigned int g_ScopeCursor = 0;
40
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
70 DataBuffer* g_IfExpression = NULL;
57
96f60ca748a0 Fixed: else was alloed even without if strictly preceding it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
71 bool g_CanElse = false;
62
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
72 str* g_UndefinedLabels[MAX_MARKS];
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
73 bool g_Neurosphere = false; // neurosphere-compat
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
74 array<constinfo_t> g_ConstInfo;
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
75
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
76 // ============================================================================
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
77 // 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
78 // 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
79 // 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
80 void ScriptReader::ParseBotScript (ObjWriter* w) {
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
81 // Zero the entire block stack first
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
82 for (int i = 0; i < MAX_SCOPE; i++)
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
83 ZERO(scopestack[i]);
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
84
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
85 for (int i = 0; i < MAX_MARKS; i++)
62
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
86 g_UndefinedLabels[i] = NULL;
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
87
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 while (Next()) {
57
96f60ca748a0 Fixed: else was alloed even without if strictly preceding it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
89 // Check if else is potentically valid
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
90 if (token == "else" && !g_CanElse)
57
96f60ca748a0 Fixed: else was alloed even without if strictly preceding it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
91 ParserError ("else without preceding if");
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
92 if (token != "else")
57
96f60ca748a0 Fixed: else was alloed even without if strictly preceding it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
93 g_CanElse = false;
96f60ca748a0 Fixed: else was alloed even without if strictly preceding it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
94
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
95 // ============================================================
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
96 if (token == "state") {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 MUST_TOPLEVEL
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
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
99 MustString ();
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 // 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
102 if (token.first (" ") != token.len())
65
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
103 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
104 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
105
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
106 // 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
107 // encountered it, then mark down that we have it.
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
108 if (-token == "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
109 g_stateSpawnDefined = true;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 // Must end in a colon
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 MustNext (":");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113
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
114 // 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
115 // 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
116 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
117 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
118
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
119 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
120 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
121 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
122 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
123
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
124 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
125 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
126 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
127 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
128 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
129
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
130 // ============================================================
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
131 if (token == "event") {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 MUST_TOPLEVEL
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 // 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
135 MustString ();
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136
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
137 EventDef* e = FindEventByName (token);
2
bb2c45522eb6 Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
138 if (!e)
65
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
139 ParserError ("bad event, got `%s`\n", token.chars());
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 MustNext ("{");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
143 g_CurMode = MODE_EVENT;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 w->Write (DH_EVENT);
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
146 w->Write (e->number);
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
147 g_NumEvents++;
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
148 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
149 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
150
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
151 // ============================================================
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
152 if (token == "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
153 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
154 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
155
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
156 // 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
157 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
158 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
159 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
160 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
161
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
162 // ============================================================
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
163 if (token == "onenter" || token == "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
164 MUST_TOPLEVEL
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
165 bool onenter = token == "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
166 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
167
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
168 // 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
169 // 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
170 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
171 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
172 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
173 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
174
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
175 // ============================================================
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
176 if (token == "int" || token == "str" || token == "bool") {
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
177 // For now, only globals are supported
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
178 if (g_CurMode != MODE_TOPLEVEL || g_CurState.len())
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
179 ParserError ("variables must only be global for now");
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
180
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
181 type_e type = (token == "int") ? TYPE_INT :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
182 (token == "str") ? TYPE_STRING :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
183 TYPE_BOOL;
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
184
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
185 MustNext ();
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
186
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
187 // 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
188 if (token.isnumber())
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
189 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
190
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
191 str varname = token;
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
192 ScriptVar* var = DeclareGlobalVariable (this, type, varname);
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
193
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
194 if (!var)
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
195 ParserError ("declaring %s variable %s failed",
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
196 g_CurState.len() ? "state" : "global", varname.chars());
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
197
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
198 MustNext (";");
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
199 continue;
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
200 }
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
201
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
202 // ============================================================
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
203 // Goto
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
204 if (token == "goto") {
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
205 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
206
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
207 // 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
208 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
209
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
210 // Find the mark this goto statement points to
45
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
211 unsigned int m = w->FindMark (token);
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
212
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
213 // If not set, define it
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
214 if (m == MAX_MARKS) {
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
215 m = w->AddMark (token);
62
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
216 g_UndefinedLabels[m] = new str (token);
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
217 }
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
218
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
219 // Add a reference to the mark.
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
220 w->Write (DH_GOTO);
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
221 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
222 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
223 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
224 }
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
225
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
226 // ============================================================
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
227 // If
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
228 if (token == "if") {
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
229 MUST_NOT_TOPLEVEL
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
230 PushScope ();
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
231
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
232 // Condition
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
233 MustNext ("(");
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
234
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
235 // Read the expression and write it.
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
236 MustNext ();
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
237 DataBuffer* c = ParseExpression (TYPE_INT);
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
238 w->WriteBuffer (c);
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
239
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
240 MustNext (")");
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
241 MustNext ("{");
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
242
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
243 // 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
244 // 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
245 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
246
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
247 // Use DH_IFNOTGOTO - if the expression is not true, we goto the mark
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
248 // we just defined - and this mark will be at the end of the scope block.
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
249 w->Write (DH_IFNOTGOTO);
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
250 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
251
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
252 // Store it
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
253 SCOPE(0).mark1 = marknum;
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
254 SCOPE(0).type = SCOPETYPE_IF;
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
255 continue;
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
256 }
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
257
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
258 if (token == "else") {
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
259 MUST_NOT_TOPLEVEL
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
260 MustNext ("{");
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
261
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
262 // Don't use PushScope as it resets the scope
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
263 g_ScopeCursor++;
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
264 if (g_ScopeCursor >= MAX_SCOPE)
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
265 ParserError ("too deep scope");
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
266
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
267 if (SCOPE(0).type != SCOPETYPE_IF)
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
268 ParserError ("else without preceding if");
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
269
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
270 // Write down to jump to the end of the else statement
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
271 // Otherwise we have fall-throughs
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
272 SCOPE(0).mark2 = w->AddMark ("");
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
273
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
274 // Instruction to jump to the end after if block is complete
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
275 w->Write (DH_GOTO);
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
276 w->AddReference (SCOPE(0).mark2);
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
277
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
278 // Move the ifnot mark here and set type to else
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
279 w->MoveMark (SCOPE(0).mark1);
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
280 SCOPE(0).type = SCOPETYPE_ELSE;
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
281 continue;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
282 }
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
283
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
284 // ============================================================
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
285 // While
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
286 if (token == "while") {
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
287 MUST_NOT_TOPLEVEL
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
288 PushScope ();
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
289
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
290 // 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
291 // 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
292 // 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
293 // 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
294 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
295 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
296
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
297 // Condition
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
298 MustNext ("(");
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
299 MustNext ();
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
300 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
301 MustNext (")");
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
302 MustNext ("{");
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
303
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
304 // Write condition
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
305 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
306
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
307 // Instruction to go to the end if it fails
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
308 w->Write (DH_IFNOTGOTO);
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
309 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
310
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
311 // Store the needed stuff
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
312 SCOPE(0).mark1 = mark1;
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
313 SCOPE(0).mark2 = mark2;
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
314 SCOPE(0).type = SCOPETYPE_WHILE;
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
315 continue;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
316 }
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
317
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
318 // ============================================================
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
319 // For loop
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
320 if (token == "for") {
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
321 MUST_NOT_TOPLEVEL
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
322 PushScope ();
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
323
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
324 // Initializer
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
325 MustNext ("(");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
326 MustNext ();
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
327 DataBuffer* init = ParseStatement (w);
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
328 if (!init)
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
329 ParserError ("bad statement for initializer of for");
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
330
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
331 MustNext (";");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
332
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
333 // Condition
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
334 MustNext ();
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
335 DataBuffer* cond = ParseExpression (TYPE_INT);
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
336 if (!cond)
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
337 ParserError ("bad statement for condition of for");
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
338
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
339 MustNext (";");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
340
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
341 // Incrementor
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
342 MustNext ();
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
343 DataBuffer* incr = ParseStatement (w);
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
344 if (!incr)
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
345 ParserError ("bad statement for incrementor of for");
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
346
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
347 MustNext (")");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
348 MustNext ("{");
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
349
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
350 // First, write out the initializer
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
351 w->WriteBuffer (init);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
352
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
353 // Init two marks
45
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
354 int mark1 = w->AddMark ("");
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
355 int mark2 = w->AddMark ("");
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
356
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
357 // Add the condition
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
358 w->WriteBuffer (cond);
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
359 w->Write (DH_IFNOTGOTO);
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
360 w->AddReference (mark2);
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
361
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
362 // Store the marks and incrementor
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
363 SCOPE(0).mark1 = mark1;
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
364 SCOPE(0).mark2 = mark2;
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
365 SCOPE(0).buffer1 = incr;
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
366 SCOPE(0).type = SCOPETYPE_FOR;
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
367 continue;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
368 }
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
369
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
370 // ============================================================
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
371 // Do/while loop
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
372 if (token == "do") {
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
373 MUST_NOT_TOPLEVEL
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
374 PushScope ();
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
375 MustNext ("{");
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
376 SCOPE(0).mark1 = w->AddMark ("");
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
377 SCOPE(0).type = SCOPETYPE_DO;
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
378 continue;
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
379 }
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
380
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
381 // ============================================================
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
382 // Switch
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
383 if (token == "switch") {
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
384 /* 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
385 * bytecode followingly:
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
386 * (expression)
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
387 * 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
388 * 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
389 * 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
390 * 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
391 * casemark1: ...
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
392 * casemark2: ...
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
393 * casemark3: ...
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
394 * mark1: // end mark
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
395 */
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
396
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
397 MUST_NOT_TOPLEVEL
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
398 PushScope ();
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
399 MustNext ("(");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
400 MustNext ();
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
401 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
402 MustNext (")");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
403 MustNext ("{");
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
404 SCOPE(0).type = SCOPETYPE_SWITCH;
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
405 SCOPE(0).mark1 = w->AddMark (""); // end mark
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
406 SCOPE(0).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
407 continue;
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
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
410 // ============================================================
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
411 if (token == "case") {
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
412 // case is only allowed inside switch
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
413 if (SCOPE(0).type != SCOPETYPE_SWITCH)
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
414 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
415
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
416 // 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
417 MustNumber ();
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
418 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
419 MustNext (":");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
420
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
421 for (int i = 0; i < MAX_CASE; i++)
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
422 if (SCOPE(0).casenumbers[i] == num)
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
423 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
424
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
425 // 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
426 // 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
427 // blocks and move the marks appropriately.
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
428 // AddSwitchCase will add the reference to the mark
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
429 // 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
430 // of buffering setup and stuff like that.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
431 // 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
432 // 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
433 w->SwitchBuffer = NULL;
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
434 w->Write (DH_CASEGOTO);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
435 w->Write (num);
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
436 AddSwitchCase (w, NULL);
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
437 SCOPE(0).casenumbers[SCOPE(0).casecursor] = num;
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
438 continue;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
439 }
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
440
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
441 if (token == "default") {
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
442 if (SCOPE(0).type != SCOPETYPE_SWITCH)
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
443 ParserError ("default label outside switch");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
444
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
445 if (SCOPE(0).buffer1)
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
446 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
447
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
448 MustNext (":");
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 // The default header is buffered into buffer1, since
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
451 // 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
452 //
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
453 // Since the expression is pushed into the switch
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
454 // and is only popped when case succeeds, we have
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
455 // 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
456 // a default.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
457 DataBuffer* b = new DataBuffer;
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
458 SCOPE(0).buffer1 = b;
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
459 b->Write (DH_DROP);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
460 b->Write (DH_GOTO);
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
461 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
462 continue;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
463 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
464
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
465 // ============================================================
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
466 // Break statement.
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
467 if (token == "break") {
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
468 if (!g_ScopeCursor)
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
469 ParserError ("unexpected `break`");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
470
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
471 w->Write (DH_GOTO);
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
472
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
473 // 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
474 // for and while use mark2.
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
475 switch (SCOPE(0).type) {
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
476 case SCOPETYPE_IF:
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
477 case SCOPETYPE_SWITCH:
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
478 w->AddReference (SCOPE(0).mark1);
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
479 break;
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
480 case SCOPETYPE_FOR:
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
481 case SCOPETYPE_WHILE:
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
482 w->AddReference (SCOPE(0).mark2);
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
483 break;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
484 default:
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
485 ParserError ("unexpected `break`");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
486 break;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
487 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
488
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
489 MustNext (";");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
490 continue;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
491 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
492
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
493 // ============================================================
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
494 // Continue
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
495 if (token == "continue") {
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
496 MustNext (";");
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
497
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
498 int curs;
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
499 bool found = false;
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
500
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
501 // Drop through the scope until we find a loop block
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
502 for (curs = g_ScopeCursor; curs > 0 && !found; curs--) {
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
503 switch (scopestack[curs].type) {
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
504 case SCOPETYPE_FOR:
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
505 case SCOPETYPE_WHILE:
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
506 case SCOPETYPE_DO:
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
507 w->Write (DH_GOTO);
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
508 w->AddReference (scopestack[curs].mark1);
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
509 found = true;
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
510 break;
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
511 default:
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
512 break;
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
513 }
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
514 }
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
515
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
516 // No loop blocks
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
517 if (!found)
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
518 ParserError ("`continue`-statement not inside a loop");
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
519
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
520 continue;
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
521 }
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
522
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
523 // ============================================================
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
524 // Label
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
525 if (PeekNext() == ":") {
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
526 MUST_NOT_TOPLEVEL
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
527
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
528 // want no conflicts..
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
529 if (IsKeyword (token))
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
530 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
531 if (FindCommand (token))
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
532 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
533 if (FindGlobalVariable (token))
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
534 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
535
62
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
536 // See if a mark already exists for this label
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
537 int mark = -1;
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
538 for (int i = 0; i < MAX_MARKS; i++) {
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
539 if (g_UndefinedLabels[i] && *g_UndefinedLabels[i] == token) {
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
540 mark = i;
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
541 w->MoveMark (i);
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
542
62
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
543 // No longer undefinde
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
544 delete g_UndefinedLabels[i];
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
545 g_UndefinedLabels[i] = NULL;
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
546 }
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
547 }
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
548
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
549 // Not found in unmarked lists, define it now
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
550 if (mark == -1)
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
551 w->AddMark (token);
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
552
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
553 MustNext (":");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
554 continue;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
555 }
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
556
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
557 // ============================================================
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
558 if (token == "const") {
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
559 constinfo_t info;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
560
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
561 // Get the type
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
562 MustNext ();
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
563 info.type = GetTypeByName (token);
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
564
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
565 if (info.type == TYPE_UNKNOWN || info.type == TYPE_VOID)
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
566 ParserError ("unknown type `%s` for constant", (char*)token);
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
567
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
568 MustNext ();
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
569 info.name = token;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
570
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
571 MustNext ("=");
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
572
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
573 switch (info.type) {
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
574 case TYPE_BOOL:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
575 case TYPE_INT:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
576 MustNumber (false);
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
577 info.val = token;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
578 break;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
579 case TYPE_STRING:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
580 MustString ();
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
581 info.val = token;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
582 break;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
583 case TYPE_UNKNOWN:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
584 case TYPE_VOID:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
585 break;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
586 }
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
587
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
588 g_ConstInfo << info;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
589
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
590 MustNext (";");
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
591 continue;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
592 }
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
593
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
594 // ============================================================
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
595 if (token == "}") {
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
596 // Closing brace
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
597
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
598 // If we're in the block stack, we're descending down from it now
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
599 if (g_ScopeCursor > 0) {
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
600 switch (SCOPE(0).type) {
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
601 case SCOPETYPE_IF:
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
602 // Adjust the closing mark.
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
603 w->MoveMark (SCOPE(0).mark1);
57
96f60ca748a0 Fixed: else was alloed even without if strictly preceding it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
604
96f60ca748a0 Fixed: else was alloed even without if strictly preceding it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
605 // We're returning from if, thus else can be next
96f60ca748a0 Fixed: else was alloed even without if strictly preceding it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
606 g_CanElse = true;
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
607 break;
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
608 case SCOPETYPE_ELSE:
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
609 // else instead uses mark1 for itself (so if expression
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
610 // fails, jump to else), mark2 means end of else
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
611 w->MoveMark (SCOPE(0).mark2);
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
612 break;
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
613 case SCOPETYPE_FOR:
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
614 // Write the incrementor at the end of the loop block
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
615 w->WriteBuffer (SCOPE(0).buffer1);
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
616 // fall-thru
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
617 case SCOPETYPE_WHILE:
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
618 // 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
619 w->Write (DH_GOTO);
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
620 w->AddReference (SCOPE(0).mark1);
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
621
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
622 // Move the closing mark here since we're at the end of the while loop
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
623 w->MoveMark (SCOPE(0).mark2);
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
624 break;
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
625 case SCOPETYPE_DO: {
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
626 MustNext ("while");
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
627 MustNext ("(");
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
628 MustNext ();
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
629 DataBuffer* expr = ParseExpression (TYPE_INT);
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
630 MustNext (")");
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
631 MustNext (";");
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
632
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
633 // 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
634 w->WriteBuffer (expr);
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
635 w->Write (DH_IFGOTO);
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
636 w->AddReference (SCOPE(0).mark1);
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
637 break;
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
638 }
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
639 case SCOPETYPE_SWITCH: {
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
640 // 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
641 // the lower block.
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
642 if (SCOPE(1).casecursor != -1)
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
643 w->SwitchBuffer = SCOPE(1).casebuffers[SCOPE(1).casecursor];
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
644 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
645 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
646
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
647 // 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
648 // 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
649 // the headers (thus won't fall-through if no case matched)
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
650 if (SCOPE(0).buffer1)
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
651 w->WriteBuffer (SCOPE(0).buffer1);
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
652 else {
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
653 w->Write (DH_DROP);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
654 w->Write (DH_GOTO);
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
655 w->AddReference (SCOPE(0).mark1);
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
656 }
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
657
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
658 // 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
659 // 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
660 for (unsigned int u = 0; u < MAX_CASE; u++) {
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
661 if (!SCOPE(0).casebuffers[u])
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
662 continue;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
663
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
664 w->MoveMark (SCOPE(0).casemarks[u]);
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
665 w->WriteBuffer (SCOPE(0).casebuffers[u]);
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
666 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
667
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
668 // Move the closing mark here
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
669 w->MoveMark (SCOPE(0).mark1);
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
670 break;
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
671 }
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
672 case SCOPETYPE_UNKNOWN:
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
673 break;
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
674 }
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
675
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
676 // Descend down the stack
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
677 g_ScopeCursor--;
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
678 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
679 }
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
680
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
681 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
682 (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
683 (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
684 (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
685
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
686 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
687 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
688
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
689 // 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
690 // 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
691 // 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
692 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
693 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
694
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
695 if (PeekNext() == ";")
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
696 MustNext (";");
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
697 continue;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
698 }
24
7dcc8419dbdb Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
699
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
700 // Check if it's a command
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
701 CommandDef* comm = FindCommand (token);
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
702 if (comm) {
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
703 w->GetCurrentBuffer()->Merge (ParseCommand (comm));
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
704 MustNext (";");
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
705 continue;
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
706 }
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
707
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
708 // ============================================================
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
709 // If nothing else, parse it as a statement
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
710 DataBuffer* b = ParseStatement (w);
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
711 if (!b)
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
712 ParserError ("unknown token `%s`", token.chars());
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
713
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
714 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
715 MustNext (";");
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
716 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
717
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
718 // ===============================================================================
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
719 // Script file ended. Do some last checks and write the last things to main buffer
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
720 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
721 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
722
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
723 // 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
724 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
725 ParserError ("script must have a state named `stateSpawn`!");
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
726
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
727 for (int i = 0; i < MAX_MARKS; i++)
62
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
728 if (g_UndefinedLabels[i])
824ab7b28e3c renamed to undefined labels, not unmarked labels...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 61
diff changeset
729 ParserError ("label `%s` is referenced via `goto` but isn't defined\n", g_UndefinedLabels[i]->chars());
61
11451e7a2fe6 goto is now allowed to reference labels that will get defined afterwards.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 59
diff changeset
730
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
731 // 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
732 w->WriteBuffers ();
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
733
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 // 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
735 w->WriteStringTable ();
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
736 }
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
737
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
738 // ============================================================================
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
739 // 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
740 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
741 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
742 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
743 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
744
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
745 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
746 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
747
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
748 int curarg = 0;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
749 while (1) {
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
750 if (token == ")") {
64
dc5db6335601 Fixed: parser allowed command calls with one too few arguments
Teemu Piippo <crimsondusk64@gmail.com>
parents: 62
diff changeset
751 if (curarg < comm->numargs)
65
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
752 ParserError ("too few arguments passed to %s\n\tprototype: %s",
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
753 comm->name.chars(), GetCommandPrototype (comm).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
754 break;
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
755 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
756 }
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
757
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
758 if (curarg >= comm->maxargs)
65
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
759 ParserError ("too many arguments passed to %s\n\tprototype: %s",
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
760 comm->name.chars(), GetCommandPrototype (comm).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
761
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
762 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
763 MustNext ();
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
764
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
765 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
766 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
767 MustNext ();
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
768 } else if (curarg < comm->maxargs - 1) {
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
769 // Can continue, but can terminate as well.
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
770 if (token == ")") {
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
771 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
772 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
773 } 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
774 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
775 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
776 }
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
777 }
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
778
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
779 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
780 }
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
781
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
782 // 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
783 while (curarg < comm->maxargs) {
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
784 r->Write (DH_PUSHNUMBER);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
785 r->Write (comm->defvals[curarg]);
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
786 curarg++;
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
787 }
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
788
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
789 r->Write (DH_COMMAND);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
790 r->Write (comm->number);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
791 r->Write (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
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 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
794 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
795
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
796 // ============================================================================
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
797 // 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
798 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
799 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
800 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
801 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
802 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
803 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
804 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
805 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
806 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
807 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
808 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
809 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
810 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
811 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
812
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
813 // ============================================================================
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
814 // 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
815 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
816 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
817 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
818 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
819
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
820 // 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
821 // 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
822 // 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
823 // 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
824 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
825 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
826 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
827 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
828 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
829 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
830 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
831 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
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 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
834
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
835 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
836 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
837 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
838 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
839 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
840 case OPER_MODULUS: return DH_MODULUS;
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
841 case OPER_EQUALS: return DH_EQUALS;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
842 case OPER_NOTEQUALS: return DH_NOTEQUALS;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
843 case OPER_LESSTHAN: return DH_LESSTHAN;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
844 case OPER_GREATERTHAN: return DH_GREATERTHAN;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
845 case OPER_LESSTHANEQUALS: return DH_LESSTHANEQUALS;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
846 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
847 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
848 case OPER_RIGHTSHIFT: return DH_RSHIFT;
53
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
849 case OPER_OR: return DH_ORLOGICAL;
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
850 case OPER_AND: return DH_ANDLOGICAL;
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
851 case OPER_BITWISEOR: return DH_ORBITWISE;
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
852 case OPER_BITWISEEOR: return DH_EORBITWISE;
9ef7e549391f Added support for operators &&, ||, &, | and ^
Teemu Piippo <crimsondusk64@gmail.com>
parents: 52
diff changeset
853 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
854 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
855
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
856 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
857 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
858 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
859
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
860 // ============================================================================
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
861 // Parses an expression, potentially recursively
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
862 DataBuffer* ScriptReader::ParseExpression (type_e 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
863 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
864
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
865 // Parse first operand
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
866 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
867
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
868 // Parse any and all operators we get
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
869 int oper;
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
870 while ((oper = ParseOperator (true)) != -1) {
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
871 // We peeked the operator, move forward now
65
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
872 Next ();
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
873
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
874 // 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
875 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
876 ParserError ("assignment operator inside expression");
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
877
65
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
878 // Parse the right operand.
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
879 MustNext ();
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
880 DataBuffer* rb = ParseExprValue (reqtype);
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
881
55
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
882 if (oper == OPER_TERNARY) {
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
883 // Ternary operator requires - naturally - a third operand.
55
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
884 MustNext (":");
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
885 MustNext ();
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
886 DataBuffer* tb = ParseExprValue (reqtype);
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
887
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
888 // It also is handled differently: there isn't a dataheader for ternary
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
889 // operator. Instead, we abuse PUSHNUMBER and IFNOTGOTO for this.
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
890 // Behold, big block of writing madness! :P
55
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
891 int mark1 = retbuf->AddMark (""); // start of "else" case
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
892 int mark2 = retbuf->AddMark (""); // end of expression
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
893 retbuf->Write (DH_IFNOTGOTO); // if the first operand (condition)
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
894 retbuf->AddMarkReference (mark1); // didn't eval true, jump into mark1
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
895 retbuf->Merge (rb); // otherwise, perform second operand (true case)
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
896 retbuf->Write (DH_GOTO); // afterwards, jump to the end, which is
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
897 retbuf->AddMarkReference (mark2); // marked by mark2.
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
898 retbuf->MoveMark (mark1); // move mark1 at the end of the true case
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
899 retbuf->Merge (tb); // perform third operand (false case)
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
900 retbuf->MoveMark (mark2); // move the ending mark2 here
55
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
901 } else {
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
902 // Write to buffer
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
903 retbuf->Merge (rb);
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
904 retbuf->Write (DataHeaderByOperator (NULL, oper));
55
173956c1ac27 Added support for the ternary operator.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 54
diff changeset
905 }
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
906 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
907
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
908 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
909 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
910
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
911 // ============================================================================
46
a29eeecf3ecb Expressions now allow multiple operators
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
912 // Parses an operator string. Returns the operator number code.
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
913 #define ISNEXT(char) (!PeekNext (peek ? 1 : 0) == 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
914 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
915 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
916 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
917 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
918 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
919 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
920
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
921 if (-oper == "strlen")
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
922 return OPER_STRLEN;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
923
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
924 // 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
925 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
926
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
927 int o = (oper == "=" && !equalsnext) ? OPER_ASSIGN :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
928 (oper == ">" && !equalsnext && !ISNEXT (">")) ? OPER_GREATERTHAN :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
929 (oper == "<" && !equalsnext && !ISNEXT ("<")) ? OPER_LESSTHAN :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
930 (oper == "&" && !ISNEXT ("&")) ? OPER_BITWISEAND :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
931 (oper == "|" && !ISNEXT ("|")) ? OPER_BITWISEOR :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
932 (oper == "+" && !equalsnext) ? OPER_ADD :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
933 (oper == "-" && !equalsnext) ? OPER_SUBTRACT :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
934 (oper == "*" && !equalsnext) ? OPER_MULTIPLY :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
935 (oper == "/" && !equalsnext) ? OPER_DIVIDE :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
936 (oper == "%" && !equalsnext) ? OPER_MODULUS :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
937 (oper == "^") ? OPER_BITWISEEOR :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
938 (oper == "?") ? OPER_TERNARY :
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
939 -1;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
940
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
941 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
942 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
943 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
944
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
945 // 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
946 oper += PeekNext (peek ? 1 : 0);
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
947 equalsnext = PeekNext (peek ? 2 : 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
948
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
949 o = (oper == "+=") ? OPER_ASSIGNADD :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
950 (oper == "-=") ? OPER_ASSIGNSUB :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
951 (oper == "*=") ? OPER_ASSIGNMUL :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
952 (oper == "/=") ? OPER_ASSIGNDIV :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
953 (oper == "%=") ? OPER_ASSIGNMOD :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
954 (oper == "==") ? OPER_EQUALS :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
955 (oper == "!=") ? OPER_NOTEQUALS :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
956 (oper == ">=") ? OPER_GREATERTHANEQUALS :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
957 (oper == "<=") ? OPER_LESSTHANEQUALS :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
958 (oper == "&&") ? OPER_AND :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
959 (oper == "||") ? OPER_OR :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
960 (oper == "<<" && !equalsnext) ? OPER_LEFTSHIFT :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
961 (oper == ">>" && !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
962 -1;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
963
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
964 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
965 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
966 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
967 }
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
968
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
969 // 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
970 oper += PeekNext (peek ? 2 : 1);
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
971 o = oper == "<<=" ? OPER_ASSIGNLEFTSHIFT :
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
972 oper == ">>=" ? OPER_ASSIGNRIGHTSHIFT :
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
973 -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
974
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
975 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
976 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
977 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
978 }
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
979
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
980 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
981 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
982
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
983 // ============================================================================
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
984 str ScriptReader::ParseFloat () {
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
985 MustNumber (true);
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
986 str floatstring = token;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
987
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
988 // Go after the decimal point
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
989 if (PeekNext () == ".") {
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
990 Next (".");
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
991 MustNumber (false);
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
992 floatstring += ".";
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
993 floatstring += token;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
994 }
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
995
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
996 return floatstring;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
997 }
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
998
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
999 // ============================================================================
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1000 // 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
1001 // 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
1002 // a literal or an expression.
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1003 DataBuffer* ScriptReader::ParseExprValue (type_e 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
1004 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
1005
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1006 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
1007
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
1008 // Prefixing "!" means negation.
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
1009 bool negate = (token == "!");
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
1010 if (negate) // Jump past the "!"
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
1011 Next ();
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
1012
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1013 // Handle strlen
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1014 if (token == "strlen") {
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1015 MustNext ("(");
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1016 MustNext ();
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1017
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1018 // By this token we should get a string constant.
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1019 constinfo_t* constant = FindConstant (token);
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1020 if (!constant || constant->type != TYPE_STRING)
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1021 ParserError ("strlen only works with const str");
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1022
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1023 if (reqtype != TYPE_INT)
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1024 ParserError ("strlen returns int but %s is expected\n", (char*)GetTypeName (reqtype));
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1025
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1026 b->Write (DH_PUSHNUMBER);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1027 b->Write (constant->val.len ());
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1028
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1029 MustNext (")");
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1030 } else if (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
1031 // Expression
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1032 MustNext ();
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1033 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
1034 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
1035 MustNext (")");
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1036 } 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
1037 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
1038
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1039 // Command
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1040 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
1041 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
1042 b = ParseCommand (comm);
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1043 } else if (constinfo_t* constant = FindConstant (token)) {
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1044 // Type check
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1045 if (reqtype != constant->type)
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1046 ParserError ("constant `%s` is %s, expression requires %s\n",
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1047 (char*)constant->name, (char*)GetTypeName (constant->type),
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1048 (char*)GetTypeName (reqtype));
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1049
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1050 switch (constant->type) {
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1051 case TYPE_BOOL:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1052 case TYPE_INT:
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1053 b->Write (DH_PUSHNUMBER);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1054 b->Write (atoi (constant->val));
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1055 break;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1056 case TYPE_STRING:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1057 b->WriteString (constant->val);
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1058 break;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1059 case TYPE_VOID:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1060 case TYPE_UNKNOWN:
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1061 break;
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1062 }
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
1063 } else if ((g = FindGlobalVariable (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
1064 // Global variable
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1065 b->Write (DH_PUSHGLOBALVAR);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1066 b->Write (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
1067 } else {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1068 // 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
1069 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
1070 case TYPE_VOID:
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1071 case TYPE_UNKNOWN:
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
1072 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
1073 break;
65
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
1074 case TYPE_BOOL:
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1075 case TYPE_INT: {
35
3d3f6ed40171 Negative literal integers work properly now..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 34
diff changeset
1076 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
1077
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1078 // All values are written unsigned - thus we need to write the value's
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
1079 // absolute value, followed by an unary minus for negatives.
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1080 b->Write (DH_PUSHNUMBER);
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
1081
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
1082 long v = atol (token);
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1083 b->Write (static_cast<word> (abs (v)));
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
1084 if (v < 0)
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1085 b->Write (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
1086 break;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1087 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1088 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
1089 // 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
1090 // 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
1091 // 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
1092 MustString (true);
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1093 b->WriteString (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
1094 break;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1095 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1096 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1097
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
1098 // 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
1099 if (negate)
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1100 b->Write (DH_NEGATELOGICAL);
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
1101
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
1102 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
1103 }
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
1104
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
1105 // ============================================================================
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
1106 // 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
1107 // 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
1108 // 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
1109 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
1110 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
1111
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
1112 // 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
1113 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
1114 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
1115 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
1116 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
1117
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
1118 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
1119 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
1120
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
1121 // 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
1122 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
1123 DataBuffer* retbuf = new DataBuffer;
67
0a202714eea4 Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
1124 DataBuffer* expr = ParseExpression (var->type);
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
1125
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
1126 // <<= 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
1127 // 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
1128 // 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
1129 if (oper == OPER_ASSIGNLEFTSHIFT || oper == OPER_ASSIGNRIGHTSHIFT) {
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1130 retbuf->Write (global ? DH_PUSHGLOBALVAR : DH_PUSHLOCALVAR);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1131 retbuf->Write (var->index);
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
1132 retbuf->Merge (expr);
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1133 retbuf->Write ((oper == OPER_ASSIGNLEFTSHIFT) ? DH_LSHIFT : DH_RSHIFT);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1134 retbuf->Write (global ? DH_ASSIGNGLOBALVAR : DH_ASSIGNLOCALVAR);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1135 retbuf->Write (var->index);
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
1136 } 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
1137 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
1138 long dh = DataHeaderByOperator (var, oper);
69
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1139 retbuf->Write (dh);
29a3e669d648 So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 68
diff changeset
1140 retbuf->Write (var->index);
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
1141 }
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
1142
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
1143 return retbuf;
40
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
1144 }
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
1145
58
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
1146 void ScriptReader::PushScope () {
bc9317d1b9c9 Renamed 'block stack' to scope stack for more clarified code..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 57
diff changeset
1147 g_ScopeCursor++;
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
1148 if (g_ScopeCursor >= MAX_SCOPE)
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
1149 ParserError ("too deep scope");
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 55
diff changeset
1150
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
1151 ScopeInfo* info = &SCOPE(0);
66
4fc1ec88aa41 Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
1152 info->type = SCOPETYPE_UNKNOWN;
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
1153 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
1154 info->mark2 = 0;
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
1155 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
1156 info->casecursor = -1;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 47
diff changeset
1157 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
1158 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
1159 info->casebuffers[i] = NULL;
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1160 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
1161 }
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
1162 }
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
1163
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
1164 DataBuffer* ScriptReader::ParseStatement (ObjWriter* w) {
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1165 if (FindConstant (token)) // There should not be constants here.
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1166 ParserError ("invalid use for constant\n");
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1167
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
1168 // If it's a variable, expect assignment.
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1169 if (ScriptVar* var = FindGlobalVariable (token))
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1170 return ParseAssignment (var);
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
1171
65
ec04357f5bb9 Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 64
diff changeset
1172 return NULL;
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1173 }
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1174
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1175 void ScriptReader::AddSwitchCase (ObjWriter* w, DataBuffer* b) {
59
891b9e6ee139 Added support for continue-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
1176 ScopeInfo* info = &SCOPE(0);
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1177
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1178 info->casecursor++;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1179 if (info->casecursor >= MAX_CASE)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1180 ParserError ("too many cases in one switch");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1181
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1182 // Init a mark for the case buffer
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1183 int m = w->AddMark ("");
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1184 info->casemarks[info->casecursor] = m;
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1185
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1186 // 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
1187 // add the necessary bytecode before the reference.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1188 if (b)
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1189 b->AddMarkReference (m);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1190 else
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1191 w->AddReference (m);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1192
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1193 // 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
1194 // writer to record all written data to it.
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 49
diff changeset
1195 info->casebuffers[info->casecursor] = w->SwitchBuffer = new DataBuffer;
68
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1196 }
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1197
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1198 constinfo_t* FindConstant (str token) {
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1199 for (uint i = 0; i < g_ConstInfo.size(); i++)
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1200 if (g_ConstInfo[i].name == token)
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1201 return &g_ConstInfo[i];
588cc27e84bb Added constants, these are defined with const, take their value immediately and are replaced out with their value when used. The strlen operator can be used to get a string constant's length.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
1202 return NULL;
15
284c2fc6c1cd Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
1203 }

mercurial