~ubuntu-branches/ubuntu/wily/oolite/wily-proposed

« back to all changes in this revision

Viewing changes to tools/ScriptConverter/OOLegacyScriptToJavaScriptConverterCore.m

  • Committer: Package Import Robot
  • Author(s): Nicolas Boulenguez
  • Date: 2011-12-22 00:22:39 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20111222002239-pr3upeupp4jw1psp
Tags: 1.76-1
* New upstream.
* watch: scan upstream stable releases instead of dev snapshots.
* control: use default gobjc instead of explicit 4.6.
* rules: use dpkg-dev build flags.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  OOLegacyScriptToJavaScriptConverterCore.m
 
3
//  ScriptConverter
 
4
//
 
5
//  Created by Jens Ayton on 2008-08-30.
 
6
//  Copyright 2008 Jens Ayton. All rights reserved.
 
7
//
 
8
 
 
9
#import "OOLegacyScriptToJavaScriptConverterCore.h"
 
10
#import "NSScannerOOExtensions.h"
 
11
#import "OOCollectionExtractors.h"
 
12
#import "OOJSExprNodeHelpers.h"
 
13
 
 
14
 
 
15
#define COMMENT_SELECTOR                0
 
16
 
 
17
 
 
18
typedef enum
 
19
{
 
20
        kComparisonEqual,
 
21
        kComparisonNotEqual,
 
22
        kComparisonLessThan,
 
23
        kComparisonGreaterThan,
 
24
        kComparisonOneOf,
 
25
        kComparisonUndefined
 
26
} OOComparisonType;
 
27
 
 
28
 
 
29
typedef enum
 
30
{
 
31
        kTypeInvalid,
 
32
        kTypeString,
 
33
        kTypeNumber,
 
34
        kTypeBool
 
35
} OOOperandType;
 
36
 
 
37
 
 
38
static NSMutableArray *ScanTokensFromString(NSString *values);
 
39
 
 
40
 
 
41
@interface OOLegacyScriptToJavaScriptConverter (ConverterCorePrivate)
 
42
 
 
43
- (void) convertConditional:(NSDictionary *)conditional;
 
44
 
 
45
- (void) convertOneAction:(NSString *)action;
 
46
 
 
47
- (NSString *) convertOneCondition:(NSString *)condition;
 
48
- (OOJSExprNode *) convertQuery:(NSString *)query gettingType:(OOOperandType *)outType;
 
49
- (OOJSExprNode *) convertStringCondition:(OOComparisonType)comparator comparatorString:(NSString *)comparatorString lhs:(OOJSExprNode *)lhs rhs:(OOJSExprNode *)rhs rawCondition:(NSString *)rawCondition;
 
50
- (OOJSExprNode *) convertNumberCondition:(OOComparisonType)comparator comparatorString:(NSString *)comparatorString lhs:(OOJSExprNode *)lhs rhs:(OOJSExprNode *)rhs rawCondition:(NSString *)rawCondition;
 
51
- (OOJSExprNode *) convertBoolCondition:(OOComparisonType)comparator comparatorString:(NSString *)comparatorString lhs:(OOJSExprNode *)lhs rhs:(OOJSExprNode *)rhs rawCondition:(NSString *)rawCondition;
 
52
 
 
53
#if OBSOLETE
 
54
- (NSString *) stringifyBooleanExpression:(NSString *)expr;
 
55
#endif
 
56
 
 
57
@end
 
58
 
 
59
 
 
60
@implementation OOLegacyScriptToJavaScriptConverter (ConverterCore)
 
61
 
 
62
- (void) convertActions:(NSArray *)actions
 
63
{
 
64
        OOUInteger                              i, count;
 
65
        id                                              action;
 
66
        NSAutoreleasePool               *pool = nil;
 
67
        
 
68
        count = [actions count];
 
69
        
 
70
        NS_DURING
 
71
                for (i = 0; i != count; ++i)
 
72
                {
 
73
                        pool = [[NSAutoreleasePool alloc] init];
 
74
                        
 
75
                        action = [actions objectAtIndex:i];
 
76
                        
 
77
                        if ([action isKindOfClass:[NSString class]])
 
78
                        {
 
79
                                [self convertOneAction:action];
 
80
                        }
 
81
                        else if ([action isKindOfClass:[NSDictionary class]])
 
82
                        {
 
83
                                [self convertConditional:action];
 
84
                        }
 
85
                        else
 
86
                        {
 
87
                                [self addStopIssueWithKey:@"invalid-type"
 
88
                                                                   format:@"Expected string (action) or dictionary (conditional), but found %@.", [action class]];
 
89
                                [self appendWithFormat:@"<** Invalid object of class %@ **>", [action class]];
 
90
                        }
 
91
                        
 
92
                        [pool release];
 
93
                        pool = nil;
 
94
                }
 
95
        NS_HANDLER
 
96
                [pool release];
 
97
                [localException raise];
 
98
        NS_ENDHANDLER
 
99
}
 
100
 
 
101
@end
 
102
 
 
103
 
 
104
@implementation OOLegacyScriptToJavaScriptConverter (ConverterCorePrivate)
 
105
 
 
106
- (void) convertConditional:(NSDictionary *)conditional
 
107
{
 
108
        NSArray                                 *conditions = nil;
 
109
        NSArray                                 *ifTrue = nil;
 
110
        NSArray                                 *ifFalse = nil;
 
111
        OOUInteger                              i, count;
 
112
        NSString                                *cond = nil;
 
113
        BOOL                                    flipCondition = NO;
 
114
        
 
115
        conditions = [conditional arrayForKey:@"conditions"];
 
116
        ifTrue = [conditional arrayForKey:@"do"];
 
117
        ifFalse = [conditional arrayForKey:@"else"];
 
118
        
 
119
        if (ifTrue == nil && ifFalse == nil)
 
120
        {
 
121
                [self addWarningIssueWithKey:@"empty-conditional-action"
 
122
                                                          format:@"Conditional expression with neither \"do\" clause nor \"else\" clause, ignoring."];
 
123
                return;
 
124
        }
 
125
        
 
126
        if ([ifTrue count] == 0)
 
127
        {
 
128
                flipCondition = YES;
 
129
                ifTrue = ifFalse;
 
130
                ifFalse = nil;
 
131
        }
 
132
        
 
133
        count = [conditions count];
 
134
        if (count == 0)
 
135
        {
 
136
                [self addWarningIssueWithKey:@"empty-conditions"
 
137
                                                          format:@"Empty or invalid conditions array, treating as always true."];
 
138
                ifFalse = nil;
 
139
                // Treat as always-true for backwards-compatibility
 
140
        }
 
141
        else
 
142
        {
 
143
                [self append:@"if ("];
 
144
                if (flipCondition)  [self append:@"!("];
 
145
                
 
146
                for (i = 0; i != count; ++i)
 
147
                {
 
148
                        if (i != 0)
 
149
                        {
 
150
                                [self append:@" &&\n\t"];
 
151
                                if (flipCondition)  [self append:@"  "];
 
152
                        }
 
153
                        
 
154
                        cond = [self convertOneCondition:[conditions objectAtIndex:i]];
 
155
                        if (cond == nil)
 
156
                        {
 
157
                                if (_validConversion)
 
158
                                {
 
159
                                        [self addBugIssueWithKey:@"unreported-error"
 
160
                                                                          format:@"An error occurred while converting a condition, but no appropriate message was generated."];
 
161
                                }
 
162
                                cond = @"<** invalid **>";
 
163
                        }
 
164
                        
 
165
                        if (count != 1)  cond = [NSString stringWithFormat:@"(%@)", cond];
 
166
                        
 
167
                        [self append:cond];
 
168
                }
 
169
                
 
170
                if (flipCondition)  [self append:@")"];
 
171
                [self append:@")\n{\n"];
 
172
        }
 
173
        
 
174
        [self indent];
 
175
        [self convertActions:ifTrue];
 
176
        [self outdent];
 
177
        [self append:@"}\n"];
 
178
        if (ifFalse != nil)
 
179
        {
 
180
                [self append:@"else\n{\n"];
 
181
                [self indent];
 
182
                [self convertActions:ifFalse];
 
183
                [self outdent];
 
184
                [self append:@"}\n"];
 
185
        }
 
186
}
 
