1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-22 03:07:41 +00:00

Make the -s option more useful.

This commit is contained in:
Calvin Rose
2018-05-07 01:04:24 -04:00
parent 55f0e759d9
commit 104b09d848
4 changed files with 27 additions and 20 deletions

View File

@@ -981,16 +981,14 @@ environment is needed, use run-context."
(defmacro import [path & args] (defmacro import [path & args]
(apply tuple import* '_env 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 "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." get a chunk of source code. Should return nil for end of file."
(def newenv (make-env)) (def newenv (make-env))
(defn chunks [buf p] (default getchunk (fn [buf]
(file-write stdout (string (parser-state p) "> ")) (file-read stdin :line buf)))
(file-flush stdout) (default onvalue (fn [x]
(file-read stdin :line buf))
(defn onvalue [x]
(put newenv '_ @{:value x}) (put newenv '_ @{:value x})
(pp x)) (pp x)))
(run-context newenv (if getchunk getchunk chunks) (default onerr default-error-handler)
onvalue default-error-handler)) (run-context newenv getchunk onvalue onerr))

View File

@@ -2,7 +2,7 @@
(var *should-repl* :private false) (var *should-repl* :private false)
(var *no-file* :private true) (var *no-file* :private true)
(var *use-getline* :private true) (var *raw-stdin* :private false)
# Flag handlers # Flag handlers
(def handlers :private { (def handlers :private {
@@ -17,7 +17,7 @@
(os-exit 0) (os-exit 0)
1) 1)
"v" (fn [] (print VERSION) (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) "r" (fn [] (:= *should-repl* true) 1)
"e" (fn [i] "e" (fn [i]
(:= *no-file* false) (:= *no-file* false)
@@ -46,7 +46,10 @@
(getline prompt buf)) (getline prompt buf))
(when (or *should-repl* *no-file*) (when (or *should-repl* *no-file*)
(print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose")) (if *raw-stdin*
(repl (if *use-getline* xgetline))) (repl nil identity)
(do
(print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose"))
(repl xgetline))))
) )

View File

@@ -124,6 +124,7 @@ struct DstParseState {
#define PFLAG_SQRBRACKETS 8 #define PFLAG_SQRBRACKETS 8
#define PFLAG_CURLYBRACKETS 16 #define PFLAG_CURLYBRACKETS 16
#define PFLAG_STRING 32 #define PFLAG_STRING 32
#define PFLAG_LONGSTRING 64
static void pushstate(DstParser *p, Consumer consumer, int flags) { static void pushstate(DstParser *p, Consumer consumer, int flags) {
DstParseState s; DstParseState s;
@@ -375,8 +376,8 @@ static int dotable(DstParser *p, DstParseState *state, uint8_t c) {
return root(p, state, c); return root(p, state, c);
} }
#define PFLAG_INSTRING 64 #define PFLAG_INSTRING 128
#define PFLAG_END_CANDIDATE 128 #define PFLAG_END_CANDIDATE 256
static int longstring(DstParser *p, DstParseState *state, uint8_t c) { static int longstring(DstParser *p, DstParseState *state, uint8_t c) {
if (state->flags & PFLAG_INSTRING) { if (state->flags & PFLAG_INSTRING) {
/* We are inside the long string */ /* 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); pushstate(p, stringchar, PFLAG_BUFFER | PFLAG_STRING);
return 1; return 1;
case '\\': case '\\':
pushstate(p, longstring, PFLAG_BUFFER | PFLAG_STRING); pushstate(p, longstring, PFLAG_BUFFER | PFLAG_LONGSTRING);
return 1; return 1;
case '[': case '[':
pushstate(p, doarray, PFLAG_CONTAINER | PFLAG_SQRBRACKETS); pushstate(p, doarray, PFLAG_CONTAINER | PFLAG_SQRBRACKETS);
@@ -465,7 +466,7 @@ static int root(DstParser *p, DstParseState *state, uint8_t c) {
state->qcount++; state->qcount++;
return 1; return 1;
case '"': case '"':
pushstate(p, stringchar, 0); pushstate(p, stringchar, PFLAG_STRING);
return 1; return 1;
case '#': case '#':
pushstate(p, comment, 0); pushstate(p, comment, 0);
@@ -474,7 +475,7 @@ static int root(DstParser *p, DstParseState *state, uint8_t c) {
pushstate(p, ampersand, 0); pushstate(p, ampersand, 0);
return 1; return 1;
case '\\': case '\\':
pushstate(p, longstring, 0); pushstate(p, longstring, PFLAG_LONGSTRING);
return 1; return 1;
case ')': case ')':
case ']': case ']':
@@ -718,6 +719,13 @@ static int cfun_state(DstArgs args) {
dst_v_push(buf, '{'); dst_v_push(buf, '{');
} else if (s->flags & PFLAG_STRING) { } else if (s->flags & PFLAG_STRING) {
dst_v_push(buf, '"'); 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)); str = dst_string(buf, dst_v_count(buf));

View File

@@ -73,6 +73,4 @@
(assert (= "hello, \"world\"" \\hello, "world"\\), "long string with embedded quotes") (assert (= "hello, \"world\"" \\hello, "world"\\), "long string with embedded quotes")
(assert (= "hello, \\\\\\ \"world\"" \=\hello, \\\ "world"\=\), "long string with embedded quotes and backslashes") (assert (= "hello, \\\\\\ \"world\"" \=\hello, \\\ "world"\=\), "long string with embedded quotes and backslashes")
(print :hello)
(end-suite) (end-suite)