-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSTStringWithCode.m
96 lines (74 loc) · 2.14 KB
/
STStringWithCode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// STEmbeddedCodeSequences.m
// stein
//
// Created by Kevin MacWhinnie on 09/12/23.
// Copyright 2009 Stein Language. All rights reserved.
//
#import "STStringWithCode.h"
#import "STList.h"
#import "STInterpreter.h"
@implementation STStringWithCode
#pragma mark Creation
- (id)init
{
if((self = [super init]))
{
mCodeRanges = [NSMutableArray new];
mCodeExpressions = [NSMutableArray new];
return self;
}
return nil;
}
#pragma mark - Properties
@synthesize string = mString;
#pragma mark - Identity
- (BOOL)isEqualTo:(id)object
{
if([object isKindOfClass:[self class]])
{
STStringWithCode *otherStringWithCode = object;
return ([mString isEqualTo:otherStringWithCode->mString] &&
[mCodeRanges isEqualTo:otherStringWithCode->mCodeRanges] &&
[mCodeExpressions isEqualTo:otherStringWithCode->mCodeExpressions]);
}
else if([object isKindOfClass:[NSString class]])
{
return [mString isEqualTo:object];
}
return [super isEqualTo:object];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@:%p %@>", [self className], self, mString];
}
- (NSString *)prettyDescription
{
return mString;
}
#pragma mark - Adding Ranges
- (void)addExpression:(id)expression inRange:(NSRange)range
{
NSParameterAssert(expression);
[mCodeExpressions addObject:expression];
[mCodeRanges addObject:[NSValue valueWithRange:range]];
}
#pragma mark - Application
- (id)applyInScope:(STScope *)scope
{
STScope *expressionEvaluationScope = [STScope scopeWithParentScope:scope];
NSMutableArray *evaluatedExpressionStrings = [NSMutableArray array];
for (id expression in mCodeExpressions)
{
id resultOfExpression = STEvaluate(expression, expressionEvaluationScope);
[evaluatedExpressionStrings addObject:[resultOfExpression description]];
}
NSMutableString *resultString = [NSMutableString stringWithString:mString];
[mCodeRanges enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSValue *value, NSUInteger index, BOOL *stop) {
NSRange range = [value rangeValue];
[resultString replaceCharactersInRange:range
withString:[evaluatedExpressionStrings objectAtIndex:index]];
}];
return resultString;
}
@end