187
 
 
188
 
 
189
- (void) convertOneAction:(NSString *)action
 
190
{
 
191
        NSMutableArray          *tokens = nil;
 
192
        NSString                        *selectorString = nil;
 
193
        unsigned                        tokenCount;
 
194
        BOOL                            takesParam;
 
195
        NSString                        *valueString = nil;
 
196
        SEL                                     selector = NULL;
 
197
        NSString                        *converted = nil;
 
198
        
 
199
        // Work around one of Eric's evil hacks
 
200
        if ([action hasPrefix:@"/*"] && [action hasSuffix:@"*/"])
 
201
        {
 
202
                [self append:[action stringByAppendingString:@"\n"]];
 
203
                return;
 
204
        }
 
205
        
 
206
        tokens = ScanTokensFromString(action);
 
207
        
 
208
        tokenCount = [tokens count];
 
209
        if (tokenCount < 1)
 
210
        {
 
211
                // This is a hard error in the interpreter, so it's a failure here.
 
212
                [self addStopIssueWithKey:@"no-tokens"
 
213
                                                   format:@"Invalid or empty script action \"%@\"", action];
 
214
        }
 
215
        
 
216
        selectorString = [tokens objectAtIndex:0];
 
217
        takesParam = [selectorString hasSuffix:@":"];
 
218
        
 
219
        if (takesParam && tokenCount > 1)
 
220
        {
 
221
                if (tokenCount == 2) valueString = [tokens objectAtIndex:1];
 
222
                else
 
223
                {
 
224
                        [tokens removeObjectAtIndex:0];
 
225
                        valueString = [tokens componentsJoinedByString:@" "];
 
226
                }
 
227
        }
 
228
        
 
229
        selector = NSSelectorFromString([@"convertAction_" stringByAppendingString:selectorString]);
 
230
        if ([self respondsToSelector:selector])
 
231
        {
 
232
                if (takesParam)
 
233
                {
 
234
                        converted = [self performSelector:selector withObject:valueString];
 
235
                }
 
236
                else
 
237
                {
 
238
                        converted = [self performSelector:selector];
 
239
                }
 
240
                
 
241
                if (converted == nil)
 
242
                {
 
243
                        if (_validConversion)
 
244
                        {
 
245
                                [self addBugIssueWithKey:@"unreported-error"
 
246
                                                                  format:@"An error occurred while converting an action, but no appropriate message was generated (selector: \"%@\").", selectorString];
 
247
                        }
 
248
                        converted = [NSString stringWithFormat:@"<** bad %@ **>", selectorString];
 
249
                }
 
250
                
 
251
#if COMMENT_SELECTOR
 
252
                converted = [NSString stringWithFormat:@"%@\t\t// %@", converted, selectorString];
 
253
#endif
 
254
        }
 
255
        else
 
256
        {
 
257
                converted = [NSString stringWithFormat:@"<** %@ **>\t\t// *** UNKNOWN ***", action];
 
258
                [self addUnknownSelectorIssueWithKey:@"unknown-selector"
 
259
                                                                          format:@"Could not convert unknown action selector \"%@\".", selectorString];
 
260
        }
 
261
        
 
262
        if ([converted length] > 0)  [self appendWithFormat:@"%@\n", converted];
 
263
}
 
264
 
 
265
 
 
266
- (NSString *) convertOneCondition:(NSString *)condition
 
267
{
 
268
        NSArray                         *tokens = nil;
 
269
        NSString                        *comparisonString = nil;
 
270
        OOComparisonType        comparator = kComparisonUndefined;
 
271
        unsigned                        tokenCount;
 
272
        OOJSExprNode            *lhs = nil;
 
273
        OOOperandType           lhsType = kTypeInvalid;
 
274
        NSString                        *rhsString = nil;
 
275
        OOJSExprNode            *rhs = nil;
 
276
        OOJSExprNode            *result = nil;
 
277
        
 
278
        if (![condition isKindOfClass:[NSString class]])
 
279
        {
 
280
                [self addStopIssueWithKey:@"invalid-condition"
 
281
                                                   format:@"Condition should be string, but found %@.", [condition class]];
 
282
                return [NSString stringWithFormat:@"<** Invalid object of class %@ **>", [condition class]];
 
283
        }
 
284
        
 
285
        tokens = ScanTokensFromString(condition);
 
286
        tokenCount = [tokens count];
 
287
        if (tokenCount == 0)
 
288
        {
 
289
                // This is a hard error in the interpreter, so it's a failure here.
 
290
                [self addStopIssueWithKey:@"no-tokens"
 
291
                                                   format:@"Invalid or empty script condition \"%@\"", condition];
 
292
                return @"<** invalid/empty condition **>";
 
293
        }
 
294
        
 
295
        lhs = [self convertQuery:[tokens objectAtIndex:0] gettingType:&lhsType];
 
296
        
 
297
        if (tokenCount > 1)
 
298
        {
 
299
                comparisonString = [tokens objectAtIndex:1];
 
300
                
 
301
                if ([comparisonString isEqualToString:@"equal"])  comparator = kComparisonEqual;
 
302
                else if ([comparisonString isEqualToString:@"notequal"])  comparator = kComparisonNotEqual;
 
303
                else if ([comparisonString isEqualToString:@"lessthan"])  comparator = kComparisonLessThan;
 
304
                else if ([comparisonString isEqualToString:@"greaterthan"])  comparator = kComparisonGreaterThan;
 
305
                else if ([comparisonString isEqualToString:@"morethan"])  comparator = kComparisonGreaterThan;
 
306
                else if ([comparisonString isEqualToString:@"oneof"])  comparator = kComparisonOneOf;
 
307
                else if ([comparisonString isEqualToString:@"undefined"])  comparator = kComparisonUndefined;
 
308
                else
 
309
                {
 
310
                        [self addStopIssueWithKey:@"invalid-comparator"
 
311
                                                           format:@"Unknown comparison operator \"%@\" in condition \"%@\".", comparisonString, condition];
 
312
                        return [NSString stringWithFormat:@"<** unknown comparison operator \"%@\" **>", comparisonString];
 
313
                }
 
314
        }
 
315
        
 
316
        if (tokenCount > 2)
 
317
        {
 
318
                rhsString = [[tokens subarrayWithRange:NSMakeRange(2, tokenCount - 2)] componentsJoinedByString:@" "];
 
319
                rhs = [self expandRightHandSide:rhsString];
 
320
        }
 
321
        
 
322
        if (lhsType == kTypeString)  result = [self convertStringCondition:comparator comparatorString:comparisonString lhs:lhs rhs:rhs rawCondition:condition];
 
323
        else if (lhsType == kTypeNumber)  result = [self convertNumberCondition:comparator comparatorString:comparisonString lhs:lhs rhs:rhs rawCondition:condition];
 
324
        else if (lhsType == kTypeBool)  result = [self convertBoolCondition:comparator comparatorString:comparisonString lhs:lhs rhs:rhs rawCondition:condition];
 
325
        
 
326
        if (result == nil)
 
327
        {
 
328
                [self addBugIssueWithKey:@"condition-conversion-failed"
 
329
                                                  format:@"Conversion of condition \"%@\" failed for unknown reasons.", condition];
 
330
                result = EX_ERROR(@"condition conversion failed");
 
331
        }
 
332
        return [result jsCodeRepresentation];
 
333
}
 
