Permalink
Browse files

A bit of cleanup, 2 more rewrite rules

  • Loading branch information...
1 parent 06a1c5f commit 5e8ddf0896524905f9578427e02fa5f62ec24ed3 @davedelong committed Oct 16, 2011
View
@@ -6,14 +6,17 @@
void listFunctions(void);
NSString* readLine() {
-
+ NSCharacterSet *valid = [DDMathStringTokenizer legalCharacters];
NSMutableData *data = [NSMutableData data];
+
do {
char c = getchar();
- if (c == '\r' || c == '\n') { break; }
- [data appendBytes:&c length:1];
+ if (c > 0xffff) { continue; }
+ if ([[NSCharacterSet newlineCharacterSet] characterIsMember:(unichar)c]) { break; }
+ if (![valid characterIsMember:(unichar)c]) { continue; }
+ [data appendBytes:&c length:sizeof(char)];
} while (1);
return DD_AUTORELEASE([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
@@ -58,16 +61,18 @@ int main (int argc, const char * argv[]) {
DDExpression *expression = [parser parsedExpressionWithError:&error];
DDExpression *rewritten = [[DDMathEvaluator sharedMathEvaluator] expressionByRewritingExpression:expression];
- NSNumber *value = [expression evaluateWithSubstitutions:nil evaluator:nil error:&error];
+ NSNumber *value = [rewritten evaluateWithSubstitutions:nil evaluator:nil error:&error];
DD_RELEASE(tokenizer);
if (value == nil) {
printf("\tERROR: %s\n", [[error description] UTF8String]);
} else {
- printf("\t%s = %s\n", [[expression description] UTF8String], [[value description] UTF8String]);
+ if (rewritten != expression) {
+ printf("\t%s REWRITTEN AS %s\n", [[expression description] UTF8String], [[rewritten description] UTF8String]);
+ }
+ printf("\t%s = %s\n", [[rewritten description] UTF8String], [[value description] UTF8String]);
}
- printf("\tRewritten: %s\n", [[rewritten description] UTF8String]);
} while (1);
@@ -337,6 +337,8 @@ + (NSDictionary *)_standardRewriteRules {
@"__exp1", @"__exp1 * 1",
@"pow(__exp1, 2)", @"__exp1 * __exp1",
@"multiply(__var1, __num1)", @"multiply(__num1, __var1)",
+ @"0", @"0 * __exp1",
+ @"0", @"__exp1 * 0",
//other stuff
@"__exp1", @"--__exp1",
@@ -20,6 +20,8 @@
}
++ (NSCharacterSet *)legalCharacters;
+
+ (id)tokenizerWithString:(NSString *)expressionString error:(NSError **)error;
- (id)initWithString:(NSString *)expressionString error:(NSError **)error;
@@ -32,11 +32,25 @@ - (DDMathStringToken *)_parseOperatorWithError:(NSError **)error;
+ (NSCharacterSet *)_operatorCharacterSet;
+ (NSCharacterSet *)_functionCharacterSet;
++ (NSCharacterSet *)_singleCharacterFunctionCharacterSet;
@end
@implementation DDMathStringTokenizer
++ (NSCharacterSet *)legalCharacters {
+ static dispatch_once_t onceToken;
+ static NSCharacterSet *legal = nil;
+ dispatch_once(&onceToken, ^{
+ NSMutableCharacterSet *mutable = [NSMutableCharacterSet characterSetWithCharactersInString:@"$."];
+ [mutable formUnionWithCharacterSet:[self _operatorCharacterSet]];
+ [mutable formUnionWithCharacterSet:[self _functionCharacterSet]];
+ [mutable formUnionWithCharacterSet:[self _singleCharacterFunctionCharacterSet]];
+ legal = [mutable copy];
+ });
+ return legal;
+}
+
+ (NSCharacterSet *)_operatorCharacterSet {
static dispatch_once_t onceToken;
static NSCharacterSet *_operatorSet = nil;

0 comments on commit 5e8ddf0

Please sign in to comment.