forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSInterpreterResult.m
104 lines (82 loc) · 4.01 KB
/
FSInterpreterResult.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* FSInterpreterResult.m Copyright (c) 1998-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "FSInterpreterResult.h"
#import "FSInterpreterResultPrivate.h"
#import "FSMiscTools.h"
NSString *FSInterpreterResultIllegalCallException = @"FSInterpreterResultIllegalCallException";
@implementation FSInterpreterResult
-(void)dealloc
{
//NSLog(@"FSInterpreterResult dealloc");
[errorMessage release];
[result release];
[callStack release];
[super dealloc];
}
-(NSArray *)callStack
{
if (status == FS_OK) [NSException raise:FSInterpreterResultIllegalCallException format:@"illegal call of method \"callStack\" on an FSInterpreterResult object which is not denoting an error"];
return callStack;
}
-(NSRange)errorRange
{
if (status == FS_OK) [NSException raise:FSInterpreterResultIllegalCallException format:@"illegal call of method \"errorRange\" on an FSInterpreterResult object which is not denoting an error"];
return errorRange;
}
-(NSString *)errorMessage
{
if (status == FS_OK) [NSException raise:FSInterpreterResultIllegalCallException format:@"illegal call of method \"errorMessage\" on an FSInterpreterResult object which is not denoting an error"];
return errorMessage;
}
-(BOOL)isOk {return [self isOK];}
-(BOOL)isOK {return status == FS_OK;}
-(BOOL)isExecutionError {return status == FS_EXECUTION_ERROR;}
-(BOOL)isSyntaxError {return status == FS_SYNTAX_ERROR;}
-(id)result
{
if (status != FS_OK) [NSException raise:FSInterpreterResultIllegalCallException format:@"illegal call of method \"result\" on an FSInterpreterResult object denoting an error"];
return result;
}
-(void)inspectBlocksInCallStack
{
if (status == FS_OK) [NSException raise:FSInterpreterResultIllegalCallException format:@"illegal call of method \"inspectBlocksInCallStack\" on an FSInterpreterResult object which is not denoting an error"];
if (callStack == nil) return;
inspectBlocksInCallStack(callStack);
}
@end
@implementation FSInterpreterResult (FSInterpreterResultPrivate)
// This method may return an object initialized with other values than the ones provided. This happens when an error occurs while trying to retain the "theResult" parameter. In this case, this method returns an object representing an F-Script execution error.
+ (FSInterpreterResult *)interpreterResultWithStatus:(enum e_status)theStatus result:(id)theResult errorRange:(NSRange)theErrorRange errorMessage:(NSString *)theErrorMessage callStack:(NSArray *)theCallStack
{
return [[[FSInterpreterResult alloc] initWithStatus:theStatus result:theResult errorRange:theErrorRange errorMessage:theErrorMessage callStack:theCallStack] autorelease];
}
// This method may return an object initialized with other values than the ones provided. This happens when an error occurs while trying to retain the "theResult" parameter. In this case, this method returns an object representing an F-Script execution error.
- (FSInterpreterResult *)initWithStatus:(enum e_status)theStatus result:(id)theResult errorRange:(NSRange)theErrorRange errorMessage:(NSString *)theErrorMessage callStack:(NSArray *)theCallStack
{
if ((self = [super init]))
{
BOOL error = NO;
@try
{
result = [theResult retain]; // May throw an exception (example: NSAutoreleasePool throws an exception when sent retain)
}
@catch (id exception)
{
error = YES;
status = FS_EXECUTION_ERROR;
errorRange = NSMakeRange(0,0);
errorMessage = [[@"The following exception was raised when F-Script tried to retain an object: " stringByAppendingString:FSErrorMessageFromException(exception)] retain];
callStack = nil; // We might be able to do better than that (i.e., providing more info related to the callStack).
}
if (!error)
{
status = theStatus;
errorRange = theErrorRange;
errorMessage = [theErrorMessage retain];
callStack = [theCallStack retain];
}
return self;
}
return nil;
}
@end