334
 
 
335
 
 
336
- (OOJSExprNode *) convertStringCondition:(OOComparisonType)comparator comparatorString:(NSString *)comparatorString lhs:(OOJSExprNode *)lhs rhs:(OOJSExprNode *)rhs rawCondition:(NSString *)rawCondition
 
337
{
 
338
        OOJSExprNode                    *parseFloatOrZero = nil;
 
339
        
 
340
        switch (comparator)
 
341
        {
 
342
                case kComparisonEqual:
 
343
                        return EX_EQUAL(lhs, rhs);
 
344
                        
 
345
                case kComparisonNotEqual:
 
346
                        return EX_NOT_EQUAL(lhs, rhs);
 
347
                        
 
348
                case kComparisonLessThan:
 
349
                        [self setParseFloatOrZeroHelper];
 
350
                        parseFloatOrZero = EX_THIS_PROP(@"parseFloatOrZero");
 
351
                        
 
352
                        if (![rhs isNumberLiteral])
 
353
                        {
 
354
                                rhs = EX_CALL(parseFloatOrZero, rhs);
 
355
                        }
 
356
                        // this.parseFloatOrZero(lhs) < rhs
 
357
                        return EX_LESS(EX_CALL(parseFloatOrZero, lhs), rhs);
 
358
                        
 
359
                case kComparisonGreaterThan:
 
360
                        [self setParseFloatOrZeroHelper];
 
361
                        parseFloatOrZero = EX_THIS_PROP(@"parseFloatOrZero");
 
362
                        
 
363
                        if (![rhs isNumberLiteral])
 
364
                        {
 
365
                                rhs = EX_CALL(parseFloatOrZero, rhs);
 
366
                        }
 
367
                        // this.parseFloatOrZero(lhs) > rhs
 
368
                        return EX_GRTR(EX_CALL(parseFloatOrZero, lhs), rhs);
 
369
                        
 
370
                case kComparisonOneOf:
 
371
                        [self setHelperFunction:
 
372
                                        @"function (string, list)\n{\n"
 
373
                                        "\tlet items = list.split(\",\");\n"
 
374
                                        "\treturn items.indexOf(string) != -1;\n}"
 
375
                                forKey:@"oneOf"];
 
376
                        // this.oneOf(lhs, rhs)
 
377
                        return EX_CALL(EX_THIS_PROP(@"oneOf"), lhs, rhs);
 
378
                        
 
379
                case kComparisonUndefined:
 
380
                        [self setHelperFunction:
 
381
                                        @"function (value)\n{\n"
 
382
                                        "\treturn value == undefined || value == null;\n}"
 
383
                                forKey:@"isUndefined"];
 
384
                        // this.isUndefined(lhs)
 
385
                        return EX_CALL(EX_THIS_PROP(@"isUndefined"), lhs);
 
386
        }
 
387
        
 
388
        return nil;
 
389
}
 
390
 
 
391
 
 
392
- (OOJSExprNode *) convertNumberCondition:(OOComparisonType)comparator comparatorString:(NSString *)comparatorString lhs:(OOJSExprNode *)lhs rhs:(OOJSExprNode *)rhs rawCondition:(NSString *)rawCondition
 
393
{
 
394
        switch (comparator)
 
395
        {
 
396
                case kComparisonEqual:
 
397
                        return EX_EQUAL(lhs, rhs);
 
398
                        
 
399
                case kComparisonNotEqual:
 
400
                        return EX_NOT_EQUAL(lhs, rhs);
 
401
                        
 
402
                case kComparisonLessThan:
 
403
                        return EX_LESS(lhs, rhs);
 
404
                        
 
405
                case kComparisonGreaterThan:
 
406
                        return EX_GRTR(lhs, rhs);
 
407
                        
 
408
                case kComparisonOneOf:
 
409
                        [self setParseFloatOrZeroHelper];
 
410
                        [self setHelperFunction:
 
411
                                         @"function (number, list)\n{\n"
 
412
                                         "\tlet items = list.split(\",\");\n"
 
413
                                         "\tfor (let i = 0; i < items.length; ++i)  if (number == parseFloatOrZero(list[i]))  return true;\n"
 
414
                                         "\treturn false;\n}"
 
415
                                 forKey:@"oneOfNumber"];
 
416
                        return EX_CALL(EX_THIS_PROP(@"oneOfNumber"), lhs, rhs);
 
417
                        
 
418
                case kComparisonUndefined:
 
419
                        [self addBugIssueWithKey:@"invalid-comparator"
 
420
                                                          format:@"Operator %@ is not valid for number expressions (condition: %@).", comparatorString, rawCondition];
 
421
                        return EX_ERROR_FMT(@"invalid operator %@", comparatorString);
 
422
        }
 
423
        
 
424
        return nil;
 
425
}
 
426
 
 
427
 
 
428
- (OOJSExprNode *) convertBoolCondition:(OOComparisonType)comparator comparatorString:(NSString *)comparatorString lhs:(OOJSExprNode *)lhs rhs:(OOJSExprNode *)rhs rawCondition:(NSString *)rawCondition
 
429
{
 
430
        BOOL                                    convertedRHS = NO;
 
431
        
 
432
        if ([rhs isStringLiteral])
 
433
        {
 
434
                NSString *rhsString = [(OOJSStringLiteralExprNode *)rhs stringValue];
 
435
                if ([rhsString isEqual:@"YES"])
 
436
                {
 
437
                        rhs = EX_TRUE;
 
438
                        convertedRHS = YES;
 
439
                }
 
440
                else if ([rhsString isEqual:@"NO"])
 
441
                {
 
442
                        rhs = EX_FALSE;
 
443
                        convertedRHS = YES;
 
444
                }
 
445
        }
 
446
        if (!convertedRHS)
 
447
        {
 
448
                [self setHelperFunction:
 
449
                 @"function (flag)\n{\n"
 
450
                 "\t// Convert booleans to YES/NO for comparisons.\n"
 
451
                 "\treturn flag ? \"YES\" : \"NO\";\n}"
 
452
                                                 forKey:@"boolToString"];
 
453
                
 
454
                lhs = EX_CALL(EX_THIS_PROP(@"boolToString"), lhs);
 
455
        }
 
456
        
 
457
        switch (comparator)
 
458
        {
 
459
                case kComparisonEqual:
 
460
                        if ([rhs isBooleanLiteral])
 
461
                        {
 
462
                                if ([(OOJSBoolLiteralExprNode *)rhs boolValue])  return lhs;
 
463
                                else  return EX_NOT(lhs);
 
464
                        }
 
465
                        else
 
466
                        {
 
467
                                return EX_EQUAL(lhs, rhs);
 
468
                        }
 
469
                        
 
470
                case kComparisonNotEqual:
 
471
                        if ([rhs isBooleanLiteral])
 
472
                        {
 
473
                                if ([(OOJSBoolLiteralExprNode *)rhs boolValue])  return EX_NOT(lhs);
 
474
                                else  return lhs;
 
475
                        }
 
476
                        else
 
477
                        {
 
478
                                return EX_NOT_EQUAL(lhs, rhs);
 
479
                        }
 
480
                        
 
481
                case kComparisonLessThan:
 
482
                case kComparisonGreaterThan:
 
483
                case kComparisonOneOf:
 
484
                case kComparisonUndefined:
 
485
                        [self addBugIssueWithKey:@"invalid-comparator"
 
486
                                                          format:@"Operator %@ is not valid for boolean expressions (condition: %@).", comparatorString, rawCondition];
 
487
                        return EX_ERROR_FMT(@"invalid operator %@", comparatorString);
 
488
        }
 
489
        
 
490
        return nil;
 
491
}
 
