mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-11 07:00:58 +08:00
parent
925ba3504d
commit
f980e91b60
15
cli/repl.c
15
cli/repl.c
@ -35,12 +35,15 @@ const char* read_line(uint32_t* length) {
|
|||||||
|
|
||||||
// Read a line from stdin and returns it without the line ending.
|
// Read a line from stdin and returns it without the line ending.
|
||||||
static void readLine(ByteBuffer* buff) {
|
static void readLine(ByteBuffer* buff) {
|
||||||
|
char c;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
char c = (char)fgetc(stdin);
|
c = (char)fgetc(stdin);
|
||||||
if (c == EOF || c == '\n') break;
|
if (c == '\n') break;
|
||||||
|
|
||||||
byteBufferWrite(buff, (uint8_t)c);
|
byteBufferWrite(buff, (uint8_t)c);
|
||||||
} while (true);
|
|
||||||
|
} while (c != EOF);
|
||||||
|
|
||||||
byteBufferWrite(buff, '\0');
|
byteBufferWrite(buff, '\0');
|
||||||
}
|
}
|
||||||
@ -92,6 +95,12 @@ int repl(PKVM* vm, const PkCompileOptions* options) {
|
|||||||
readLine(&line);
|
readLine(&line);
|
||||||
bool is_empty = is_str_empty((const char*)line.data);
|
bool is_empty = is_str_empty((const char*)line.data);
|
||||||
|
|
||||||
|
// If the line contains EOF, REPL should be stopped.
|
||||||
|
if (line.count >= 2 && *((char*)(line.data) + line.count -2) == EOF) {
|
||||||
|
fprintf(stdout, "\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// If the line is empty, we don't have to compile it.
|
// If the line is empty, we don't have to compile it.
|
||||||
if (is_empty && !need_more_lines) {
|
if (is_empty && !need_more_lines) {
|
||||||
byteBufferClear(&line);
|
byteBufferClear(&line);
|
||||||
|
Loading…
Reference in New Issue
Block a user