Sun, 12 Aug 2012 04:10:25 +0300
Added do-while loop support
0 | 1 | /* |
2 | * botc source code | |
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
0 | 17 | * 4. Redistributions in any form must be accompanied by information on how to |
18 | * obtain complete source code for the software and any accompanying | |
19 | * software that uses the software. The source code must either be included | |
20 | * in the distribution or be available for no more than the cost of | |
21 | * distribution plus a nominal fee, and must be freely redistributable | |
22 | * under reasonable conditions. For an executable file, complete source | |
23 | * code means the source code for all modules it contains. It does not | |
24 | * include source code for modules or files that typically accompany the | |
25 | * major components of the operating system on which the executable file | |
26 | * runs. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
37 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
38 | * POSSIBILITY OF SUCH DAMAGE. | |
39 | */ | |
40 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
41 | #define __PARSER_CXX__ |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
42 | |
0 | 43 | #include <stdio.h> |
44 | #include <stdlib.h> | |
45 | #include "common.h" | |
46 | #include "str.h" | |
47 | #include "objwriter.h" | |
48 | #include "scriptreader.h" | |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
49 | #include "events.h" |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
50 | #include "commands.h" |
20
d7b13805d1e0
Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
51 | #include "stringtable.h" |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
52 | #include "variables.h" |
0 | 53 | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
54 | #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \ |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
55 | ParserError ("%s-statements may only be defined at top level!", token.chars()); |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
56 | |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
57 | #define MUST_NOT_TOPLEVEL if (g_CurMode == MODE_TOPLEVEL) \ |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
58 | ParserError ("%s-statements may not be defined at top level!", token.chars()); |
0 | 59 | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
60 | int g_NumStates = 0; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
61 | int g_NumEvents = 0; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
62 | int g_CurMode = MODE_TOPLEVEL; |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
63 | str g_CurState = ""; |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
64 | bool g_stateSpawnDefined = false; |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
65 | bool g_GotMainLoop = false; |
38 | 66 | unsigned int g_BlockStackCursor = 0; |
40
9e4f785501db
Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents:
39
diff
changeset
|
67 | DataBuffer* g_IfExpression = NULL; |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
68 | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
69 | // ============================================================================ |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
70 | // Main parser code. Begins read of the script file, checks the syntax of it |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
71 | // and writes the data to the object file via ObjWriter - which also takes care |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
72 | // of necessary buffering so stuff is written in the correct order. |
0 | 73 | void ScriptReader::BeginParse (ObjWriter* w) { |
74 | while (Next()) { | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
75 | // ============================================================ |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
76 | if (!token.icompare ("state")) { |
0 | 77 | MUST_TOPLEVEL |
78 | ||
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
79 | MustString (); |
0 | 80 | |
81 | // State name must be a word. | |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
82 | if (token.first (" ") != token.len()) |
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
83 | ParserError ("state name must be a single word! got `%s`", token.chars()); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
84 | str statename = token; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
85 | |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
86 | // stateSpawn is special - it *must* be defined. If we |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
87 | // encountered it, then mark down that we have it. |
33
fd35f6cb5f28
Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
31
diff
changeset
|
88 | if (!token.icompare ("statespawn")) |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
89 | g_stateSpawnDefined = true; |
0 | 90 | |
91 | // Must end in a colon | |
92 | MustNext (":"); | |
93 | ||
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
94 | // Write the previous state's onenter and |
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
95 | // mainloop buffers to file now |
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
96 | if (g_CurState.len()) |
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
97 | w->WriteBuffers(); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
98 | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
99 | w->Write (DH_STATENAME); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
100 | w->WriteString (statename); |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
101 | w->Write (DH_STATEIDX); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
102 | w->Write (g_NumStates); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
103 | |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
104 | g_NumStates++; |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
105 | g_CurState = token; |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
106 | g_GotMainLoop = false; |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
107 | continue; |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
108 | } |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
109 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
110 | // ============================================================ |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
111 | if (!token.icompare ("event")) { |
0 | 112 | MUST_TOPLEVEL |
113 | ||
114 | // Event definition | |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
115 | MustString (); |
0 | 116 | |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
117 | EventDef* e = FindEventByName (token); |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
118 | if (!e) |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
119 | ParserError ("bad event! got `%s`\n", token.chars()); |
0 | 120 | |
121 | MustNext ("{"); | |
122 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
123 | g_CurMode = MODE_EVENT; |
0 | 124 | |
125 | w->Write (DH_EVENT); | |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
126 | w->Write<word> (e->number); |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
127 | g_NumEvents++; |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
128 | continue; |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
129 | } |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
130 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
131 | // ============================================================ |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
132 | if (!token.icompare ("mainloop")) { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
133 | MUST_TOPLEVEL |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
134 | MustNext ("{"); |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
135 | |
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
136 | // Mode must be set before dataheader is written here! |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
137 | g_CurMode = MODE_MAINLOOP; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
138 | w->Write (DH_MAINLOOP); |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
139 | continue; |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
140 | } |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
141 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
142 | // ============================================================ |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
143 | if (!token.icompare ("onenter") || !token.icompare ("onexit")) { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
144 | MUST_TOPLEVEL |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
145 | bool onenter = !token.compare ("onenter"); |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
146 | MustNext ("{"); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
147 | |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
148 | // Mode must be set before dataheader is written here, |
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
149 | // because onenter goes to a separate buffer. |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
150 | g_CurMode = onenter ? MODE_ONENTER : MODE_ONEXIT; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
15
diff
changeset
|
151 | w->Write (onenter ? DH_ONENTER : DH_ONEXIT); |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
152 | continue; |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
153 | } |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
154 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
155 | // ============================================================ |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
156 | if (!token.compare ("var")) { |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
157 | // For now, only globals are supported |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
158 | if (g_CurMode != MODE_TOPLEVEL || g_CurState.len()) |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
159 | ParserError ("variables must only be global for now"); |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
160 | |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
161 | MustNext (); |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
162 | |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
163 | // Var name must not be a number |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
164 | if (token.isnumber()) |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
165 | ParserError ("variable name must not be a number"); |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
166 | |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
167 | str varname = token; |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
168 | ScriptVar* var = DeclareGlobalVariable (this, varname); |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
169 | |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
170 | if (!var) |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
171 | ParserError ("declaring %s variable %s failed", |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
172 | g_CurState.len() ? "state" : "global", varname.chars()); |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
173 | |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
174 | MustNext (";"); |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
175 | continue; |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
176 | } |
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
177 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
178 | // ============================================================ |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
179 | // 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
|
180 | if (!PeekNext().compare (":")) { |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
181 | MUST_NOT_TOPLEVEL |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
182 | |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
183 | if (IsKeyword (token)) |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
184 | ParserError ("label name `%s` conflicts with keyword\n", 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
|
185 | if (FindCommand (token)) |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
186 | ParserError ("label name `%s` conflicts with command name\n", 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
|
187 | if (FindGlobalVariable (token)) |
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
|
188 | ParserError ("label name `%s` conflicts with variable\n", 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
|
189 | |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
190 | w->AddMark (MARKTYPE_LABEL, token); |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
191 | 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
|
192 | 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
|
193 | } |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
194 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
195 | // ============================================================ |
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
|
196 | // Goto |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
197 | if (!token.icompare ("goto")) { |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
198 | 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
|
199 | |
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
|
200 | // 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
|
201 | 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
|
202 | |
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 | // Find the mark this goto statement points to |
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
|
204 | unsigned int m = w->FindMark (MARKTYPE_LABEL, token); |
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
|
205 | if (m == MAX_MARKS) |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
206 | ParserError ("unknown label `%s`!", token.chars()); |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
207 | |
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 | // Add a reference to the mark. |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
209 | w->Write<word> (DH_GOTO); |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
210 | 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
|
211 | 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
|
212 | 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
|
213 | } |
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
|
214 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
215 | // ============================================================ |
38 | 216 | // If |
217 | if (!token.icompare ("if")) { | |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
218 | 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
|
219 | PushBlockStack (); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
220 | |
38 | 221 | // Condition |
222 | MustNext ("("); | |
223 | ||
40
9e4f785501db
Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents:
39
diff
changeset
|
224 | // Read the expression and write it. Store it to memory too for else statements. |
38 | 225 | MustNext (); |
226 | DataBuffer* c = ParseExpression (TYPE_INT); | |
227 | w->WriteBuffer (c); | |
228 | ||
229 | MustNext (")"); | |
230 | MustNext ("{"); | |
231 | ||
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
232 | // 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
|
233 | // Upon a closing brace, the mark will be adjusted. |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
234 | unsigned int marknum = w->AddMark (MARKTYPE_IF, ""); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
235 | |
38 | 236 | // Use DH_IFNOTGOTO - if the expression is not true, we goto the mark |
237 | // we just defined - and this mark will be at the end of the block. | |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
238 | w->Write<word> (DH_IFNOTGOTO); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
239 | 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
|
240 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
241 | // Store it in the block stack |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
242 | blockstack[g_BlockStackCursor].mark1 = marknum; |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
243 | blockstack[g_BlockStackCursor].type = BLOCKTYPE_IF; |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
244 | continue; |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
245 | } |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
246 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
247 | // ============================================================ |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
248 | // While |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
249 | if (!token.compare ("while")) { |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
250 | 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
|
251 | PushBlockStack (); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
252 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
253 | // 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
|
254 | // 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
|
255 | // 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
|
256 | // the beginning with a go-to statement. |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
257 | unsigned int mark1 = w->AddMark (MARKTYPE_INTERNAL, ""); // start |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
258 | unsigned int mark2 = w->AddMark (MARKTYPE_INTERNAL, ""); // end |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
259 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
260 | // Condition |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
261 | MustNext ("("); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
262 | MustNext (); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
263 | 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
|
264 | MustNext (")"); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
265 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
266 | MustNext ("{"); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
267 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
268 | // Write condition |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
269 | 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
|
270 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
271 | // Instruction to go to the end if it fails |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
272 | w->Write<word> (DH_IFNOTGOTO); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
273 | 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
|
274 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
275 | // Store the needed stuff |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
276 | blockstack[g_BlockStackCursor].mark1 = mark1; |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
277 | blockstack[g_BlockStackCursor].mark2 = mark2; |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
278 | blockstack[g_BlockStackCursor].type = BLOCKTYPE_WHILE; |
38 | 279 | continue; |
280 | } | |
281 | ||
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
282 | // ============================================================ |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
283 | // For loop |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
284 | if (!token.icompare ("for")) { |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
285 | MUST_NOT_TOPLEVEL |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
286 | PushBlockStack (); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
287 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
288 | // Initializer |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
289 | MustNext ("("); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
290 | MustNext (); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
291 | DataBuffer* init = ParseStatement (w); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
292 | MustNext (";"); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
293 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
294 | // Condition |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
295 | MustNext (); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
296 | DataBuffer* cond = ParseExpression (TYPE_INT); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
297 | MustNext (";"); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
298 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
299 | // Incrementor |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
300 | MustNext (); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
301 | DataBuffer* incr = ParseStatement (w); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
302 | MustNext (")"); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
303 | MustNext ("{"); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
304 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
305 | // First, write out the initializer |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
306 | w->WriteBuffer (init); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
307 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
308 | // Init two marks |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
309 | int mark1 = w->AddMark (MARKTYPE_INTERNAL, ""); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
310 | int mark2 = w->AddMark (MARKTYPE_INTERNAL, ""); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
311 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
312 | // Add the condition |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
313 | w->WriteBuffer (cond); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
314 | w->Write<long> (DH_IFNOTGOTO); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
315 | w->AddReference (mark2); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
316 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
317 | // Store the marks and incrementor |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
318 | blockstack[g_BlockStackCursor].mark1 = mark1; |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
319 | blockstack[g_BlockStackCursor].mark2 = mark2; |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
320 | blockstack[g_BlockStackCursor].buffer1 = incr; |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
321 | blockstack[g_BlockStackCursor].type = BLOCKTYPE_FOR; |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
322 | continue; |
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 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
325 | // ============================================================ |
44
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
326 | // Do/while loop |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
327 | if (!token.icompare ("do")) { |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
328 | MUST_NOT_TOPLEVEL |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
329 | PushBlockStack (); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
330 | MustNext ("{"); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
331 | |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
332 | // Store the marks and incrementor |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
333 | blockstack[g_BlockStackCursor].mark1 = w->AddMark (MARKTYPE_INTERNAL, ""); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
334 | blockstack[g_BlockStackCursor].type = BLOCKTYPE_DO; |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
335 | continue; |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
336 | } |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
337 | |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
338 | // ============================================================ |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
339 | if (!token.compare ("}")) { |
38 | 340 | // Closing brace |
341 | ||
342 | // If we're in the block stack, we're descending down from it now | |
343 | if (g_BlockStackCursor > 0) { | |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
344 | BlockInformation* info = &blockstack[g_BlockStackCursor]; |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
345 | switch (info->type) { |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
346 | case BLOCKTYPE_IF: |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
347 | // Adjust the closing mark. |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
348 | w->MoveMark (info->mark1); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
349 | break; |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
350 | case BLOCKTYPE_FOR: |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
351 | // Write the incrementor at the end of the loop block |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
352 | w->WriteBuffer (info->buffer1); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
353 | // fall-thru |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
354 | case BLOCKTYPE_WHILE: |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
355 | // 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
|
356 | w->Write (DH_GOTO); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
357 | w->AddReference (info->mark1); |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
358 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
359 | // Move the closing mark here since we're at the end of the while loop |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
360 | w->MoveMark (info->mark2); |
44
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
361 | break; |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
362 | case BLOCKTYPE_DO: |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
363 | MustNext ("while"); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
364 | MustNext ("("); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
365 | MustNext (); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
366 | DataBuffer* expr = ParseExpression (TYPE_INT); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
367 | MustNext (")"); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
368 | MustNext (";"); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
369 | |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
370 | // 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
|
371 | w->WriteBuffer (expr); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
372 | w->Write<long> (DH_IFGOTO); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
373 | w->AddReference (info->mark1); |
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
374 | break; |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
375 | } |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
376 | |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
377 | // Descend down the stack |
38 | 378 | g_BlockStackCursor--; |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
379 | 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
|
380 | } |
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
|
381 | |
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
|
382 | 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
|
383 | (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
|
384 | (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
|
385 | (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
|
386 | |
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
|
387 | 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
|
388 | 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
|
389 | |
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
|
390 | // 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
|
391 | // 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
|
392 | // 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
|
393 | 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
|
394 | 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
|
395 | |
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
|
396 | if (!PeekNext().compare (";")) |
ad027ea58097
Lifted the ';'-after-events rule, though it can still be present. C++ allows it for functions, so this feels like the best solution regarding consistency.. I'm not even sure am I going overboard with this kind of stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
397 | MustNext (";"); |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
398 | continue; |
0 | 399 | } |
24
7dcc8419dbdb
Variables can now be assigned values, unfortunately not at top level.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
400 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
401 | // ============================================================ |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
402 | // If nothing else, parse it as a statement (which is either |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
403 | // assignment or expression) |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
404 | DataBuffer* b = ParseStatement (w); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
405 | 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
|
406 | MustNext (";"); |
0 | 407 | } |
408 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
409 | 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
|
410 | 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
|
411 | |
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
|
412 | // 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
|
413 | 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
|
414 | ParserError ("script must have a state named `stateSpawn`!"); |
0 | 415 | |
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
|
416 | // 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
|
417 | w->WriteBuffers (); |
19
66993500719f
Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
418 | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
419 | // 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
|
420 | w->WriteStringTable (); |
0 | 421 | } |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
422 | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
423 | // ============================================================================ |
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
|
424 | // 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
|
425 | 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
|
426 | 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
|
427 | 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
|
428 | 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
|
429 | |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
430 | 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
|
431 | 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
|
432 | |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
433 | int curarg = 0; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
434 | while (1) { |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
435 | if (!token.compare (")")) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
436 | if (curarg < comm->numargs - 1) |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
437 | ParserError ("too few arguments passed to %s\n", comm->name.chars()); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
438 | break; |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
439 | curarg++; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
440 | } |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
441 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
442 | if (curarg >= comm->maxargs) |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
443 | ParserError ("too many arguments passed to %s\n", comm->name.chars()); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
444 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
445 | 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
|
446 | MustNext (); |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
447 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
448 | 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
|
449 | 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
|
450 | MustNext (); |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
451 | } else if (curarg < comm->maxargs - 1) { |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
452 | // Can continue, but can terminate as well. |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
453 | if (!token.compare (")")) { |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
454 | curarg++; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
455 | 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
|
456 | } 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
|
457 | 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
|
458 | 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
|
459 | } |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
460 | } |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
461 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
462 | curarg++; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
463 | } |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
464 | |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
465 | // 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
|
466 | while (curarg < comm->maxargs) { |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
467 | r->Write<word> (DH_PUSHNUMBER); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
468 | r->Write<word> (comm->defvals[curarg]); |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
469 | curarg++; |
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
470 | } |
19
66993500719f
Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
471 | |
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
|
472 | r->Write<word> (DH_COMMAND); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
473 | r->Write<word> (comm->number); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
474 | r->Write<word> (comm->maxargs); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
475 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
476 | 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
|
477 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
478 | |
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
|
479 | // ============================================================================ |
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
|
480 | // 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
|
481 | 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
|
482 | 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
|
483 | 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
|
484 | 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
|
485 | 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
|
486 | 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
|
487 | case OPER_ASSIGNMOD: |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
488 | 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
|
489 | 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
|
490 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
491 | 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
|
492 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
493 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
494 | // ============================================================================ |
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
|
495 | // Finds an operator's corresponding dataheader |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
496 | static long DataHeaderByOperator (ScriptVar* var, 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
|
497 | 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
|
498 | 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
|
499 | 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
|
500 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
501 | // TODO: At the moment, vars only are global |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
502 | 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
|
503 | 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
|
504 | 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
|
505 | 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
|
506 | 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
|
507 | 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
|
508 | 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
|
509 | 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
|
510 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
511 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
512 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
513 | 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
|
514 | 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
|
515 | 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
|
516 | 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
|
517 | 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
|
518 | case OPER_MODULUS: return DH_MODULUS; |
38 | 519 | case OPER_EQUALS: return DH_EQUALS; |
520 | case OPER_NOTEQUALS: return DH_NOTEQUALS; | |
521 | case OPER_LESSTHAN: return DH_LESSTHAN; | |
522 | case OPER_GREATERTHAN: return DH_GREATERTHAN; | |
523 | case OPER_LESSTHANEQUALS: return DH_LESSTHANEQUALS; | |
524 | case OPER_GREATERTHANEQUALS: return DH_GREATERTHANEQUALS; | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
525 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
526 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
527 | 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
|
528 | 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
|
529 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
530 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
531 | // ============================================================================ |
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
|
532 | // Parses an expression, potentially recursively |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
533 | DataBuffer* ScriptReader::ParseExpression (int reqtype) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
534 | 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
|
535 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
536 | DataBuffer* lb = NULL; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
537 | |
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
|
538 | lb = 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
|
539 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
540 | // Get an operator |
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
|
541 | int oper = ParseOperator (true); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
542 | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
543 | // No operator found - stop here. |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
544 | if (oper == -1) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
545 | retbuf->Merge (lb); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
546 | 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
|
547 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
548 | |
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
|
549 | // We peeked the operator, move forward now |
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
|
550 | 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
|
551 | |
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
|
552 | // Can't be an assignement operator, those belong in assignments. |
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
|
553 | 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
|
554 | ParserError ("assignment operator inside expressions"); |
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
|
555 | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
556 | // Parse the right operand, |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
557 | MustNext (); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
558 | DataBuffer* rb = ParseExprValue (reqtype); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
559 | |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
560 | retbuf->Merge (lb); |
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
|
561 | retbuf->Merge (rb); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
562 | |
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
|
563 | long dh = DataHeaderByOperator (NULL, 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
|
564 | retbuf->Write<word> (dh); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
565 | 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
|
566 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
567 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
568 | // ============================================================================ |
39
07b7ab8080cf
Fixed mark positioning - multiple if statements should work properly now
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
569 | // `arses an operator string. Returns the operator number code. |
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
|
570 | 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
|
571 | 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
|
572 | 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
|
573 | 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
|
574 | 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
|
575 | 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
|
576 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
577 | // Check one-char operators |
38 | 578 | bool equalsnext = !PeekNext (peek ? 1 : 0).compare ("="); |
579 | int o = (!oper.compare ("=") && !equalsnext) ? OPER_ASSIGN : | |
580 | (!oper.compare (">") && !equalsnext) ? OPER_GREATERTHAN : | |
581 | (!oper.compare ("<") && !equalsnext) ? OPER_LESSTHAN : | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
582 | !oper.compare ("+") ? OPER_ADD : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
583 | !oper.compare ("-") ? OPER_SUBTRACT : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
584 | !oper.compare ("*") ? OPER_MULTIPLY : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
585 | !oper.compare ("/") ? OPER_DIVIDE : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
586 | !oper.compare ("%") ? OPER_MODULUS : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
587 | -1; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
588 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
589 | 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
|
590 | 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
|
591 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
592 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
593 | // 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
|
594 | oper += PeekNext (peek ? 1 : 0); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
595 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
596 | o = !oper.compare ("+=") ? OPER_ASSIGNADD : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
597 | !oper.compare ("-=") ? OPER_ASSIGNSUB : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
598 | !oper.compare ("*=") ? OPER_ASSIGNMUL : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
599 | !oper.compare ("/=") ? OPER_ASSIGNDIV : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
600 | !oper.compare ("%=") ? OPER_ASSIGNMOD : |
38 | 601 | !oper.compare ("==") ? OPER_EQUALS : |
602 | !oper.compare ("!=") ? OPER_NOTEQUALS : | |
603 | !oper.compare (">=") ? OPER_GREATERTHANEQUALS : | |
604 | !oper.compare ("<=") ? OPER_LESSTHANEQUALS : | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
605 | -1; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
606 | |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
607 | 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
|
608 | MustNext (); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
609 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
610 | 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
|
611 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
612 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
613 | // ============================================================================ |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
614 | // 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
|
615 | // 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
|
616 | // a literal or an expression. |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
617 | DataBuffer* ScriptReader::ParseExprValue (int reqtype) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
618 | 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
|
619 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
620 | 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
|
621 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
622 | if (!token.compare ("(")) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
623 | // Expression |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
624 | MustNext (); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
625 | 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
|
626 | 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
|
627 | MustNext (")"); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
628 | } 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
|
629 | 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
|
630 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
631 | // Command |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
632 | 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
|
633 | 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
|
634 | b = ParseCommand (comm); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
635 | } else if ((g = FindGlobalVariable (token)) && reqtype != TYPE_STRING) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
636 | // Global variable |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
637 | b->Write<word> (DH_PUSHGLOBALVAR); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
638 | b->Write<word> (g->index); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
639 | } else { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
640 | // 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
|
641 | 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
|
642 | case TYPE_VOID: |
44
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
643 | 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
|
644 | break; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
645 | case TYPE_INT: { |
35
3d3f6ed40171
Negative literal integers work properly now..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
646 | 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
|
647 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
648 | // All values are written unsigned - thus we need to write the value's |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
649 | // absolute value, followed by an unary minus if it was negative. |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
650 | b->Write<word> (DH_PUSHNUMBER); |
38 | 651 | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
652 | long v = atoi (token.chars ()); |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
653 | b->Write<word> (static_cast<word> (abs (v))); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
654 | if (v < 0) |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
655 | b->Write<word> (DH_UNARYMINUS); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
656 | break; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
657 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
658 | 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
|
659 | // 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
|
660 | // 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
|
661 | // 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
|
662 | MustString (true); |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
663 | b->Write<word> (DH_PUSHSTRINGINDEX); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
664 | b->Write<word> (PushToStringTable (token.chars())); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
665 | break; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
666 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
667 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
668 | |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
669 | 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
|
670 | } |
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
|
671 | |
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
|
672 | // ============================================================================ |
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
|
673 | // 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
|
674 | // 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
|
675 | // 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
|
676 | DataBuffer* ScriptReader::ParseAssignment (ScriptVar* var) { |
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
|
677 | // 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
|
678 | 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
|
679 | 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
|
680 | 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
|
681 | 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
|
682 | |
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
|
683 | 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
|
684 | 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
|
685 | |
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
|
686 | // Parse the right operand, |
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
|
687 | 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
|
688 | DataBuffer* retbuf = ParseExprValue (TYPE_INT); |
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
|
689 | |
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 | long dh = DataHeaderByOperator (var, 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
|
691 | retbuf->Write<word> (dh); |
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
|
692 | retbuf->Write<word> (var->index); |
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
|
693 | |
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
|
694 | return retbuf; |
40
9e4f785501db
Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents:
39
diff
changeset
|
695 | } |
9e4f785501db
Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents:
39
diff
changeset
|
696 | |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
697 | void ScriptReader::PushBlockStack () { |
42
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
698 | g_BlockStackCursor++; |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
699 | BlockInformation* info = &blockstack[g_BlockStackCursor]; |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
700 | info->type = 0; |
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
40
diff
changeset
|
701 | 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
|
702 | info->mark2 = 0; |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
703 | info->buffer1 = NULL; |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
704 | } |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
705 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
706 | DataBuffer* ScriptReader::ParseStatement (ObjWriter* w) { |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
707 | // If it's a variable, expect assignment. |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
708 | if (ScriptVar* var = FindGlobalVariable (token)) { |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
709 | DataBuffer* b = ParseAssignment (var); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
710 | return b; |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
711 | } |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
712 | |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
713 | // If it's not a keyword, parse it as an expression. |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
714 | DataBuffer* b = ParseExpression (TYPE_VOID); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
715 | return b; |
15
284c2fc6c1cd
Moved command parser to a new function.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
14
diff
changeset
|
716 | } |