492
 
 
493
 
 
494
- (OOJSExprNode *) convertQuery:(NSString *)query gettingType:(OOOperandType *)outType
 
495
{
 
496
        SEL                                     selector;
 
497
        OOJSExprNode            *converted = nil;
 
498
        
 
499
        assert(outType != NULL);
 
500
        
 
501
        if ([query hasPrefix:@"mission_"] || [query hasPrefix:@"local_"])
 
502
        {
 
503
                // Variables in legacy engine are always considered strings.
 
504
                *outType = kTypeString;
 
505
                return [self convertVariableAccess:query];
 
506
        }
 
507
        
 
508
        if ([query hasSuffix:@"_string"])  *outType = kTypeString;
 
509
        else if ([query hasSuffix:@"_number"])  *outType = kTypeNumber;
 
510
        else if ([query hasSuffix:@"_bool"])  *outType = kTypeBool;
 
511
        else  *outType = kTypeInvalid;
 
512
        
 
513
        selector = NSSelectorFromString([@"convertQuery_" stringByAppendingString:query]);
 
514
        if ([self respondsToSelector:selector])
 
515
        {
 
516
                converted = [self performSelector:selector];
 
517
                if (converted == nil)
 
518
                {
 
519
                        if (_validConversion)
 
520
                        {
 
521
                                [self addBugIssueWithKey:@"unreported-error"
 
522
                                                                  format:@"An error occurred while converting a condition, but no appropriate message was generated (selector: \"%@\").", query];
 
523
                        }
 
524
                        converted = EX_ERROR(@"unknown error");
 
525
                }
 
526
        }
 
527
        else    
 
528
        {
 
529
                [self addUnknownSelectorIssueWithKey:@"unknown-selector"
 
530
                                                                          format:@"Could not convert unknown conditional selector \"%@\".", query];
 
531
                converted = EX_ERROR(query);
 
532
        }
 
533
        
 
534
        return converted;
 
535
}
 
536
 
 
537
 
 
538
#if OBSOLETE
 
539
- (NSString *) stringifyBooleanExpression:(NSString *)expr
 
540
{
 
541
        [self setHelperFunction:
 
542
                        @"function (flag)\n{\n"
 
543
                        "\t// Convert booleans to YES/NO for comparisons.\n"
 
544
                        "\treturn flag ? \"YES\" : \"NO\";\n}"
 
545
                forKey:@"boolToString"];
 
546
        return [NSString stringWithFormat:@"this.boolToString(%@)", expr];
 
547
}
 
548
#endif
 
549
 
 
550
 
 
551
/*** Action handlers ***/
 
552
 
 
553
- (NSString *) convertAction_set:(NSString *)params
 
554
{
 
555
        NSMutableArray          *tokens = nil;
 
556
        NSString                        *missionVariableString = nil;
 
557
        NSString                        *valueString = nil;
 
558
        
 
559
        tokens = ScanTokensFromString(params);
 
560
        
 
561
        if ([tokens count] < 2)
 
562
        {
 
563
                [self addStopIssueWithKey:@"set-syntax-error"
 
564
                                                   format:@"Bad syntax for set: -- expected mission_variable or local_variable followed by value expression, got \"%@\".", params];
 
565
                return nil;
 
566
        }
 
567
        
 
568
        missionVariableString = [tokens objectAtIndex:0];
 
569
        [tokens removeObjectAtIndex:0];
 
570
        valueString = [tokens componentsJoinedByString:@" "];
 
571
        
 
572
        if ([missionVariableString hasPrefix:@"mission_"] || [missionVariableString hasPrefix:@"local_"])
 
573
        {
 
574
                return [NSString stringWithFormat:@"%@ = %@;", [self legalizedVariableName:missionVariableString], [self expandStringOrNumber:valueString]];
 
575
        }
 
576
        else
 
577
        {
 
578
                [self addStopIssueWithKey:@"set-syntax-error"
 
579
                                                   format:@"Bad syntax for set: -- expected mission_variable or local_variable, got \"%@\".", missionVariableString];
 
580
                return nil;
 
581
        }
 
582
}
 
583
 
 
584
 
 
585
- (NSString *) convertAction_reset:(NSString *)variable
 
586
{
 
587
        NSString *legalized = [self legalizedVariableName:variable];
 
588
        if (legalized == nil)  return nil;
 
589
        
 
590
        return [NSString stringWithFormat:@"%@ = null;", legalized];
 
591
}
 
592
 
 
593
 
 
594
- (NSString *) convertAction_add:(NSString *)params
 
595
{
 
596
        NSMutableArray          *tokens = nil;
 
597
        NSString                        *missionVariableString = nil;
 
598
        NSString                        *valueString = nil;
 
599
        
 
600
        tokens = ScanTokensFromString(params);
 
601
        
 
602
        if ([tokens count] < 2)
 
603
        {
 
604
                [self addStopIssueWithKey:@"add-syntax-error"
 
605
                                                   format:@"Bad syntax for add: -- expected mission_variable or local_variable followed by value expression, got \"%@\".", params];
 
606
                return nil;
 
607
        }
 
608
        
 
609
        missionVariableString = [tokens objectAtIndex:0];
 
610
        [tokens removeObjectAtIndex:0];
 
611
        valueString = [tokens componentsJoinedByString:@" "];
 
612
        
 
613
        if ([missionVariableString hasPrefix:@"mission_"] || [missionVariableString hasPrefix:@"local_"])
 
614
        {
 
615
                [self setParseFloatOrZeroHelper];
 
616
                missionVariableString = [self legalizedVariableName:missionVariableString];
 
617
                return [NSString stringWithFormat:@"%@ = this.parseFloatOrZero(%@) + %@;", missionVariableString, missionVariableString, [self expandFloatExpression:valueString]];
 
618
        }
 
619
        else
 
620
        {
 
621
                [self addStopIssueWithKey:@"add-syntax-error"
 
622
                                                   format:@"Bad syntax for add: -- expected mission_variable or local_variable, got \"%@\".", missionVariableString];
 
623
                return nil;
 
624
        }
 
625
}
 
626
 
 
627
 
 
628
- (NSString *) convertAction_subtract:(NSString *)params
 
629
{
 
630
        NSMutableArray          *tokens = nil;
 
631
        NSString                        *missionVariableString = nil;
 
632
        NSString                        *valueString = nil;
 
633
        
 
634
        tokens = ScanTokensFromString(params);
 
635
        
 
636
        if ([tokens count] < 2)
 
637
        {
 
638
                [self addStopIssueWithKey:@"subtract-syntax-error"
 
639
                                                   format:@"Bad syntax for subtract: -- expected mission_variable or local_variable followed by value expression, got \"%@\".", params];
 
640
                return nil;
 
641
        }
 
642
        
 
643
        missionVariableString = [tokens objectAtIndex:0];
 
644
        [tokens removeObjectAtIndex:0];
 
645
        valueString = [tokens componentsJoinedByString:@" "];
 
646
        
 
647
        if ([missionVariableString hasPrefix:@"mission_"] || [missionVariableString hasPrefix:@"local_"])
 
648
        {
 
649
                [self setParseFloatOrZeroHelper];
 
650
                missionVariableString = [self legalizedVariableName:missionVariableString];
 
651
                return [NSString stringWithFormat:@"%@ = this.parseFloatOrZero(%@) - %@;", missionVariableString, missionVariableString, [self expandFloatExpression:valueString]];
 
652
        }
 
653
        else
 
654
        {
 
655
                [self addStopIssueWithKey:@"subtract-syntax-error"
 
656
                                                   format:@"Bad syntax for subtract: -- expected mission_variable or local_variable, got \"%@\".", missionVariableString];
 
657
                return nil;
 
658
        }
 
659
}
 
