From e454888b9f27e2689f5427b4d7cfebacd1b77784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Mon, 12 Apr 2021 17:36:18 +0200 Subject: [PATCH] Discard returns values from choices --- README.md | 19 +++++++++++++++++++ interpreter/interpreter.lua | 3 ++- test/tests/return in choice.ans | 10 ++++++++++ test/tests/return in choice.lua | 30 ++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/tests/return in choice.ans create mode 100644 test/tests/return in choice.lua diff --git a/README.md b/README.md index 6110267..1cadb08 100644 --- a/README.md +++ b/README.md @@ -264,6 +264,25 @@ $ hey {hey} = 5 ``` +Be careful when using `@` in a choice block. Choice blocks are not ran right as they are read, but at the next event flush (i.e. empty line). This means that if there is no flush in the function itself, the choice will be ran *after* the function has already been executed and returning a value at this point makes no sense: + +``` +$ f + > a + @1 + @2 + +(f will return 2 since the choice is run after the @2 line) +~ f = 2 + + Yes. + +(Choice block is actually ran right before the "Yes" line, when the choice event is flushed.) + +``` + +For this reason, Anselme will discard returns values sent from within a choice block. Returns inside choice block still have the expected behaviour of stopping the execution of the block. + * empty line: flush events, i.e., if there are any pending lines of text or choices, send them to your game. See [Event buffer](#event-buffer). This line always keep the same identation as the last non-empty line, so you don't need to put invisible whitespace on an empty-looking line. Is also automatically added at the end of a file. * regular text: write some text into the event buffer. Support [text interpolation](#text-interpolation). diff --git a/interpreter/interpreter.lua b/interpreter/interpreter.lua index 3c55eb6..209aa48 100644 --- a/interpreter/interpreter.lua +++ b/interpreter/interpreter.lua @@ -143,7 +143,8 @@ local function run_line(state, line) local v, e = run_block(state, choice.block) tags:pop(state) if e then return v, e end - if v then return v end + -- discard return value from choice block as the execution is delayed until an event flush + -- and we don't want to stop the execution of another function unexpectedly end end end diff --git a/test/tests/return in choice.ans b/test/tests/return in choice.ans new file mode 100644 index 0000000..1c8d28c --- /dev/null +++ b/test/tests/return in choice.ans @@ -0,0 +1,10 @@ +$ f + > a + x + @1 + y + @2 + +~ f = 2 + ~ choose(1) + Yes. diff --git a/test/tests/return in choice.lua b/test/tests/return in choice.lua new file mode 100644 index 0000000..365d386 --- /dev/null +++ b/test/tests/return in choice.lua @@ -0,0 +1,30 @@ +local _={} +_[13]={} +_[12]={} +_[11]={} +_[10]={data="Yes.",tags=_[13]} +_[9]={data="x",tags=_[12]} +_[8]={data="a",tags=_[11]} +_[7]={_[10]} +_[6]={_[9]} +_[5]={_[8]} +_[4]={"return"} +_[3]={"text",_[7]} +_[2]={"text",_[6]} +_[1]={"choice",_[5]} +return {_[1],_[2],_[3],_[4]} +--[[ +{ "choice", { { + data = "a", + tags = {} + } } } +{ "text", { { + data = "x", + tags = {} + } } } +{ "text", { { + data = "Yes.", + tags = {} + } } } +{ "return" } +]]-- \ No newline at end of file