Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a REPL to the jstalk command line tool. I'm going to try and co…

…mmit this with Tower as well- I expect it to blow up somehow.
  • Loading branch information...
commit eb90eecd69b3f43f66edb8c93c8bfb33f42a95ee 1 parent 18d8bd7
@ccgus authored
Showing with 47 additions and 8 deletions.
  1. +47 −8 src/jstalkmain.m
View
55 src/jstalkmain.m
@@ -2,6 +2,7 @@
#import "JSTListener.h"
#import "JSTalk.h"
+BOOL JSCErrorHandlerExitOnError = YES;
@interface JSCErrorHandler : NSObject {
@@ -10,17 +11,49 @@ @interface JSCErrorHandler : NSObject {
@implementation JSCErrorHandler
-- (void) JSCocoa:(JSCocoaController*)controller hadError:(NSString*)error onLineNumber:(NSInteger)lineNumber atSourceURL:(id)url {
+- (void)JSCocoa:(JSCocoaController*)controller hadError:(NSString*)error onLineNumber:(NSInteger)lineNumber atSourceURL:(id)url {
- NSLog(@"Error line %d, %@", (int)lineNumber, error);
+ printf("Error line %d, %s\n", (int)lineNumber, [[error description] UTF8String]);
- exit(1);
+ if (JSCErrorHandlerExitOnError) {
+ exit(1);
+ }
}
@end
+void runREPL(JSTalk *t) {
+
+ // thanks http://tlrobinson.net/blog/2008/10/10/command-line-interpreter-and-repl-for-jscocoa/ !
+
+ JSCErrorHandlerExitOnError = NO;
+
+ while (1) {
+ char buffer[1024];
+
+ printf("js> ");
+
+ if (fgets(buffer, 1024, stdin) == NULL) {
+ exit(0);
+ }
+
+ NSString *s = [[NSString alloc] initWithUTF8String:buffer];
+
+ id o = [t executeString:s];
+
+ if (o) {
+ printf("%s\n", [[o description] UTF8String]);
+ }
+
+ [s release];
+
+ }
+
+
+}
+
int main(int argc, char *argv[]) {
@@ -30,10 +63,8 @@ int main(int argc, char *argv[]) {
}
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-
- NSString *s = [NSString stringWithContentsOfFile:[NSString stringWithUTF8String:argv[1]]
- encoding:NSUTF8StringEncoding
- error:nil];
+
+ NSString *arg = [NSString stringWithUTF8String:argv[1]];
JSCErrorHandler *eh = [[[JSCErrorHandler alloc] init] autorelease];
@@ -42,7 +73,15 @@ int main(int argc, char *argv[]) {
JSCocoaController *jsController = [t jsController];
jsController.delegate = eh;
- [t.env setObject:[NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[1]]] forKey:@"scriptURL"];
+
+ if ([arg isEqualToString:@"-e"]) {
+ runREPL(t);
+ exit(0);
+ }
+
+ NSString *s = [NSString stringWithContentsOfFile:arg encoding:NSUTF8StringEncoding error:nil];
+
+ [t.env setObject:[NSURL fileURLWithPath:arg] forKey:@"scriptURL"];
if ([s hasPrefix:@"#!"]) {
Please sign in to comment.
Something went wrong with that request. Please try again.