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/MPParsedVariable.m
2014-09-13 23:16:44 +02:00

48 lines
1.0 KiB
Objective-C

//
// MPParsedVariable.m
// MathPad
//
// Created by Kim Wittenburg on 13.09.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import "MPParsedVariable.h"
#import "MPEvaluationContext.h"
@interface MPParsedVariable ()
@property (nonatomic) NSRange range;
@property (nonatomic, strong) NSString *variableName;
@end
@implementation MPParsedVariable
- (instancetype)initWithRange:(NSRange)range
inString:(NSString *)string
{
self = [super init];
if (self) {
_range = range;
if (range.location != NSNotFound) {
_variableName = [string substringWithRange:range];
}
}
return self;
}
- (BOOL)exists
{
return self.range.location != NSNotFound;
}
- (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error
{
NSDecimalNumber *value = [[MPEvaluationContext sharedContext] valueForVariableName:self.variableName];
if (!value) {
*error = MPParseError(self.range, @"Undefined Variable.");
return nil;
}
return value;
}
@end