History

improve interactive repl

Print the prompt when it makes sense only.

Sigrid Solveig Haflínudóttir committed 1 year ago

Commit a8374051 parent 9971c956 files · +37 −30Browse files

Unified · Split

6 files
  1. boot/sl.boot +6 −6
  2. src/io.c +2 −2
  3. src/read.c +17 −16
  4. src/read.h +1 −1
  5. src/sl.c +1 −1
  6. src/system.sl +10 −4
boot/sl.boot+6−6
@@ -424,12 +424,12 @@ · 423 unmodified lines
424424 #fn(ash)) ref-s32-LE)
425425 remprop #fn("n220711q5386;3F042286052;3:042386062:" #(#fn(get) *properties* #fn(has?)
426426 #fn(del!)) remprop)
427− repl #fn("n0IIb4b5208421_5142085228485>2_51485<5047360:" #(#0#
428− #fn("n07050421725142324{257651S;3Z04778451788551360q@=079855147:5047;85w<61:" #(*prompt*
429− #fn(io-flush) *io-out* #fn("n02060:" #(#fn(read)))
430− #fn("n1207151422061:" #(#fn(io-discardbuffer) *io-in* #fn(raise)))
431− #fn(io-eof?) *io-in* load-process void? print newline void that) prompt)
432− #fn("n020A>121{370F<60:q:" #(#fn("n0A<60:")
427+ repl #fn("n0IIb4b5705042172514238424_5142385258485>2_51485<5047660:" #(*prompt* #fn(io-flush)
428+ *io-out* #0#
429+ #fn("n02021{227351S;3q047484517585513M07584513@076504277851@30q@=079855147:5047;85w<61:" #(#fn("n02071D62:" #(#fn(read)
430+ *io-in*)) #fn("n1207151422061:" #(#fn(io-discardbuffer) *io-in* #fn(raise)))
431+ #fn(io-eof?) *io-in* load-process void? *prompt* #fn(io-flush) *io-out* print newline void that) prompt)
432+ #fn("n020A>121{370F<60:q:" #(#fn("n0A<60:")
433433 #fn("n1700514D:" #(top-level-exception-handler))) reploop) newline) repl)
434434 revappend #fn("n2701062:" #(reverse-) revappend) reverse
435435 #fn("n170q062:" #(reverse-) reverse) reverse! #fn("n170q062:" #(reverse!-) reverse!)
src/io.c+2−2
@@ -147,10 +147,10 @@ BUILTIN("buffer", buffer) · 146 unmodified lines
147147 BUILTIN("read", read)
148148 {
149149 if(nargs > 1)
150− argcount(nargs, 1);
150+ argcount(nargs, 2);
151151 sl_v a = nargs == 0 ? sym_value(sl_ioinsym) : args[0];
152152 sl_gc_handle(&a);
153− sl_v v = sl_read_sexpr(a);
153+ sl_v v = sl_read_sexpr(a, nargs > 1 && args[1] == sl_t);
154154 sl_free_gc_handles(1);
155155 return ios_eof(toio(a)) ? sl_eof : v;
156156 }
src/read.c+17−16
@@ -16,9 +16,10 @@ enum { · 15 unmodified lines
1616 typedef struct Rctx Rctx;
1717
1818 struct Rctx {
19− u32int toktype;
2019 sl_v tokval;
2120 sl_loc loc;
21+ u32int toktype;
22+ bool ws;
2223 char buf[1024];
2324 };
2425
@@ -91,26 +92,25 @@ sl_read_numtok(const char *tok, sl_v *pval, int base) · 66 unmodified lines
9192 }
9293
9394 static char
94−nextchar(void)
95+nextchar(Rctx *ctx)
9596 {
96− int ch;
97− char c;
97+ int c;
9898 sl_ios *f = RS;
9999
100100 do{
101− ch = ios_getc(RS);
102− if(ch == IOS_EOF)
101+ c = ios_getc(RS);
102+ if(c == IOS_EOF)
103103 return 0;
104− c = ch;
105104 if(c == ';'){
106105 // single-line comment
107106 do{
108− ch = ios_getc(f);
109− if(ch == IOS_EOF)
107+ c = ios_getc(f);
108+ if(c == IOS_EOF)
110109 return 0;
111− }while(ch != '\n');
112− c = ch;
110+ }while(c != '\n');
113111 }
112+ if(c == '\n' && ctx->ws)
113+ return '\n';
114114 }while(c == ' ' || isspace(c));
115115 return c;
116116 }
@@ -206,9 +206,10 @@ peek(Rctx *ctx) · 89 unmodified lines
206206
207207 if(ctx->toktype != TOK_NONE)
208208 return ctx->toktype;
209− c = nextchar();
209+ c = nextchar(ctx);
210210 ctx->loc = RS->loc;
211− if(ios_eof(RS))
211+ ctx->ws = false;
212+ if(ios_eof(RS) || isspace(c))
212213 return TOK_NONE;
213214 if(c == '(')
214215 ctx->toktype = TOK_OPEN;
@@ -320,7 +321,6 @@ peek(Rctx *ctx) · 105 unmodified lines
320321 goto hashpipe_gotc;
321322 }
322323 }
323− // this was whitespace, so keep peeking
324324 return peek(ctx);
325325 }else if(c == ';'){
326326 // datum comment
@@ -699,7 +699,7 @@ do_read_sexpr(Rctx *ctx, sl_v label) · 372 unmodified lines
699699 case TOK_SHARPSYM:
700700 sym = ctx->tokval;
701701 // constructor notation
702− c = nextchar();
702+ c = nextchar(ctx);
703703 ctx->loc = RS->loc;
704704 if(c != '('){
705705 take(ctx);
@@ -767,7 +767,7 @@ do_read_sexpr(Rctx *ctx, sl_v label) · 61 unmodified lines
767767 }
768768
769769 sl_v
770−sl_read_sexpr(sl_v f)
770+sl_read_sexpr(sl_v f, bool ws)
771771 {
772772 sl_readstate state;
773773 state.prev = sl.readstate;
@@ -777,6 +777,7 @@ sl_read_sexpr(sl_v f) · 3 unmodified lines
777777 sl.readstate = &state;
778778 Rctx ctx;
779779 ctx.toktype = TOK_NONE;
780+ ctx.ws = ws;
780781 sl_gc_handle(&ctx.tokval);
781782
782783 sl_v v = do_read_sexpr(&ctx, UNBOUND);
src/read.h+1−1
@@ -1,6 +1,6 @@
11 #pragma once
22
3−sl_v sl_read_sexpr(sl_v f);
3+sl_v sl_read_sexpr(sl_v f, bool ws);
44 bool sl_read_numtok(const char *tok, sl_v *pval, int base);
55
66 // defines which characters are ordinary symbol characters.
src/sl.c+1−1
@@ -1355,7 +1355,7 @@ sl_load_system_image(sl_v sys_image_io) · 1354 unmodified lines
13551355 SL_TRY{
13561356 while(1){
13571357 sl.sp = saveSP;
1358− sl_v e = sl_read_sexpr(sl.sp[-1]);
1358+ sl_v e = sl_read_sexpr(sl.sp[-1], false);
13591359 if(ios_eof(value2c(sl_ios*, sl.sp[-1])))
13601360 break;
13611361 if(isfn(e)){
src/system.sl+10−4
@@ -1,3 +1,4 @@
1+
12 ; StreetLISP standard library
23 ; by Jeff Bezanson (C) 2009
34 ; Distributed under the BSD License
@@ -1342,15 +1343,20 @@ · 1338 unmodified lines
13421343 (raise `(load-error ,filename ,e))))))
13431344
13441345 (def (repl)
1346+ (*prompt*)
1347+ (io-flush *io-out*)
13451348 (def (prompt)
1346− (*prompt*)
1347− (io-flush *io-out*)
1348− (let ((v (trycatch (read)
1349+ (let ((v (trycatch (read *io-in* T)
13491350 (λ (e) (io-discardbuffer *io-in*)
13501351 (raise e)))))
13511352 (and (not (io-eof? *io-in*))
13521353 (let ((V (load-process v)))
1353− (unless (void? V) (print V) (newline))
1354+ (if (void? V)
1355+ (when (void? v)
1356+ (*prompt*)
1357+ (io-flush *io-out*))
1358+ (begin (print V)
1359+ (newline)))
13541360 (void (set! that V))))))
13551361 (def (reploop)
13561362 (when (trycatch (prompt)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t