Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit717a1bd

Browse files
committed
pyexec: Add named constants for raw repl control characters
The raw repl language is gradually getting more complex. To structurethe code a bit more, introduce some names for the different controlcharacters and the "init command" sequence which is currentlyonly used for starting a paste.
1 parentef54cdb commit717a1bd

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

‎shared/runtime/pyexec.c

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,23 @@ STATIC bool repl_display_debugging_info = 0;
5959
#defineEXEC_FLAG_SOURCE_IS_FILENAME (1 << 5)
6060
#defineEXEC_FLAG_SOURCE_IS_READER (1 << 6)
6161

62-
#defineNUM_ESCAPED 8 // This value has to match the value in tools/pyboard.py
62+
#defineRAWCODE_PASTE_NUM_ESCAPED 8 // This value has to match the same constant in tools/pyboard.py
63+
64+
// Raw REPL serial protocol control sequences
65+
#defineRAW_REPL_CTRL_INIT CHAR_CTRL_A
66+
#defineRAW_REPL_CTRL_EXIT_TO_FRIENDLY CHAR_CTRL_B
67+
#defineRAW_REPL_CTRL_CLEAR_LINE CHAR_CTRL_C
68+
#defineRAW_REPL_CTRL_EOF CHAR_CTRL_D
69+
#defineRAW_REPL_CTRL_INIT_CMD CHAR_CTRL_E
70+
// CHAR_CTRL_F is recognised in raw paste mode (as an escape sequence), but not in raw REPL mode
71+
72+
// Sequence ^A ^E (RAW_REPL_CTRL_INIT, RAW_REPL_CTRL_INIT_CMD) can initiate one or more "init commands" based on the next
73+
// character in the sequence:
74+
#defineRAW_REPL_INIT_CMD_PASTE_SOURCE 'A'
75+
#defineRAW_REPL_INIT_CMD_PASTE_RAWCODE 'B'
76+
77+
#defineRAW_REPL_INIT_CMD_RESP_UNSUPPORTED "R\x00"
78+
#defineRAW_REPL_INIT_CMD_RESP_OK "R\x01"
6379

6480
// parses, compiles and executes the code in the lexer
6581
// frees the lexer before returning
@@ -234,9 +250,9 @@ STATIC mp_uint_t mp_reader_stdin_readbyte(void *data) {
234250
returnMP_READER_EOF;
235251
}
236252
}elseif (c==CHAR_CTRL_F) {
237-
// escape sequence, next character is escaped by addingNUM_ESCAPED to it
253+
// escape sequence, next character is escaped by addingRAWCODE_PASTE_NUM_ESCAPED to it
238254
inte=mp_hal_stdin_rx_chr();
239-
c=e-NUM_ESCAPED;
255+
c=e-RAWCODE_PASTE_NUM_ESCAPED;
240256
}
241257