660
 
 
661
 
 
662
- (NSString *) convertAction_increment:(NSString *)string
 
663
{
 
664
        [self setParseIntOrZeroHelper];
 
665
        NSString *varStr = [self legalizedVariableName:string];
 
666
        return [NSString stringWithFormat:@"%@ = this.parseIntOrZero(%@) + 1;", varStr, varStr];
 
667
}
 
668
 
 
669
 
 
670
- (NSString *) convertAction_decrement:(NSString *)string
 
671
{
 
672
        [self setParseIntOrZeroHelper];
 
673
        NSString *varStr = [self legalizedVariableName:string];
 
674
        return [NSString stringWithFormat:@"%@ = this.parseIntOrZero(%@) - 1;", varStr, varStr];
 
675
}
 
676
 
 
677
 
 
678
- (NSString *) convertAction_commsMessage:(NSString *)string
 
679
{
 
680
        return [NSString stringWithFormat:@"player.commsMessage(%@);", [self expandString:string]];
 
681
}
 
682
 
 
683
 
 
684
- (NSString *) convertAction_setMissionImage:(NSString *)string
 
685
{
 
686
        return [NSString stringWithFormat:@"mission.setBackgroundImage(%@);", [self expandString:string]];
 
687
}
 
688
 
 
689
 
 
690
- (NSString *) convertAction_showShipModel:(NSString *)string
 
691
{
 
692
        return [NSString stringWithFormat:@"mission.showShipModel(%@);", [self expandString:string]];
 
693
}
 
694
 
 
695
 
 
696
- (NSString *) convertAction_checkForShips:(NSString *)string
 
697
{
 
698
        [self setInitializer:@"this.shipsFound = 0;" forKey:@"shipsFound"];
 
699
        return [NSString stringWithFormat:@"this.shipsFound = system.countShipsWithPrimaryRole(%@);", [self expandString:string]];
 
700
}
 
701
 
 
702
 
 
703
- (NSString *) convertAction_awardCredits:(NSString *)string
 
704
{
 
705
        return [NSString stringWithFormat:@"player.credits += %@;", [self expandIntegerExpression:string]];
 
706
}
 
707
 
 
708
 
 
709
- (NSString *) convertAction_awardShipKills:(NSString *)string
 
710
{
 
711
        return [NSString stringWithFormat:@"player.score += %@;", [self expandIntegerExpression:string]];
 
712
}
 
713
 
 
714
 
 
715
- (NSString *) convertAction_awardFuel:(NSString *)string
 
716
{
 
717
        return [NSString stringWithFormat:@"player.ship.fuel += %@;", [self expandFloatExpression:string]];
 
718
}
 
719
 
 
720
 
 
721
- (NSString *) convertAction_addFuel:(NSString *)string
 
722
{
 
723
        return [NSString stringWithFormat:@"player.ship.fuel += %@;", [self expandFloatExpression:string]];
 
724
}
 
725
 
 
726
 
 
727
- (NSString *) convertAction_setLegalStatus:(NSString *)string
 
728
{
 
729
        return [NSString stringWithFormat:@"player.bounty = %@;", [self expandIntegerExpression:string]];
 
730
}
 
731
 
 
732
 
 
733
- (NSString *) convertAction_addMissionText:(NSString *)string
 
734
{
 
735
        return [NSString stringWithFormat:@"mission.addMessageTextKey(%@);", [self expandString:string]];
 
736
}
 
737
 
 
738
 
 
739
- (NSString *) convertAction_setMissionChoices:(NSString *)string
 
740
{
 
741
        return [NSString stringWithFormat:@"mission.setChoicesKey(%@);", [self expandString:string]];
 
742
}
 
743
 
 
744
 
 
745
- (NSString *) convertAction_useSpecialCargo:(NSString *)string
 
746
{
 
747
        return [NSString stringWithFormat:@"mission.useSpecialCargo(%@);", [self expandString:string]];
 
748
}
 
749
 
 
750
 
 
751
- (NSString *) convertAction_consoleMessage3s:(NSString *)string
 
752
{
 
753
        return [NSString stringWithFormat:@"player.consoleMessage(%@);", [self expandString:string]];
 
754
}
 
755
 
 
756
 
 
757
- (NSString *) convertAction_consoleMessage6s:(NSString *)string
 
758
{
 
759
        return [NSString stringWithFormat:@"player.consoleMessage(%@, 6.0);", [self expandString:string]];
 
760
}
 
761
 
 
762
 
 
763
- (NSString *) convertAction_testForEquipment:(NSString *)string
 
764
{
 
765
        [self setInitializer:@"this.foundEqipment = false;" forKey:@"foundEqipment"];
 
766
        return [NSString stringWithFormat:@"this.foundEqipment = player.ship.hasEquipment(%@);", [self expandString:string]];
 
767
}
 
768
 
 
769
 
 
770
- (NSString *) convertAction_awardEquipment:(NSString *)string
 
771
{
 
772
        return [NSString stringWithFormat:@"player.ship.awardEquipment(%@);", [self expandString:string]];
 
773
}
 
774
 
 
775
 
 
776
- (NSString *) convertAction_removeEquipment:(NSString *)string
 
777
{
 
778
        return [NSString stringWithFormat:@"player.ship.removeEquipment(%@);", [self expandString:string]];
 
779
}
 
780
 
 
781
 
 
782
- (NSString *) convertAction_setFuelLeak:(NSString *)string
 
783
{
 
784
        return [NSString stringWithFormat:@"player.ship.fuelLeakRate = %@;", [self expandFloatExpression:string]];
 
785
}
 
786
 
 
787
 
 
788
- (NSString *) convertAction_setSunNovaIn:(NSString *)string
 
789
{
 
790
        return [NSString stringWithFormat:@"system.sun.goNova(%@);", [self expandFloatExpression:string]];
 
791
}
 
792
 
 
793
 
 
794
- (NSString *) convertAction_setMissionDescription:(NSString *)string
 
795
{
 
796
        return [NSString stringWithFormat:@"mission.setInstructionsKey(%@);", [self expandString:string]];
 
797
}
 
798
 
 
799
 
 
800
- (NSString *) convertAction_clearMissionDescription
 
801
{
 
802
        return [NSString stringWithFormat:@"mission.setInstructionsKey(null);"];
 
803
}
 
804
 
 
805
 
 
806
- (NSString *) convertAction_setMissionMusic:(NSString *)string
 
807
{
 
808
        return [NSString stringWithFormat:@"mission.setMusic(%@);", [self expandString:string]];
 
809
}
 
810
 
 
811
 
 
812
- (NSString *) convertAction_addMissionDestination:(NSString *)string
 
813
{
 
814
        /*      expandStringOrNumber: is used because more than one destination can be
 
815
                specified, as a space-separated list. mission.markSystem() supports
 
816
                this format, as well as comma-separated lists.
 
817
        */
 
818
        return [NSString stringWithFormat:@"mission.markSystem(%@);", [self expandStringOrNumber:string]];
 
819
}
 
820
 
 
821
 
 
822
- (NSString *) convertAction_removeMissionDestination:(NSString *)string
 
823
{
 
824
        /*      expandStringOrNumber: is used because more than one destination can be
 
825
                specified, as a space-separated list. mission.unmarkSystem() supports
 
826
                this format, as well as comma-separated lists.
 
827
        */
 
828
        return [NSString stringWithFormat:@"mission.unmarkSystem(%@);", [self expandStringOrNumber:string]];
 
829
}
 
830
 
 
831
 
 
832
- (NSString *) convertAction_addShips:(NSString *)params
 
