1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 08:39:30 +00:00

[language] automatically call text when they appear directly as a statement; remove autocalling of every text returned by a statement

The previous behavior, where any Text value returned by a line in a Block would be automatically called, may lead to unexpected Text event being written as it is not obvious which line can return a Text value after evaluation.

The new behavior only triggers if a Text node directly appear in the original script as a statement.
This commit is contained in:
Étienne Fildadut 2024-04-23 19:57:36 +02:00
parent 991f9fed35
commit b534a3c4a2
21 changed files with 80 additions and 40 deletions

View file

@ -1,5 +1,5 @@
local ast = require("anselme.ast")
local Nil, Return, AutoCall, ArgumentTuple, Flush
local Nil, Return, Flush
local resume_manager = require("anselme.state.resume_manager")
@ -43,10 +43,7 @@ local Block = ast.abstract.Node {
for _, e in ipairs(self.expressions) do
if e:contains_resume_target(target) then resumed = true end
if resumed then
r = e:eval(state)
if AutoCall:issub(r) then
r = r:call(state, ArgumentTuple:new())
end
r = e:eval_statement(state)
if Return:is(r) then
break -- pass on to parent block until we reach a function boundary
end
@ -54,10 +51,7 @@ local Block = ast.abstract.Node {
end
else
for _, e in ipairs(self.expressions) do
r = e:eval(state)
if AutoCall:issub(r) then
r = r:call(state, ArgumentTuple:new())
end
r = e:eval_statement(state)
if Return:is(r) then
break -- pass on to parent block until we reach a function boundary
end
@ -69,6 +63,6 @@ local Block = ast.abstract.Node {
}
package.loaded[...] = Block
Nil, Return, AutoCall, ArgumentTuple, Flush = ast.Nil, ast.Return, ast.abstract.AutoCall, ast.ArgumentTuple, ast.Flush
Nil, Return, Flush = ast.Nil, ast.Return, ast.Flush
return Block

View file

@ -1,7 +1,8 @@
local ast = require("anselme.ast")
local AutoCall, Event, Runtime = ast.abstract.AutoCall, ast.abstract.Event, ast.abstract.Runtime
local Event, Runtime = ast.abstract.Event, ast.abstract.Runtime
local ArgumentTuple
return Runtime(AutoCall, Event) {
local Text = Runtime(Event) {
type = "text",
list = nil, -- { { String, tag Table }, ... }
@ -28,9 +29,19 @@ return Runtime(AutoCall, Event) {
return ("| %s |"):format(table.concat(t, " "))
end,
-- autocall when used directly as a statement
eval_statement = function(self, state)
return self:call(state, ArgumentTuple:new())
end,
-- Text comes from TextInterpolation which already evals the contents
to_event_data = function(self)
return self
end
}
package.loaded[...] = Text
ArgumentTuple = ast.ArgumentTuple
return Text

View file

@ -42,6 +42,10 @@ local Translatable = ast.abstract.Node {
_eval = function(self, state)
return translation_manager:eval(state, self.context, self)
end,
-- pass on eval_statement state to the translated node
eval_statement = function(self, state)
return translation_manager:eval(state, self.context, self):eval_statement(state)
end,
list_translatable = function(self, t)
t = t or {}

View file

@ -1,8 +0,0 @@
-- called automatically when returned by one of the expression in a block
local ast = require("anselme.ast")
return ast.abstract.Node {
type = "auto call",
init = false
}

View file

@ -106,6 +106,11 @@ Node = class {
error(format_error(state, self, r), 0)
end
end,
-- same as eval but called on the top node of a statement (i.e. a line of a block)
-- redefine if the statement behavior should be different
eval_statement = function(self, state)
return self:eval(state)
end,
_evaluated = false, -- if true, node is assumed to be already evaluated and :eval will be the identity function
-- evaluate this node and return the result
-- by default assume the node can't be evaluated further and return itself; redefine for everything else, probably

View file

@ -1,7 +1,7 @@
local class = require("anselme.lib.class")
local ast = require("anselme.ast")
local Table, Identifier
local Table, Identifier, Translatable
local translations_identifier, translations_symbol
@ -65,12 +65,16 @@ local translation_manager = class {
end
-- no matching translation
return original.expression:eval(state)
if Translatable:is(original) then
return original.expression:eval(state)
else
return original:eval(state)
end
end,
}
package.loaded[...] = translation_manager
Table, Identifier = ast.Table, ast.Identifier
Table, Identifier, Translatable = ast.Table, ast.Identifier, ast.Translatable
translations_identifier = Identifier:new("_translations") -- Table of { Translatable = Table{ Struct context = translated node, ... }, ... }
translations_symbol = translations_identifier:to_symbol()

View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {}"Hello" |
--- text ---
| {}"Hello" |
--- text ---
| {}"Wor" {}"ld" |
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
| Hello
| Wor | + | ld |
(| Hello)!
(| Wor | + | ld |)!

View file

@ -1,7 +1,8 @@
:@choice=1
:$ jump button
1 # |A
1 #
|A
*| Suprise choice!
()
return("JOIN")

View file

@ -1,7 +1,8 @@
:@choice=1
:$ jump button
1 # | a
1 #
| a
return("SPLIT")

View file

@ -2,7 +2,8 @@
return(1 # | A)
:$ move axis
1 # | left
1 #
| left
return(" joystick")
*| Press {jump button!} to jump.

View file

@ -1,3 +1,3 @@
if((), $|ko)
if(1, $|ok)
if(1, $|ok bis)
if((), $|ko|!)
if(1, $|ok|!)
if(1, $|ok bis|!)

View file

@ -4,7 +4,8 @@
if(1, $()("x":"x" # _))
"b":"b" #
#p!checkpoint($_)
"c":"c"# |b
"c":"c"#
|b
|c

View file

@ -1,4 +1,4 @@
1 #
|foo
if(1, $()("b":[1,2] # _))
if(1, $()("a":[2,3] # | bar))
if(1, $()("a":[2,3] # | bar |!))

View file

@ -1,3 +1,4 @@
1 #
| foo
"a":[2,3] # | bar
"a":[2,3] #
| bar

View file

@ -1,5 +1,6 @@
:$ f
1 # | lol
1 #
| lol
return(2 # |d)

View file

@ -2,4 +2,4 @@
:b = ("b":"c" # | world and { "x":"y" # |friends })
a+b
(a+b)!

View file

@ -1,12 +1,14 @@
:@choice = 1
:$ jump button
1 # | A
1 #
| A
*| Surprise choice!
| ok
:$ move axis
1 # | left
1 #
| left
*| Surprise choice!
| ok2
return(" joystick")

View file

@ -1,10 +1,12 @@
:$ jump button
1 # | A
1 #
| A
return!
:$ move axis
1 # | left
1 #
| left
return(" joystick")

View file

@ -2,7 +2,8 @@
return(1 # | A)
:$ move axis
2 # | left
2 #
| left
return(" joystick")
| Press {jump button!} to jump.

View file

@ -1,6 +1,7 @@
: x = "x"!script($_)
|a
if(run == 0, $|seen only once)
if(run == 0)
|seen only once
|b
x!