From a184d4c5e32f6ca88525b4217ddfb49489bd2bc2 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sun, 9 Feb 2025 15:03:11 +0100 Subject: [PATCH] You can now return a constant string Rather than having to store it as a variable --- 0-lang0py/lang0py.py | 5 ++++- 1-lang0py/lang0py.lang0 | 10 +++++++++- 2-lang0c/lang0c.lang0 | 12 +++++++++++- tests/flowreturnvalueconstant.lang0 | 9 +++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/flowreturnvalueconstant.lang0 diff --git a/0-lang0py/lang0py.py b/0-lang0py/lang0py.py index 089888f..adfed09 100644 --- a/0-lang0py/lang0py.py +++ b/0-lang0py/lang0py.py @@ -162,7 +162,10 @@ def parsestatreturn(indent): emit('return ') if ' ' == peek(): skip() - parseexprvarref() + if quote == peek(): + parseconststring() + else: + parseexprvarref() emit(eol) skipchar(eol) diff --git a/1-lang0py/lang0py.lang0 b/1-lang0py/lang0py.lang0 index 0628b7b..cbdd484 100644 --- a/1-lang0py/lang0py.lang0 +++ b/1-lang0py/lang0py.lang0 @@ -151,7 +151,15 @@ parsestatreturn indent: calc isspace eq char " " if isspace skip - parseexprvarref + calc char peek + calc isquote eq char quote + calc isnotquote not isquote + if isquote + parseconststring + / + if isnotquote + parseexprvarref + / / emit eol skipchar eol diff --git a/2-lang0c/lang0c.lang0 b/2-lang0c/lang0c.lang0 index 7b6cbca..995ef4e 100644 --- a/2-lang0c/lang0c.lang0 +++ b/2-lang0c/lang0c.lang0 @@ -195,6 +195,8 @@ parsestatbreak indent: parsestatreturn indent: declare char declare isspace + declare isnotquote + declare isquote declare isnotspace emit indent emit "return " @@ -203,7 +205,15 @@ parsestatreturn indent: calc isnotspace not isspace if isspace skip - parseexprvarref + calc char peek + calc isquote eq char quote + calc isnotquote not isquote + if isquote + parseconststring + / + if isnotquote + parseexprvarref + / / if isnotspace emit "0" diff --git a/tests/flowreturnvalueconstant.lang0 b/tests/flowreturnvalueconstant.lang0 new file mode 100644 index 0000000..4c23208 --- /dev/null +++ b/tests/flowreturnvalueconstant.lang0 @@ -0,0 +1,9 @@ +func: + return "Success" +/ + +main: + declare result + calc result func + emit result +/