From 097bb1fb97b9a4da82d191d316fa821d74d12152 Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Mon, 12 Jan 2026 19:46:26 +0000 Subject: Fixed bug within REPL mode --- src/repl/repl.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 2 deletions(-) (limited to 'src/repl') diff --git a/src/repl/repl.c b/src/repl/repl.c index 9027efd..c259bff 100644 --- a/src/repl/repl.c +++ b/src/repl/repl.c @@ -1195,7 +1195,101 @@ void run_repl(const char *self_path) if (history_len > 0 && !is_header_line(history[history_len - 1])) { - strcat(full_code, history[history_len - 1]); + char *last_line = history[history_len - 1]; + + char *check_buf = malloc(strlen(last_line) + 2); + strcpy(check_buf, last_line); + strcat(check_buf, ";"); + + ParserContext ctx = {0}; + ctx.is_repl = 1; + ctx.skip_preamble = 1; + Lexer l; + lexer_init(&l, check_buf); + ASTNode *node = parse_statement(&ctx, &l); + free(check_buf); + + int is_expr = 0; + if (node) + { + ASTNode *child = node; + if (child->type == NODE_EXPR_BINARY || child->type == NODE_EXPR_UNARY || + child->type == NODE_EXPR_LITERAL || child->type == NODE_EXPR_VAR || + child->type == NODE_EXPR_CALL || child->type == NODE_EXPR_MEMBER || + child->type == NODE_EXPR_INDEX || child->type == NODE_EXPR_CAST || + child->type == NODE_EXPR_SIZEOF || child->type == NODE_EXPR_STRUCT_INIT || + child->type == NODE_EXPR_ARRAY_LITERAL || + child->type == NODE_EXPR_SLICE || child->type == NODE_TERNARY || + child->type == NODE_MATCH) + { + is_expr = 1; + } + } + + if (is_expr) + { + size_t probesz = 4096; + for(int i=0; i&1", self_path, p_path); + + FILE *pp = popen(p_cmd, "r"); + int is_void = 0; + if (pp) { + char buf[1024]; + while(fgets(buf, sizeof(buf), pp)) { + if (strstr(buf, "void") && strstr(buf, "expression")) { + is_void = 1; + } + } + pclose(pp); + } + + if (!is_void) { + strcat(full_code, "println \"{"); + strcat(full_code, last_line); + strcat(full_code, "}\";"); + } else { + strcat(full_code, last_line); + } + } else { + strcat(full_code, last_line); + } + free(probe_code); + } + else + { + strcat(full_code, last_line); + } } if (watches_len > 0) @@ -1229,7 +1323,7 @@ void run_repl(const char *self_path) free(full_code); char cmd[2048]; - sprintf(cmd, "%s run -q --repl %s", self_path, tmp_path); + sprintf(cmd, "%s run -q %s", self_path, tmp_path); int ret = system(cmd); printf("\n"); -- cgit v1.2.3