src/parser.cxx

Mon, 13 Jan 2014 00:15:38 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 13 Jan 2014 00:15:38 +0200
changeset 74
007fbadfa7f9
parent 73
1ee9b312dc18
permissions
-rw-r--r--

more rework

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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
134 if (g_CurState.is_empty() == false)
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
135 w->write_member_buffers();
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
136
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
137 w->write (dh_state_name);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
138 w->write_string (statename);
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
139 w->write (dh_state_index);
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
164 w->write (dh_event);
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
179 w->write (dh_main_loop);
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
195 w->write (onenter ? dh_on_enter : dh_on_exit);
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
209 type_e type = (token_is (tk_int)) ? TYPE_INT :
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
210 (token_is (tk_str)) ? TYPE_STRING :
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
220 script_variable* var = declare_global_variable (this, type, varname);
73
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.
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
245 w->write (dh_goto);
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
273 // Use dh_if_not_goto - if the expression is not true, we goto the mark
73
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.
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
275 w->write (dh_if_not_goto);
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
280 SCOPE (0).type = e_if_scope;
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
296 if (SCOPE (0).type != e_if_scope)
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
304 w->write (dh_goto);
73
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);
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
309 SCOPE (0).type = e_else_scope;
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
338 w->write (dh_if_not_goto);
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
344 SCOPE (0).type = e_while_scope;
73
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);
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
393 w->write (dh_if_not_goto);
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
400 SCOPE (0).type = e_for_scope;
73
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 ("");
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
412 SCOPE (0).type = e_do_scope;
73
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);
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
440 SCOPE (0).type = e_switch_scope;
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
451 if (SCOPE (0).type != e_switch_scope)
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
472 w->write (dh_case_goto);
73
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 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
483 if (SCOPE (0).type != e_switch_scope)
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
496 // to pop it with dh_drop manually if we end up in
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
500 b->write (dh_drop);
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
501 b->write (dh_goto);
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
513 w->write (dh_goto);
73
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 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
519 case e_if_scope:
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
520 case e_switch_scope:
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
525 case e_for_scope:
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
526 case e_while_scope:
73
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 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
555 case e_for_scope:
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
556 case e_while_scope:
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
557 case e_do_scope:
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
558 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
559 w->write (dh_goto);
73
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 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
583 case e_if_scope:
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
591 case e_else_scope:
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
597 case e_for_scope:
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
602 case e_while_scope:
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
604 w->write (dh_goto);
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
611 case e_do_scope:
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
612 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
613 m_lx->must_get_next (tk_while);
73
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);
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
622 w->write (dh_if_goto);
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
627 case e_switch_scope:
73
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 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
643 w->write (dh_drop);
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
644 w->write (dh_goto);
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
664 case e_unknown_scope:
73
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
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
673 int dataheader = (g_CurMode == MODE_EVENT) ? dh_end_event :
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
674 (g_CurMode == MODE_MAINLOOP) ? dh_end_main_loop :
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
675 (g_CurMode == MODE_ONENTER) ? dh_end_on_enter :
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
676 (g_CurMode == MODE_ONEXIT) ? dh_end_on_exit : -1;
73
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;
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
686 lexer::token* tok;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
687 m_lx->get_next (tk_semicolon);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
688 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
689 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
690
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 case tk_const:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
693 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
694 constant_info info;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
695
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
696 // Get the type
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
697 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
698 info.type = GetTypeByName (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
699
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
700 if (info.type == TYPE_UNKNOWN || info.type == TYPE_VOID)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
701 error ("unknown type `%s` for constant", TOKEN.c_str());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
702
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
703 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
704 info.name = TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
705
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
706 m_lx->must_get_next (tk_assign);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
707
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
708 switch (info.type)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
709 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
710 case TYPE_BOOL:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
711 case TYPE_INT:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
712 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
713 m_lx->must_get_next (tk_number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
714 info.val = m_lx->get_token()->text.to_long();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
715 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
716
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
717 case TYPE_STRING:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
718 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
719 m_lx->must_get_next (tk_string);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
720 info.val = m_lx->get_token()->text;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
721 } break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
722
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
723 case TYPE_UNKNOWN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
724 case TYPE_VOID:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
725 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
726 }
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 g_ConstInfo << info;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
729
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
730 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
731 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
732 }
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 default:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
735 {
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 // Label
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
738 lexer::token* next;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
739 if (m_lx->get_token() == tk_symbol &&
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
740 m_lx->peek_next (next) &&
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
741 next->type == tk_colon)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
742 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
743 check_not_toplevel();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
744 string label_name = token_string();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
745
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
746 // want no conflicts..
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
747 if (FindCommand (label_name))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
748 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
749
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
750 if (find_global_variable (label_name))
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
751 error ("label name `%s` conflicts with variable\n", label_name);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
752
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
753 // See if a mark already exists for this label
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
754 int mark = -1;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
755
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
756 for (int i = 0; i < MAX_MARKS; i++)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
757 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
758 if (g_UndefinedLabels[i] && *g_UndefinedLabels[i] == label_name)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
759 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
760 mark = i;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
761 w->move_mark (i);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
762
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
763 // No longer undefinde
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
764 delete g_UndefinedLabels[i];
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
765 g_UndefinedLabels[i] = null;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
766 }
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 // Not found in unmarked lists, define it now
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
770 if (mark == -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
771 w->add_mark (label_name);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
772
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
773 m_lx->must_get_next (tk_colon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
774 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
775 }
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 // Check if it's a command
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
778 CommandDef* comm = FindCommand (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
779
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
780 if (comm)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
781 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
782 w->get_current_buffer()->merge (ParseCommand (comm));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
783 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
784 continue;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
785 }
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 // If nothing else, parse it as a statement
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
789 data_buffer* b = parse_statement (w);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
790
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
791 if (!b)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
792 error ("unknown TOKEN `%s`", TOKEN.chars());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
793
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
794 w->write_buffer (b);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
795 m_lx->must_get_next (tk_semicolon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
796 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
797 break;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
798 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
799 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
800
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
801 // ===============================================================================
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
802 // 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
803 if (g_CurMode != MODE_TOPLEVEL)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
804 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
805
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
806 // stateSpawn must be defined!
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
807 if (!g_stateSpawnDefined)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
808 error ("script must have a state named `stateSpawn`!");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
809
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
810 for (int i = 0; i < MAX_MARKS; i++)
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
811 if (g_UndefinedLabels[i])
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
812 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
813
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
814 // Dump the last state's onenter and mainloop
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
815 w->write_member_buffers();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
816
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
817 // String table
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
818 w->write_string_table();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
819 }
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 // Parses a command call
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
823 data_buffer* botscript_parser::ParseCommand (CommandDef* comm)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
824 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
825 data_buffer* r = new data_buffer (64);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
826
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
827 if (g_CurMode == MODE_TOPLEVEL)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
828 error ("command call at top level");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
829
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
830 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
831 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
832
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
833 int curarg = 0;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
834
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
835 while (1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
836 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
837 if (m_lx->get_token() == tk_paren_end)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
838 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
839 if (curarg < comm->numargs)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
840 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
841 comm->name.chars(), GetCommandPrototype (comm).chars());
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
842
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
843 break;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
844 curarg++;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
845 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
846
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
847 if (curarg >= comm->maxargs)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
848 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
849 comm->name.chars(), GetCommandPrototype (comm).chars());
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
850
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
851 r->merge (parse_expression (comm->argtypes[curarg]));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
852 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
853
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
854 if (curarg < comm->numargs - 1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
855 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
856 m_lx->must_be (tk_comma);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
857 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
858 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
859 else if (curarg < comm->maxargs - 1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
860 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
861 // Can continue, but can terminate as well.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
862 if (m_lx->get_token() == tk_paren_end)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
863 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
864 curarg++;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
865 break;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
866 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
867 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
868 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
869 m_lx->must_be (tk_comma);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
870 m_lx->must_get_next();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
871 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
872 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
873
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
874 curarg++;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
875 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
876
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
877 // 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
878 while (curarg < comm->maxargs)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
879 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
880 r->write (dh_push_number);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
881 r->write (comm->defvals[curarg]);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
882 curarg++;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
883 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
884
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
885 r->write (dh_command);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
886 r->write (comm->number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
887 r->write (comm->maxargs);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
888
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
889 return r;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
890 }
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 // Is the given operator an assignment operator?
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
894 static bool is_assignment_operator (int oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
895 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
896 switch (oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
897 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
898 case OPER_ASSIGNADD:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
899 case OPER_ASSIGNSUB:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
900 case OPER_ASSIGNMUL:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
901 case OPER_ASSIGNDIV:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
902 case OPER_ASSIGNMOD:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
903 case OPER_ASSIGNLEFTSHIFT:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
904 case OPER_ASSIGNRIGHTSHIFT:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
905 case OPER_ASSIGN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
906 return true;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
907 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
908
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
909 return false;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
910 }
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 // Finds an operator's corresponding dataheader
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
914 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
915 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
916 if (is_assignment_operator (oper))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
917 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
918 if (!var)
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
919 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
920
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
921 // TODO: At the moment, vars only are global
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
922 // OPER_ASSIGNLEFTSHIFT and OPER_ASSIGNRIGHTSHIFT do not
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
923 // 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
924 // the operator parser
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
925 switch (oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
926 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
927 case OPER_ASSIGNADD: return dh_add_global_var;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
928 case OPER_ASSIGNSUB: return dh_subtract_global_var;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
929 case OPER_ASSIGNMUL: return dh_multiply_global_var;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
930 case OPER_ASSIGNDIV: return dh_divide_global_var;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
931 case OPER_ASSIGNMOD: return dh_mod_global_var;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
932 case OPER_ASSIGN: return dh_assign_global_var;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
933
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
934 default: error ("bad assignment operator!!\n");
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
935 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
936 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
937
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
938 switch (oper)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
939 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
940 case OPER_ADD: return dh_add;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
941 case OPER_SUBTRACT: return dh_subtract;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
942 case OPER_MULTIPLY: return dh_multiply;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
943 case OPER_DIVIDE: return dh_divide;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
944 case OPER_MODULUS: return dh_modulus;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
945 case OPER_EQUALS: return dh_equals;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
946 case OPER_NOTEQUALS: return dh_not_equals;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
947 case OPER_LESSTHAN: return dh_less_than;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
948 case OPER_GREATERTHAN: return dh_greater_than;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
949 case OPER_LESSTHANEQUALS: return dh_at_most;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
950 case OPER_GREATERTHANEQUALS: return dh_at_least;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
951 case OPER_LEFTSHIFT: return dh_left_shift;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
952 case OPER_RIGHTSHIFT: return dh_right_shift;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
953 case OPER_OR: return dh_or_logical;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
954 case OPER_AND: return dh_and_logical;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
955 case OPER_BITWISEOR: return dh_or_bitwise;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
956 case OPER_BITWISEEOR: return dh_eor_bitwise;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
957 case OPER_BITWISEAND: return dh_and_bitwise;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
958 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
959
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
960 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
961 return 0;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
962 }
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 // Parses an expression, potentially recursively
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
966 data_buffer* botscript_parser::parse_expression (type_e reqtype)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
967 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
968 data_buffer* retbuf = new data_buffer (64);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
969
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
970 // Parse first operand
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
971 retbuf->merge (parse_expr_value (reqtype));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
972
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
973 // Parse any and all operators we get
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
974 int oper;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
975
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
976 while ( (oper = parse_operator (true)) != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
977 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
978 // We peeked the operator, move forward now
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
979 m_lx->skip();
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
980
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
981 // 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
982 if (is_assignment_operator (oper))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
983 error ("assignment operator inside expression");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
984
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
985 // Parse the right operand.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
986 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
987 data_buffer* rb = parse_expr_value (reqtype);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
988
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
989 if (oper == OPER_TERNARY)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
990 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
991 // Ternary operator requires - naturally - a third operand.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
992 m_lx->must_get_next (tk_colon);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
993 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
994 data_buffer* tb = parse_expr_value (reqtype);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
995
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
996 // 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
997 // 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
998 // Behold, big block of writing madness! :P
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
999 int mark1 = retbuf->add_mark (""); // start of "else" case
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1000 int mark2 = retbuf->add_mark (""); // end of expression
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1001 retbuf->write (dh_if_not_goto); // if the first operand (condition)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1002 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
1003 retbuf->merge (rb); // otherwise, perform second operand (true case)
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1004 retbuf->write (dh_goto); // afterwards, jump to the end, which is
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1005 retbuf->add_reference (mark2); // marked by mark2.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1006 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
1007 retbuf->merge (tb); // perform third operand (false case)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1008 retbuf->move_mark (mark2); // move the ending mark2 here
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1009 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1010 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1011 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1012 // write to buffer
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1013 retbuf->merge (rb);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1014 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
1015 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1016 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1017
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1018 return retbuf;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1019 }
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 // Parses an operator string. Returns the operator number code.
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1023 #define ISNEXT(C) (PeekNext (peek ? 1 : 0) == C)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1024 int botscript_parser::parse_operator (bool peek)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1025 {
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1026 string oper;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1027
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1028 if (peek)
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1029 oper += m_lx->peek_next_string();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1030 else
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1031 oper += TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1032
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1033 if (-oper == "strlen")
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1034 return OPER_STRLEN;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1035
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1036 // Check one-char operators
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1037 bool equalsnext = ISNEXT ("=");
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1038
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1039 int o = (oper == "=" && !equalsnext) ? OPER_ASSIGN :
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1040 (oper == ">" && !equalsnext && !ISNEXT (">")) ? OPER_GREATERTHAN :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1041 (oper == "<" && !equalsnext && !ISNEXT ("<")) ? OPER_LESSTHAN :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1042 (oper == "&" && !ISNEXT ("&")) ? OPER_BITWISEAND :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1043 (oper == "|" && !ISNEXT ("|")) ? OPER_BITWISEOR :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1044 (oper == "+" && !equalsnext) ? OPER_ADD :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1045 (oper == "-" && !equalsnext) ? OPER_SUBTRACT :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1046 (oper == "*" && !equalsnext) ? OPER_MULTIPLY :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1047 (oper == "/" && !equalsnext) ? OPER_DIVIDE :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1048 (oper == "%" && !equalsnext) ? OPER_MODULUS :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1049 (oper == "^") ? OPER_BITWISEEOR :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1050 (oper == "?") ? OPER_TERNARY :
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1051 -1;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1052
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1053 if (o != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1054 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1055 return o;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1056 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1057
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1058 // Two-char operators
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1059 oper += m_lx->peek_next_string ( (peek ? 1 : 0);
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1060 equalsnext = m_lx->peek_next_string (peek ? 2 : 1) == ("=");
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1061
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1062 o = (oper == "+=") ? OPER_ASSIGNADD :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1063 (oper == "-=") ? OPER_ASSIGNSUB :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1064 (oper == "*=") ? OPER_ASSIGNMUL :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1065 (oper == "/=") ? OPER_ASSIGNDIV :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1066 (oper == "%=") ? OPER_ASSIGNMOD :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1067 (oper == "==") ? OPER_EQUALS :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1068 (oper == "!=") ? OPER_NOTEQUALS :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1069 (oper == ">=") ? OPER_GREATERTHANEQUALS :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1070 (oper == "<=") ? OPER_LESSTHANEQUALS :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1071 (oper == "&&") ? OPER_AND :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1072 (oper == "||") ? OPER_OR :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1073 (oper == "<<" && !equalsnext) ? OPER_LEFTSHIFT :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1074 (oper == ">>" && !equalsnext) ? OPER_RIGHTSHIFT :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1075 -1;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1076
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1077 if (o != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1078 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1079 m_lx->must_get_next();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1080 return o;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1081 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1082
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1083 // Three-char opers
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1084 oper += m_lx->peek_next_string (peek ? 2 : 1);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1085 o = oper == "<<=" ? OPER_ASSIGNLEFTSHIFT :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1086 oper == ">>=" ? OPER_ASSIGNRIGHTSHIFT :
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1087 -1;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1088
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1089 if (o != -1)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1090 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1091 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1092 m_lx->must_get_next();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1093 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1094
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1095 return o;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1096 }
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 // ============================================================================
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1099 string botscript_parser::parse_float()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1100 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1101 m_lx->must_be (tk_number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1102 string floatstring = TOKEN;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1103 lexer::token* tok;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1104
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1105 // Go after the decimal point
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1106 if (m_lx->peek_next (tok) && tok->type == tk_dot)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1107 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1108 m_lx->skip();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1109 m_lx->must_get_next (tk_number);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1110 floatstring += ".";
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1111 floatstring += token_string();
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1112 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1113
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1114 return floatstring;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1115 }
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 // 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
1119 // 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
1120 // a literal or an expression.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1121 data_buffer* botscript_parser::parse_expr_value (type_e reqtype)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1122 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1123 data_buffer* b = new data_buffer (16);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1124
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1125 script_variable* g;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1126
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1127 // Prefixing "!" means negation.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1128 bool negate = (m_lx->get_token() == tk_exclamation_mark);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1129
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1130 if (negate) // Jump past the "!"
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1131 m_lx->skip();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1132
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1133 // Handle strlen
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1134 if (TOKEN == "strlen")
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1135 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1136 m_lx->must_get_next (tk_paren_start);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1137 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1138
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1139 // By this TOKEN we should get a string constant.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1140 constant_info* constant = find_constant (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1141
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1142 if (!constant || constant->type != TYPE_STRING)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1143 error ("strlen only works with const str");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1144
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1145 if (reqtype != TYPE_INT)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1146 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
1147
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1148 b->write (dh_push_number);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1149 b->write (constant->val.len());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1150
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1151 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1152 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1153 else if (TOKEN == "(")
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1154 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1155 // Expression
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1156 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1157 data_buffer* c = parse_expression (reqtype);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1158 b->merge (c);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1159 m_lx->must_get_next (tk_paren_end);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1160 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1161 else if (CommandDef* comm = FindCommand (TOKEN))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1162 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1163 delete b;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1164
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1165 // Command
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1166 if (reqtype && comm->returnvalue != reqtype)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1167 error ("%s returns an incompatible data type", comm->name.chars());
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1168
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1169 b = ParseCommand (comm);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1170 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1171 else if (constant_info* constant = find_constant (TOKEN))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1172 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1173 // Type check
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1174 if (reqtype != constant->type)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1175 error ("constant `%s` is %s, expression requires %s\n",
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1176 constant->name.c_str(), GetTypeName (constant->type).c_str(),
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1177 GetTypeName (reqtype).c_str());
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1178
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1179 switch (constant->type)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1180 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1181 case TYPE_BOOL:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1182 case TYPE_INT:
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1183 b->write (dh_push_number);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1184 b->write (atoi (constant->val));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1185 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1186
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1187 case TYPE_STRING:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1188 b->write_string (constant->val);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1189 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1190
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1191 case TYPE_VOID:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1192 case TYPE_UNKNOWN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1193 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1194 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1195 }
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1196 else if ((g = find_global_variable (TOKEN)))
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1197 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1198 // Global variable
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1199 b->write (dh_push_global_var);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1200 b->write (g->index);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1201 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1202 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1203 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1204 // If nothing else, check for literal
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1205 switch (reqtype)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1206 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1207 case TYPE_VOID:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1208 case TYPE_UNKNOWN:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1209 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
1210 break;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1211
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1212 case TYPE_BOOL:
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1213 case TYPE_INT:
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1214 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1215 m_lx->must_be (tk_number);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1216
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1217 // 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
1218 // absolute value, followed by an unary minus for negatives.
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1219 b->write (dh_push_number);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1220
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1221 long v = atol (TOKEN);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1222 b->write (static_cast<word> (abs (v)));
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1223
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1224 if (v < 0)
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1225 b->write (dh_unary_minus);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1226
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1227 break;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1228 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1229
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1230 case TYPE_STRING:
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1231 // PushToStringTable either returns the string index of the
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1232 // 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
1233 // table and returns it index if it doesn't find it there.
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1234 m_lx->must_be (tk_string);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1235 b->write_string (TOKEN);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1236 break;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1237 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1238 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1239
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1240 // Negate it now if desired
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1241 if (negate)
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1242 b->write (dh_negate_logical);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1243
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1244 return b;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1245 }
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 // 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
1249 // 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
1250 // 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
1251 data_buffer* botscript_parser::ParseAssignment (script_variable* var)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1252 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1253 bool global = !var->statename.len();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1254
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1255 // Get an operator
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1256 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1257 int oper = parse_operator();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1258
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1259 if (!is_assignment_operator (oper))
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1260 error ("expected assignment operator");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1261
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1262 if (g_CurMode == MODE_TOPLEVEL)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1263 error ("can't alter variables at top level");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1264
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1265 // Parse the right operand
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1266 m_lx->must_get_next();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1267 data_buffer* retbuf = new data_buffer;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1268 data_buffer* expr = parse_expression (var->type);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1269
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1270 // <<= 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
1271 // a <<= b -> a = a << b
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1272 // a >>= b -> a = a >> b
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1273 if (oper == OPER_ASSIGNLEFTSHIFT || oper == OPER_ASSIGNRIGHTSHIFT)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1274 {
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1275 retbuf->write (global ? dh_push_global_var : dh_push_local_var);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1276 retbuf->write (var->index);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1277 retbuf->merge (expr);
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1278 retbuf->write ((oper == OPER_ASSIGNLEFTSHIFT) ? dh_left_shift : dh_right_shift);
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1279 retbuf->write (global ? dh_assign_global_var : dh_assign_local_var);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1280 retbuf->write (var->index);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1281 }
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1282 else
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1283 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1284 retbuf->merge (expr);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1285 long dh = get_data_header_by_operator (var, oper);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1286 retbuf->write (dh);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1287 retbuf->write (var->index);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1288 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1289
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1290 return retbuf;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1291 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1292
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1293 void botscript_parser::push_scope()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1294 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1295 g_ScopeCursor++;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1296
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1297 if (g_ScopeCursor >= MAX_SCOPE)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1298 error ("too deep scope");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1299
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1300 ScopeInfo* info = &SCOPE (0);
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1301 info->type = e_unknown_scope;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1302 info->mark1 = 0;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1303 info->mark2 = 0;
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1304 info->buffer1 = null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1305 info->casecursor = -1;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1306
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1307 for (int i = 0; i < MAX_CASE; i++)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1308 {
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1309 info->casemarks[i] = MAX_MARKS;
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1310 info->casebuffers[i] = null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1311 info->casenumbers[i] = -1;
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1312 }
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
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1315 data_buffer* botscript_parser::parse_statement (object_writer* w)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1316 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1317 if (find_constant (TOKEN)) // There should not be constants here.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1318 error ("invalid use for constant\n");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1319
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1320 // If it's a variable, expect assignment.
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1321 if (script_variable* var = find_global_variable (TOKEN))
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1322 return ParseAssignment (var);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1323
72
03e4d9db3fd9 - major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents: 71
diff changeset
1324 return null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1325 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1326
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1327 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
1328 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1329 ScopeInfo* info = &SCOPE (0);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1330
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1331 info->casecursor++;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1332
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1333 if (info->casecursor >= MAX_CASE)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1334 error ("too many cases in one switch");
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1335
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1336 // Init a mark for the case buffer
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1337 int m = w->add_mark ("");
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1338 info->casemarks[info->casecursor] = m;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1339
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1340 // 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
1341 // add the necessary bytecode before the reference.
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1342 if (b)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1343 b->add_reference (m);
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1344 else
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1345 w->add_reference (m);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1346
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1347 // 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
1348 // writer to record all written data to it.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1349 info->casebuffers[info->casecursor] = w->SwitchBuffer = new data_buffer;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1350 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1351
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1352 constant_info* find_constant (string tok)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1353 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1354 for (int i = 0; i < g_ConstInfo.size(); i++)
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
1355 if (g_ConstInfo[i].name == tok)
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1356 return &g_ConstInfo[i];
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1357
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1358 return null;
71
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1359 }
11f23fabf8a6 - moved sources to src/, migrated to cmake
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1360
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1361 // ============================================================================
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 bool botscript_parser::token_is (e_token a)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1364 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1365 return (m_lx->get_token() == a);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1366 }
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 string botscript_parser::token_string()
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1371 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1372 return m_lx->get_token()->text;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1373 }
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 string botscript_parser::describe_position() const
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1378 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1379 lexer::token* tok = m_lx->get_token();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
1380 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
1381 }

mercurial