diff --git a/anselme/ast/Block.lua b/anselme/ast/Block.lua index a7f46d5..caa3f40 100644 --- a/anselme/ast/Block.lua +++ b/anselme/ast/Block.lua @@ -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 diff --git a/anselme/ast/Text.lua b/anselme/ast/Text.lua index ce782df..eb7329e 100644 --- a/anselme/ast/Text.lua +++ b/anselme/ast/Text.lua @@ -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 diff --git a/anselme/ast/Translatable.lua b/anselme/ast/Translatable.lua index c1f4487..add771e 100644 --- a/anselme/ast/Translatable.lua +++ b/anselme/ast/Translatable.lua @@ -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 {} diff --git a/anselme/ast/abstract/AutoCall.lua b/anselme/ast/abstract/AutoCall.lua deleted file mode 100644 index d27ec89..0000000 --- a/anselme/ast/abstract/AutoCall.lua +++ /dev/null @@ -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 -} diff --git a/anselme/ast/abstract/Node.lua b/anselme/ast/abstract/Node.lua index 1b510bf..57f0093 100644 --- a/anselme/ast/abstract/Node.lua +++ b/anselme/ast/abstract/Node.lua @@ -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 diff --git a/anselme/state/translation_manager.lua b/anselme/state/translation_manager.lua index 751f3cf..0b7c2c7 100644 --- a/anselme/state/translation_manager.lua +++ b/anselme/state/translation_manager.lua @@ -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() diff --git a/test/results/autocall text statement.ans b/test/results/autocall text statement.ans new file mode 100644 index 0000000..deaa764 --- /dev/null +++ b/test/results/autocall text statement.ans @@ -0,0 +1,11 @@ +--# run #-- +--- text --- +| {}"Hello" | +--- text --- +| {}"Hello" | +--- text --- +| {}"Wor" {}"ld" | +--- return --- +() +--# saved #-- +{} \ No newline at end of file diff --git a/test/tests/autocall text statement.ans b/test/tests/autocall text statement.ans new file mode 100644 index 0000000..698b949 --- /dev/null +++ b/test/tests/autocall text statement.ans @@ -0,0 +1,7 @@ +| Hello + +| Wor | + | ld | + +(| Hello)! + +(| Wor | + | ld |)! diff --git a/test/tests/choice line interpolation with choice event.ans b/test/tests/choice line interpolation with choice event.ans index e99783e..9f8537a 100644 --- a/test/tests/choice line interpolation with choice event.ans +++ b/test/tests/choice line interpolation with choice event.ans @@ -1,7 +1,8 @@ :@choice=1 :$ jump button - 1 # |A + 1 # + |A *| Suprise choice! () return("JOIN") diff --git a/test/tests/choice line interpolation with event flush.ans b/test/tests/choice line interpolation with event flush.ans index de2b3b9..a616891 100644 --- a/test/tests/choice line interpolation with event flush.ans +++ b/test/tests/choice line interpolation with event flush.ans @@ -1,7 +1,8 @@ :@choice=1 :$ jump button - 1 # | a + 1 # + | a return("SPLIT") diff --git a/test/tests/choice line interpolation with text event.ans b/test/tests/choice line interpolation with text event.ans index 95ef233..69a6db9 100644 --- a/test/tests/choice line interpolation with text event.ans +++ b/test/tests/choice line interpolation with text event.ans @@ -2,7 +2,8 @@ return(1 # | A) :$ move axis - 1 # | left + 1 # + | left return(" joystick") *| Press {jump button!} to jump. diff --git a/test/tests/condition decorator.ans b/test/tests/condition decorator.ans index 22a131f..3469234 100644 --- a/test/tests/condition decorator.ans +++ b/test/tests/condition decorator.ans @@ -1,3 +1,3 @@ -if((), $|ko) -if(1, $|ok) -if(1, $|ok bis) \ No newline at end of file +if((), $|ko|!) +if(1, $|ok|!) +if(1, $|ok bis|!) \ No newline at end of file diff --git a/test/tests/resume from paragraph restore tags.ans b/test/tests/resume from paragraph restore tags.ans index c7c16a0..0a8af95 100644 --- a/test/tests/resume from paragraph restore tags.ans +++ b/test/tests/resume from paragraph restore tags.ans @@ -4,7 +4,8 @@ if(1, $()("x":"x" # _)) "b":"b" # #p!checkpoint($_) - "c":"c"# |b + "c":"c"# + |b |c diff --git a/test/tests/tag decorator nested.ans b/test/tests/tag decorator nested.ans index ada213a..1708ad9 100644 --- a/test/tests/tag decorator nested.ans +++ b/test/tests/tag decorator nested.ans @@ -1,4 +1,4 @@ 1 # |foo if(1, $()("b":[1,2] # _)) - if(1, $()("a":[2,3] # | bar)) + if(1, $()("a":[2,3] # | bar |!)) diff --git a/test/tests/tag decorator.ans b/test/tests/tag decorator.ans index 1f12920..a3df7b5 100644 --- a/test/tests/tag decorator.ans +++ b/test/tests/tag decorator.ans @@ -1,3 +1,4 @@ 1 # | foo - "a":[2,3] # | bar + "a":[2,3] # + | bar diff --git a/test/tests/text buffer with tags.ans b/test/tests/text buffer with tags.ans index cf2400f..a28e665 100644 --- a/test/tests/text buffer with tags.ans +++ b/test/tests/text buffer with tags.ans @@ -1,5 +1,6 @@ :$ f - 1 # | lol + 1 # + | lol return(2 # |d) diff --git a/test/tests/text concat.ans b/test/tests/text concat.ans index 04b38cf..be8d821 100644 --- a/test/tests/text concat.ans +++ b/test/tests/text concat.ans @@ -2,4 +2,4 @@ :b = ("b":"c" # | world and { "x":"y" # |friends }) -a+b +(a+b)! diff --git a/test/tests/text line interpolation with choice event.ans b/test/tests/text line interpolation with choice event.ans index 66a8b92..5154262 100644 --- a/test/tests/text line interpolation with choice event.ans +++ b/test/tests/text line interpolation with choice event.ans @@ -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") diff --git a/test/tests/text line interpolation with event flush.ans b/test/tests/text line interpolation with event flush.ans index 1296a10..e636316 100644 --- a/test/tests/text line interpolation with event flush.ans +++ b/test/tests/text line interpolation with event flush.ans @@ -1,10 +1,12 @@ :$ jump button - 1 # | A + 1 # + | A return! :$ move axis - 1 # | left + 1 # + | left return(" joystick") diff --git a/test/tests/text line interpolation with text event.ans b/test/tests/text line interpolation with text event.ans index d7dd431..46a4d1e 100644 --- a/test/tests/text line interpolation with text event.ans +++ b/test/tests/text line interpolation with text event.ans @@ -2,7 +2,8 @@ return(1 # | A) :$ move axis - 2 # | left + 2 # + | left return(" joystick") | Press {jump button!} to jump. diff --git a/test/tests/unseen line.ans b/test/tests/unseen line.ans index 9f45ee1..124756b 100644 --- a/test/tests/unseen line.ans +++ b/test/tests/unseen line.ans @@ -1,6 +1,7 @@ : x = "x"!script($_) |a - if(run == 0, $|seen only once) + if(run == 0) + |seen only once |b x!