833
{
 
834
        NSMutableArray          *tokens = nil;
 
835
        NSString                        *roleString = nil;
 
836
        NSString                        *countString = nil;
 
837
        
 
838
        tokens = ScanTokensFromString(params);
 
839
        if ([tokens count] != 2)
 
840
        {
 
841
                [self addStopIssueWithKey:@"addShips-syntax-error"
 
842
                                                   format:@"Bad syntax for addShips: -- expected role followed by count, got \"%@\".", params];
 
843
                return nil;
 
844
        }
 
845
        
 
846
        roleString = [tokens objectAtIndex:0];
 
847
        countString = [tokens objectAtIndex:1];
 
848
        
 
849
        return [NSString stringWithFormat:@"system.legacy_addShips(%@, %@);",
 
850
                        [self expandString:roleString],
 
851
                        [self expandIntegerExpression:countString]];
 
852
}
 
853
 
 
854
 
 
855
- (NSString *) convertAction_addSystemShips:(NSString *)params
 
856
{
 
857
        NSMutableArray          *tokens = nil;
 
858
        NSString                        *roleString = nil;
 
859
        NSString                        *countString = nil;
 
860
        NSString                        *positionString = nil;
 
861
        
 
862
        tokens = ScanTokensFromString(params);
 
863
        if ([tokens count] != 3)
 
864
        {
 
865
                [self addStopIssueWithKey:@"addSystemShips-syntax-error"
 
866
                                                   format:@"Bad syntax for addSystemShips: -- expected <role> <count> <position>, got \"%@\".", params];
 
867
                return nil;
 
868
        }
 
869
        
 
870
        roleString = [tokens objectAtIndex:0];
 
871
        countString = [tokens objectAtIndex:1];
 
872
        positionString = [tokens objectAtIndex:2];
 
873
        
 
874
        return [NSString stringWithFormat:@"system.legacy_addSystemShips(%@, %@, %@);",
 
875
                        [self expandString:roleString],
 
876
                        [self expandIntegerExpression:countString],
 
877
                        [self expandFloatExpression:positionString]];
 
878
}
 
879
 
 
880
 
 
881
- (NSString *) convertAction_addShipsAt:(NSString *)params
 
882
{
 
883
        NSMutableArray          *tokens = nil;
 
884
        NSString                        *roleString = nil;
 
885
        NSString                        *countString = nil;
 
886
        NSString                        *systemString = nil;
 
887
        NSString                        *xString = nil;
 
888
        NSString                        *yString = nil;
 
889
        NSString                        *zString = nil;
 
890
        
 
891
        tokens = ScanTokensFromString(params);
 
892
        if ([tokens count] != 6)
 
893
        {
 
894
                [self addStopIssueWithKey:@"addShipsAt-syntax-error"
 
895
                                                   format:@"Bad syntax for addShipsAt: -- expected <role> <count> <coordinate-system> <x> <y> <z>, got \"%@\".", params];
 
896
                return nil;
 
897
        }
 
898
        
 
899
        roleString = [tokens objectAtIndex:0];
 
900
        countString = [tokens objectAtIndex:1];
 
901
        systemString = [tokens objectAtIndex:2];
 
902
        xString = [tokens objectAtIndex:3];
 
903
        yString = [tokens objectAtIndex:4];
 
904
        zString = [tokens objectAtIndex:5];
 
905
        
 
906
        return [NSString stringWithFormat:@"system.legacy_addShipsAt(%@, %@, %@, [%@, %@, %@]);",
 
907
                        [self expandString:roleString],
 
908
                        [self expandIntegerExpression:countString],
 
909
                        [self expandString:systemString],
 
910
                        [self expandFloatExpression:xString],
 
911
                        [self expandFloatExpression:yString],
 
912
                        [self expandFloatExpression:zString]];
 
913
}
 
914
 
 
915
 
 
916
- (NSString *) convertAction_addShipsAtPrecisely:(NSString *)params
 
917
{
 
918
        NSMutableArray          *tokens = nil;
 
919
        NSString                        *roleString = nil;
 
920
        NSString                        *countString = nil;
 
921
        NSString                        *systemString = nil;
 
922
        NSString                        *xString = nil;
 
923
        NSString                        *yString = nil;
 
924
        NSString                        *zString = nil;
 
925
        
 
926
        tokens = ScanTokensFromString(params);
 
927
        if ([tokens count] != 6)
 
928
        {
 
929
                [self addStopIssueWithKey:@"addShipsAtPrecisely-syntax-error"
 
930
                                                   format:@"Bad syntax for addShipsAtPrecisely: -- expected <role> <count> <coordinate-system> <x> <y> <z>, got \"%@\".", params];
 
931
                return nil;
 
932
        }
 
933
        
 
934
        roleString = [tokens objectAtIndex:0];
 
935
        countString = [tokens objectAtIndex:1];
 
936
        systemString = [tokens objectAtIndex:2];
 
937
        xString = [tokens objectAtIndex:3];
 
938
        yString = [tokens objectAtIndex:4];
 
939
        zString = [tokens objectAtIndex:5];
 
940
        
 
941
        return [NSString stringWithFormat:@"system.legacy_addShipsAtPrecisely(%@, %@, %@, [%@, %@, %@]);",
 
942
                        [self expandString:roleString],
 
943
                        [self expandIntegerExpression:countString],
 
944
                        [self expandString:systemString],
 
945
                        [self expandFloatExpression:xString],
 
946
                        [self expandFloatExpression:yString],
 
947
                        [self expandFloatExpression:zString]];
 
948
}
 
949
 
 
950
 
 
951
- (NSString *) convertAction_addShipsWithinRadius:(NSString *)params
 
952
{
 
953
        NSMutableArray          *tokens = nil;
 
954
        NSString                        *roleString = nil;
 
955
        NSString                        *countString = nil;
 
956
        NSString                        *systemString = nil;
 
957
        NSString                        *xString = nil;
 
958
        NSString                        *yString = nil;
 
959
        NSString                        *zString = nil;
 
960
        NSString                        *radiusString = nil;
 
961
        
 
962
        tokens = ScanTokensFromString(params);
 
963
        if ([tokens count] != 7)
 
964
        {
 
965
                [self addStopIssueWithKey:@"addShipsWithinRadius-syntax-error"
 
966
                                                   format:@"Bad syntax for addShipsWithinRadius: -- expected <role> <count> <coordinate-system> <x> <y> <z> <radius>, got \"%@\".", params];
 
967
                return nil;
 
968
        }
 
969
        
 
970
        roleString = [tokens objectAtIndex:0];
 
971
        countString = [tokens objectAtIndex:1];
 
972
        systemString = [tokens objectAtIndex:2];
 
973
        xString = [tokens objectAtIndex:3];
 
974
        yString = [tokens objectAtIndex:4];
 
975
        zString = [tokens objectAtIndex:5];
 
976
        radiusString = [tokens objectAtIndex:6];
 
977
        
 
978
        return [NSString stringWithFormat:@"system.legacy_addShipsWithinRadius(%@, %@, %@, [%@, %@, %@], %@);",
 
979
                        [self expandString:roleString],
 
980
                        [self expandIntegerExpression:countString],
 
981
                        [self expandString:systemString],
 
982
                        [self expandFloatExpression:xString],
 
983
                        [self expandFloatExpression:yString],
 
984
                        [self expandFloatExpression:zString],
 
985
                        [self expandFloatExpression:radiusString]];
 
986
}
 
987
 
 
988
 
 
989
- (NSString *) convertAction_awardCargo:(NSString *)params
 
