From e0455902b0109d99aada495a7ec1381ee5d1444b Mon Sep 17 00:00:00 2001 From: cpunch Date: Thu, 28 Dec 2023 23:41:10 -0600 Subject: [PATCH] 'return' is now a valid statement outside of a function lets us do some cool things later. also the repl can print returned values. eg. `return "hello world"` will print ` "hello world"` to the console --- main.c | 12 ++++++++++++ src/cparse.c | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 9346503..afb43b6 100644 --- a/main.c +++ b/main.c @@ -50,9 +50,21 @@ static bool interpret(CState *state, const char *script, const char *mod) // cosmoV_compileString pushes the result onto the stack (COBJ_ERROR or COBJ_CLOSURE) if (cosmoV_compileString(state, script, mod)) { + if (!cosmoV_pcall(state, 0, 1)) { cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state))); return false; } + + // if the result is nil, we don't print it + if (IS_NIL(*cosmoV_getTop(state, 0))) { + cosmoV_pop(state); + return true; + } + + // otherwise, we print the result + cosmoV_printValue(*cosmoV_getTop(state, 0)); + printf("\n"); + cosmoV_pop(state); } else { cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state))); return false; diff --git a/src/cparse.c b/src/cparse.c index c7553a8..0353363 100644 --- a/src/cparse.c +++ b/src/cparse.c @@ -1471,10 +1471,10 @@ static void functionDeclaration(CParseState *pstate) static void returnStatement(CParseState *pstate) { - if (pstate->compiler->type != FTYPE_FUNCTION && pstate->compiler->type != FTYPE_METHOD) { - error(pstate, "Expected 'return' in function!"); - return; - } + // if (pstate->compiler->type != FTYPE_FUNCTION && pstate->compiler->type != FTYPE_METHOD) { + // error(pstate, "Expected 'return' in function!"); + // return; + // } if (blockFollow(pstate->current)) { // does this return have a value writeu8(pstate, OP_NIL);