From 0aedf99a7887ca021bfaffc4590c61945722ed9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Sun, 17 Nov 2024 16:07:46 +0100 Subject: [PATCH] [stdlib] add group text by tags, defined --- anselme/ast/Text.lua | 10 +++++- anselme/stdlib/environment.lua | 15 ++++++++- anselme/stdlib/language/frFR.lua | 4 +++ anselme/stdlib/text.lua | 34 +++++++++++++++++++ doc/standard_library.md | 56 +++++++++++++++++++++++++------- 5 files changed, 105 insertions(+), 14 deletions(-) diff --git a/anselme/ast/Text.lua b/anselme/ast/Text.lua index 95c93dc..3ce38d1 100644 --- a/anselme/ast/Text.lua +++ b/anselme/ast/Text.lua @@ -5,6 +5,8 @@ local ArgumentTuple, Struct local to_anselme = require("anselme.common.to_anselme") +local group_text_by_tag_identifier + --- A Lua-friendly representation of an Anselme Text value. -- They appear in both TextEventData and ChoiceEventData to represent the text that has to be shown. -- @@ -191,11 +193,17 @@ Text = Runtime(Event) { for _, text in event_buffer:iter(state) do table.insert(l, text:to_lua(state)) end - return l + if state.scope:defined(group_text_by_tag_identifier) then + local tag_key = state.scope:get(group_text_by_tag_identifier) + return l:group_by(tag_key) + else + return { l } + end end, } package.loaded[...] = Text ArgumentTuple, Struct = ast.ArgumentTuple, ast.Struct +group_text_by_tag_identifier = ast.Identifier:new("_group_text_by_tag") return Text diff --git a/anselme/stdlib/environment.lua b/anselme/stdlib/environment.lua index 06b2c85..27483b1 100644 --- a/anselme/stdlib/environment.lua +++ b/anselme/stdlib/environment.lua @@ -16,7 +16,20 @@ return { if l:truthy() then return Boolean:new(env:defined(state, s:to_identifier())) else - return Boolean:new(env:defined_in_current(state, s:to_identifier())) + return Boolean:new(env:defined_in_current(state, s:to_symbol())) + end + end + }, + { + --- Returns true if the variable named `var` is defined in in the current scope, false otherwise. + -- + -- If `search parent` is true, this will also search in parent scopes of the current scope. + "defined", "(var::is string, search parent::is boolean=true)", + function(state, s, l) + if l:truthy() then + return Boolean:new(state.scope:defined(s:to_identifier())) + else + return Boolean:new(state.scope:defined_in_current(s:to_symbol())) end end }, diff --git a/anselme/stdlib/language/frFR.lua b/anselme/stdlib/language/frFR.lua index 3e4242e..4b771b4 100644 --- a/anselme/stdlib/language/frFR.lua +++ b/anselme/stdlib/language/frFR.lua @@ -1,6 +1,8 @@ -- TODO: doc in other language return [[ +:@format = stdlib.format + :@bloc attaché = stdlib.attached block :@afficher = stdlib.print @@ -83,4 +85,6 @@ return [[ :@persister = stdlib.persist :@écrire choix = stdlib.write choice + +:@grouper texte par tag = stdlib.group text by tag ]] diff --git a/anselme/stdlib/text.lua b/anselme/stdlib/text.lua index 5d42117..8ad5544 100644 --- a/anselme/stdlib/text.lua +++ b/anselme/stdlib/text.lua @@ -9,6 +9,9 @@ local translation_manager = require("anselme.state.translation_manager") local tag_manager = require("anselme.state.tag_manager") local resume_manager = require("anselme.state.resume_manager") +local group_text_by_tag_identifier = Identifier:new("_group_text_by_tag") +local group_text_by_tag_symbol = group_text_by_tag_identifier:to_symbol { exported = true } + return { -- text { @@ -41,6 +44,37 @@ return { end }, + { + --- Cause future text events to be each split into separate text event every time the value of the tag with the key `tag_key` changes. + -- + -- For example, with the following Anselme script: + -- ``` + -- group text by tag("speaker") + -- speaker: "John" # + -- | A + -- | B + -- speaker: "Lana" # + -- | C + -- speaker: "John" # + -- | D + -- ``` + -- will produce three separate text events instead of one: + -- * the first with the texts "A" and "B"; both with the tag `speaker="John"` + -- * the second with the text "C"; with the tag `speaker="Lana"` + -- * the last with the text "D"; wiith the tag `speaker="John"` + -- + -- This setting affect will affect the whole state. + "group text by tag", "(tag::is string)", + function(state, tag) + if not state.scope:defined(group_text_by_tag_identifier) then + state.scope:define(group_text_by_tag_symbol, tag) + else + state.scope:set(group_text_by_tag_identifier, tag) + end + return Nil:new() + end + }, + -- choice { --- Write a choice event to the event buffer using this text and `fn` as the function to call if the choice is selected. diff --git a/doc/standard_library.md b/doc/standard_library.md index 114091b..7298246 100644 --- a/doc/standard_library.md +++ b/doc/standard_library.md @@ -650,19 +650,43 @@ _defined at line 21 of [anselme/stdlib/string.lua](../anselme/stdlib/string.lua) Concatenate two texts, returning a new text value. -_defined at line 16 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_+_", "(a::is text, b::is text)",` +_defined at line 19 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_+_", "(a::is text, b::is text)",` ### txt::is text ! Write a text event in the event buffer using this text. -_defined at line 30 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_!", "(txt::is text)",` +_defined at line 33 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_!", "(txt::is text)",` ### tag (txt::is text, tags::is struct) Create and return a new text from `text`, with the tags from `tags` added. -_defined at line 38 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"tag", "(txt::is text, tags::is struct)",` +_defined at line 41 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"tag", "(txt::is text, tags::is struct)",` + +### group text by tag (tag::is string) + +Cause future text events to be each split into separate text event every time the value of the tag with the key `tag_key` changes. + +For example, with the following Anselme script: +``` +group text by tag("speaker") +speaker: "John" # + | A + | B +speaker: "Lana" # + | C +speaker: "John" # + | D +``` +will produce three separate text events instead of one: +* the first with the texts "A" and "B"; both with the tag `speaker="John"` +* the second with the text "C"; with the tag `speaker="Lana"` +* the last with the text "D"; wiith the tag `speaker="John"` + +This setting affect will affect the whole state. + +_defined at line 67 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"group text by tag", "(tag::is string)",` ### write choice (text::is text, fn=attached block(keep return=true, default=($()()))) @@ -678,13 +702,13 @@ write choice(| Choice |, $42) If we are currently resuming to an anchor contained in `fn`, `fn` is directly called and the current choice event buffer will be discarded on flush, simulating the choice event buffer being sent to the host game and this choice being selected. -_defined at line 57 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"write choice", "(text::is text, fn=attached block(keep return=true, default=($()())))",` +_defined at line 91 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"write choice", "(text::is text, fn=attached block(keep return=true, default=($()())))",` ### original -> translated Add a translation so `original` is replaced with `translated`. -_defined at line 74 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_->_", "(original::is(\"quote\"), translated::is(\"quote\"))",` +_defined at line 108 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_->_", "(original::is(\"quote\"), translated::is(\"quote\"))",` # Symbols @@ -1135,23 +1159,31 @@ If `search parent` is true, this will also search in parent scopes of the enviro _defined at line 14 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"defined", "(env::is environment, var::is string, search parent::is boolean=false)",` +### defined (var::is string, search parent::is boolean=true) + +Returns true if the variable named `var` is defined in in the current scope, false otherwise. + +If `search parent` is true, this will also search in parent scopes of the current scope. + +_defined at line 27 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"defined", "(var::is string, search parent::is boolean=true)",` + ### c::is environment . s::is string Gets the variable named `s` defined in the environment `c`. -_defined at line 26 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is string)",` +_defined at line 39 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is string)",` ### c::is environment . s::is string = v Sets the variable named `s` defined in the environment `c` to `v`. -_defined at line 35 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is string) = v",` +_defined at line 48 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is string) = v",` ### c::is environment . s::is symbol = v Define a new variable `s` in the environment `c` with the value `v`. -_defined at line 45 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is symbol) = v",` +_defined at line 58 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is symbol) = v",` ### import (env::is environment, symbol tuple::is tuple) @@ -1164,7 +1196,7 @@ import(env, [:a, :b]) :b = env.b ``` -_defined at line 63 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"import", "(env::is environment, symbol tuple::is tuple)",` +_defined at line 76 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"import", "(env::is environment, symbol tuple::is tuple)",` ### import (env::is environment, symbol::is symbol) @@ -1176,14 +1208,14 @@ import(env, :a) :a = env.a ``` -_defined at line 79 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"import", "(env::is environment, symbol::is symbol)",` +_defined at line 92 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"import", "(env::is environment, symbol::is symbol)",` ### load (path::is string) Load an Anselme script from a file and run it. Returns the environment containing the exported variables from the file. -_defined at line 89 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"load", "(path::is string)",` +_defined at line 102 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"load", "(path::is string)",` # Typed values @@ -1336,4 +1368,4 @@ _defined at line 14 of [anselme/stdlib/wrap.lua](../anselme/stdlib/wrap.lua):_ ` --- -_file generated at 2024-11-09T16:02:36Z_ \ No newline at end of file +_file generated at 2024-11-17T15:00:50Z_ \ No newline at end of file