src/Expression.cc

changeset 92
3a00d396bce2
parent 91
427eb377d53e
child 93
11a24b697f43
equal deleted inserted replaced
91:427eb377d53e 92:3a00d396bce2
52 retbuf->AdjustMark (mark2); // move the ending mark2 here 52 retbuf->AdjustMark (mark2); // move the ending mark2 here
53 */ 53 */
54 54
55 // ============================================================================= 55 // =============================================================================
56 // 56 //
57 Expression::Expression (BotscriptParser* parser, EType reqtype, Lexer* lx) : 57 Expression::Expression (BotscriptParser* parser, Lexer* lx, EType reqtype) :
58 mParser (parser), 58 mParser (parser),
59 mLexer (lx), 59 mLexer (lx),
60 mType (reqtype), 60 mType (reqtype),
61 mResult (null) 61 mResult (null)
62 { 62 {
66 mSymbols << sym; 66 mSymbols << sym;
67 67
68 if (mSymbols.IsEmpty()) 68 if (mSymbols.IsEmpty())
69 Error ("Expected expression"); 69 Error ("Expected expression");
70 70
71 AdjustOperators();
72
73 for (ExpressionSymbol* sym : mSymbols)
74 {
75 switch (sym->GetType())
76 {
77 case ExpressionSymbol::eOperatorSymbol:
78 {
79 Print ("\t- Operator: %1\n", static_cast<ExpressionOperator*> (sym)->GetID());
80 break;
81 }
82
83 case ExpressionSymbol::eValueSymbol:
84 {
85 ExpressionValue* val = static_cast<ExpressionValue*> (sym);
86
87 if (val->IsConstexpr())
88 Print ("\t- Constant expression value: %1\n", val->GetValue());
89 else
90 {
91 Print ("\t- Databuffer value:\n");
92 val->GetBuffer()->Dump();
93 }
94 break;
95 }
96
97 case ExpressionSymbol::eColonSymbol:
98 {
99 Print ("\t- Colon");
100 break;
101 }
102 }
103 }
104
105 exit (0);
106
107 /*
71 Verify(); 108 Verify();
72 mResult = Evaluate(); 109 mResult = Evaluate();
110 */
73 } 111 }
74 112
75 // ============================================================================= 113 // =============================================================================
76 // 114 //
77 Expression::~Expression() 115 Expression::~Expression()
96 try 134 try
97 { 135 {
98 ScriptVariable* globalvar; 136 ScriptVariable* globalvar;
99 mLexer->MustGetNext(); 137 mLexer->MustGetNext();
100 138
139 Print ("Token type: %1\n", mLexer->DescribeTokenType (mLexer->GetTokenType()));
140
101 if (mLexer->GetTokenType() == tkColon) 141 if (mLexer->GetTokenType() == tkColon)
102 return new ExpressionColon; 142 return new ExpressionColon;
103 143
104 // Check for operator 144 // Check for operator
105 for (const OperatorInfo* op : gOperators) 145 for (const OperatorInfo& op : gOperators)
106 if (mLexer->GetTokenType() == op->token) 146 if (mLexer->GetTokenType() == op.token)
107 return new ExpressionOperator (op - gOperators); 147 return new ExpressionOperator ((EOperator) (&op - &gOperators[0]));
108 148
109 // Check sub-expression 149 // Check sub-expression
110 if (mLexer->GetTokenType() == tkParenStart) 150 if (mLexer->GetTokenType() == tkParenStart)
111 { 151 {
112 mLexer->MustGetNext();
113 Expression expr (mParser, mLexer, mType); 152 Expression expr (mParser, mLexer, mType);
114 mLexer->MustGetNext (tkParenEnd); 153 mLexer->MustGetNext (tkParenEnd);
115 return expr.GetResult(); 154 return expr.GetResult();
116 } 155 }
117 156
118 op = new ExpressionValue; 157 op = new ExpressionValue (mType);
119 158
120 // Check function 159 // Check function
121 if (CommandInfo* comm = FindCommandByName (GetTokenString())) 160 if (CommandInfo* comm = FindCommandByName (GetTokenString()))
122 { 161 {
123 if (mType != EUnknownType && comm->returnvalue != mType) 162 if (mType != EUnknownType && comm->returnvalue != mType)
169 // Check for literal 208 // Check for literal
170 switch (mType) 209 switch (mType)
171 { 210 {
172 case EVoidType: 211 case EVoidType:
173 case EUnknownType: 212 case EUnknownType:
213 {
174 Error ("unknown identifier `%1` (expected keyword, function or variable)", GetTokenString()); 214 Error ("unknown identifier `%1` (expected keyword, function or variable)", GetTokenString());
175 break; 215 break;
216 }
176 217
177 case EBoolType: 218 case EBoolType:
219 {
178 if ((tt = mLexer->GetTokenType()) == tkTrue || tt == tkFalse) 220 if ((tt = mLexer->GetTokenType()) == tkTrue || tt == tkFalse)
179 { 221 {
180 op->SetValue (tt == tkTrue ? 1 : 0); 222 op->SetValue (tt == tkTrue ? 1 : 0);
181 return op; 223 return op;
182 } 224 }
225 }
183 case EIntType: 226 case EIntType:
184 if (!mLexer->GetTokenType() != tkNumber) 227 {
228 if (mLexer->GetTokenType() != tkNumber)
185 throw failed; 229 throw failed;
186 230
187 op->SetValue (GetTokenString().ToLong()); 231 op->SetValue (GetTokenString().ToLong());
188 return op; 232 return op;
233 }
189 234
190 case EStringType: 235 case EStringType:
191 if (!mLexer->GetTokenType() != tkString) 236 {
237 if (mLexer->GetTokenType() != tkString)
192 throw failed; 238 throw failed;
193 239
194 op->SetValue (GetStringTableIndex (GetTokenString())); 240 op->SetValue (GetStringTableIndex (GetTokenString()));
195 return op; 241 return op;
242 }
196 } 243 }
197 244
198 assert (false); 245 assert (false);
199 throw failed; 246 throw failed;
200 } 247 }
201 catch (ELocalException&) 248 catch (ELocalException&)
202 { 249 {
203 // We use a local enum here since catch(...) would catch Error() calls. 250 // We use a local enum here since catch(...) would catch Error() calls.
204 mLexer->SetPosition (pos); 251 mLexer->SetPosition (pos);
205 delete op; 252 delete op;
206 return false; 253 return null;
207 } 254 }
208 255
209 assert (false); 256 assert (false);
210 return false; 257 return null;
258 }
259
260 // =============================================================================
261 //
262 // The symbol parsing process only does token-based checking for operators. Thus
263 // ALL minus operators are actually unary minuses simply because both have
264 // tkMinus as their token and the unary minus is prior to the binary minus in
265 // the operator table. Now that we have all symbols present, we can correct
266 // cases like this.
267 //
268 void Expression::AdjustOperators()
269 {
270 for (auto it = mSymbols.begin() + 1; it != mSymbols.end(); ++it)
271 {
272 if ((*it)->GetType() != ExpressionSymbol::eOperatorSymbol)
273 continue;
274
275 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it);
276
277 // Unary minus with a value as the previous symbol cannot really be
278 // unary; replace with binary minus.
279 if (op->GetID() == opUnaryMinus && (*(it - 1))->GetType() == ExpressionSymbol::eValueSymbol)
280 {
281 Print ("Changing symbol operator #%1 from %2 to %3\n",
282 it - mSymbols.begin(), op->GetID(), opSubtraction);
283 op->SetID (opSubtraction);
284 }
285 }
211 } 286 }
212 287
213 // ============================================================================= 288 // =============================================================================
214 // 289 //
215 ExpressionValue* Expression::Evaluate() 290 ExpressionValue* Expression::Evaluate()
231 return mLexer->GetToken()->text; 306 return mLexer->GetToken()->text;
232 } 307 }
233 308
234 // ============================================================================= 309 // =============================================================================
235 // 310 //
236 ExpressionOperator::ExpressionOperator (int id) : 311 ExpressionOperator::ExpressionOperator (EOperator id) :
237 mID (id), 312 ExpressionSymbol (eOperatorSymbol),
238 mType (eOperator) {} 313 mID (id) {}
239 314
240 // ============================================================================= 315 // =============================================================================
241 // 316 //
242 ExpressionValue::ExpressionValue(EType valuetype) : 317 ExpressionValue::ExpressionValue (EType valuetype) :
318 ExpressionSymbol (eValueSymbol),
243 mBuffer (null), 319 mBuffer (null),
244 mType (eOperand),
245 mValueType (valuetype) {} 320 mValueType (valuetype) {}
321
322 // =============================================================================
323 //
324 ExpressionValue::~ExpressionValue()
325 {
326 delete mBuffer;
327 }
246 328
247 // ============================================================================= 329 // =============================================================================
248 // 330 //
249 void ExpressionValue::ConvertToBuffer() 331 void ExpressionValue::ConvertToBuffer()
250 { 332 {

mercurial