Skip to content

Commit

Permalink
feat: add ast node compile_hints and make it so that AstSourceMap can…
Browse files Browse the repository at this point in the history
…'t end up inlined into execute
  • Loading branch information
vberlier committed Dec 1, 2022
1 parent 62d6583 commit a6537ff
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
Expand Up @@ -2,3 +2,10 @@ say abc

function demo:bar:
say def

execute as @p:
say something

execute as @p:
say foo
say bar
3 changes: 2 additions & 1 deletion mecha/ast.py
Expand Up @@ -99,7 +99,7 @@
)
from uuid import UUID

from beet.core.utils import extra_field, required_field
from beet.core.utils import JsonDict, extra_field, required_field
from nbtlib import Byte, ByteArray, Compound, CompoundMatch, Double, Int, IntArray
from nbtlib import List as ListTag
from nbtlib import ListIndex, LongArray, NamedKey, Numeric, Path, String
Expand Down Expand Up @@ -140,6 +140,7 @@ class AstNode:
end_location: SourceLocation = extra_field(default=UNKNOWN_LOCATION)

parser: ClassVar[Optional[str]] = None
compile_hints: ClassVar[JsonDict] = {}

def __iter__(self) -> Iterator["AstNode"]:
for f in fields(self):
Expand Down
14 changes: 12 additions & 2 deletions mecha/contrib/nesting.py
Expand Up @@ -152,8 +152,18 @@ def nesting_execute_function(self, node: AstCommand):
def nesting_execute_commands(self, node: AstCommand):
root = cast(AstRoot, node.arguments[0])

if len(root.commands) == 1:
subcommand = root.commands[0]
single_command = None
for command in root.commands:
if command.compile_hints.get("skip_execute_inline_single_command"):
continue
if single_command is None:
single_command = command
else:
single_command = None
break

if single_command:
subcommand = single_command

if subcommand.identifier == "execute:subcommand":
return subcommand.arguments[0]
Expand Down
2 changes: 2 additions & 0 deletions mecha/contrib/source_map.py
Expand Up @@ -59,6 +59,8 @@ class AstSourceMap(AstCommandSentinel):

header: str = required_field()

compile_hints = {"skip_execute_inline_single_command": True}


@dataclass
class SourceMapTransformer(MutatingReducer):
Expand Down
10 changes: 10 additions & 0 deletions tests/snapshots/examples__build_basic_source_map__0.pack.md
Expand Up @@ -20,6 +20,8 @@
```mcfunction
# [source_map] src/data/demo/functions/foo.mcfunction
say abc
execute as @p run say something
execute as @p run function demo:foo/nested_execute_0
```

`@function demo:thing`
Expand All @@ -29,6 +31,14 @@ say abc
say 456
```

`@function demo:foo/nested_execute_0`

```mcfunction
# [source_map] src/data/demo/functions/foo.mcfunction
say foo
say bar
```

`@function demo:bar`

```mcfunction
Expand Down

0 comments on commit a6537ff

Please sign in to comment.