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
424 #fn(ash)) ref-s32-LE)424 #fn(ash)) ref-s32-LE)
425 remprop #fn("n220711q5386;3F042286052;3:042386062:" #(#fn(get) *properties* #fn(has?)425 remprop #fn("n220711q5386;3F042286052;3:042386062:" #(#fn(get) *properties* #fn(has?)
426 #fn(del!)) remprop)426 #fn(del!)) remprop)
427 repl #fn("n0IIb4b5208421_5142085228485>2_51485<5047360:" #(#0#427 repl #fn("n0IIb4b5705042172514238424_5142385258485>2_51485<5047660:" #(*prompt* #fn(io-flush)
428 #fn("n07050421725142324{257651S;3Z04778451788551360q@=079855147:5047;85w<61:" #(*prompt*428 *io-out* #0#
429 #fn(io-flush) *io-out* #fn("n02060:" #(#fn(read)))429 #fn("n02021{227351S;3q047484517585513M07584513@076504277851@30q@=079855147:5047;85w<61:" #(#fn("n02071D62:" #(#fn(read)
430 #fn("n1207151422061:" #(#fn(io-discardbuffer) *io-in* #fn(raise)))430 *io-in*)) #fn("n1207151422061:" #(#fn(io-discardbuffer) *io-in* #fn(raise)))
431 #fn(io-eof?) *io-in* load-process void? print newline void that) prompt)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:")432 #fn("n020A>121{370F<60:q:" #(#fn("n0A<60:")
433 #fn("n1700514D:" #(top-level-exception-handler))) reploop) newline) repl)433 #fn("n1700514D:" #(top-level-exception-handler))) reploop) newline) repl)
434 revappend #fn("n2701062:" #(reverse-) revappend) reverse434 revappend #fn("n2701062:" #(reverse-) revappend) reverse
435 #fn("n170q062:" #(reverse-) reverse) reverse! #fn("n170q062:" #(reverse!-) reverse!)435 #fn("n170q062:" #(reverse-) reverse) reverse! #fn("n170q062:" #(reverse!-) reverse!)
src/io.c+2−2
@@ -147,10 +147,10 @@ BUILTIN("buffer", buffer) · 146 unmodified lines
147BUILTIN("read", read)147BUILTIN("read", read)
148{148{
149 if(nargs > 1)149 if(nargs > 1)
150 argcount(nargs, 1);150 argcount(nargs, 2);
151 sl_v a = nargs == 0 ? sym_value(sl_ioinsym) : args[0];151 sl_v a = nargs == 0 ? sym_value(sl_ioinsym) : args[0];
152 sl_gc_handle(&a);152 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);
154 sl_free_gc_handles(1);154 sl_free_gc_handles(1);
155 return ios_eof(toio(a)) ? sl_eof : v;155 return ios_eof(toio(a)) ? sl_eof : v;
156}156}
src/read.c+17−16
@@ -16,9 +16,10 @@ enum { · 15 unmodified lines
16typedef struct Rctx Rctx;16typedef struct Rctx Rctx;
17 17
18struct Rctx {18struct Rctx {
19 u32int toktype;
20 sl_v tokval;19 sl_v tokval;
21 sl_loc loc;20 sl_loc loc;
21 u32int toktype;
22 bool ws;
22 char buf[1024];23 char buf[1024];
23};24};
24 25
@@ -91,26 +92,25 @@ sl_read_numtok(const char *tok, sl_v *pval, int base) · 66 unmodified lines
91}92}
92 93
93static char94static char
94nextchar(void)95nextchar(Rctx *ctx)
95{96{
96 int ch;97 int c;
97 char c;
98 sl_ios *f = RS;98 sl_ios *f = RS;
99 99
100 do{100 do{
101 ch = ios_getc(RS);101 c = ios_getc(RS);
102 if(ch == IOS_EOF)102 if(c == IOS_EOF)
103 return 0;103 return 0;
104 c = ch;
105 if(c == ';'){104 if(c == ';'){
106 // single-line comment105 // single-line comment
107 do{106 do{
108 ch = ios_getc(f);107 c = ios_getc(f);
109 if(ch == IOS_EOF)108 if(c == IOS_EOF)
110 return 0;109 return 0;
111 }while(ch != '\n');110 }while(c != '\n');
112 c = ch;
113 }111 }
112 if(c == '\n' && ctx->ws)
113 return '\n';
114 }while(c == ' ' || isspace(c));114 }while(c == ' ' || isspace(c));
115 return c;115 return c;
116}116}
@@ -206,9 +206,10 @@ peek(Rctx *ctx) · 89 unmodified lines
206 206
207 if(ctx->toktype != TOK_NONE)207 if(ctx->toktype != TOK_NONE)
208 return ctx->toktype;208 return ctx->toktype;
209 c = nextchar();209 c = nextchar(ctx);
210 ctx->loc = RS->loc;210 ctx->loc = RS->loc;
211 if(ios_eof(RS))211 ctx->ws = false;
212 if(ios_eof(RS) || isspace(c))
212 return TOK_NONE;213 return TOK_NONE;
213 if(c == '(')214 if(c == '(')
214 ctx->toktype = TOK_OPEN;215 ctx->toktype = TOK_OPEN;
@@ -320,7 +321,6 @@ peek(Rctx *ctx) · 105 unmodified lines
320 goto hashpipe_gotc;321 goto hashpipe_gotc;
321 }322 }
322 }323 }
323 // this was whitespace, so keep peeking
324 return peek(ctx);324 return peek(ctx);
325 }else if(c == ';'){325 }else if(c == ';'){
326 // datum comment326 // datum comment
@@ -699,7 +699,7 @@ do_read_sexpr(Rctx *ctx, sl_v label) · 372 unmodified lines
699 case TOK_SHARPSYM:699 case TOK_SHARPSYM:
700 sym = ctx->tokval;700 sym = ctx->tokval;
701 // constructor notation701 // constructor notation
702 c = nextchar();702 c = nextchar(ctx);
703 ctx->loc = RS->loc;703 ctx->loc = RS->loc;
704 if(c != '('){704 if(c != '('){
705 take(ctx);705 take(ctx);
@@ -767,7 +767,7 @@ do_read_sexpr(Rctx *ctx, sl_v label) · 61 unmodified lines
767}767}
768 768
769sl_v769sl_v
770sl_read_sexpr(sl_v f)770sl_read_sexpr(sl_v f, bool ws)
771{771{
772 sl_readstate state;772 sl_readstate state;
773 state.prev = sl.readstate;773 state.prev = sl.readstate;
@@ -777,6 +777,7 @@ sl_read_sexpr(sl_v f) · 3 unmodified lines
777 sl.readstate = &state;777 sl.readstate = &state;
778 Rctx ctx;778 Rctx ctx;
779 ctx.toktype = TOK_NONE;779 ctx.toktype = TOK_NONE;
780 ctx.ws = ws;
780 sl_gc_handle(&ctx.tokval);781 sl_gc_handle(&ctx.tokval);
781 782
782 sl_v v = do_read_sexpr(&ctx, UNBOUND);783 sl_v v = do_read_sexpr(&ctx, UNBOUND);
src/read.h+1−1
@@ -1,6 +1,6 @@
1#pragma once1#pragma once
2 2
3sl_v sl_read_sexpr(sl_v f);3sl_v sl_read_sexpr(sl_v f, bool ws);
4bool sl_read_numtok(const char *tok, sl_v *pval, int base);4bool sl_read_numtok(const char *tok, sl_v *pval, int base);
5 5
6// defines which characters are ordinary symbol characters.6// 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
1355 SL_TRY{1355 SL_TRY{
1356 while(1){1356 while(1){
1357 sl.sp = saveSP;1357 sl.sp = saveSP;
1358 sl_v e = sl_read_sexpr(sl.sp[-1]);1358 sl_v e = sl_read_sexpr(sl.sp[-1], false);
1359 if(ios_eof(value2c(sl_ios*, sl.sp[-1])))1359 if(ios_eof(value2c(sl_ios*, sl.sp[-1])))
1360 break;1360 break;
1361 if(isfn(e)){1361 if(isfn(e)){
src/system.sl+10−4
@@ -1,3 +1,4 @@
1
1; StreetLISP standard library2; StreetLISP standard library
2; by Jeff Bezanson (C) 20093; by Jeff Bezanson (C) 2009
3; Distributed under the BSD License4; Distributed under the BSD License
@@ -1342,15 +1343,20 @@ · 1338 unmodified lines
1342 (raise `(load-error ,filename ,e))))))1343 (raise `(load-error ,filename ,e))))))
1343 1344
1344(def (repl)1345(def (repl)
1346 (*prompt*)
1347 (io-flush *io-out*)
1345 (def (prompt)1348 (def (prompt)
1346 (*prompt*)1349 (let ((v (trycatch (read *io-in* T)
1347 (io-flush *io-out*)
1348 (let ((v (trycatch (read)
1349 (λ (e) (io-discardbuffer *io-in*)1350 (λ (e) (io-discardbuffer *io-in*)
1350 (raise e)))))1351 (raise e)))))
1351 (and (not (io-eof? *io-in*))1352 (and (not (io-eof? *io-in*))
1352 (let ((V (load-process v)))1353 (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)))
1354 (void (set! that V))))))1360 (void (set! that V))))))
1355 (def (reploop)1361 (def (reploop)
1356 (when (trycatch (prompt)1362 (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