Archived
1

Cleaned Code

This commit is contained in:
Kim Wittenburg
2014-10-21 15:06:57 +02:00
parent 5592128926
commit 91e7dbe9f2
20 changed files with 126 additions and 92 deletions

View File

@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6245" systemVersion="13F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES"> <document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6245" systemVersion="14A389" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies> <dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6245"/> <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6245"/>
</dependencies> </dependencies>
<objects> <objects>
<customObject id="-2" userLabel="File's Owner" customClass="MPDocument"> <customObject id="-2" userLabel="File's Owner" customClass="MPDocument">
<connections> <connections>
<outlet property="expressionView" destination="lcd-Ip-jjR" id="Vww-eh-hP7"/> <outlet property="expressionView" destination="bhV-kp-c0I" id="VkG-iX-blr"/>
<outlet property="resultLabel" destination="Cdb-3b-4iC" id="7O2-I6-gqP"/> <outlet property="resultLabel" destination="4B1-d4-7wd" id="kgz-yK-Ud3"/>
<outlet property="window" destination="xOd-HO-29H" id="JIz-fz-R2o"/> <outlet property="window" destination="xOd-HO-29H" id="JIz-fz-R2o"/>
</connections> </connections>
</customObject> </customObject>
@@ -16,17 +16,18 @@
<window title="MathPad" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="xOd-HO-29H" userLabel="Window"> <window title="MathPad" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="xOd-HO-29H" userLabel="Window">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/> <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<rect key="contentRect" x="525" y="411" width="432" height="181"/> <rect key="contentRect" x="525" y="411" width="432" height="181"/>
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1178"/> <rect key="screenRect" x="0.0" y="0.0" width="1920" height="1177"/>
<value key="minSize" type="size" width="432" height="181"/>
<view key="contentView" id="gIp-Ho-8D9"> <view key="contentView" id="gIp-Ho-8D9">
<rect key="frame" x="0.0" y="0.0" width="432" height="181"/> <rect key="frame" x="0.0" y="0.0" width="432" height="181"/>
<autoresizingMask key="autoresizingMask"/> <autoresizingMask key="autoresizingMask"/>
<subviews> <subviews>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="lcd-Ip-jjR" customClass="MPExpressionView"> <customView translatesAutoresizingMaskIntoConstraints="NO" id="bhV-kp-c0I" customClass="MPExpressionView">
<rect key="frame" x="0.0" y="0.0" width="432" height="181"/> <rect key="frame" x="0.0" y="0.0" width="432" height="181"/>
<subviews> <subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Cdb-3b-4iC"> <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="4B1-d4-7wd">
<rect key="frame" x="410" y="20" width="4" height="17"/> <rect key="frame" x="410" y="20" width="4" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" id="I9X-Yv-EiR"> <textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" id="53e-7L-BTd">
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/> <color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/> <color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
@@ -34,22 +35,22 @@
</textField> </textField>
</subviews> </subviews>
<constraints> <constraints>
<constraint firstAttribute="bottom" secondItem="Cdb-3b-4iC" secondAttribute="bottom" constant="20" id="3T9-HB-OrZ"/> <constraint firstAttribute="bottom" secondItem="4B1-d4-7wd" secondAttribute="bottom" constant="20" symbolic="YES" id="Gnf-6p-lx6"/>
<constraint firstAttribute="trailing" secondItem="Cdb-3b-4iC" secondAttribute="trailing" constant="20" id="toz-uq-kuq"/> <constraint firstAttribute="trailing" secondItem="4B1-d4-7wd" secondAttribute="trailing" constant="20" symbolic="YES" id="paI-1L-J2e"/>
</constraints> </constraints>
</customView> </customView>
</subviews> </subviews>
<constraints> <constraints>
<constraint firstItem="lcd-Ip-jjR" firstAttribute="leading" secondItem="gIp-Ho-8D9" secondAttribute="leading" id="5G8-rw-mds"/> <constraint firstAttribute="bottom" secondItem="bhV-kp-c0I" secondAttribute="bottom" id="VRz-hN-lFY"/>
<constraint firstAttribute="bottom" secondItem="lcd-Ip-jjR" secondAttribute="bottom" id="JAt-GL-atf"/> <constraint firstItem="bhV-kp-c0I" firstAttribute="leading" secondItem="gIp-Ho-8D9" secondAttribute="leading" id="WbH-hd-lvs"/>
<constraint firstAttribute="trailing" secondItem="lcd-Ip-jjR" secondAttribute="trailing" id="Wi2-6P-2Li"/> <constraint firstAttribute="trailing" secondItem="bhV-kp-c0I" secondAttribute="trailing" id="nYd-dS-gsv"/>
<constraint firstItem="lcd-Ip-jjR" firstAttribute="top" secondItem="gIp-Ho-8D9" secondAttribute="top" id="hvP-0r-1OZ"/> <constraint firstItem="bhV-kp-c0I" firstAttribute="top" secondItem="gIp-Ho-8D9" secondAttribute="top" id="png-CY-BRy"/>
</constraints> </constraints>
</view> </view>
<connections> <connections>
<outlet property="delegate" destination="-2" id="0bl-1N-x8E"/> <outlet property="delegate" destination="-2" id="0bl-1N-x8E"/>
</connections> </connections>
<point key="canvasLocation" x="102" y="112.5"/> <point key="canvasLocation" x="-147" y="119.5"/>
</window> </window>
<collectionViewItem id="J9S-PW-LCL"> <collectionViewItem id="J9S-PW-LCL">
<connections> <connections>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 854 KiB