990
{
 
991
        NSMutableArray          *tokens = nil;
 
992
        NSString                        *quantityString = nil;
 
993
        NSString                        *typeString = nil;
 
994
        
 
995
        tokens = ScanTokensFromString(params);
 
996
        if ([tokens count] != 2)
 
997
        {
 
998
                [self addStopIssueWithKey:@"awardCargo-syntax-error"
 
999
                                                   format:@"Bad syntax for awardCargo: -- expected count followed by type, got \"%@\".", params];
 
1000
                return nil;
 
1001
        }
 
1002
        
 
1003
        quantityString = [tokens objectAtIndex:0];
 
1004
        typeString = [tokens objectAtIndex:1];
 
1005
        
 
1006
        if ([quantityString isEqualToString:@"1"])
 
1007
        {
 
1008
                return [NSString stringWithFormat:@"player.ship.awardCargo(%@);", [self expandString:typeString]];
 
1009
        }
 
1010
        else
 
1011
        {
 
1012
                return [NSString stringWithFormat:@"player.ship.awardCargo(%@, %@);", [self expandString:typeString], [self expandIntegerExpression:quantityString]];
 
1013
        }
 
1014
}
 
1015
 
 
1016
 
 
1017
- (NSString *) convertAction_setPlanetinfo:(NSString *)params
 
