1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +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]
(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))

View File

@ -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))))
)

View File

@ -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));

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 and backslashes")
(print :hello)
(end-suite)