Skip to content
Browse files

add stdin evaluation support. Closes #31

  • Loading branch information...
1 parent b3ccaf4 commit 35c9d0b90330d9a491b8c44bf973a7489bdd25f7 @tj tj committed
Showing with 78 additions and 34 deletions.
  1. +1 −1 Readme.md
  2. +49 −33 src/luna.c
  3. +25 −0 src/utils.c
  4. +3 −0 src/utils.h
View
2 Readme.md
@@ -161,7 +161,7 @@ not | right
Examples:
- $ luna - < some.luna
+ $ luna < some.luna
$ luna some.luna
$ luna some
$ luna
View
82 src/luna.c
@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <errno.h>
#include <string.h>
+#include <unistd.h>
#include <sys/stat.h>
#include "linenoise.h"
#include "luna.h"
@@ -30,7 +31,7 @@ static int tokens = 0;
* Output usage information.
*/
-static void
+void
usage() {
fprintf(stderr,
"\n Usage: luna [options] [file]"
@@ -58,7 +59,7 @@ usage() {
* Output luna version.
*/
-static void
+void
version() {
printf("%s\n", LUNA_VERSION);
exit(0);
@@ -68,7 +69,7 @@ version() {
* Line-noise REPL.
*/
-static void
+void
repl() {
char *line;
while(line = linenoise("luna> ")) {
@@ -125,6 +126,43 @@ parse_args(int *argc, const char **argv) {
}
/*
+ * Evaluate `source` with the given
+ * `path` name and return status.
+ */
+
+int
+eval(char *source, const char *path) {
+ // parse the input
+ luna_lexer_t lex;
+ luna_lexer_init(&lex, source, path);
+ luna_parser_t parser;
+ luna_parser_init(&parser, &lex);
+ luna_block_node_t *root;
+
+ // --tokens
+ if (tokens) {
+ while (luna_scan(&lex)) {
+ printf(" \e[90m%d : \e[m", lex.lineno);
+ luna_token_inspect(&lex.tok);
+ }
+ return 0;
+ }
+
+ // oh noes!
+ if (!(root = luna_parse(&parser))) {
+ luna_report_error(&parser);
+ return 1;
+ }
+
+ // --ast
+ if (ast) {
+ luna_prettyprint((luna_node_t *) root);
+ }
+
+ return 0;
+}
+
+/*
* Parse arguments and scan from stdin (for now).
*/
@@ -137,10 +175,16 @@ main(int argc, const char **argv){
// parse arguments
argv = parse_args(&argc, argv);
+ // eval stdin
+ if (1 == argc && !isatty(0)) {
+ source = read_until_eof(stdin);
+ return eval(source, "stdin");
+ }
+
// REPL
if (1 == argc) repl();
- // read
+ // eval file
orig = path = argv[1];
read:
if (!(source = file_read(path))) {
@@ -156,33 +200,5 @@ main(int argc, const char **argv){
exit(1);
}
- // parse the input
- luna_lexer_t lex;
- luna_lexer_init(&lex, source, path);
- luna_parser_t parser;
- luna_parser_init(&parser, &lex);
- luna_block_node_t *root;
-
- // --tokens
- if (tokens) {
- while (luna_scan(&lex)) {
- printf(" \e[90m%d : \e[m", lex.lineno);
- luna_token_inspect(&lex.tok);
- }
- exit(0);
- }
-
- // oh noes!
- if (!(root = luna_parse(&parser))) {
- luna_report_error(&parser);
- exit(1);
- }
-
- // --ast
- if (ast) {
- luna_prettyprint((luna_node_t *) root);
- exit(0);
- }
-
- return 0;
+ return eval(source, path);
}
View
25 src/utils.c
@@ -5,10 +5,12 @@
// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
//
+#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
+#include <string.h>
#include "utils.h"
/*
@@ -41,4 +43,27 @@ file_read(const char *filename) {
if (size != len) return NULL;
return buf;
+}
+
+/*
+ * Read `stream` until EOF.
+ */
+
+char *
+read_until_eof(FILE *stream) {
+ // alloc
+ off_t len = 0;
+ char buf[1024];
+ char *str = malloc(1);
+ assert(str);
+
+ // read
+ while (!feof(stream)) {
+ size_t n = fread(buf, 1, 1024, stream);
+ len += strlen(buf);
+ str = realloc(str, len);
+ strcat(str, buf);
+ }
+
+ return str;
}
View
3 src/utils.h
@@ -16,4 +16,7 @@ file_size(const char *filename);
char *
file_read(const char *filename);
+char *
+read_until_eof(FILE *stream);
+
#endif

0 comments on commit 35c9d0b

Please sign in to comment.
Something went wrong with that request. Please try again.