Skip to content

Commit

Permalink
feat: make it possible to use variable names that conflict with commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jan 17, 2024
1 parent d68313c commit f19ae15
Show file tree
Hide file tree
Showing 279 changed files with 915 additions and 552 deletions.
6 changes: 6 additions & 0 deletions bolt/ast.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__all__ = [
"AstNonFunctionRoot",
"AstStatement",
"AstExpression",
"AstExpressionBinary",
"AstExpressionUnary",
Expand Down Expand Up @@ -87,6 +88,11 @@ class AstNonFunctionRoot(AstRoot):
"""Non-function root ast node."""


@dataclass(frozen=True, slots=True)
class AstStatement(AstCommandSentinel):
"""Ast statement node."""


@dataclass(frozen=True, slots=True)
class AstExpression(AstNode):
"""Base ast node for expressions."""
Expand Down
3 changes: 2 additions & 1 deletion bolt/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
AstPrelude,
AstProcMacro,
AstSlice,
AstStatement,
AstTarget,
AstTargetAttribute,
AstTargetIdentifier,
Expand Down Expand Up @@ -596,7 +597,7 @@ def command(
arguments = acc.children([f"*{arguments}", nesting] if arguments else [nesting])
return [acc.replace(acc.make_ref(node), arguments=arguments)]

@rule(AstCommand, identifier="statement")
@rule(AstStatement)
def statement(
self,
node: AstCommand,
Expand Down
17 changes: 13 additions & 4 deletions bolt/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"create_bolt_command_parser",
"UndefinedIdentifier",
"ImprovedErrorHandler",
"parse_statement",
"BranchScopeManager",
"FinalExpressionParser",
"InterpolationParser",
Expand Down Expand Up @@ -155,6 +156,7 @@
AstProcMacroMarker,
AstProcMacroResult,
AstSlice,
AstStatement,
AstTarget,
AstTargetAttribute,
AstTargetIdentifier,
Expand Down Expand Up @@ -218,13 +220,14 @@ def get_bolt_parsers(
macro_handler=macro_handler,
),
"nested_root": create_bolt_root_parser(parsers["nested_root"], macro_handler),
"root_item": ImprovedErrorHandler(parsers["root_item"]),
"root_item": ImprovedErrorHandler(
AlternativeParser([parsers["root_item"], parse_statement])
),
"command": create_bolt_command_parser(macro_handler, modules, bolt_prototypes),
"command:argument:bolt:if_block": delegate("bolt:if_block"),
"command:argument:bolt:elif_condition": delegate("bolt:elif_condition"),
"command:argument:bolt:elif_block": delegate("bolt:elif_block"),
"command:argument:bolt:else_block": delegate("bolt:else_block"),
"command:argument:bolt:statement": delegate("bolt:statement"),
"command:argument:bolt:assignment_target": delegate("bolt:assignment_target"),
"command:argument:bolt:with_expression": delegate("bolt:with_expression"),
"command:argument:bolt:with_target": delegate("bolt:with_target"),
Expand Down Expand Up @@ -704,6 +707,12 @@ def __call__(self, stream: TokenStream) -> Any:
raise


def parse_statement(stream: TokenStream) -> AstStatement:
"""Parse statement."""
node = delegate("bolt:statement", stream)
return set_location(AstStatement(arguments=AstChildren([node])), node)


@dataclass
class BranchScopeManager:
"""Parser that manages accessible identifiers for conditional branches."""
Expand Down Expand Up @@ -1076,7 +1085,7 @@ def __call__(self, stream: TokenStream) -> AstRoot:
result: List[AstCommand] = []

for command in node.commands:
if command.identifier == "statement" and isinstance(
if isinstance(command, AstStatement) and isinstance(
decorator := command.arguments[0], AstDecorator
):
changed = True
Expand Down Expand Up @@ -2051,7 +2060,7 @@ def __call__(self, stream: TokenStream) -> Any:

for command in node.commands:
if (
command.identifier == "statement"
isinstance(command, AstStatement)
and command.arguments
and isinstance(literal := command.arguments[0], AstValue)
and isinstance(literal.value, str)
Expand Down
5 changes: 0 additions & 5 deletions bolt/resources/commands.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
{
"type": "root",
"children": {
"statement": {
"type": "argument",
"parser": "bolt:statement",
"executable": true
},
"def": {
"type": "literal",
"children": {
Expand Down
15 changes: 15 additions & 0 deletions examples/bolt_class/src/data/demo/functions/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from dataclasses import dataclass

class Foo:
def __init__(self, name):
self.name = name
Expand Down Expand Up @@ -73,3 +75,16 @@ class B:

A().b().a()
B().a().b()

@dataclass
class PreviouslyNotPossible:
text: str
data: str

@staticmethod
def you_can_do_this():
setblock = 0
setblock setblock setblock setblock air

say PreviouslyNotPossible("no", "conflict")
PreviouslyNotPossible.you_can_do_this()
9 changes: 9 additions & 0 deletions tests/resources/bolt_examples.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -1355,3 +1355,12 @@ class A:
pass
class B(A, foo=True):
pass
###
setblock = 0
setblock setblock setblock setblock air
###
text = "demo:foo"
###
class A:
text: str
data: str
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_0__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=2, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=1, lineno=1, colno=2)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstValue'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_0__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=2, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_102__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ tellraw @p a
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=19, lineno=2, colno=13)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=6, lineno=1, colno=7)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstAssignment'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_102__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ _bolt_refs[3]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=19, lineno=2, colno=13)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
<class 'mecha.ast.AstCommand'>
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_103__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ as @a at @s if block ~ ~-1 ~ #wool give @s stone{
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=210, lineno=9, colno=2)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=41, lineno=1, colno=42)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstAssignment'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_103__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,5 @@ _bolt_refs[18]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=210, lineno=9, colno=2)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
<class 'mecha.ast.AstCommand'>
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_104__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ thing = (1, 'hey') + () + ("wow",)
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=35, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=34, lineno=1, colno=35)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstAssignment'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_104__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=35, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_105__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ f"hello"
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=9, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=8, lineno=1, colno=9)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstFormatString'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_105__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=9, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
8 changes: 4 additions & 4 deletions tests/snapshots/bolt__parse_106__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ f"hello {foo}"
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=29, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=13, lineno=1, colno=14)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstAssignment'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand All @@ -26,10 +26,10 @@ f"hello {foo}"
end_location: SourceLocation(pos=13, lineno=1, colno=14)
value: 'world'
type_annotation: None
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=14, lineno=2, colno=1)
end_location: SourceLocation(pos=28, lineno=2, colno=15)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstFormatString'>
location: SourceLocation(pos=14, lineno=2, colno=1)
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_106__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=29, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
<class 'bolt.ast.AstStatement'>
8 changes: 4 additions & 4 deletions tests/snapshots/bolt__parse_107__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ f"hello {foo!r}"
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=31, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=13, lineno=1, colno=14)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstAssignment'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand All @@ -26,10 +26,10 @@ f"hello {foo!r}"
end_location: SourceLocation(pos=13, lineno=1, colno=14)
value: 'world'
type_annotation: None
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=14, lineno=2, colno=1)
end_location: SourceLocation(pos=30, lineno=2, colno=17)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstFormatString'>
location: SourceLocation(pos=14, lineno=2, colno=1)
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_107__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=31, lineno=3, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
<class 'bolt.ast.AstStatement'>
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_108__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ f"hello {1 + 1}"
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=17, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=16, lineno=1, colno=17)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstFormatString'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_108__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=17, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_109__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ f"hello {1 + 1:03}"
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=20, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=19, lineno=1, colno=20)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstFormatString'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_109__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=20, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_10__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=13, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=12, lineno=1, colno=13)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstExpressionBinary'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_10__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=13, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
4 changes: 2 additions & 2 deletions tests/snapshots/bolt__parse_110__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ f'{{}}nope{1}'
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=15, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=14, lineno=1, colno=15)
identifier: 'statement'
identifier: 'mecha:sentinel'
arguments:
<class 'bolt.ast.AstFormatString'>
location: SourceLocation(pos=0, lineno=1, colno=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/bolt__parse_110__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ _bolt_refs[0]
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=15, lineno=2, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
<class 'bolt.ast.AstStatement'>
Loading

0 comments on commit f19ae15

Please sign in to comment.