From 06a1d2e33d3186d01b07548b2b3f4c78de7e4b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Wed, 29 Mar 2023 12:16:47 +0200 Subject: [PATCH] 'Eval()' now can evaluate several expressions at once, provided that they are separated by newlines. The value of the last expression is returned. [INTERPRETER] * NEW: Support for the new Eval() feature. [GB.EVAL] * NEW: 'Eval()' now can evaluate several expressions at once, provided that they are separated by newlines. The value of the last expression is returned. * NEW: 'Eval()' now returns NULL if its last expression returns nothing. --- main/gbx/gbx_exec_loop.c | 7 +++++- main/lib/eval/eval.c | 47 ++++++++++++++++++++++----------------- main/lib/eval/eval_read.c | 8 +++---- main/lib/eval/eval_read.h | 4 ++-- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/main/gbx/gbx_exec_loop.c b/main/gbx/gbx_exec_loop.c index 38117cdd0..bf8e47ad5 100644 --- a/main/gbx/gbx_exec_loop.c +++ b/main/gbx/gbx_exec_loop.c @@ -1145,7 +1145,7 @@ _JUMP_IF_FALSE_FAST: _RETURN: { - static const void *return_jump[] = { &&__RETURN_GOSUB, &&__RETURN_VALUE, &&__RETURN_VOID, &&__INIT_BYTECODE_CHECK }; + static const void *return_jump[] = { &&__RETURN_GOSUB, &&__RETURN_VALUE, &&__RETURN_VOID, &&__INIT_BYTECODE_CHECK, &&__RETURN_VALUE_OR_VOID }; goto *return_jump[GET_UX()]; @@ -1171,6 +1171,11 @@ _RETURN: goto _MAIN; + __RETURN_VALUE_OR_VOID: + + if (SP[-1].type == T_VOID) + goto __RETURN_VOID; + __RETURN_VALUE: VALUE_conv(&SP[-1], FP->type); diff --git a/main/lib/eval/eval.c b/main/lib/eval/eval.c index 8234f3634..1c64f1f55 100644 --- a/main/lib/eval/eval.c +++ b/main/lib/eval/eval.c @@ -136,9 +136,10 @@ void EVAL_start(EXPRESSION *expr) } } -bool EVAL_compile(EXPRESSION *expr, bool assign) +bool EVAL_compile(EXPRESSION *expr, bool force_assign) { bool error = FALSE; + bool assign; EVAL = expr; @@ -155,25 +156,33 @@ bool EVAL_compile(EXPRESSION *expr, bool assign) EVAL->current = EVAL->pattern; - if (PATTERN_is(*EVAL->current, RS_LET)) + for(;;) { - EVAL->current++; - assign = TRUE; + if (PATTERN_is(*EVAL->current, RS_LET)) + { + EVAL->current++; + assign = TRUE; + } + else + assign = force_assign; + + if (assign) + { + if (!TRANS_affectation()) + THROW(E_SYNTAX); + } + else + TRANS_expression(); + + if (PATTERN_is_end(*EVAL->current)) + { + CODE_return(4); + break; + } + else + CODE_drop(); } - if (assign) - { - if (!TRANS_affectation()) - THROW(E_SYNTAX); - } - else - TRANS_expression(); - - if (!PATTERN_is_end(*EVAL->current)) - THROW(E_SYNTAX); - - CODE_return(1); - EVAL->stack_usage = CODE_stack_usage; } CATCH @@ -213,10 +222,8 @@ void EVAL_new(EXPRESSION **expr, char *src, int len) { GB.Alloc((void **)expr, sizeof(EXPRESSION)); CLEAR(*expr); - (*expr)->source = GB.NewString(src, len); - (*expr)->source = GB.AddString((*expr)->source, "\n\0", 2); + (*expr)->source = GB.AddString(GB.NewString(src, len), "\n\0", 2); (*expr)->len = len + 2; - /*(*expr)->option = option;*/ } diff --git a/main/lib/eval/eval_read.c b/main/lib/eval/eval_read.c index 381797538..1a520dd75 100644 --- a/main/lib/eval/eval_read.c +++ b/main/lib/eval/eval_read.c @@ -34,7 +34,7 @@ //#define DEBUG 1 -PUBLIC const char *READ_source_ptr; +const char *READ_source_ptr; #define source_ptr READ_source_ptr static bool is_init = FALSE; @@ -113,7 +113,7 @@ static bool _no_quote = FALSE; #define BUF_MAX 255 static char _buffer[BUF_MAX + 1]; -PUBLIC char *READ_get_pattern(PATTERN *pattern) +char *READ_get_pattern(PATTERN *pattern) { int type = PATTERN_type(*pattern); int index = PATTERN_index(*pattern); @@ -177,7 +177,7 @@ PUBLIC char *READ_get_pattern(PATTERN *pattern) } #if DEBUG -PUBLIC void READ_dump_pattern(PATTERN *pattern) +void READ_dump_pattern(PATTERN *pattern) { int type = PATTERN_type(*pattern); int index = PATTERN_index(*pattern); @@ -623,7 +623,7 @@ static void add_spaces() } -PUBLIC void EVAL_read(void) +void EVAL_read(void) { static const void *jump_char[12] = { diff --git a/main/lib/eval/eval_read.h b/main/lib/eval/eval_read.h index f4dec483c..adaf86655 100644 --- a/main/lib/eval/eval_read.h +++ b/main/lib/eval/eval_read.h @@ -30,7 +30,7 @@ EXTERN const char *READ_source_ptr; #endif -PUBLIC void EVAL_read(void); -PUBLIC char *READ_get_pattern(PATTERN *pattern); +void EVAL_read(void); +char *READ_get_pattern(PATTERN *pattern); #endif