|
1 /* |
|
2 Copyright (c) 2013-2014, Santeri Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions are met: |
|
7 |
|
8 * Redistributions of source code must retain the above copyright |
|
9 notice, this list of conditions and the following disclaimer. |
|
10 |
|
11 * Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 |
|
15 * Neither the name of the <organization> nor the |
|
16 names of its contributors may be used to endorse or promote products |
|
17 derived from this software without specific prior written permission. |
|
18 |
|
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
|
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #ifndef BOTC_PARSER_H |
|
32 #define BOTC_PARSER_H |
|
33 |
|
34 #include <stdio.h> |
|
35 #include "main.h" |
|
36 #include "commands.h" |
|
37 #include "object_writer.h" |
|
38 #include "lexer_scanner.h" |
|
39 #include "tokens.h" |
|
40 |
|
41 #define MAX_FILESTACK 8 |
|
42 #define MAX_SCOPE 32 |
|
43 #define MAX_CASE 64 |
|
44 |
|
45 class lexer; |
|
46 class script_variable; |
|
47 |
|
48 // Operators |
|
49 enum operator_e |
|
50 { |
|
51 OPER_ADD, |
|
52 OPER_SUBTRACT, |
|
53 OPER_MULTIPLY, |
|
54 OPER_DIVIDE, |
|
55 OPER_MODULUS, |
|
56 OPER_ASSIGN, |
|
57 OPER_ASSIGNADD, |
|
58 OPER_ASSIGNSUB, |
|
59 OPER_ASSIGNMUL, |
|
60 OPER_ASSIGNDIV, |
|
61 OPER_ASSIGNMOD, // -- 10 |
|
62 OPER_EQUALS, |
|
63 OPER_NOTEQUALS, |
|
64 OPER_LESSTHAN, |
|
65 OPER_GREATERTHAN, |
|
66 OPER_LESSTHANEQUALS, |
|
67 OPER_GREATERTHANEQUALS, |
|
68 OPER_LEFTSHIFT, |
|
69 OPER_RIGHTSHIFT, |
|
70 OPER_ASSIGNLEFTSHIFT, |
|
71 OPER_ASSIGNRIGHTSHIFT, // -- 20 |
|
72 OPER_OR, |
|
73 OPER_AND, |
|
74 OPER_BITWISEOR, |
|
75 OPER_BITWISEAND, |
|
76 OPER_BITWISEEOR, |
|
77 OPER_TERNARY, |
|
78 OPER_STRLEN, |
|
79 }; |
|
80 |
|
81 struct operator_info |
|
82 { |
|
83 operator_e opercode; |
|
84 DATAHEADERS_e dataheader; |
|
85 e_token token; |
|
86 }; |
|
87 |
|
88 // Mark types |
|
89 enum marktype_e |
|
90 { |
|
91 MARKTYPE_LABEL, |
|
92 MARKTYPE_IF, |
|
93 MARKTYPE_INTERNAL, // internal structures |
|
94 }; |
|
95 |
|
96 // Block types |
|
97 enum scopetype_e |
|
98 { |
|
99 SCOPETYPE_UNKNOWN, |
|
100 SCOPETYPE_IF, |
|
101 SCOPETYPE_WHILE, |
|
102 SCOPETYPE_FOR, |
|
103 SCOPETYPE_DO, |
|
104 SCOPETYPE_SWITCH, |
|
105 SCOPETYPE_ELSE, |
|
106 }; |
|
107 |
|
108 // ============================================================================ |
|
109 // Meta-data about blocks |
|
110 struct ScopeInfo |
|
111 { |
|
112 int mark1; |
|
113 int mark2; |
|
114 scopetype_e type; |
|
115 data_buffer* buffer1; |
|
116 |
|
117 // switch-related stuff |
|
118 // Which case are we at? |
|
119 short casecursor; |
|
120 |
|
121 // Marks to case-blocks |
|
122 int casemarks[MAX_CASE]; |
|
123 |
|
124 // Numbers of the case labels |
|
125 int casenumbers[MAX_CASE]; |
|
126 |
|
127 // actual case blocks |
|
128 data_buffer* casebuffers[MAX_CASE]; |
|
129 |
|
130 // What is the current buffer of the block? |
|
131 data_buffer* recordbuffer; |
|
132 }; |
|
133 |
|
134 // ============================================================================ |
|
135 struct constant_info |
|
136 { |
|
137 string name; |
|
138 type_e type; |
|
139 string val; |
|
140 }; |
|
141 |
|
142 // ============================================================================ |
|
143 // The script reader reads the script, parses it and tells the object writer |
|
144 // the bytecode it needs to write to file. |
|
145 class botscript_parser |
|
146 { |
|
147 public: |
|
148 // ==================================================================== |
|
149 // TODO: make private |
|
150 FILE* fp[MAX_FILESTACK]; |
|
151 string filepath[MAX_FILESTACK]; |
|
152 int fc; |
|
153 |
|
154 int pos[MAX_FILESTACK]; |
|
155 int curline[MAX_FILESTACK]; |
|
156 int curchar[MAX_FILESTACK]; |
|
157 ScopeInfo scopestack[MAX_SCOPE]; |
|
158 long savedpos[MAX_FILESTACK]; // filepointer cursor position |
|
159 int commentmode; |
|
160 long prevpos; |
|
161 string prevtoken; |
|
162 |
|
163 // ==================================================================== |
|
164 // METHODS |
|
165 botscript_parser(); |
|
166 ~botscript_parser(); |
|
167 void parse_botscript (string file_name, object_writer* w); |
|
168 data_buffer* ParseCommand (CommandDef* comm); |
|
169 data_buffer* parse_expression (type_e reqtype); |
|
170 data_buffer* ParseAssignment (script_variable* var); |
|
171 int parse_operator (bool peek = false); |
|
172 data_buffer* parse_expr_value (type_e reqtype); |
|
173 string parse_float (); |
|
174 void push_scope (); |
|
175 data_buffer* parse_statement (object_writer* w); |
|
176 void add_switch_case (object_writer* w, data_buffer* b); |
|
177 void check_toplevel(); |
|
178 void check_not_toplevel(); |
|
179 bool token_is (e_token a); |
|
180 string token_string(); |
|
181 string describe_position() const; |
|
182 |
|
183 private: |
|
184 lexer* m_lx; |
|
185 }; |
|
186 |
|
187 constant_info* find_constant_by_name (string token); |
|
188 |
|
189 #endif // BOTC_PARSER_H |