1018
{
 
1019
        NSArray                         *tokens = nil;
 
1020
        NSString                        *keyString = nil;
 
1021
        NSString                        *valueString = nil;
 
1022
        
 
1023
        tokens = [params componentsSeparatedByString:@"="];
 
1024
        if ([tokens count] != 2)
 
1025
        {
 
1026
                [self addStopIssueWithKey:@"setPlanetinfo-syntax-error"
 
1027
                                                   format:@"Bad syntax for setPlanetinfo: -- expected key=value, got \"%@\".", params];
 
1028
                return nil;
 
1029
        }
 
1030
        
 
1031
        keyString = [[tokens objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 
1032
        valueString = [[tokens objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 
1033
        
 
1034
        return [NSString stringWithFormat:@"system.info%@ = %@;", [self expandPropertyReference:keyString], [self expandString:valueString]];
 
1035
}
 
1036
 
 
1037
 
 
1038
- (NSString *) convertAction_setSpecificPlanetInfo:(NSString *)params
 
1039
{
 
1040
        NSArray                         *tokens = nil;
 
1041
        NSString                        *galaxyString = nil;
 
1042
        NSString                        *systemString = nil;
 
1043
        NSString                        *keyString = nil;
 
1044
        NSString                        *valueString = nil;
 
1045
        
 
1046
        tokens = [params componentsSeparatedByString:@"="];
 
1047
        if ([tokens count] != 4)
 
1048
        {
 
1049
                [self addStopIssueWithKey:@"setPlanetinfo-syntax-error"
 
1050
                                                   format:@"Bad syntax for setSpecificPlanetInfo: -- expected galaxy=system=key=value, got \"%@\".", params];
 
1051
                return nil;
 
1052
        }
 
1053
        
 
1054
        galaxyString = [[tokens objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 
1055
        systemString = [[tokens objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 
1056
        keyString = [[tokens objectAtIndex:2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 
1057
        valueString = [[tokens objectAtIndex:3] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 
1058
        
 
1059
        return [NSString stringWithFormat:@"System.infoForSystem(%@, %@)%@ = %@;", [self expandIntegerExpression:galaxyString], [self expandIntegerExpression:systemString], [self expandPropertyReference:keyString], [self expandString:valueString]];
 
1060
}
 
1061
 
 
1062
 
 
1063
- (NSString *) convertAction_sendAllShipsAway
 
1064
{
 
1065
        return @"system.sendAllShipsAway();";
 
1066
}
 
1067
 
 
1068
 
 
1069
- (NSString *) convertAction_launchFromStation
 
1070
{
 
1071
        return @"player.ship.launch();";
 
1072
}
 
1073
 
 
1074
 
 
1075
- (NSString *) convertAction_blowUpStation
 
1076
{
 
1077
        return @"system.mainStation.explode();";
 
1078
}
 
1079
 
 
1080
 
 
1081
- (NSString *) convertAction_removeAllCargo
 
1082
{
 
1083
        return @"player.ship.removeAllCargo();";
 
1084
}
 
1085
 
 
1086
 
 
1087
- (NSString *) convertAction_clearMissionScreen
 
1088
{
 
1089
        return @"mission.clearMissionScreen();";
 
1090
}
 
1091
 
 
1092
 
 
1093
- (NSString *) convertAction_setGuiToMissionScreen
 
1094
{
 
1095
        return @"mission.showMissionScreen();";
 
1096
}
 
1097
 
 
1098
 
 
1099
- (NSString *) convertAction_resetMissionChoice
 
1100
{
 
1101
        return @"mission.choice = null;";
 
1102
}
 
1103
 
 
1104
 
 
1105
- (NSString *) convertAction_setGuiToStatusScreen
 
1106
{
 
1107
        // FIXME: is this OK in general?
 
1108
        return @"";
 
1109
}
 
1110
 
 
1111
 
 
1112
- (NSString *) convertAction_debugOn
 
1113
{
 
1114
        return @"if (debugConsole)  debugConsole.setDisplayMessagesInClass(\"$scriptDebugOn\", true);";
 
1115
}
 
1116
 
 
1117
 
 
1118
- (NSString *) convertAction_debugOff
 
1119
{
 
1120
        return @"if (debugConsole)  debugConsole.setDisplayMessagesInClass(\"$scriptDebugOn\", false);";
 
1121
}
 
1122
 
 
1123
 
 
1124
/*** Query handlers ***/
 
1125
 
 
1126
- (OOJSExprNode *) convertQuery_dockedAtMainStation_bool
 
1127
{
 
1128
        /*      player.ship.docked && player.ship.dockedStation == system.mainStation
 
1129
                (player.ship.docked is required, because player.ship.dockedStation ==
 
1130
                system.mainStation is true in interstellar space.)
 
1131
        */
 
1132
        return EX_AND(EX_PROP(EX_PROP(@"player", @"ship"), @"docked"), EX_EQUAL(EX_PROP(EX_PROP(@"player", @"ship"), @"dockedStation"), EX_PROP(@"system", @"mainStation")));
 
1133
}
 
1134
 
 
1135
 
 
1136
- (OOJSExprNode *) convertQuery_galaxy_number
 
1137
{
 
1138
        return EX_ID(@"galaxyNumber");
 
1139
}
 
1140
 
 
1141
 
 
1142
- (OOJSExprNode *) convertQuery_planet_number
 
1143
{
 
1144
        return EX_PROP(@"system", @"ID");
 
1145
}
 
1146
 
 
1147
 
 
1148
- (OOJSExprNode *) convertQuery_score_number
 
1149
{
 
1150
        return EX_PROP(@"player", @"score");
 
1151
}
 
1152
 
 
1153
 
 
1154
- (OOJSExprNode *) convertQuery_d100_number
 
1155
{
 
1156
        // Math.floor(Math.random() * 256)
 
1157
        return EX_CALL(EX_PROP(@"Math", @"floor"), EX_MULTIPLY(EX_VOID_CALL(EX_PROP(@"Math", @"random")), EX_INT(100)));
 
1158
}
 
1159
 
 
1160
 
 
1161
- (OOJSExprNode *) convertQuery_d256_number
 
1162
{
 
1163
        // Math.floor(Math.random() * 256)
 
1164
        return EX_CALL(EX_PROP(@"Math", @"floor"), EX_MULTIPLY(EX_VOID_CALL(EX_PROP(@"Math", @"random")), EX_INT(256)));
 
1165
}
 
1166
 
 
1167
 
 
1168
- (OOJSExprNode *) convertQuery_sunWillGoNova_bool
 
1169
{
 
1170
        return EX_PROP(EX_PROP(@"system", @"sun"), @"isGoingNova");
 
1171
}
 
1172
 
 
1173
 
 
1174
- (OOJSExprNode *) convertQuery_sunGoneNova_bool
 
1175
{
 
1176
        return EX_PROP(EX_PROP(@"system", @"sun"), @"hasGoneNova");
 
1177
}
 
1178
 
 
1179
 
 
1180
- (OOJSExprNode *) convertQuery_status_string
 
1181
{
 
1182
        return EX_PROP(EX_PROP(@"player", @"ship"), @"status");
 
1183
}
 
1184
 
 
1185
 
 
1186
- (OOJSExprNode *) convertQuery_shipsFound_number
 
1187
{
 
1188
        [self setInitializer:@"this.shipsFound = 0;" forKey:@"shipsFound"];
 
1189
        return EX_THIS_PROP(@"shipsFound");
 
1190
}
 
1191
 
 
1192
 
 
1193
- (OOJSExprNode *) convertQuery_foundEquipment_bool
 
1194
{
 
1195
        [self setInitializer:@"this.foundEqipment = false;" forKey:@"foundEqipment"];
 
1196
        return EX_THIS_PROP(@"foundEqipment");
 
1197
}
 
1198
 
 
1199
 
 
1200
- (OOJSExprNode *) convertQuery_missionChoice_string
 
1201
{
 
1202
        return EX_PROP(@"mission", @"choice");
 
1203
}
 
1204
 
 
1205
 
 
1206
- (OOJSExprNode *) convertQuery_scriptTimer_number
 
1207
{
 
1208
        return EX_PROP(@"clock", @"legacy_scriptTimer");
 
1209
}
 
1210
 
 
1211
 
 
1212
- (OOJSExprNode *) convertQuery_gui_screen_string
 
1213
{
 
1214
        return EX_ID(@"guiScreen");
 
1215
}
 
1216
 
 
1217
 
 
1218
- (OOJSExprNode *) convertQuery_credits_number
 
1219
{
 
1220
        return EX_PROP(@"player", @"credits");
 
1221
}
 
1222
 
 
1223
 
 
1224
- (OOJSExprNode *) convertQuery_dockedStationName_string
 
1225
{
 
1226
        /*
 
1227
                this.dockedStationName = function ()
 
1228
                {
 
1229
                        if (player.ship.docked)
 
1230
                        {
 
1231
                                var result = player.ship.dockedStation.name;
 
1232
                                if (!result)  result = "UNKNOWN";
 
1233
                                        
 
1234
                        }
 
1235
                        else
 
1236
                        {
 
1237
                                var result = "NONE";
 
1238
                        }
 
1239
                        return result;
 
1240
                }
 
1241
        */
 
1242
        [self setHelperFunction:
 
1243
                        @"function ()\n{\n"
 
1244
                        "\tif (player.ship.docked)\n\t{\n"
 
1245
                        "\t\tvar result = player.ship.dockedStation.name;\n"
 
1246
                        "\t\tif (!result)  result = \"UNKNOWN\";\n"
 
1247
                        "\t}\n\telse\n\t{\n"
 
1248
                        "\t\tvar result = \"NONE\";\n\t}\n"
 
1249
                        "\treturn result;\n}"
 
1250
                forKey:@"dockedStationName"];
 
1251
        
 
1252
        return EX_VOID_CALL(EX_THIS_PROP(@"dockedStationName"));
 
1253
}
 
1254
 
 
1255
 
 
1256
- (OOJSExprNode *) convertQuery_systemGovernment_string
 
1257
{
 
1258
        return EX_PROP(@"system", @"governmentDescription");
 
1259
}
 
1260
 
 
1261
 
 
1262
- (OOJSExprNode *) convertQuery_systemGovernment_number
 
1263
{
 
1264
        return EX_PROP(@"system", @"government");
 
1265
}
 
1266
 
 
1267
 
 
1268
- (OOJSExprNode *) convertQuery_systemEconomy_string
 
1269
{
 
1270
        return EX_PROP(@"system", @"economyDescription");
 
1271
}
 
1272
 
 
1273
 
 
1274
- (OOJSExprNode *) convertQuery_systemEconomy_number
 
1275
{
 
1276
        return EX_PROP(@"system", @"economy");
 
1277
}
 
1278
 
 
1279
 
 
1280
- (OOJSExprNode *) convertQuery_systemTechLevel_number
 
1281
{
 
1282
        return EX_PROP(@"system", @"techLevel");
 
1283
}
 
1284
 
 
1285
 
 
1286
- (OOJSExprNode *) convertQuery_systemPopulation_number
 
1287
{
 
1288
        return EX_PROP(@"system", @"population");
 
1289
}
 
1290
 
 
1291
 
 
1292
- (OOJSExprNode *) convertQuery_systemProductivity_number
 
1293
{
 
1294
        return EX_PROP(@"system", @"productivity");
 
1295
}
 
1296
 
 
1297
 
 
1298
- (OOJSExprNode *) convertQuery_commanderName_string
 
1299
{
 
1300
        return EX_PROP(@"player", @"name");
 
1301
}
 
1302
 
 
1303
 
 
1304
- (OOJSExprNode *) convertQuery_commanderRank_string
 
1305
{
 
1306
        return EX_PROP(@"player", @"rank");
 
1307
}
 
1308
 
 
1309
 
 
1310
- (OOJSExprNode *) convertQuery_commanderShip_string
 
1311
{
 
1312
        return EX_PROP(EX_PROP(@"player", @"ship"), @"name");
 
1313
}
 
1314
 
 
1315
 
 
1316
- (OOJSExprNode *) convertQuery_commanderShipDisplayName_string
 
1317
{
 
1318
        return EX_PROP(EX_PROP(@"player", @"ship"), @"displayName");
 
1319
}
 
1320
 
 
1321
 
 
1322
 
 
1323
- (OOJSExprNode *) convertQuery_commanderLegalStatus_string
 
1324
{
 
1325
        return EX_PROP(@"player", @"legalStatus");
 
1326
}
 
1327
 
 
1328
 
 
1329
- (OOJSExprNode *) convertQuery_commanderLegalStatus_number
 
1330
{
 
1331
        return EX_PROP(@"player", @"bounty");
 
1332
}
 
1333
 
 
1334
 
 
1335
- (OOJSExprNode *) convertQuery_legalStatus_number
 
1336
{
 
1337
        return EX_PROP(@"player", @"bounty");
 
1338
}
 
1339
 
 
1340
 
 
1341
- (OOJSExprNode *) convertQuery_pseudoFixedD100_number
 
1342
{
 
1343
        return EX_PROP(@"system", @"psuedoRandom100");
 
1344
}
 
1345
 
 
1346
 
 
1347
- (OOJSExprNode *) convertQuery_pseudoFixedD256_number
 
1348
{
 
1349
        return EX_PROP(@"system", @"psuedoRandom256");
 
1350
}
 
1351
 
 
1352
@end
 
1353
 
 
1354
 
 
1355
static NSMutableArray *ScanTokensFromString(NSString *values)
 
1356
{
 
1357
        NSMutableArray                  *result = nil;
 
1358
        NSScanner                               *scanner = nil;
 
1359
        NSString                                *token = nil;
 
1360
        static NSCharacterSet   *space_set = nil;
 
1361
        
 
1362
        if (values == nil)  return [NSArray array];
 
1363
        if (space_set == nil) space_set = [[NSCharacterSet whitespaceAndNewlineCharacterSet] retain];
 
1364
        
 
1365
        result = [NSMutableArray array];
 
1366
        scanner = [NSScanner scannerWithString:values];
 
1367
        
 
1368
        while (![scanner isAtEnd])
 
1369
        {
 
1370
                [scanner ooliteScanCharactersFromSet:space_set intoString:NULL];
 
1371
                if ([scanner ooliteScanUpToCharactersFromSet:space_set intoString:&token])
 
1372
                {
 
1373
                        [result addObject:token];
 
1374
                }
 
1375
        }
 
1376
        
 
1377
        return result;
 
1378
}