From 104b09d8484fd79b24528d80428fb1663d949026 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 7 May 2018 01:04:24 -0400 Subject: [PATCH] Make the -s option more useful. --- src/compiler/boot.dst | 16 +++++++--------- src/mainclient/init.dst | 11 +++++++---- src/parser/parse.c | 18 +++++++++++++----- test/suite1.dst | 2 -- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/compiler/boot.dst b/src/compiler/boot.dst index 68f18f5f..5bf638ab 100644 --- a/src/compiler/boot.dst +++ b/src/compiler/boot.dst @@ -981,16 +981,14 @@ environment is needed, use run-context." (defmacro import [path & args] (apply tuple import* '_env path args)) -(defn repl [getchunk] +(defn repl [getchunk onvalue onerr] "Run a repl. The first parameter is an optional function to call to get a chunk of source code. Should return nil for end of file." (def newenv (make-env)) - (defn chunks [buf p] - (file-write stdout (string (parser-state p) "> ")) - (file-flush stdout) - (file-read stdin :line buf)) - (defn onvalue [x] + (default getchunk (fn [buf] + (file-read stdin :line buf))) + (default onvalue (fn [x] (put newenv '_ @{:value x}) - (pp x)) - (run-context newenv (if getchunk getchunk chunks) - onvalue default-error-handler)) + (pp x))) + (default onerr default-error-handler) + (run-context newenv getchunk onvalue onerr)) diff --git a/src/mainclient/init.dst b/src/mainclient/init.dst index 24494b5a..be97e5b5 100644 --- a/src/mainclient/init.dst +++ b/src/mainclient/init.dst @@ -2,7 +2,7 @@ (var *should-repl* :private false) (var *no-file* :private true) -(var *use-getline* :private true) +(var *raw-stdin* :private false) # Flag handlers (def handlers :private { @@ -17,7 +17,7 @@ (os-exit 0) 1) "v" (fn [] (print VERSION) (os-exit 0) 1) - "s" (fn [] (:= *use-getline* false) (:= *should-repl* true) 1) + "s" (fn [] (:= *raw-stdin* true) (:= *should-repl* true) 1) "r" (fn [] (:= *should-repl* true) 1) "e" (fn [i] (:= *no-file* false) @@ -46,7 +46,10 @@ (getline prompt buf)) (when (or *should-repl* *no-file*) - (print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose")) - (repl (if *use-getline* xgetline))) + (if *raw-stdin* + (repl nil identity) + (do + (print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose")) + (repl xgetline)))) ) diff --git a/src/parser/parse.c b/src/parser/parse.c index 3c450e0b..c31d8f77 100644 --- a/src/parser/parse.c +++ b/src/parser/parse.c @@ -124,6 +124,7 @@ struct DstParseState { #define PFLAG_SQRBRACKETS 8 #define PFLAG_CURLYBRACKETS 16 #define PFLAG_STRING 32 +#define PFLAG_LONGSTRING 64 static void pushstate(DstParser *p, Consumer consumer, int flags) { DstParseState s; @@ -375,8 +376,8 @@ static int dotable(DstParser *p, DstParseState *state, uint8_t c) { return root(p, state, c); } -#define PFLAG_INSTRING 64 -#define PFLAG_END_CANDIDATE 128 +#define PFLAG_INSTRING 128 +#define PFLAG_END_CANDIDATE 256 static int longstring(DstParser *p, DstParseState *state, uint8_t c) { if (state->flags & PFLAG_INSTRING) { /* We are inside the long string */ @@ -435,7 +436,7 @@ static int ampersand(DstParser *p, DstParseState *state, uint8_t c) { pushstate(p, stringchar, PFLAG_BUFFER | PFLAG_STRING); return 1; case '\\': - pushstate(p, longstring, PFLAG_BUFFER | PFLAG_STRING); + pushstate(p, longstring, PFLAG_BUFFER | PFLAG_LONGSTRING); return 1; case '[': pushstate(p, doarray, PFLAG_CONTAINER | PFLAG_SQRBRACKETS); @@ -465,7 +466,7 @@ static int root(DstParser *p, DstParseState *state, uint8_t c) { state->qcount++; return 1; case '"': - pushstate(p, stringchar, 0); + pushstate(p, stringchar, PFLAG_STRING); return 1; case '#': pushstate(p, comment, 0); @@ -474,7 +475,7 @@ static int root(DstParser *p, DstParseState *state, uint8_t c) { pushstate(p, ampersand, 0); return 1; case '\\': - pushstate(p, longstring, 0); + pushstate(p, longstring, PFLAG_LONGSTRING); return 1; case ')': case ']': @@ -718,6 +719,13 @@ static int cfun_state(DstArgs args) { dst_v_push(buf, '{'); } else if (s->flags & PFLAG_STRING) { dst_v_push(buf, '"'); + } else if (s->flags & PFLAG_LONGSTRING) { + int32_t i; + dst_v_push(buf, '\\'); + for (i = 0; i < s->argn; i++) { + dst_v_push(buf, '='); + } + dst_v_push(buf, '\\'); } } str = dst_string(buf, dst_v_count(buf)); diff --git a/test/suite1.dst b/test/suite1.dst index e78a1919..5703010b 100644 --- a/test/suite1.dst +++ b/test/suite1.dst @@ -73,6 +73,4 @@ (assert (= "hello, \"world\"" \\hello, "world"\\), "long string with embedded quotes") (assert (= "hello, \\\\\\ \"world\"" \=\hello, \\\ "world"\=\), "long string with embedded quotes and backslashes") -(print :hello) - (end-suite)