Skip to content

Commit 779fad6

Browse files
committed
fix: macro_line statistics (close #485)
1 parent 4c55a5c commit 779fad6

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

packages/mecha/examples/basic_stats/src/data/demo/functions/foo.mcfunction

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ scoreboard objectives add my_consts dummy
22
scoreboard players set 10 my_consts 10
33
scoreboard players operation @e[tag=hello,scores={foo=1..}] foo += 10 my_consts
44
execute if score @s foo matches 20.. as @e[tag=hello] run setblock ~ ~ ~ stone
5+
function demo:macro {text: "hello"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$say $(text)

packages/mecha/src/mecha/contrib/statistics.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
from mecha import (
2020
AstCommand,
21+
AstCommandSentinel,
22+
AstMacroLine,
23+
AstMacroLineText,
24+
AstMacroLineVariable,
2125
AstObjective,
2226
AstObjectiveCriteria,
2327
AstPlayerName,
@@ -44,6 +48,7 @@ class Statistics(BaseModel):
4448
command_behind_execute_count: dict[str, int] = Field(
4549
default_factory=lambda: defaultdict(int)
4650
)
51+
macro_line_count: dict[str, int] = Field(default_factory=lambda: defaultdict(int))
4752
execute_count: int = 0
4853
execute_clause_count: dict[str, int] = Field(
4954
default_factory=lambda: defaultdict(int)
@@ -105,6 +110,9 @@ def root(self, node: AstRoot):
105110
self.stats.function_count += 1
106111

107112
for command in node.commands:
113+
if isinstance(command, AstCommandSentinel):
114+
continue
115+
108116
behind_execute = False
109117

110118
while command.identifier.startswith("execute"):
@@ -163,6 +171,18 @@ def command(self, node: AstCommand):
163171
self.stats.scoreboard_fake_player_references[objective][ref] += 1
164172
ref = None
165173

174+
@rule(AstMacroLine)
175+
def macro_line(self, node: AstMacroLine):
176+
parts = ["$"]
177+
for arg in node.arguments:
178+
if isinstance(arg, AstMacroLineText):
179+
parts.append(arg.value)
180+
elif isinstance(arg, AstMacroLineVariable):
181+
parts.append("$(")
182+
parts.append(arg.value)
183+
parts.append(")")
184+
self.stats.macro_line_count["".join(parts)] += 1
185+
166186

167187
@dataclass
168188
class Summary:
@@ -203,6 +223,9 @@ def format_commands(self) -> Dict[Tuple[str, ...], List[Tuple[str, ...]]]:
203223
for prefix, stats in self.stats.command_count.items()
204224
}
205225

226+
for macro_line, count in self.stats.macro_line_count.items():
227+
command_stats[macro_line, count, 0] = []
228+
206229
if self.stats.execute_count:
207230
command_stats["/execute", self.stats.execute_count, 0] = [
208231
(

packages/mecha/tests/snapshots/examples__build_basic_stats__0.pack.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@
2525
`@function demo:foo`
2626

2727
```mcfunction
28-
# Analyzed 1 function
28+
# Analyzed 2 functions
2929
# -------------------------------------------------------------------------------
30-
# Total commands (1 behind execute) | 4
30+
# Total commands (1 behind execute) | 6
3131
# -------------------------------------------------------------------------------
3232
# /scoreboard | 3
3333
# objectives add <objective> <criteria> | 1
3434
# players set <targets> <objective> <score> | 1
3535
# players operation <targets> <targetObjective> <o... | 1
3636
# /setblock (1 behind execute) | 1
37+
# /function | 1
38+
# $say $(text) | 1
3739
# /execute | 1
3840
# if score <target> <targetObjective> matches <range>... | 1
3941
# as <targets> <subcommand> | 1
@@ -56,4 +58,11 @@ scoreboard objectives add my_consts dummy
5658
scoreboard players set 10 my_consts 10
5759
scoreboard players operation @e[tag=hello, scores={foo=1..}] foo += 10 my_consts
5860
execute if score @s foo matches 20.. as @e[tag=hello] run setblock ~ ~ ~ stone
61+
function demo:macro {text: "hello"}
62+
```
63+
64+
`@function demo:macro`
65+
66+
```mcfunction
67+
$say $(text)
5968
```

0 commit comments

Comments
 (0)