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

17
Brainfuck/AppDelegate.h Executable file
View File

@@ -0,0 +1,17 @@
//
// AppDelegate.h
// Brainfuck
//
// Created by Kim Wittenburg on 20.05.13.
// Copyright (c) 2013 brainfuck. All rights reserved.
//
#import <Foundation/Foundation.h>
@class CodeSnippet;
@interface AppDelegate : NSObject<NSApplicationDelegate, NSWindowDelegate>
- (IBAction)colorChanged:(id)sender;
@end

49
Brainfuck/AppDelegate.m Executable file
View File

@@ -0,0 +1,49 @@
//
// AppDelegate.m
// Brainfuck
//
// Created by Kim Wittenburg on 20.05.13.
// Copyright (c) 2013 brainfuck. All rights reserved.
//
#import "AppDelegate.h"
#define Color(r,g,b) [NSArchiver archivedDataWithRootObject:[NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0]]
#define WhiteColor(white) [NSArchiver archivedDataWithRootObject:[NSColor colorWithCalibratedWhite:white alpha:1.0]]
#define DefaultColor(color) [NSArchiver archivedDataWithRootObject:[NSColor color]]
@implementation AppDelegate
#pragma mark - Application delegate
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
NSMutableDictionary *defaults = [NSMutableDictionary dictionaryWithCapacity:9];
[defaults setObject:WhiteColor(0.333333) forKey:@"default"];
[defaults setObject:Color(11/255.0, 86/255.0, 2/255.0) forKey:@"\\+"];
[defaults setObject:Color(11/255.0, 86/255.0, 2/255.0) forKey:@"\\-"];
[defaults setObject:Color(0.0, 0.0, 83/255.0) forKey:@"\\<"];
[defaults setObject:Color(0.0, 0.0, 83/255.0) forKey:@"\\>"];
[defaults setObject:Color(82/255.0, 0.0, 2/255.0) forKey:@"\\["];
[defaults setObject:Color(82/255.0, 0.0, 2/255.0) forKey:@"\\]"];
[defaults setObject:DefaultColor(blackColor) forKey:@"\\."];
[defaults setObject:DefaultColor(blackColor) forKey:@"\\,"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
- (IBAction)colorChanged:(id)sender {
for (id obj in [[NSDocumentController sharedDocumentController] documents]) {
if ([obj respondsToSelector:@selector(updateColors)]) {
[obj performSelector:@selector(updateColors)];
}
}
}
#pragma mark - Window delegate
- (void)windowDidResignMain:(NSNotification *)notification
{
[self colorChanged:nil];
}
@end

44
Brainfuck/Brainfuck-Info.plist Normal file → Executable file
View File

@@ -12,23 +12,33 @@
<string>brainfuck</string>
</array>
<key>CFBundleTypeIconFile</key>
<string></string>
<string>BrainfuckDocument.icns</string>
<key>CFBundleTypeName</key>
<string>DocumentType</string>
<string>Brainfuck Document</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>????</string>
</array>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSItemContentTypes</key>
<array>
<string>tk.brainfuck</string>
</array>
<key>LSTypeIsPackage</key>
<false/>
<key>NSDocumentClass</key>
<string>Document</string>
<key>NSExportableTypes</key>
<array>
<string>tk.brainfuck</string>
</array>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<string>Brainfuck</string>
<key>CFBundleIdentifier</key>
<string>tk.brainfuck.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
@@ -53,5 +63,33 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.plain-text</string>
</array>
<key>UTTypeDescription</key>
<string>Brainfuck Document</string>
<key>UTTypeIconFile</key>
<string>BrainfuckDocument</string>
<key>UTTypeIdentifier</key>
<string>tk.brainfuck</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>com.apple.ostype</key>
<array>
<string>????</string>
</array>
<key>public.filename-extension</key>
<array>
<string>brainfuck</string>
</array>
<key>public.mime-type</key>
<array/>
</dict>
</dict>
</array>
</dict>
</plist>

0
Brainfuck/Brainfuck-Prefix.pch Normal file → Executable file
View File

18
Brainfuck/Default.brainfuck Executable file
View File

@@ -0,0 +1,18 @@
++++++++++
[
>+++++++>++++++++++>+++>+<<<<-
]
>++.
>+.
+++++++.
.
+++.
>++.
<<+++++++++++++++.
>.
+++.
------.
--------.
>+.
>.
+++.

13
Brainfuck/Document.h Normal file → Executable file
View File

@@ -8,6 +8,17 @@
#import <Cocoa/Cocoa.h>
@interface Document : NSDocument
#import "Printer.h"
@interface Document : NSDocument<NSWindowDelegate, NSSplitViewDelegate, NSTextStorageDelegate, NSTextViewDelegate, Printer>
@property (unsafe_unretained) IBOutlet NSTextView *editorView;
@property (weak) IBOutlet NSTextField *inputView;
@property (unsafe_unretained) IBOutlet NSTextView *outputView;
@property (copy) NSString *text;
- (IBAction)executeScript:(id)sender;
- (void)updateColors;
@end

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

23
Brainfuck/Interpreter.h Executable file
View File

@@ -0,0 +1,23 @@
//
// Interpreter.h
// Brainfuck
//
// Created by Kim Wittenburg on 20.05.13.
// Copyright (c) 2013 brainfuck. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Printer.h"
@interface Interpreter : NSObject
+ (Interpreter *)interpreter;
- (id)init;
@property (strong, nonatomic) id<Printer> printer;
- (void)execute:(NSString *)script withInput:(NSString *)input error:(NSError **)error;
@end

130
Brainfuck/Interpreter.m Executable file
View File

@@ -0,0 +1,130 @@
//
// Interpreter.m
// Brainfuck
//
// Created by Kim Wittenburg on 20.05.13.
// Copyright (c) 2013 brainfuck. All rights reserved.
//
#import "Interpreter.h"
#import "Lexer.h"
@implementation Interpreter {
NSMutableArray *a;
NSInteger level;
NSString *input;
NSInteger inPos;
}
@synthesize printer;
+ (Interpreter *)interpreter
{
return [[self alloc] init];
}
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (void)execute:(NSString *)script withInput:(NSString *)i error:(NSError *__autoreleasing *)error
{
input = i;
inPos = 0;
Lexer *lexer = [Lexer lexer];
NSArray *cmds = [lexer performLexingOnString:script error:error];
level = 0;
a = [NSMutableArray new];
[a addObject:[NSNumber numberWithInt:0]];
[self executeCommands:cmds];
}
- (void)executeCommands:(NSArray *)cmds
{
for (id cmd in cmds) {
if ([cmd isKindOfClass:[NSArray class]]) {
[self loop:cmd];
} else {
[self executeCommand:cmd];
}
}
}
- (void)executeCommand:(NSString *)cmd
{
int c = [cmd characterAtIndex:0];
if (c == '+') {
[self increment];
} else if (c == '-') {
[self decrement];
} else if (c == '<') {
[self previousCell];
} else if (c == '>') {
[self nextCell];
} else if (c == '.') {
[self printCellValue];
} else if (c == ',') {
[self readNextInput];
}
}
- (void)increment
{
NSNumber *number = a[level];
number = [NSNumber numberWithInt:number.intValue+1];
a[level] = number;
}
- (void)decrement
{
NSNumber *number = a[level];
number = [NSNumber numberWithInt:number.intValue-1];
a[level] = number;
}
- (void)nextCell
{
if (++level == a.count) {
[a addObject:[NSNumber numberWithInt:0]];
}
}
- (void)previousCell
{
if (--level == -1) {
[a insertObject:[NSNumber numberWithInt:0] atIndex:0];
level++;
}
}
- (void)printCellValue
{
NSNumber *ascii = a[level];
NSString *text = [NSString stringWithFormat:@"%c", ascii.intValue];
[self.printer print:text];
}
- (void)readNextInput
{
int ascii;
if (inPos == input.length) {
ascii = 0;
} else {
ascii = [input characterAtIndex:inPos++];
}
a[level] = [NSNumber numberWithInt:ascii];
}
- (void)loop:(NSArray *)cmds
{
while (((NSNumber *)a[level]).intValue != 0) {
[self executeCommands:cmds];
}
}
@end

19
Brainfuck/Lexer.h Executable file
View File

@@ -0,0 +1,19 @@
//
// Lexer.h
// Brainfuck
//
// Created by Kim Wittenburg on 20.05.13.
// Copyright (c) 2013 brainfuck. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Lexer : NSObject
+ (Lexer *)lexer;
- (id)init;
- (NSArray *)performLexingOnString:(NSString *)string error:(NSError **)error;
@end

70
Brainfuck/Lexer.m Executable file
View File

@@ -0,0 +1,70 @@
//
// Lexer.m
// Brainfuck
//
// Created by Kim Wittenburg on 20.05.13.
// Copyright (c) 2013 brainfuck. All rights reserved.
//
#import "Lexer.h"
@implementation Lexer {
NSString *script;
NSInteger pos;
NSInteger loopNestingLevel;
NSMutableArray *tokens;
}
+ (Lexer *)lexer
{
return [[self alloc] init];
}
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (NSArray *)performLexingOnString:(NSString *)s error:(NSError *__autoreleasing *)error
{
pos = 0;
loopNestingLevel = -1;
script = s;
tokens = [[NSMutableArray alloc] init];
[self matchNextTokensInto:tokens error:error];
if (error) {
return nil;
}
return tokens;
}
- (void)matchNextTokensInto:(NSMutableArray *)target error:(NSError *__autoreleasing *)error;
{
while (pos < script.length) {
int c = [script characterAtIndex:pos++];
NSString *character = [NSString stringWithFormat:@"%c", c];
if (c == '-' || c == '+' || c == '<' || c == '>' || c == '.' || c == ',') {
[target addObject:character];
} else if (c == '[') {
loopNestingLevel++;
NSMutableArray *subTokens = [NSMutableArray new];
[self matchNextTokensInto:subTokens error:error];
[target addObject:subTokens];
} else if (c == ']') {
if (loopNestingLevel-- == -1) {
NSError *err = [NSError errorWithDomain:@"Lexer" code:101 userInfo:@{NSLocalizedDescriptionKey: @"] without mathcing ["}];
*error = err;
return;
}
return;
}
}
if (loopNestingLevel >= 0) {
*error = [NSError errorWithDomain:@"Lexer" code:102 userInfo:@{NSLocalizedDescriptionKey: @"unclosed loop"}];
}
}
@end

16
Brainfuck/Printer.h Executable file
View File

@@ -0,0 +1,16 @@
//
// Printer.h
// Brainfuck
//
// Created by Kim Wittenburg on 20.05.13.
// Copyright (c) 2013 brainfuck. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol Printer <NSObject>
@required
- (void)print:(NSString *)string;
@end

28
Brainfuck/en.lproj/Credits.rtf Normal file → Executable file
View File

@@ -1,29 +1,39 @@
{\rtf0\ansi{\fonttbl\f0\fswiss Helvetica;}
{\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf370
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw9840\paperh8400
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural
\paperw11900\paperh16840\vieww9600\viewh8400\viewkind0
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720
\f0\b\fs24 \cf0 Engineering:
\b0 \
Some people\
Kim Wittenburg\
\
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural
{\field{\*\fldinst{HYPERLINK "http://de.wikipedia.org/wiki/Brainfuck"}}{\fldrslt
\b \cf0 Brainfuck}}
\b :\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural
\cf0
\b0 Urban M\'fcller (1993)\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720
\cf0 \
\b Human Interface Design:
\b0 \
Some other people\
Kim Wittenburg\
\
\b Testing:
\b0 \
Hopefully not nobody\
Kim Wittenburg\
\
\b Documentation:
\b0 \
Whoever\
What Documentation?\
\
\b With special thanks to:
\b0 \
Mom\
}
Nobody. I hate everyone.\
}

1310
Brainfuck/en.lproj/Document.xib Normal file → Executable file

File diff suppressed because it is too large Load Diff

0
Brainfuck/en.lproj/InfoPlist.strings Normal file → Executable file
View File

3874
Brainfuck/en.lproj/MainMenu.xib Normal file → Executable file

File diff suppressed because it is too large Load Diff

0
Brainfuck/main.m Normal file → Executable file
View File