Implemented Evaluation
This commit is contained in:
@@ -14,46 +14,109 @@
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_summands = [[NSMutableArray alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (double)standaloneValue
|
||||
- (NSDecimalNumber *)valueAtBeginning
|
||||
{
|
||||
NSDecimalNumber *value = self.value;
|
||||
if (self.prefixValueExplicit) {
|
||||
value = [value decimalNumberByAdding:self.prefixMultiplicator];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)valueAtEnd
|
||||
{
|
||||
NSDecimalNumber *value = self.value;
|
||||
if (self.suffixValueExplicit) {
|
||||
value = [value decimalNumberByAdding:self.suffixMultiplicator];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)standaloneValue
|
||||
{
|
||||
if (self.isFactor) {
|
||||
return self.factor;
|
||||
return self.value;
|
||||
}
|
||||
return self.prefixMultiplicator + [[self.summands valueForKeyPath:@"@sum.self"] doubleValue];
|
||||
NSDecimalNumber *value = self.value;
|
||||
if (self.prefixValueExplicit) {
|
||||
value = [value decimalNumberByAdding:self.prefixMultiplicator];
|
||||
}
|
||||
if (self.suffixValueExplicit) {
|
||||
value = [value decimalNumberByAdding:self.suffixMultiplicator];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
- (BOOL)isValidElementAtBeginning
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL)isValidElementInBetween
|
||||
- (BOOL)isValidElementAtBeginning:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
if (self.prefixOperatorExplicit) {
|
||||
if (error) {
|
||||
*error = MPParseError(0, @"Expected Number");
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)isValidElementAtEnd
|
||||
- (BOOL)isValidStandaloneElement:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
return NO;
|
||||
return [self isValidElementAtBeginning:error] && [self isValidElementAtEnd:error];
|
||||
}
|
||||
|
||||
#pragma mark NSCopying
|
||||
- (BOOL)isValidElementAtEnd:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
if (self.suffixOperatorExplicit) {
|
||||
if (error) {
|
||||
*error = MPParseError(self.suffixIndex, @"Expected Number");
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)isValidVariableDefinition:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
if (self.definedVariable == nil) {
|
||||
if (error) {
|
||||
*error = MPParseError(0, @"Expected Variable Definition");
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
return [self isValidElementAtBeginning:error];
|
||||
}
|
||||
|
||||
#pragma mark - NSObject Overrides
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
NSMutableString *description = [[NSMutableString alloc] initWithString:@"MPParsedElement<"];
|
||||
if (self.isFactor) {
|
||||
[description appendFormat:@"factor=%@%@%@", self.prefixOperatorExplicit?@"*":@"", self.value, self.suffixOperatorExplicit?@"*":@""];
|
||||
} else {
|
||||
[description appendFormat:@"prefix=%@%@ prefixValueExplicit=%@", self.prefixOperatorExplicit?@"*":@"", self.prefixMultiplicator, self.prefixValueExplicit?@"YES":@"NO"];
|
||||
[description appendFormat:@" suffix=%@%@ suffixValueExplicit=%@", self.suffixMultiplicator, self.suffixOperatorExplicit?@"*":@"", self.suffixValueExplicit?@"YES":@"NO"];
|
||||
[description appendFormat:@" value=%@", self.value];
|
||||
}
|
||||
[description appendString:@">"];
|
||||
return [description copy];
|
||||
}
|
||||
|
||||
#pragma mark - NSCopying
|
||||
- (id)copyWithZone:(NSZone *)zone
|
||||
{
|
||||
MPParsedElement *copy = [[MPParsedElement allocWithZone:zone] init];
|
||||
copy.isFactor = self.isFactor;
|
||||
copy.factor = self.factor;
|
||||
copy.summands = self.summands.mutableCopy;
|
||||
copy.prefixMultiplicator = self.prefixMultiplicator;
|
||||
copy.hasPrefixMultiplicator = self.hasPrefixMultiplicator;
|
||||
copy.value = self.value;
|
||||
copy.prefixOperatorExplicit = self.prefixOperatorExplicit;
|
||||
copy.prefixValueExplicit = self.prefixValueExplicit;
|
||||
copy.prefixMultiplicator = self.prefixMultiplicator;
|
||||
copy.suffixOperatorExplicit = self.suffixOperatorExplicit;
|
||||
copy.suffixValueExplicit = self.suffixValueExplicit;
|
||||
copy.suffixMultiplicator = self.suffixMultiplicator;
|
||||
copy.hasSuffixMultiplicator = self.hasSuffixMultiplicator;
|
||||
return copy;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user