From a9bb2387a1fff1cc038424eadc927402bcbea267 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Sun, 4 Jan 2015 13:00:53 -0800 Subject: [PATCH] Handle empty (or comment-only) files. Fix #57. --- src/main.c | 8 ++------ src/wren_compiler.c | 4 +--- test/comments/only_line_comment.wren | 1 + test/comments/only_line_comment_and_line.wren | 1 + test/empty_file.wren | 0 5 files changed, 5 insertions(+), 9 deletions(-) create mode 100644 test/comments/only_line_comment.wren create mode 100644 test/comments/only_line_comment_and_line.wren create mode 100644 test/empty_file.wren diff --git a/src/main.c b/src/main.c index f0198044..871933d5 100644 --- a/src/main.c +++ b/src/main.c @@ -90,12 +90,8 @@ static int runRepl(WrenVM* vm) return 0; } - // Do nothing if we received an empty line. - if (strcmp(line, "\n") != 0) - { - // TODO: Handle failure. - wrenInterpret(vm, "Prompt", line); - } + // TODO: Handle failure. + wrenInterpret(vm, "Prompt", line); free(line); diff --git a/src/wren_compiler.c b/src/wren_compiler.c index a11bca78..ccbea58c 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -2747,7 +2747,7 @@ ObjFn* wrenCompile(WrenVM* vm, const char* sourcePath, const char* source) WREN_UNPIN(vm); - for (;;) + while (!match(&compiler, TOKEN_EOF)) { definition(&compiler); @@ -2757,8 +2757,6 @@ ObjFn* wrenCompile(WrenVM* vm, const char* sourcePath, const char* source) consume(&compiler, TOKEN_EOF, "Expect end of file."); break; } - - if (match(&compiler, TOKEN_EOF)) break; } emit(&compiler, CODE_NULL); diff --git a/test/comments/only_line_comment.wren b/test/comments/only_line_comment.wren new file mode 100644 index 00000000..e2c01e9c --- /dev/null +++ b/test/comments/only_line_comment.wren @@ -0,0 +1 @@ +// comment \ No newline at end of file diff --git a/test/comments/only_line_comment_and_line.wren b/test/comments/only_line_comment_and_line.wren new file mode 100644 index 00000000..fef83a9c --- /dev/null +++ b/test/comments/only_line_comment_and_line.wren @@ -0,0 +1 @@ +// comment diff --git a/test/empty_file.wren b/test/empty_file.wren new file mode 100644 index 00000000..e69de29b