src/parser.cxx

Sat, 11 Jan 2014 22:36:31 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 11 Jan 2014 22:36:31 +0200
changeset 73
1ee9b312dc18
parent 72
03e4d9db3fd9
child 74
007fbadfa7f9
permissions
-rw-r--r--

- *hot stuff coming through*

73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1 /*
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
2 Copyright (c) 2013-2014, Santeri Piippo
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
3 All rights reserved.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
4
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
5 Redistribution and use in source and binary forms, with or without
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
6 modification, are permitted provided that the following conditions are met:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
7
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
8 * Redistributions of source code must retain the above copyright
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
9 notice, this list of conditions and the following disclaimer.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
10
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
11 * Redistributions in binary form must reproduce the above copyright
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
12 notice, this list of conditions and the following disclaimer in the
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
13 documentation and/or other materials provided with the distribution.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
14
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
15 * Neither the name of the <organization> nor the
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
16 names of its contributors may be used to endorse or promote products
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
17 derived from this software without specific prior written permission.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
18
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
29 */
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
30
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
31 #include "object_writer.h"
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
32 #include "parser.h"
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include "events.h"
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 #include "commands.h"
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 #include "stringtable.h"
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 #include "variables.h"
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
37 #include "containers.h"
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
38 #include "lexer.h"
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
40 #define TOKEN (string (m_lx->get_token()->string))
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 #define SCOPE(n) scopestack[g_ScopeCursor - n]
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
43 // TODO: make these static
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 int g_NumStates = 0;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 int g_NumEvents = 0;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 parsermode_e g_CurMode = MODE_TOPLEVEL;
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
47 string g_CurState = "";
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 bool g_stateSpawnDefined = false;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 bool g_GotMainLoop = false;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
50 int g_ScopeCursor = 0;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
51 data_buffer* g_IfExpression = null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 bool g_CanElse = false;
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
53 string* g_UndefinedLabels[MAX_MARKS];
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
54 list<constant_info> g_ConstInfo;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
55
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
56 static botscript_parser* g_current_parser = null;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
57
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
58 // ============================================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
59 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
60 botscript_parser::botscript_parser() :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
61 m_lx (new lexer) {}
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
62
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
63 // ============================================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
64 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
65 botscript_parser::~botscript_parser()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
66 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
67 delete m_lx;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
68 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
69
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
70 // ============================================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
71 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
72 void botscript_parser::check_toplevel()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
73 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
74 if (g_CurMode != MODE_TOPLEVEL)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
75 error ("%1-statements may only be defined at top level!", TOKEN.chars());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
76 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
77
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
78 // ============================================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
79 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
80 void botscript_parser::check_not_toplevel()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
81 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
82 if (g_CurMode == MODE_TOPLEVEL)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
83 error ("%1-statements must not be defined at top level!", TOKEN.chars());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
84 }
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 // ============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 // Main parser code. Begins read of the script file, checks the syntax of it
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
88 // and writes the data to the object file via Objwriter - which also takes care
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 // of necessary buffering so stuff is written in the correct order.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
90 void botscript_parser::parse_botscript (string file_name, object_writer* w)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
91 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
92 // Lex and preprocess the file
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
93 m_lx->process_file (file_name);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
94
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 // Zero the entire block stack first
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 for (int i = 0; i < MAX_SCOPE; i++)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
97 ZERO (scopestack[i]);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
98
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 for (int i = 0; i < MAX_MARKS; i++)
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
100 g_UndefinedLabels[i] = null;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
101
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
102 while (m_lx->get_next())
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
103 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 // Check if else is potentically valid
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
105 if (TOKEN == "else" && !g_CanElse)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
106 error ("else without preceding if");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
107
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
108 if (TOKEN != "else")
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 g_CanElse = false;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
110
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
111 switch (m_lx->get_token())
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
112 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
113 case tk_state:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
114 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
115 check_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
116 m_lx->must_get_next (tk_string);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
117
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
118 // State name must be a word.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
119 if (TOKEN.first (" ") != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
120 error ("state name must be a single word, got `%1`", TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
121
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
122 string statename = TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
123
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
124 // stateSpawn is special - it *must* be defined. If we
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
125 // encountered it, then mark down that we have it.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
126 if (-TOKEN == "statespawn")
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
127 g_stateSpawnDefined = true;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
128
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
129 // Must end in a colon
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
130 m_lx->must_get_next (tk_colon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
131
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
132 // write the previous state's onenter and
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
133 // mainloop buffers to file now
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
134 if (g_CurState.len())
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
135 w->write (Buffers();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
136
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
137 w->write (DH_STATENAME);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
138 w->write_string (statename);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
139 w->write (DH_STATEIDX);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
140 w->write (g_NumStates);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
141
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
142 g_NumStates++;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
143 g_CurState = TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
144 g_GotMainLoop = false;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
146 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
147
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
148 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
149 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
150 case tk_event:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
151 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
152 check_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
153
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
154 // Event definition
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
155 m_lx->must_get_next (tk_string);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
156
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
157 event_info* e = find_event_by_name (token_string());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
158
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
159 if (!e)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
160 error ("bad event, got `%1`\n", token_string());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
161
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
162 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
163 g_CurMode = MODE_EVENT;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
164 w->write (DH_EVENT);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
165 w->write (e->number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
166 g_NumEvents++;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
167 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
168 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
169
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
170 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
171 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
172 case tk_mainloop:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
173 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
174 check_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
175 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
176
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
177 // Mode must be set before dataheader is written here!
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
178 g_CurMode = MODE_MAINLOOP;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
179 w->write (DH_MAINLOOP);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
180 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
181 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
182
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
183 // ============================================================
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 //
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
185 case tk_onenter:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
186 case tk_onexit:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
187 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
188 check_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
189 bool onenter = (m_lx->get_token() == "onenter");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
190 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
191
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
192 // Mode must be set before dataheader is written here,
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
193 // because onenter goes to a separate buffer.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
194 g_CurMode = onenter ? MODE_ONENTER : MODE_ONEXIT;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
195 w->write (onenter ? DH_ONENTER : DH_ONEXIT);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
197 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
198
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
199 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
200 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
201 case tk_int:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
202 case tk_str:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
203 case tk_void:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
204 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
205 // For now, only globals are supported
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
206 if (g_CurMode != MODE_TOPLEVEL || g_CurState.len())
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
207 error ("variables must only be global for now");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
208
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
209 type_e type = (TOKEN == "int") ? TYPE_INT :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
210 (TOKEN == "str") ? TYPE_STRING :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
211 TYPE_BOOL;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
212
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
213 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
214
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
215 // Var name must not be a number
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
216 if (TOKEN.is_numeric())
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
217 error ("variable name must not be a number");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
218
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
219 string varname = TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
220 script_variable* var = DeclareGlobalVariable (this, type, varname);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
221 m_lx->must_get_next (tk_semicolon);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
223 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
224
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
225 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
226 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
227 case tk_goto:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
228 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
229 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
230
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
231 // Get the name of the label
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
232 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
233
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
234 // Find the mark this goto statement points to
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
235 int m = w->find_byte_mark (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
236
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
237 // If not set, define it
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
238 if (m == MAX_MARKS)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
239 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
240 m = w->add_mark (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
241 g_UndefinedLabels[m] = new string (TOKEN);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
243
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
244 // Add a reference to the mark.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
245 w->write (DH_GOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
246 w->add_reference (m);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
247 m_lx->must_get_next (tk_semicolon);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 continue;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
250
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
251 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
252 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
253 case tk_if:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
254 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
255 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
256 push_scope();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
257
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
258 // Condition
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
259 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
260
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
261 // Read the expression and write it.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
262 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
263 data_buffer* c = parse_expression (TYPE_INT);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
264 w->write_buffer (c);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
265
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
266 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
267 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
268
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
269 // Add a mark - to here temporarily - and add a reference to it.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
270 // Upon a closing brace, the mark will be adjusted.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
271 int marknum = w->add_mark ("");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
272
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
273 // Use DH_IFNOTGOTO - if the expression is not true, we goto the mark
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
274 // we just defined - and this mark will be at the end of the scope block.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
275 w->write (DH_IFNOTGOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
276 w->add_reference (marknum);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
277
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
278 // Store it
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
279 SCOPE (0).mark1 = marknum;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
280 SCOPE (0).type = SCOPETYPE_IF;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
281 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
282
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
283 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
284 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
285 case tk_else:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
286 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
287 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
288 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
289
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
290 // Don't use PushScope as it resets the scope
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
291 g_ScopeCursor++;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
292
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
293 if (g_ScopeCursor >= MAX_SCOPE)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
294 error ("too deep scope");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
295
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
296 if (SCOPE (0).type != SCOPETYPE_IF)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
297 error ("else without preceding if");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
298
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
299 // write down to jump to the end of the else statement
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
300 // Otherwise we have fall-throughs
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
301 SCOPE (0).mark2 = w->add_mark ("");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
302
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
303 // Instruction to jump to the end after if block is complete
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
304 w->write (DH_GOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
305 w->add_reference (SCOPE (0).mark2);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
306
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
307 // Move the ifnot mark here and set type to else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
308 w->move_mark (SCOPE (0).mark1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
309 SCOPE (0).type = SCOPETYPE_ELSE;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
310 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
311 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
312
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
313 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
314 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
315 case tk_while:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
316 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
317 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
318 push_scope();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
319
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
320 // While loops need two marks - one at the start of the loop and one at the
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
321 // end. The condition is checked at the very start of the loop, if it fails,
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
322 // we use goto to skip to the end of the loop. At the end, we loop back to
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
323 // the beginning with a go-to statement.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
324 int mark1 = w->add_mark (""); // start
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
325 int mark2 = w->add_mark (""); // end
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
326
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
327 // Condition
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
328 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
329 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
330 data_buffer* expr = parse_expression (TYPE_INT);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
331 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
332 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
333
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
334 // write condition
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
335 w->write_buffer (expr);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
336
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
337 // Instruction to go to the end if it fails
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
338 w->write (DH_IFNOTGOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
339 w->add_reference (mark2);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
340
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
341 // Store the needed stuff
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
342 SCOPE (0).mark1 = mark1;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
343 SCOPE (0).mark2 = mark2;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
344 SCOPE (0).type = SCOPETYPE_WHILE;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
345 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
346 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
347
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
348 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
349 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
350 case tk_for:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
351 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
352 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
353 push_scope();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
354
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
355 // Initializer
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
356 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
357 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
358 data_buffer* init = parse_statement (w);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
359
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
360 if (!init)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
361 error ("bad statement for initializer of for");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
362
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
363 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
364
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
365 // Condition
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
366 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
367 data_buffer* cond = parse_expression (TYPE_INT);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
368
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
369 if (!cond)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
370 error ("bad statement for condition of for");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
371
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
372 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
373
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
374 // Incrementor
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
375 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
376 data_buffer* incr = parse_statement (w);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
377
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
378 if (!incr)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
379 error ("bad statement for incrementor of for");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
380
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
381 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
382 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
383
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
384 // First, write out the initializer
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
385 w->write_buffer (init);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
386
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
387 // Init two marks
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
388 int mark1 = w->add_mark ("");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
389 int mark2 = w->add_mark ("");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
390
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
391 // Add the condition
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
392 w->write_buffer (cond);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
393 w->write (DH_IFNOTGOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
394 w->add_reference (mark2);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
395
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
396 // Store the marks and incrementor
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
397 SCOPE (0).mark1 = mark1;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
398 SCOPE (0).mark2 = mark2;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
399 SCOPE (0).buffer1 = incr;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
400 SCOPE (0).type = SCOPETYPE_FOR;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
401 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
402 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
403
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
404 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
405 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
406 case tk_do:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
407 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
408 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
409 push_scope();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
410 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
411 SCOPE (0).mark1 = w->add_mark ("");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
412 SCOPE (0).type = SCOPETYPE_DO;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
413 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
414 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
415
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
416 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
417 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
418 case tk_switch:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
419 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
420 // This gets a bit tricky. switch is structured in the
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
421 // bytecode followingly:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
422 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
423 // (expression)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
424 // case a: goto casemark1
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
425 // case b: goto casemark2
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
426 // case c: goto casemark3
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
427 // goto mark1 // jump to end if no matches
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
428 // casemark1: ...
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
429 // casemark2: ...
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
430 // casemark3: ...
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
431 // mark1: // end mark
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
432
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
433 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
434 push_scope();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
435 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
436 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
437 w->write_buffer (parse_expression (TYPE_INT));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
438 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
439 m_lx->must_get_next (tk_brace_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
440 SCOPE (0).type = SCOPETYPE_SWITCH;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
441 SCOPE (0).mark1 = w->add_mark (""); // end mark
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
442 SCOPE (0).buffer1 = null; // default header
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
443 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
444 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
445
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
446 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
447 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
448 case tk_case:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
449 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
450 // case is only allowed inside switch
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
451 if (SCOPE (0).type != SCOPETYPE_SWITCH)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
452 error ("case label outside switch");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
453
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
454 // Get the literal (Zandronum does not support expressions here)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
455 m_lx->must_get_next (tk_number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
456 int num = m_lx->get_token()->text.to_long();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
457 m_lx->must_get_next (tk_colon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
458
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
459 for (int i = 0; i < MAX_CASE; i++)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
460 if (SCOPE (0).casenumbers[i] == num)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
461 error ("multiple case %d labels in one switch", num);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
462
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
463 // write down the expression and case-go-to. This builds
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
464 // the case tree. The closing event will write the actual
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
465 // blocks and move the marks appropriately.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
466 // AddSwitchCase will add the reference to the mark
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
467 // for the case block that this heralds, and takes care
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
468 // of buffering setup and stuff like that.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
469 // null the switch buffer for the case-go-to statement,
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
470 // we want it all under the switch, not into the case-buffers.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
471 w->SwitchBuffer = null;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
472 w->write (DH_CASEGOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
473 w->write (num);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
474 add_switch_case (w, null);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
475 SCOPE (0).casenumbers[SCOPE (0).casecursor] = num;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
476 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
477 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
478
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
479 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
480 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
481 case tk_default:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
482 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
483 if (SCOPE (0).type != SCOPETYPE_SWITCH)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
484 error ("default label outside switch");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
485
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
486 if (SCOPE (0).buffer1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
487 error ("multiple default labels in one switch");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
488
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
489 m_lx->must_get_next (tk_colon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
490
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
491 // The default header is buffered into buffer1, since
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
492 // it has to be the last of the case headers
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
493 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
494 // Since the expression is pushed into the switch
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
495 // and is only popped when case succeeds, we have
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
496 // to pop it with DH_DROP manually if we end up in
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
497 // a default.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
498 data_buffer* b = new data_buffer;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
499 SCOPE (0).buffer1 = b;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
500 b->write (DH_DROP);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
501 b->write (DH_GOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
502 add_switch_case (w, b);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
503 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
504 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
505
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
506 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
507 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
508 case tk_break:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
509 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
510 if (!g_ScopeCursor)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
511 error ("unexpected `break`");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
512
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
513 w->write (DH_GOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
514
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
515 // switch and if use mark1 for the closing point,
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
516 // for and while use mark2.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
517 switch (SCOPE (0).type)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
518 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
519 case SCOPETYPE_IF:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
520 case SCOPETYPE_SWITCH:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
521 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
522 w->add_reference (SCOPE (0).mark1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
523 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
524
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
525 case SCOPETYPE_FOR:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
526 case SCOPETYPE_WHILE:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
527 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
528 w->add_reference (SCOPE (0).mark2);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
529 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
530
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
531 default:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
532 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
533 error ("unexpected `break`");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
534 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
535 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
536
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
537 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
538 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
539 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
540
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
541 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
542 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
543 case tk_continue:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
544 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
545 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
546
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
547 int curs;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
548 bool found = false;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
549
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
550 // Fall through the scope until we find a loop block
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
551 for (curs = g_ScopeCursor; curs > 0 && !found; curs--)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
552 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
553 switch (scopestack[curs].type)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
554 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
555 case SCOPETYPE_FOR:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
556 case SCOPETYPE_WHILE:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
557 case SCOPETYPE_DO:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
558 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
559 w->write (DH_GOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
560 w->add_reference (scopestack[curs].mark1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
561 found = true;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
562 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
563
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
564 default:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
565 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
566 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
567 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
568
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
569 // No loop blocks
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
570 if (!found)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
571 error ("`continue`-statement not inside a loop");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
572 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
573 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
574
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
575 case tk_brace_end:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
576 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
577 // Closing brace
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
578 // If we're in the block stack, we're descending down from it now
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
579 if (g_ScopeCursor > 0)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
580 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
581 switch (SCOPE (0).type)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
582 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
583 case SCOPETYPE_IF:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
584 // Adjust the closing mark.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
585 w->move_mark (SCOPE (0).mark1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
586
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
587 // We're returning from if, thus else can be next
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
588 g_CanElse = true;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
589 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
590
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
591 case SCOPETYPE_ELSE:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
592 // else instead uses mark1 for itself (so if expression
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
593 // fails, jump to else), mark2 means end of else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
594 w->move_mark (SCOPE (0).mark2);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
595 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
596
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
597 case SCOPETYPE_FOR:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
598 // write the incrementor at the end of the loop block
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
599 w->write_buffer (SCOPE (0).buffer1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
600
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
601 // fall-thru
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
602 case SCOPETYPE_WHILE:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
603 // write down the instruction to go back to the start of the loop
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
604 w->write (DH_GOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
605 w->add_reference (SCOPE (0).mark1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
606
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
607 // Move the closing mark here since we're at the end of the while loop
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
608 w->move_mark (SCOPE (0).mark2);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
609 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
610
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
611 case SCOPETYPE_DO:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
612 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
613 must_get_next (tk_while);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
614 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
615 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
616 data_buffer* expr = parse_expression (TYPE_INT);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
617 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
618 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
619
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
620 // If the condition runs true, go back to the start.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
621 w->write_buffer (expr);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
622 w->write (DH_IFGOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
623 w->add_reference (SCOPE (0).mark1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
624 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
625 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
626
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
627 case SCOPETYPE_SWITCH:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
628 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
629 // Switch closes. Move down to the record buffer of
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
630 // the lower block.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
631 if (SCOPE (1).casecursor != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
632 w->SwitchBuffer = SCOPE (1).casebuffers[SCOPE (1).casecursor];
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
633 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
634 w->SwitchBuffer = null;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
635
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
636 // If there was a default in the switch, write its header down now.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
637 // If not, write instruction to jump to the end of switch after
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
638 // the headers (thus won't fall-through if no case matched)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
639 if (SCOPE (0).buffer1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
640 w->write_buffer (SCOPE (0).buffer1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
641 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
642 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
643 w->write (DH_DROP);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
644 w->write (DH_GOTO);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
645 w->add_reference (SCOPE (0).mark1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
646 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
647
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
648 // Go through all of the buffers we
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
649 // recorded down and write them.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
650 for (int u = 0; u < MAX_CASE; u++)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
651 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
652 if (!SCOPE (0).casebuffers[u])
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
653 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
654
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
655 w->move_mark (SCOPE (0).casemarks[u]);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
656 w->write_buffer (SCOPE (0).casebuffers[u]);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
657 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
658
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
659 // Move the closing mark here
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
660 w->move_mark (SCOPE (0).mark1);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
661 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
662 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
663
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
664 case SCOPETYPE_UNKNOWN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
665 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
666 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
667
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
668 // Descend down the stack
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
669 g_ScopeCursor--;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
670 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
671 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
672
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
673 int dataheader = (g_CurMode == MODE_EVENT) ? DH_ENDEVENT :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
674 (g_CurMode == MODE_MAINLOOP) ? DH_ENDMAINLOOP :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
675 (g_CurMode == MODE_ONENTER) ? DH_ENDONENTER :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
676 (g_CurMode == MODE_ONEXIT) ? DH_ENDONEXIT : -1;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
677
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
678 if (dataheader == -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
679 error ("unexpected `}`");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
680
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
681 // Data header must be written before mode is changed because
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
682 // onenter and mainloop go into special buffers, and we want
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
683 // the closing data headers into said buffers too.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
684 w->write (dataheader);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
685 g_CurMode = MODE_TOPLEVEL;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
686
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
687 if (PeekNext() == ";")
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
688 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
689 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
690 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
691
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
692 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
693 case tk_const:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
694 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
695 constant_info info;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
696
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
697 // Get the type
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
698 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
699 info.type = GetTypeByName (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
700
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
701 if (info.type == TYPE_UNKNOWN || info.type == TYPE_VOID)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
702 error ("unknown type `%s` for constant", TOKEN.c_str());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
703
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
704 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
705 info.name = TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
706
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
707 m_lx->must_get_next (tk_assign);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
708
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
709 switch (info.type)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
710 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
711 case TYPE_BOOL:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
712 case TYPE_INT:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
713 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
714 m_lx->must_get_next (tk_number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
715 info.val = m_lx->get_token()->text.to_long();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
716 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
717
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
718 case TYPE_STRING:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
719 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
720 m_lx->must_get_next (tk_string);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
721 info.val = m_lx->get_token()->text;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
722 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
723
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
724 case TYPE_UNKNOWN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
725 case TYPE_VOID:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
726 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
727 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
728
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
729 g_ConstInfo << info;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
730
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
731 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
732 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
733 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
734
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
735 default:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
736 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
737 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
738 // Label
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
739 lexer::token* next;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
740 if (m_lx->get_token() == tk_symbol &&
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
741 m_lx->peek_next (next) &&
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
742 next->type == tk_colon)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
743 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
744 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
745 string label_name = token_string();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
746
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
747 // want no conflicts..
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
748 if (FindCommand (label_name))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
749 error ("label name `%s` conflicts with command name\n", label_name);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
750
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
751 if (FindGlobalVariable (label_name))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
752 error ("label name `%s` conflicts with variable\n", label_name);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
753
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
754 // See if a mark already exists for this label
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
755 int mark = -1;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
756
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
757 for (int i = 0; i < MAX_MARKS; i++)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
758 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
759 if (g_UndefinedLabels[i] && *g_UndefinedLabels[i] == label_name)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
760 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
761 mark = i;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
762 w->move_mark (i);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
763
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
764 // No longer undefinde
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
765 delete g_UndefinedLabels[i];
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
766 g_UndefinedLabels[i] = null;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
767 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
768 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
769
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
770 // Not found in unmarked lists, define it now
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
771 if (mark == -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
772 w->add_mark (label_name);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
773
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
774 m_lx->must_get_next (tk_colon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
775 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
776 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
777
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
778 // Check if it's a command
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
779 CommandDef* comm = FindCommand (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
780
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
781 if (comm)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
782 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
783 w->get_current_buffer()->merge (ParseCommand (comm));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
784 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
785 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
786 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
787
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
788 // ============================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
789 // If nothing else, parse it as a statement
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
790 data_buffer* b = parse_statement (w);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
791
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
792 if (!b)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
793 error ("unknown TOKEN `%s`", TOKEN.chars());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
794
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
795 w->write_buffer (b);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
796 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
797 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
798 break;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
799 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
800 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
801
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
802 // ===============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
803 // Script file ended. Do some last checks and write the last things to main buffer
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
804 if (g_CurMode != MODE_TOPLEVEL)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
805 error ("script did not end at top level; a `}` is missing somewhere");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
806
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
807 // stateSpawn must be defined!
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
808 if (!g_stateSpawnDefined)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
809 error ("script must have a state named `stateSpawn`!");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
810
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
811 for (int i = 0; i < MAX_MARKS; i++)
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
812 if (g_UndefinedLabels[i])
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
813 error ("label `%s` is referenced via `goto` but isn't defined\n", g_UndefinedLabels[i]->chars());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
814
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
815 // Dump the last state's onenter and mainloop
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
816 w->write_member_buffers();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
817
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
818 // String table
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
819 w->write_string_table();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
820 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
821
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
822 // ============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
823 // Parses a command call
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
824 data_buffer* botscript_parser::ParseCommand (CommandDef* comm)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
825 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
826 data_buffer* r = new data_buffer (64);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
827
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
828 if (g_CurMode == MODE_TOPLEVEL)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
829 error ("command call at top level");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
830
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
831 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
832 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
833
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
834 int curarg = 0;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
835
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
836 while (1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
837 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
838 if (m_lx->get_token() == tk_paren_end)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
839 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
840 if (curarg < comm->numargs)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
841 error ("too few arguments passed to %s\n\tprototype: %s",
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
842 comm->name.chars(), GetCommandPrototype (comm).chars());
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
843
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
844 break;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
845 curarg++;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
846 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
847
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
848 if (curarg >= comm->maxargs)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
849 error ("too many arguments passed to %s\n\tprototype: %s",
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
850 comm->name.chars(), GetCommandPrototype (comm).chars());
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
851
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
852 r->merge (parse_expression (comm->argtypes[curarg]));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
853 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
854
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
855 if (curarg < comm->numargs - 1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
856 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
857 m_lx->must_be (tk_comma);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
858 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
859 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
860 else if (curarg < comm->maxargs - 1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
861 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
862 // Can continue, but can terminate as well.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
863 if (m_lx->get_token() == tk_paren_end)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
864 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
865 curarg++;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
866 break;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
867 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
868 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
869 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
870 m_lx->must_be (tk_comma);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
871 m_lx->must_get_next();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
872 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
873 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
874
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
875 curarg++;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
876 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
877
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
878 // If the script skipped any optional arguments, fill in defaults.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
879 while (curarg < comm->maxargs)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
880 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
881 r->write (DH_PUSHNUMBER);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
882 r->write (comm->defvals[curarg]);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
883 curarg++;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
884 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
885
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
886 r->write (DH_COMMAND);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
887 r->write (comm->number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
888 r->write (comm->maxargs);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
889
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
890 return r;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
891 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
892
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
893 // ============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
894 // Is the given operator an assignment operator?
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
895 static bool is_assignment_operator (int oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
896 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
897 switch (oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
898 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
899 case OPER_ASSIGNADD:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
900 case OPER_ASSIGNSUB:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
901 case OPER_ASSIGNMUL:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
902 case OPER_ASSIGNDIV:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
903 case OPER_ASSIGNMOD:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
904 case OPER_ASSIGNLEFTSHIFT:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
905 case OPER_ASSIGNRIGHTSHIFT:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
906 case OPER_ASSIGN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
907 return true;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
908 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
909
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
910 return false;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
911 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
912
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
913 // ============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
914 // Finds an operator's corresponding dataheader
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
915 static word get_data_header_by_operator (script_variable* var, int oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
916 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
917 if (is_assignment_operator (oper))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
918 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
919 if (!var)
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
920 error ("operator %d requires left operand to be a variable\n", oper);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
921
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
922 // TODO: At the moment, vars only are global
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
923 // OPER_ASSIGNLEFTSHIFT and OPER_ASSIGNRIGHTSHIFT do not
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
924 // have data headers, instead they are expanded out in
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
925 // the operator parser
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
926 switch (oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
927 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
928 case OPER_ASSIGNADD: return DH_ADDGLOBALVAR;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
929 case OPER_ASSIGNSUB: return DH_SUBGLOBALVAR;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
930 case OPER_ASSIGNMUL: return DH_MULGLOBALVAR;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
931 case OPER_ASSIGNDIV: return DH_DIVGLOBALVAR;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
932 case OPER_ASSIGNMOD: return DH_MODGLOBALVAR;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
933 case OPER_ASSIGN: return DH_ASSIGNGLOBALVAR;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
934
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
935 default: error ("bad assignment operator!!\n");
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
936 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
937 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
938
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
939 switch (oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
940 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
941 case OPER_ADD: return DH_ADD;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
942 case OPER_SUBTRACT: return DH_SUBTRACT;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
943 case OPER_MULTIPLY: return DH_MULTIPLY;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
944 case OPER_DIVIDE: return DH_DIVIDE;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
945 case OPER_MODULUS: return DH_MODULUS;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
946 case OPER_EQUALS: return DH_EQUALS;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
947 case OPER_NOTEQUALS: return DH_NOTEQUALS;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
948 case OPER_LESSTHAN: return DH_LESSTHAN;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
949 case OPER_GREATERTHAN: return DH_GREATERTHAN;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
950 case OPER_LESSTHANEQUALS: return DH_LESSTHANEQUALS;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
951 case OPER_GREATERTHANEQUALS: return DH_GREATERTHANEQUALS;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
952 case OPER_LEFTSHIFT: return DH_LSHIFT;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
953 case OPER_RIGHTSHIFT: return DH_RSHIFT;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
954 case OPER_OR: return DH_ORLOGICAL;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
955 case OPER_AND: return DH_ANDLOGICAL;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
956 case OPER_BITWISEOR: return DH_ORBITWISE;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
957 case OPER_BITWISEEOR: return DH_EORBITWISE;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
958 case OPER_BITWISEAND: return DH_ANDBITWISE;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
959 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
960
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
961 error ("DataHeaderByOperator: couldn't find dataheader for operator %d!\n", oper);
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
962 return 0;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
963 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
964
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
965 // ============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
966 // Parses an expression, potentially recursively
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
967 data_buffer* botscript_parser::parse_expression (type_e reqtype)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
968 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
969 data_buffer* retbuf = new data_buffer (64);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
970
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
971 // Parse first operand
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
972 retbuf->merge (parse_expr_value (reqtype));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
973
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
974 // Parse any and all operators we get
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
975 int oper;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
976
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
977 while ( (oper = parse_operator (true)) != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
978 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
979 // We peeked the operator, move forward now
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
980 Next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
981
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
982 // Can't be an assignement operator, those belong in assignments.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
983 if (is_assignment_operator (oper))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
984 error ("assignment operator inside expression");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
985
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
986 // Parse the right operand.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
987 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
988 data_buffer* rb = parse_expr_value (reqtype);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
989
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
990 if (oper == OPER_TERNARY)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
991 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
992 // Ternary operator requires - naturally - a third operand.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
993 m_lx->must_get_next (tk_colon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
994 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
995 data_buffer* tb = parse_expr_value (reqtype);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
996
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
997 // It also is handled differently: there isn't a dataheader for ternary
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
998 // operator. Instead, we abuse PUSHNUMBER and IFNOTGOTO for this.
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
999 // Behold, big block of writing madness! :P
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1000 int mark1 = retbuf->add_mark (""); // start of "else" case
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1001 int mark2 = retbuf->add_mark (""); // end of expression
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1002 retbuf->write (DH_IFNOTGOTO); // if the first operand (condition)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1003 retbuf->add_reference (mark1); // didn't eval true, jump into mark1
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1004 retbuf->merge (rb); // otherwise, perform second operand (true case)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1005 retbuf->write (DH_GOTO); // afterwards, jump to the end, which is
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1006 retbuf->add_reference (mark2); // marked by mark2.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1007 retbuf->move_mark (mark1); // move mark1 at the end of the true case
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1008 retbuf->merge (tb); // perform third operand (false case)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1009 retbuf->move_mark (mark2); // move the ending mark2 here
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1010 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1011 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1012 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1013 // write to buffer
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1014 retbuf->merge (rb);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1015 retbuf->write (get_data_header_by_operator (null, oper));
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1016 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1017 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1018
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1019 return retbuf;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1020 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1021
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1022 // ============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1023 // Parses an operator string. Returns the operator number code.
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1024 #define ISNEXT(C) (PeekNext (peek ? 1 : 0) == C)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1025 int botscript_parser::parse_operator (bool peek)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1026 {
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1027 string oper;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1028
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1029 if (peek)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1030 oper += PeekNext();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1031 else
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1032 oper += TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1033
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1034 if (-oper == "strlen")
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1035 return OPER_STRLEN;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1036
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1037 // Check one-char operators
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1038 bool equalsnext = ISNEXT ("=");
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1039
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1040 int o = (oper == "=" && !equalsnext) ? OPER_ASSIGN :
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1041 (oper == ">" && !equalsnext && !ISNEXT (">")) ? OPER_GREATERTHAN :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1042 (oper == "<" && !equalsnext && !ISNEXT ("<")) ? OPER_LESSTHAN :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1043 (oper == "&" && !ISNEXT ("&")) ? OPER_BITWISEAND :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1044 (oper == "|" && !ISNEXT ("|")) ? OPER_BITWISEOR :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1045 (oper == "+" && !equalsnext) ? OPER_ADD :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1046 (oper == "-" && !equalsnext) ? OPER_SUBTRACT :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1047 (oper == "*" && !equalsnext) ? OPER_MULTIPLY :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1048 (oper == "/" && !equalsnext) ? OPER_DIVIDE :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1049 (oper == "%" && !equalsnext) ? OPER_MODULUS :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1050 (oper == "^") ? OPER_BITWISEEOR :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1051 (oper == "?") ? OPER_TERNARY :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1052 -1;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1053
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1054 if (o != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1055 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1056 return o;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1057 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1058
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1059 // Two-char operators
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1060 oper += PeekNext (peek ? 1 : 0);
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1061 equalsnext = PeekNext (peek ? 2 : 1) == ("=");
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1062
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1063 o = (oper == "+=") ? OPER_ASSIGNADD :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1064 (oper == "-=") ? OPER_ASSIGNSUB :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1065 (oper == "*=") ? OPER_ASSIGNMUL :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1066 (oper == "/=") ? OPER_ASSIGNDIV :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1067 (oper == "%=") ? OPER_ASSIGNMOD :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1068 (oper == "==") ? OPER_EQUALS :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1069 (oper == "!=") ? OPER_NOTEQUALS :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1070 (oper == ">=") ? OPER_GREATERTHANEQUALS :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1071 (oper == "<=") ? OPER_LESSTHANEQUALS :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1072 (oper == "&&") ? OPER_AND :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1073 (oper == "||") ? OPER_OR :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1074 (oper == "<<" && !equalsnext) ? OPER_LEFTSHIFT :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1075 (oper == ">>" && !equalsnext) ? OPER_RIGHTSHIFT :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1076 -1;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1077
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1078 if (o != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1079 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1080 m_lx->must_get_next();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1081 return o;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1082 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1083
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1084 // Three-char opers
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1085 oper += PeekNext (peek ? 2 : 1);
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1086 o = oper == "<<=" ? OPER_ASSIGNLEFTSHIFT :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1087 oper == ">>=" ? OPER_ASSIGNRIGHTSHIFT :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1088 -1;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1089
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1090 if (o != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1091 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1092 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1093 m_lx->must_get_next();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1094 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1095
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1096 return o;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1097 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1098
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1099 // ============================================================================
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1100 string botscript_parser::parse_float()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1101 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1102 m_lx->must_be (tk_number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1103 string floatstring = TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1104 lexer::token* tok;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1105
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1106 // Go after the decimal point
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1107 if (m_lx->peek_next (tok) && tok->type == tk_dot)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1108 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1109 m_lx->skip();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1110 m_lx->must_get_next (tk_number);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1111 floatstring += ".";
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1112 floatstring += token_string();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1113 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1114
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1115 return floatstring;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1116 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1117
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1118 // ============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1119 // Parses a value in the expression and returns the data needed to push
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1120 // it, contained in a data buffer. A value can be either a variable, a command,
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1121 // a literal or an expression.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1122 data_buffer* botscript_parser::parse_expr_value (type_e reqtype)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1123 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1124 data_buffer* b = new data_buffer (16);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1125
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1126 script_variable* g;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1127
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1128 // Prefixing "!" means negation.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1129 bool negate = (m_lx->get_token() == tk_exclamation_mark);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1130
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1131 if (negate) // Jump past the "!"
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1132 m_lx->skip();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1133
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1134 // Handle strlen
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1135 if (TOKEN == "strlen")
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1136 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1137 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1138 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1139
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1140 // By this TOKEN we should get a string constant.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1141 constant_info* constant = find_constant (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1142
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1143 if (!constant || constant->type != TYPE_STRING)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1144 error ("strlen only works with const str");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1145
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1146 if (reqtype != TYPE_INT)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1147 error ("strlen returns int but %s is expected\n", GetTypeName (reqtype).c_str());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1148
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1149 b->write (DH_PUSHNUMBER);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1150 b->write (constant->val.len());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1151
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1152 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1153 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1154 else if (TOKEN == "(")
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1155 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1156 // Expression
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1157 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1158 data_buffer* c = parse_expression (reqtype);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1159 b->merge (c);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1160 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1161 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1162 else if (CommandDef* comm = FindCommand (TOKEN))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1163 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1164 delete b;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1165
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1166 // Command
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1167 if (reqtype && comm->returnvalue != reqtype)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1168 error ("%s returns an incompatible data type", comm->name.chars());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1169
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1170 b = ParseCommand (comm);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1171 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1172 else if (constant_info* constant = find_constant (TOKEN))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1173 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1174 // Type check
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1175 if (reqtype != constant->type)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1176 error ("constant `%s` is %s, expression requires %s\n",
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1177 constant->name.c_str(), GetTypeName (constant->type).c_str(),
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1178 GetTypeName (reqtype).c_str());
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1179
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1180 switch (constant->type)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1181 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1182 case TYPE_BOOL:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1183 case TYPE_INT:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1184 b->write (DH_PUSHNUMBER);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1185 b->write (atoi (constant->val));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1186 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1187
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1188 case TYPE_STRING:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1189 b->write_string (constant->val);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1190 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1191
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1192 case TYPE_VOID:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1193 case TYPE_UNKNOWN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1194 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1195 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1196 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1197 else if ((g = FindGlobalVariable (TOKEN)))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1198 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1199 // Global variable
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1200 b->write (DH_PUSHGLOBALVAR);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1201 b->write (g->index);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1202 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1203 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1204 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1205 // If nothing else, check for literal
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1206 switch (reqtype)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1207 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1208 case TYPE_VOID:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1209 case TYPE_UNKNOWN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1210 error ("unknown identifier `%s` (expected keyword, function or variable)", TOKEN.chars());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1211 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1212
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1213 case TYPE_BOOL:
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1214 case TYPE_INT:
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1215 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1216 m_lx->must_be (tk_number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1217
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1218 // All values are written unsigned - thus we need to write the value's
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1219 // absolute value, followed by an unary minus for negatives.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1220 b->write (DH_PUSHNUMBER);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1221
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1222 long v = atol (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1223 b->write (static_cast<word> (abs (v)));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1224
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1225 if (v < 0)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1226 b->write (DH_UNARYMINUS);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1227
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1228 break;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1229 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1230
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1231 case TYPE_STRING:
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1232 // PushToStringTable either returns the string index of the
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1233 // string if it finds it in the table, or writes it to the
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1234 // table and returns it index if it doesn't find it there.
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1235 MustString (true);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1236 b->write_string (TOKEN);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1237 break;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1238 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1239 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1240
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1241 // Negate it now if desired
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1242 if (negate)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1243 b->write (DH_NEGATELOGICAL);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1244
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1245 return b;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1246 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1247
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1248 // ============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1249 // Parses an assignment. An assignment starts with a variable name, followed
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1250 // by an assignment operator, followed by an expression value. Expects current
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1251 // TOKEN to be the name of the variable, and expects the variable to be given.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1252 data_buffer* botscript_parser::ParseAssignment (script_variable* var)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1253 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1254 bool global = !var->statename.len();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1255
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1256 // Get an operator
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1257 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1258 int oper = parse_operator();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1259
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1260 if (!is_assignment_operator (oper))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1261 error ("expected assignment operator");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1262
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1263 if (g_CurMode == MODE_TOPLEVEL) // TODO: lift this restriction
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1264 error ("can't alter variables at top level");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1265
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1266 // Parse the right operand
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1267 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1268 data_buffer* retbuf = new data_buffer;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1269 data_buffer* expr = parse_expression (var->type);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1270
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1271 // <<= and >>= do not have data headers. Solution: expand them.
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1272 // a <<= b -> a = a << b
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1273 // a >>= b -> a = a >> b
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1274 if (oper == OPER_ASSIGNLEFTSHIFT || oper == OPER_ASSIGNRIGHTSHIFT)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1275 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1276 retbuf->write (global ? DH_PUSHGLOBALVAR : DH_PUSHLOCALVAR);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1277 retbuf->write (var->index);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1278 retbuf->merge (expr);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1279 retbuf->write ( (oper == OPER_ASSIGNLEFTSHIFT) ? DH_LSHIFT : DH_RSHIFT);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1280 retbuf->write (global ? DH_ASSIGNGLOBALVAR : DH_ASSIGNLOCALVAR);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1281 retbuf->write (var->index);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1282 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1283 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1284 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1285 retbuf->merge (expr);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1286 long dh = get_data_header_by_operator (var, oper);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1287 retbuf->write (dh);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1288 retbuf->write (var->index);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1289 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1290
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1291 return retbuf;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1292 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1293
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1294 void botscript_parser::push_scope()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1295 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1296 g_ScopeCursor++;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1297
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1298 if (g_ScopeCursor >= MAX_SCOPE)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1299 error ("too deep scope");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1300
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1301 ScopeInfo* info = &SCOPE (0);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1302 info->type = SCOPETYPE_UNKNOWN;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1303 info->mark1 = 0;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1304 info->mark2 = 0;
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1305 info->buffer1 = null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1306 info->casecursor = -1;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1307
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1308 for (int i = 0; i < MAX_CASE; i++)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1309 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1310 info->casemarks[i] = MAX_MARKS;
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1311 info->casebuffers[i] = null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1312 info->casenumbers[i] = -1;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1313 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1314 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1315
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1316 data_buffer* botscript_parser::parse_statement (object_writer* w)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1317 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1318 if (find_constant (TOKEN)) // There should not be constants here.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1319 error ("invalid use for constant\n");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1320
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1321 // If it's a variable, expect assignment.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1322 if (script_variable* var = FindGlobalVariable (TOKEN))
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1323 return ParseAssignment (var);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1324
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1325 return null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1326 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1327
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1328 void botscript_parser::add_switch_case (object_writer* w, data_buffer* b)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1329 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1330 ScopeInfo* info = &SCOPE (0);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1331
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1332 info->casecursor++;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1333
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1334 if (info->casecursor >= MAX_CASE)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1335 error ("too many cases in one switch");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1336
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1337 // Init a mark for the case buffer
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1338 int m = w->add_mark ("");
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1339 info->casemarks[info->casecursor] = m;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1340
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1341 // Add a reference to the mark. "case" and "default" both
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1342 // add the necessary bytecode before the reference.
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1343 if (b)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1344 b->add_reference (m);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1345 else
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1346 w->add_reference (m);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1347
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1348 // Init a buffer for the case block and tell the object
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1349 // writer to record all written data to it.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1350 info->casebuffers[info->casecursor] = w->SwitchBuffer = new data_buffer;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1351 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1352
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1353 constant_info* find_constant (string TOKEN)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1354 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1355 for (int i = 0; i < g_ConstInfo.size(); i++)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1356 if (g_ConstInfo[i].name == TOKEN)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1357 return &g_ConstInfo[i];
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1358
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1359 return null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1360 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1361
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1362 // ============================================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1363 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1364 bool botscript_parser::token_is (e_token a)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1365 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1366 return (m_lx->get_token() == a);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1367 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1368
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1369 // ============================================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1370 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1371 string botscript_parser::token_string()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1372 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1373 return m_lx->get_token()->text;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1374 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1375
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1376 // ============================================================================
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1377 //
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1378 string botscript_parser::describe_position() const
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1379 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1380 lexer::token* tok = m_lx->get_token();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1381 return tok->file + ":" + string (tok->line) + ":" + string (tok->column);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1382 }

mercurial