Archived
1

Started to Implement the Parser

This commit is contained in:
Kim Wittenburg
2014-09-06 01:54:15 +02:00
parent 6aafbf9d2e
commit 8df8317413
10 changed files with 570 additions and 16 deletions

View File

@@ -0,0 +1,56 @@
//
// MPExpressionEvaluator.m
// MathPad
//
// Created by Kim Wittenburg on 31.08.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import "MPExpressionEvaluator.h"
#import "MPExpression.h"
#import "MPParsedElement.h"
#import "MPMath.h"
@implementation MPExpressionEvaluator {
NSMutableDictionary *_variableBindings;
}
- (id)initWithExpression:(MPExpression *)expression
{
self = [super init];
if (self) {
_expression = expression;
_variableBindings = [[NSMutableDictionary alloc] init];
}
return self;
}
#pragma mark Lexing
- (MPParsedElement *)structuredRepresentationOfElementAtIndex:(NSUInteger)index
{
NSString *string = (NSString *)[self.expression elementAtIndex:index];
YY_BUFFER_STATE buffer = yy_scan_string(string.UTF8String);
yyparse();
yy_delete_buffer(buffer);
return [parseResult copy];
}
#pragma mark Evaluating Expressions
- (NSDictionary *)variableBindings
{
return [_variableBindings copy];
}
- (void)bindValue:(double)value toVariableName:(NSString *)name
{
[_variableBindings setObject:@(value)
forKey:name];
}
- (double)evaluateWithError:(NSError *__autoreleasing *)error
{
MPParsedElement *element = [self structuredRepresentationOfElementAtIndex:0];
return element.standaloneValue;
}
@end