From e017a391ec279d90fe226eb072e6eff97dc8af11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Fri, 16 Sep 2022 21:55:06 +0900 Subject: [PATCH] Separate function and class injections --- anselme.lua | 2 ++ notes.txt | 2 ++ parser/common.lua | 3 ++- parser/preparser.lua | 37 +++++++++++++++++++++++++------------ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/anselme.lua b/anselme.lua index c0b3ccc..14da56b 100644 --- a/anselme.lua +++ b/anselme.lua @@ -494,6 +494,8 @@ local vm_mt = { -- * `"function return"`: injected at the end of each return's children that is contained in a non-scoped function -- * `"checkpoint start"`: injected at the start of every checkpoint -- * `"checkpoint end"`: injected at the end of every checkpoint + -- * `"class start"`: injected at the start of every class + -- * `"class end"`: injected at the end of every class -- * `"scoped function start"`: injected at the start of every scoped function -- * `"scoped function end"`: injected at the end of every scoped function -- * `"scoped function return"`: injected at the end of each return's children that is contained in a scoped function diff --git a/notes.txt b/notes.txt index 9b1e4a4..c1c621e 100644 --- a/notes.txt +++ b/notes.txt @@ -106,3 +106,5 @@ Disadvantages: * could do something like `$ ()(l::list(?), i::number)::?`, but then can't return nil on not found... TODO: write a translation guide/simplify translation process + +TODO: make injection nicer. Some decorator-like syntax? to select specific functions to inject to diff --git a/parser/common.lua b/parser/common.lua index b7adfff..788dfc0 100644 --- a/parser/common.lua +++ b/parser/common.lua @@ -66,7 +66,8 @@ common = { injections = { ["function start"] = "function_start", ["function end"] = "function_end", ["function return"] = "function_return", ["scoped function start"] = "scoped_function_start", ["scoped function end"] = "scoped_function_end", ["scoped function return"] = "scoped_function_return", - ["checkpoint start"] = "checkpoint_start", ["checkpoint end"] = "checkpoint_end" + ["checkpoint start"] = "checkpoint_start", ["checkpoint end"] = "checkpoint_end", + ["class start"] = "class_start", ["class end"] = "class_end" }, --- escape a string to be used as an exact match pattern escape = function(str) diff --git a/parser/preparser.lua b/parser/preparser.lua index 4b8cc9e..f24c574 100644 --- a/parser/preparser.lua +++ b/parser/preparser.lua @@ -225,26 +225,39 @@ local function parse_line(line, state, namespace, parent_function) table.insert(line.children, 1, { content = ":🔖=()", source = line.source }) end -- custom code injection - if r.scoped then - if state.inject.scoped_function_start then - for i, ll in ipairs(state.inject.scoped_function_start) do + if r.subtype == "class" then + if state.inject.class_start then + for i, ll in ipairs(state.inject.class_start) do table.insert(line.children, 1+i, copy(ll)) end end - if state.inject.scoped_function_end then - for _, ll in ipairs(state.inject.scoped_function_end) do + if state.inject.class_end then + for _, ll in ipairs(state.inject.class_end) do table.insert(line.children, copy(ll)) end end else - if state.inject.function_start then - for i, ll in ipairs(state.inject.function_start) do - table.insert(line.children, 1+i, copy(ll)) + if r.scoped then + if state.inject.scoped_function_start then + for i, ll in ipairs(state.inject.scoped_function_start) do + table.insert(line.children, 1+i, copy(ll)) + end end - end - if state.inject.function_end then - for _, ll in ipairs(state.inject.function_end) do - table.insert(line.children, copy(ll)) + if state.inject.scoped_function_end then + for _, ll in ipairs(state.inject.scoped_function_end) do + table.insert(line.children, copy(ll)) + end + end + else + if state.inject.function_start then + for i, ll in ipairs(state.inject.function_start) do + table.insert(line.children, 1+i, copy(ll)) + end + end + if state.inject.function_end then + for _, ll in ipairs(state.inject.function_end) do + table.insert(line.children, copy(ll)) + end end end end