Archived
1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mathpad/MathPad/MPParsedElement.m
2014-09-07 16:45:31 +02:00

124 lines
3.4 KiB
Objective-C

//
// MPParsedElement.m
// MathPad
//
// Created by Kim Wittenburg on 05.09.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import "MPParsedElement.h"
@implementation MPParsedElement
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
- (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.value;
}
NSDecimalNumber *value = self.value;
if (self.prefixValueExplicit) {
value = [value decimalNumberByAdding:self.prefixMultiplicator];
}
if (self.suffixValueExplicit) {
value = [value decimalNumberByAdding:self.suffixMultiplicator];
}
return value;
}
- (BOOL)isValidElementAtBeginning:(MPParseError *__autoreleasing *)error
{
if (self.prefixOperatorExplicit) {
if (error) {
*error = MPParseError(0, @"Expected Number");
}
return NO;
}
return YES;
}
- (BOOL)isValidStandaloneElement:(MPParseError *__autoreleasing *)error
{
return [self isValidElementAtBeginning:error] && [self isValidElementAtEnd:error];
}
- (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.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;
return copy;
}
@end