242258
if (--reader->window_remain==0) {
@@ -277,30 +293,33 @@ STATIC void mp_reader_new_stdin(mp_reader_t *reader, mp_reader_stdin_t *reader_s
277293
reader->close=mp_reader_stdin_close;
278294
}
279295

280-
STATICintdo_reader_stdin(intc) {
281-
boolsupported_command=c=='A';
296+
STATICinthandle_raw_repl_init_cmd(intc) {
297+
intexec_flags=EXEC_FLAG_PRINT_EOF |EXEC_FLAG_SOURCE_IS_READER;
298+
boolsupported_command= false;
299+
300+
if (c==RAW_REPL_INIT_CMD_PASTE_SOURCE) {
301+
supported_command= true;
302+
}
282303
#ifMICROPY_PERSISTENT_CODE_LOAD
283-
supported_command= (c=='B')||supported_command;
304+
if (c==RAW_REPL_INIT_CMD_PASTE_RAWCODE) {
305+
exec_flags |=EXEC_FLAG_SOURCE_IS_RAW_CODE;
306+
supported_command= true;
307+
}
284308
#endif
285309

286310
if (!supported_command) {
287311
// Unsupported command.
288-
mp_hal_stdout_tx_strn("R\x00",2);
312+
mp_hal_stdout_tx_strn(RAW_REPL_INIT_CMD_RESP_UNSUPPORTED,2);
289313
return0;
290314
}
291315

292316
// Indicate reception of command.
293-
mp_hal_stdout_tx_strn("R\x01",2);
317+
mp_hal_stdout_tx_strn(RAW_REPL_INIT_CMD_RESP_OK,2);
294318

295319
// Entering raw paste mode
296-
// c == 'A' input is source, c == 'B' input is bytecode
297320
mp_reader_treader;
298321
mp_reader_stdin_treader_stdin;
299322
mp_reader_new_stdin(&reader,&reader_stdin,MICROPY_REPL_STDIN_BUFFER_MAX);
300-
intexec_flags=EXEC_FLAG_PRINT_EOF |EXEC_FLAG_SOURCE_IS_READER;
301-
if (c=='B') {
302-
exec_flags |=EXEC_FLAG_SOURCE_IS_RAW_CODE;
303-
}
304323
returnparse_compile_execute(&reader,MP_PARSE_FILE_INPUT,exec_flags);
305324
}
306325

@@ -335,30 +354,30 @@ void pyexec_event_repl_init(void) {
335354
}
336355

337356
STATICintpyexec_raw_repl_process_char(intc) {
338-
if (c==CHAR_CTRL_A) {
357+
if (c==RAW_REPL_CTRL_INIT) {
339358
// reset raw REPL
340-
if (vstr_len(MP_STATE_VM(repl_line))==2&&vstr_str(MP_STATE_VM(repl_line))[0]==CHAR_CTRL_E) {
341-
intret=do_reader_stdin(vstr_str(MP_STATE_VM(repl_line))[1]);
359+
if (vstr_len(MP_STATE_VM(repl_line))==2&&vstr_str(MP_STATE_VM(repl_line))[0]==RAW_REPL_CTRL_INIT_CMD) {
360+
intret=handle_raw_repl_init_cmd(vstr_str(MP_STATE_VM(repl_line))[1]);
342361
if (ret&PYEXEC_FORCED_EXIT) {
343362
returnret;
344363
}
345364
gotoreset;
346365
}
347366
mp_hal_stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
348367
gotoreset;
349-
}elseif (c==CHAR_CTRL_B) {
368+
}elseif (c==RAW_REPL_CTRL_EXIT_TO_FRIENDLY) {
350369
// change to friendly REPL
351370
pyexec_mode_kind=PYEXEC_MODE_FRIENDLY_REPL;
352371
vstr_reset(MP_STATE_VM(repl_line));
353372
repl.cont_line= false;
354373
repl.paste_mode= false;
355374
pyexec_friendly_repl_process_char(CHAR_CTRL_B);
356375
return0;
357-
}elseif (c==CHAR_CTRL_C) {
376+
}elseif (c==RAW_REPL_CTRL_CLEAR_LINE) {
358377
// clear line
359378
vstr_reset(MP_STATE_VM(repl_line));
360379
return0;
361-
}elseif (c==CHAR_CTRL_D) {
380+
}elseif (c==RAW_REPL_CTRL_EOF) {
362381
// input finished
363382
}else {
364383
// let through any other raw 8-bit value
@@ -419,7 +438,7 @@ STATIC int pyexec_friendly_repl_process_char(int c) {
419438

420439
if (!repl.cont_line) {
421440

422-
if (ret==CHAR_CTRL_A) {
441+
if (ret==RAW_REPL_CTRL_INIT) {
423442
// change to raw REPL
424443
pyexec_mode_kind=PYEXEC_MODE_RAW_REPL;
425444
mp_hal_stdout_tx_str("\r\n");
@@ -529,10 +548,10 @@ int pyexec_raw_repl(void) {
529548
mp_hal_stdout_tx_str(">");
530549
for (;;) {
531550
intc=mp_hal_stdin_rx_chr();
532-
if (c==CHAR_CTRL_A) {
551+
if (c==RAW_REPL_CTRL_INIT) {
533552
// reset raw REPL
534-
if (vstr_len(&line)==2&&vstr_str(&line)[0]==CHAR_CTRL_E) {
535-
intret=do_reader_stdin(vstr_str(&line)[1]);
553+
if (vstr_len(&line)==2&&vstr_str(&line)[0]==RAW_REPL_CTRL_INIT_CMD) {
554+
intret=handle_raw_repl_init_cmd(vstr_str(&line)[1]);
536555
if (ret&PYEXEC_FORCED_EXIT) {
537556
returnret;
538557
}
@@ -541,16 +560,16 @@ int pyexec_raw_repl(void) {
541560
continue;
542561
}
543562
gotoraw_repl_reset;
544-
}elseif (c==CHAR_CTRL_B) {
563+
}elseif (c==RAW_REPL_CTRL_EXIT_TO_FRIENDLY) {
545564
// change to friendly REPL
546565
mp_hal_stdout_tx_str("\r\n");
547566
vstr_clear(&line);
548567
pyexec_mode_kind=PYEXEC_MODE_FRIENDLY_REPL;
549568
return0;
550-
}elseif (c==CHAR_CTRL_C) {
569+
}elseif (c==RAW_REPL_CTRL_CLEAR_LINE) {
551570
// clear line
552571
vstr_reset(&line);
553-
}elseif (c==CHAR_CTRL_D) {
572+
}elseif (c==RAW_REPL_CTRL_EOF) {
554573
// input finished
555574
break;
556575
}else {
@@ -632,7 +651,7 @@ int pyexec_friendly_repl(void) {
632651
intret=readline(&line,mp_repl_get_ps1());
633652
mp_parse_input_kind_tparse_input_kind=MP_PARSE_SINGLE_INPUT;
634653

635-
if (ret==CHAR_CTRL_A) {
654+
if (ret==RAW_REPL_CTRL_INIT) {
636655
// change to raw REPL
637656
mp_hal_stdout_tx_str("\r\n");
638657
vstr_clear(&line);

‎tools/pyboard.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,10 @@ def raw_paste_write(self, command_bytes):
407407
# escape any characters that need to be escaped. Note this doesn't
408408
# count towards the window size, as unescaping happens before filling
409409
# the window buffer in the device
410-
NUM_ESCAPED=8#this valueshas to matchvalue in pyexec.c
410+
RAWCODE_PASTE_NUM_ESCAPED=8#valuehas to matchthe same constant in pyexec.c
411411
b=re.sub(
412-
rb"["+bytes(range(NUM_ESCAPED))+rb"]",
413-
lambdac:bytes((0x06,c.group()[0]+NUM_ESCAPED)),
412+
rb"["+bytes(range(RAWCODE_PASTE_NUM_ESCAPED))+rb"]",
413+
lambdac:bytes((0x06,c.group()[0]+RAWCODE_PASTE_NUM_ESCAPED)),
414414
b,
415415
)
416416

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp