Archived
1

Archive Project

This commit is contained in:
Kim Wittenburg
2017-07-25 16:07:48 +02:00
parent 641cb5b435
commit ca3436da44
37 changed files with 9402 additions and 1599 deletions

127
Brainfuck/Document.m Normal file → Executable file
View File

@@ -8,7 +8,20 @@
#import "Document.h"
@implementation Document
#import "AppDelegate.h"
#import "Interpreter.h"
@implementation Document {
NSString *changedCharacters;
NSInteger insertLocation;
}
@synthesize editorView;
@synthesize inputView;
@synthesize outputView;
@synthesize text;
- (id)init
{
@@ -21,15 +34,18 @@
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"Document";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
editorView.textStorage.delegate = self;
if (!self.text) {
NSString *defaultFilePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"Default.brainfuck"];
self.text = [NSString stringWithContentsOfFile:defaultFilePath encoding:NSASCIIStringEncoding error:nil];
}
[self updateColors];
}
+ (BOOL)autosavesInPlace
@@ -39,21 +55,104 @@
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
@throw exception;
return nil;
return [self.text dataUsingEncoding:NSASCIIStringEncoding];
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
@throw exception;
self.text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
return YES;
}
- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError *__autoreleasing *)outError
{
NSPrintOperation *op = [NSPrintOperation printOperationWithView:self.editorView];
[self updateColors];
return op;
}
- (IBAction)executeScript:(id)sender
{
self.outputView.string = @"";
Interpreter *interpreter = [Interpreter interpreter];
interpreter.printer = self;
[interpreter execute:self.editorView.string withInput:self.inputView.stringValue error:nil];
}
- (void)updateColors
{
changedCharacters = self.editorView.string;
insertLocation = 0;
[self addTextStorageAttributes:self.editorView.textStorage];
}
#pragma mark - Window delegate
- (void)windowDidResignKey:(NSNotification *)notification
{
[self updateColors];
}
#pragma mark - Text storage delegate
- (void)textStorageDidProcessEditing:(NSNotification *)notification
{
[self addTextStorageAttributes:notification.object];
}
- (void)addTextStorageAttributes:(NSTextStorage *)storage
{
if (!changedCharacters) {
return;
}
NSData *foregroundData;
NSColor *foreground;
for (NSInteger i = 0; i < changedCharacters.length; ) {
NSInteger loc = insertLocation + i;
NSInteger len = 1;
unichar c = [changedCharacters characterAtIndex:i];
while (changedCharacters.length > ++i && [changedCharacters characterAtIndex:i] == c)
len++;
foregroundData = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"\\%c", c]];
if (!foregroundData) {
foregroundData = [[NSUserDefaults standardUserDefaults] objectForKey:@"default"];
}
foreground = [NSUnarchiver unarchiveObjectWithData:foregroundData];
[storage addAttribute:NSForegroundColorAttributeName value:foreground range:NSMakeRange(loc, len)];
}
changedCharacters = nil;
}
#pragma mark - Text view delegate
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
if (replacementString.length == 0) {
changedCharacters = nil;
} else {
changedCharacters = replacementString;
insertLocation = affectedCharRange.location;
}
return YES;
}
#pragma mark - Printer
- (void)print:(NSString *)string
{
self.outputView.string = [self.outputView.string stringByAppendingString:string];
}
#pragma mark - Split view delegate
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex
{
return 100;
}
- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex
{
return proposedMaximumPosition-50;
}
@end