After

Width:  |  Height:  |  Size: 74 KiB

View File

@@ -308,10 +308,7 @@
break; break;
case MPTokenReferenceFrame: case MPTokenReferenceFrame:
[_elements enumerateObjectsUsingBlock:^(id<MPToken> obj, NSUInteger idx, BOOL *stop) { symbolIndex = [self.tokens[index] range].location;
symbolIndex = NSMaxRange(obj.range);
*stop = idx >= index - 1;
}];
break; break;
} }
@@ -345,14 +342,13 @@
case MPTokenReferenceFrame: case MPTokenReferenceFrame:
{ {
NSUInteger totalLength = 0;
NSUInteger tokenIndex = 0; NSUInteger tokenIndex = 0;
id<MPToken> token; while (true) {
while (totalLength < symbolIndex) { id<MPToken> token = self.tokens[tokenIndex++];
token = self.tokens[tokenIndex++]; if (NSMaxRange(token.range) < symbolIndex || token.range.location > symbolIndex) {
totalLength = NSMaxRange(token.range); continue;
} }
NSUInteger offsetInToken = token.range.length - totalLength + symbolIndex; NSUInteger offsetInToken = index - token.range.location;
if (offsetInToken == token.range.length) { if (offsetInToken == token.range.length) {
offsetInToken = 0; offsetInToken = 0;
tokenIndex++; tokenIndex++;
@@ -362,6 +358,9 @@
} }
return tokenIndex; return tokenIndex;
} }
// Should never get here
return 0;
}
} }
} }
@@ -525,15 +524,10 @@
- (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error - (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error
{ {
MPExpressionTree *tree = [self parse]; MPExpressionTree *tree = [self parse];
if ([tree validate:error]) { return [tree validate:error] ? [tree evaluate] : nil;
return [tree evaluate];
}
if (error) {
(*error).pathToExpression = self.indexPath;
}
return nil;
} }
- (MPExpressionTree *)parse - (MPExpressionTree *)parse
{ {
MPTokenStream *tokenStream = [[MPTokenStream alloc] initWithTokens:self.tokens]; MPTokenStream *tokenStream = [[MPTokenStream alloc] initWithTokens:self.tokens];

View File

@@ -12,6 +12,7 @@
#import "MPFunctionLayout.h" #import "MPFunctionLayout.h"
#import "MPPowerFunction.h" #import "MPPowerFunction.h"
#import "MPFractionFunction.h"
#import "MPRangePath.h" #import "MPRangePath.h"
@@ -91,11 +92,9 @@
- (void)restartCaretTimer - (void)restartCaretTimer
{ {
if (self.caretTimer) { if (self.caretTimer && [self.caretTimer isValid]) {
if ([self.caretTimer isValid]) {
[self.caretTimer invalidate]; [self.caretTimer invalidate];
} }
}
self.caretTimer = [NSTimer scheduledTimerWithTimeInterval:self.caretBlinkRate/2 target:self selector:@selector(updateCaret:) userInfo:nil repeats:YES]; self.caretTimer = [NSTimer scheduledTimerWithTimeInterval:self.caretBlinkRate/2 target:self selector:@selector(updateCaret:) userInfo:nil repeats:YES];
self.caretVisible = NO; self.caretVisible = NO;
[self updateCaret:self.caretTimer]; [self updateCaret:self.caretTimer];
@@ -104,7 +103,11 @@
- (void)updateCaret:(NSTimer *)timer - (void)updateCaret:(NSTimer *)timer
{ {
self.caretVisible = !self.caretVisible; self.caretVisible = !self.caretVisible;
self.needsDisplay = YES; NSRect updatedRect = self.selectionRect;
NSPoint expressionOrigin = self.expressionOrigin;
updatedRect.origin.x += expressionOrigin.x;
updatedRect.origin.y += expressionOrigin.y;
[self setNeedsDisplayInRect:updatedRect];
} }
- (NSRect)selectionRect - (NSRect)selectionRect
@@ -576,6 +579,10 @@
[self insertPowerFunction]; [self insertPowerFunction];
return; return;
} }
if ([characters isEqualToString:@"/"]) {
[self insertFractionFunction];
return;
}
NSString *decimalSeparator = [NSRegularExpression escapedPatternForString:[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator]]; NSString *decimalSeparator = [NSRegularExpression escapedPatternForString:[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator]];
NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet]; NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
@@ -630,6 +637,23 @@
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:0] indexPathByAddingIndex:0], self.selection.length); self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:0] indexPathByAddingIndex:0], self.selection.length);
} }
- (void)insertFractionFunction
{
MPExpression *selectedElementsExpression = [self.expressionStorage subexpressionWithRangePath:self.selection
referenceFrame:MPSymbolReferenceFrame];
MPFractionFunction *function = [[MPFractionFunction alloc] init];
function.nominatorExpression = selectedElementsExpression;
[self.expressionStorage replaceItemsInRangePath:self.selection
referenceFrame:MPSymbolReferenceFrame
withElements:@[function]];
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:[self.selection.location indexPathByRemovingLastIndex]];
NSUInteger functionElementIndex = [targetExpression convertIndex:self.selection.location.lastIndex
fromReferenceFrame:MPSymbolReferenceFrame
toReferenceFrame:MPElementReferenceFrame];
NSIndexPath *functionPath = [self.selection.location indexPathByReplacingLastIndexWithIndex:functionElementIndex];
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:1] indexPathByAddingIndex:0], 0);
}
- (void)insertNewline:(id)sender - (void)insertNewline:(id)sender
{ {
if (self.target && self.action) { if (self.target && self.action) {
@@ -900,22 +924,22 @@
[transform concat]; [transform concat];
// Draw the error // Draw the error
if (self.error) { // if (self.error) {
[[NSColor redColor] set]; // [[NSColor redColor] set];
NSRect rect = [self.expressionStorage.rootLayout boundingRectForRangePath:self.error.rangePath]; // NSRect rect = [self.expressionStorage.rootLayout boundingRectForRangePath:self.error.rangePath];
if (self.error.rangePath.length == 0) { // if (self.error.rangePath.length == 0) {
NSBezierPath *bezierPath = [NSBezierPath bezierPath]; // NSBezierPath *bezierPath = [NSBezierPath bezierPath];
[bezierPath moveToPoint:rect.origin]; // [bezierPath moveToPoint:rect.origin];
[bezierPath lineToPoint:NSMakePoint(rect.origin.x - 5, rect.origin.y - 5)]; // [bezierPath lineToPoint:NSMakePoint(rect.origin.x - 5, rect.origin.y - 5)];
[bezierPath moveToPoint:rect.origin]; // [bezierPath moveToPoint:rect.origin];
[bezierPath lineToPoint:NSMakePoint(rect.origin.x + 5, rect.origin.y - 5)]; // [bezierPath lineToPoint:NSMakePoint(rect.origin.x + 5, rect.origin.y - 5)];
bezierPath.lineWidth = 2.0; // bezierPath.lineWidth = 2.0;
[bezierPath stroke]; // [bezierPath stroke];
} else { // } else {
NSRect underlineRect = NSMakeRect(rect.origin.x, rect.origin.y + 2, rect.size.width, 2); // NSRect underlineRect = NSMakeRect(rect.origin.x, rect.origin.y + 2, rect.size.width, 2);
NSRectFill(underlineRect); // NSRectFill(underlineRect);
} // }
} // }
// Draw the selection // Draw the selection
if (self.caretVisible || self.selection.length > 0) { if (self.caretVisible || self.selection.length > 0) {

View File

@@ -9,12 +9,12 @@
#import "MPFractionFunctionLayout.h" #import "MPFractionFunctionLayout.h"
#import "MPExpressionLayout.h" #import "MPExpressionLayout.h"
#define kFractionFunctionLineWidth 2 #define kFractionFunctionLineWidth 1.0
#define kFractionFunctionHorizontalInset 2 #define kFractionFunctionHorizontalInset 2.0
#define kFractionFunctionNominatorOffset 0 #define kFractionFunctionNominatorOffset 0
#define kFractionFunctionDenominatorOffset 0 #define kFractionFunctionDenominatorOffset 0
#define MPFractionMiddle (CTFontGetAscent((CTFontRef)self.font) / 2) #define MPFractionMiddle (CTFontGetCapHeight((CTFontRef)self.font) / 2)
@implementation MPFractionFunctionLayout @implementation MPFractionFunctionLayout
@@ -41,13 +41,14 @@
- (NSPoint)offsetOfChildLayoutAtIndex:(NSUInteger)index - (NSPoint)offsetOfChildLayoutAtIndex:(NSUInteger)index
{ {
if (index == 0) { if (index == 0) {
CGFloat y = MPFractionMiddle + kFractionFunctionLineWidth / 2; NSRect nominatorBounds = [self childLayoutAtIndex:0].bounds;
return NSMakePoint(0, y); CGFloat y = MPFractionMiddle + kFractionFunctionLineWidth / 2 - nominatorBounds.origin.y;
return NSMakePoint(kFractionFunctionHorizontalInset, y);
} else { } else {
NSRect denominatorBounds = [self childLayoutAtIndex:1].bounds; NSRect denominatorBounds = [self childLayoutAtIndex:1].bounds;
CGFloat y = CTFontGetAscent((CTFontRef)self.font) / 2 - kFractionFunctionLineWidth / 2; CGFloat y = MPFractionMiddle - kFractionFunctionLineWidth / 2;
y -= denominatorBounds.size.height - denominatorBounds.origin.y; y -= denominatorBounds.size.height + denominatorBounds.origin.y;
return NSMakePoint(0, y); return NSMakePoint(kFractionFunctionHorizontalInset, y);
} }
} }
@@ -71,16 +72,15 @@
NSRect denominatorBounds = [self childLayoutAtIndex:1].bounds; NSRect denominatorBounds = [self childLayoutAtIndex:1].bounds;
NSRect bounds; NSRect bounds;
bounds.origin.x = 0; bounds.origin.x = 0;
CGFloat y = CTFontGetAscent((CTFontRef)self.font) / 2 - kFractionFunctionLineWidth / 2; bounds.origin.y = MPFractionMiddle - kFractionFunctionLineWidth / 2 - denominatorBounds.size.height;
y -= denominatorBounds.size.height; bounds.size.width = MAX(nominatorBounds.size.width, denominatorBounds.size.width) + 2 * kFractionFunctionHorizontalInset;
bounds.size.width = MAX(nominatorBounds.size.width, denominatorBounds.size.width);
bounds.size.height = nominatorBounds.size.height + denominatorBounds.size.height + kFractionFunctionLineWidth; bounds.size.height = nominatorBounds.size.height + denominatorBounds.size.height + kFractionFunctionLineWidth;
return bounds; return bounds;
} }
- (void)draw - (void)draw
{ {
CGFloat y = CTFontGetAscent((CTFontRef)self.font) / 2 - kFractionFunctionLineWidth / 2; CGFloat y = MPFractionMiddle - kFractionFunctionLineWidth / 2;
NSRectFill(NSMakeRect(0, y, self.bounds.size.width, kFractionFunctionLineWidth)); NSRectFill(NSMakeRect(0, y, self.bounds.size.width, kFractionFunctionLineWidth));
} }

View File

@@ -9,8 +9,6 @@
@import Cocoa; @import Cocoa;
#import "MPExpressionStorage.h" #import "MPExpressionStorage.h"
#define MPNull [NSNull null]
@interface MPLayout : NSObject @interface MPLayout : NSObject
#pragma mark Creation Methods #pragma mark Creation Methods
@@ -42,6 +40,7 @@
#pragma mark Calculation and Drawing Methods #pragma mark Calculation and Drawing Methods
- (CTLineRef)createLineForString:(NSString *)aString; - (CTLineRef)createLineForString:(NSString *)aString;
- (CTLineRef)createLineForString:(NSString *)aString emphasize:(BOOL)emphasize;
- (CTLineRef)createLineForString:(NSString *)aString usingFont:(NSFont *)font; - (CTLineRef)createLineForString:(NSString *)aString usingFont:(NSFont *)font;
@property (nonatomic, getter = isFlipped) BOOL flipped; @property (nonatomic, getter = isFlipped) BOOL flipped;

View File

@@ -84,7 +84,7 @@
if (index >= _cache.count) { if (index >= _cache.count) {
return NO; return NO;
} }
return _cache[index] != MPNull; return _cache[index] != [NSNull null];
} }
- (id)cachableObjectForIndex:(NSUInteger)index - (id)cachableObjectForIndex:(NSUInteger)index
@@ -102,7 +102,7 @@
- (void)ensureCacheSizeForIndex:(NSUInteger)index - (void)ensureCacheSizeForIndex:(NSUInteger)index
{ {
while (index >= _cache.count) { while (index >= _cache.count) {
[_cache addObject:MPNull]; [_cache addObject:[NSNull null]];
} }
} }
@@ -112,7 +112,7 @@
{ {
NSMutableArray *placeholders = [[NSMutableArray alloc] initWithCapacity:replacementLength]; NSMutableArray *placeholders = [[NSMutableArray alloc] initWithCapacity:replacementLength];
while (placeholders.count < replacementLength) { while (placeholders.count < replacementLength) {
[placeholders addObject:MPNull]; [placeholders addObject:[NSNull null]];
} }
[_cache replaceObjectsInRange:range [_cache replaceObjectsInRange:range
withObjectsFromArray:placeholders]; withObjectsFromArray:placeholders];
@@ -141,6 +141,12 @@
usingFont:self.font]; usingFont:self.font];
} }
- (CTLineRef)createLineForString:(NSString *)aString emphasize:(BOOL)emphasize
{
return [self createLineForString:aString
usingFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]];
}
- (CTLineRef)createLineForString:(NSString *)aString - (CTLineRef)createLineForString:(NSString *)aString
usingFont:(NSFont *)font usingFont:(NSFont *)font
{ {

View File

@@ -101,17 +101,23 @@
} }
- (NSAffineTransform *)parenthesisTransform - (NSAffineTransform *)parenthesisTransform
{
NSAffineTransform *transform = [NSAffineTransform transform];
[transform scaleXBy:1
yBy:self.scaleFactor];
return transform;
}
- (CGFloat)scaleFactor
{ {
NSRect parensBounds = self.openingParens.bounds; NSRect parensBounds = self.openingParens.bounds;
NSRect expressionBounds = [self childLayoutAtIndex:0].bounds; NSRect expressionBounds = [self childLayoutAtIndex:0].bounds;
NSAffineTransform *transform = [NSAffineTransform transform]; CGFloat parensHeight = parensBounds.size.height;
if (expressionBounds.size.height >= parensBounds.size.height) { CGFloat expressionHeight = expressionBounds.size.height;
return transform; if (expressionHeight <= parensHeight) {
return 1.0;
} }
CGFloat scaleFactor = expressionBounds.size.height / parensBounds.size.height; return expressionHeight / parensHeight;
[transform scaleXBy:1
yBy:scaleFactor];
return transform;
} }
- (NSPoint)offsetOfChildLayoutAtIndex:(NSUInteger)index - (NSPoint)offsetOfChildLayoutAtIndex:(NSUInteger)index
@@ -154,24 +160,18 @@
return bounds; return bounds;
} }
#warning Let the children be drawn automatically
- (BOOL)drawsChildrenManually
{
return YES;
}
- (void)draw - (void)draw
{ {
NSBezierPath *openingParens = self.transformedOpeningParens; NSBezierPath *openingParens = self.transformedOpeningParens;
MPLayout *expressionLayout = [self childLayoutAtIndex:0]; MPLayout *expressionLayout = [self childLayoutAtIndex:0];
NSBezierPath *closingParens = self.transformedClosingParens; NSBezierPath *closingParens = self.transformedClosingParens;
CGFloat x = 0; CGFloat x = 0;
[openingParens fill]; [openingParens fill];
x += openingParens.bounds.size.width; x += openingParens.bounds.size.width;
x += MPParenthesisFunctionOpeningParensOffset; x += MPParenthesisFunctionOpeningParensOffset;
[expressionLayout drawAtPoint:NSMakePoint(x, 0)];
x += expressionLayout.bounds.size.width; x += expressionLayout.bounds.size.width;
x += MPParenthesisFunctionClosingParensOffset; x += MPParenthesisFunctionClosingParensOffset;

View File

@@ -35,6 +35,10 @@ MPFunctionAccessorImplementation(ExponentExpression, _exponentExpression)
{ {
NSDecimalNumber *base = [self.baseValue evaluate]; NSDecimalNumber *base = [self.baseValue evaluate];
NSDecimalNumber *exponent = [[self.exponentExpression parse] evaluate]; NSDecimalNumber *exponent = [[self.exponentExpression parse] evaluate];
if ([base isEqualToNumber:@(0)] && [exponent isEqualToNumber:@(0)]) {
// The C pow function returns 1 for pow(0, 0). Mathematically this should be undefined.
return [NSDecimalNumber notANumber];
}
return [[NSDecimalNumber alloc] initWithDouble:pow(base.doubleValue, exponent.doubleValue)]; return [[NSDecimalNumber alloc] initWithDouble:pow(base.doubleValue, exponent.doubleValue)];
} }

View File

@@ -46,11 +46,18 @@
- (BOOL)validate:(MPParseError *__autoreleasing *)error - (BOOL)validate:(MPParseError *__autoreleasing *)error
{ {
if (_factors.count == 0) {
if (error) {
*error = MPParseError(_range, @"Expected Value");
}
return NO;
}
MPFactor *factor = _factors[0]; MPFactor *factor = _factors[0];
if (factor.hasMultiplicationSymbol) { if (factor.hasMultiplicationSymbol) {
if (error) { if (error) {
*error = MPParseError(factor.multiplicationSymbolRange, @"Unexpected Symbol."); *error = MPParseError(factor.multiplicationSymbolRange, @"Unexpected Symbol.");
} }
return NO;
} }
if (![factor.value validate:error]) { if (![factor.value validate:error]) {
return NO; return NO;

View File

@@ -68,7 +68,6 @@
case MPACosToken: case MPACosToken:
case MPATanToken: case MPATanToken:
{ {
[tokenStream currentTokenConsumed];
currentValue = [[MPFunctionValue alloc] initWithTokenStream:tokenStream]; currentValue = [[MPFunctionValue alloc] initWithTokenStream:tokenStream];
} }
break; break;