Skip to content

Commit

Permalink
Implement executing code from file
Browse files Browse the repository at this point in the history
  • Loading branch information
stoeffn committed Feb 6, 2018
1 parent ca09ef1 commit cc65ae9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
23 changes: 21 additions & 2 deletions Pseudo/Application.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,39 @@ - (BOOL) executeWithArguments: (nonnull NSArray<NSString *> *) arguments {
}

if ([arguments[1] isEqualToString: @"-i"]) {
[self executeWithArguments: [arguments subarrayWithRange: NSMakeRange(2, arguments.count - 2)]];
[self executeFilesAtFilePaths: [arguments subarrayWithRange: NSMakeRange(2, arguments.count - 2)]];
return [self enterREPL];
}

if ([arguments[1] hasPrefix: @"-"]) {
return [self printInvalidArgumentsError];
}

return [self executeWithArguments: [arguments subarrayWithRange: NSMakeRange(1, arguments.count - 1)]];
return [self executeFilesAtFilePaths: [arguments subarrayWithRange: NSMakeRange(1, arguments.count - 1)]];
}

#pragma mark - Commands

- (BOOL) executeFilesAtFilePaths: (NSArray<NSString *> *) filePaths {
__block NSError *error;

[filePaths enumerateObjectsUsingBlock: ^(NSString * _Nonnull filePath, NSUInteger idx, BOOL * _Nonnull stop) {
if (![filePath hasSuffix: PSDefaultFileExtension]) {
filePath = [filePath stringByAppendingString: PSDefaultFileExtension];
}

NSString *code = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: &error];
if (error) *stop = YES;

[self.interpreter executePseudoCode: code error: &error];
if (error) *stop = YES;
}];

if (error) {
[PSConsole writeString: [[NSString alloc] initWithFormat: @"%@\n", error.localizedDescription]];
return NO;
}

return YES;
}

Expand Down
4 changes: 1 addition & 3 deletions Pseudo/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

int main(int argc, const char *argv[]) {
@autoreleasepool {
Application *application = [[Application alloc] init];
BOOL isSuccess = [application executeWithArguments: NSProcessInfo.processInfo.arguments];

BOOL isSuccess = [[[Application alloc] init] executeWithArguments: NSProcessInfo.processInfo.arguments];
return isSuccess ? EXIT_SUCCESS : EXIT_FAILURE;
}
}
2 changes: 2 additions & 0 deletions PseudoKit/PSConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
#import <Foundation/Foundation.h>

FOUNDATION_EXPORT NSString *const PSParserErrorDomain;

FOUNDATION_EXPORT NSString *const PSDefaultFileExtension;
2 changes: 2 additions & 0 deletions PseudoKit/PSConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
#import "PSConstants.h"

NSString *const PSParserErrorDomain = @"SteffenRyll.Pseudo.Parser";

NSString *const PSDefaultFileExtension = @".pseudo";

0 comments on commit cc65ae9

Please sign in to comment.