Add @if with equality comparison
This commit is contained in:
parent
f83c624b08
commit
4eede74f7f
@ -28,7 +28,7 @@ SubBlock[ws=' \t']:
|
|||||||
Directive:
|
Directive:
|
||||||
UserDirective | SousDirective | ForDirective | ImportDirective |
|
UserDirective | SousDirective | ForDirective | ImportDirective |
|
||||||
RecipeEdgeDirective | ResourceEdgeDirective | ListenEdgeDirective |
|
RecipeEdgeDirective | ResourceEdgeDirective | ListenEdgeDirective |
|
||||||
IfSetDirective
|
IfSetDirective | IfCondDirective
|
||||||
;
|
;
|
||||||
|
|
||||||
UserDirective[ws=' \t']:
|
UserDirective[ws=' \t']:
|
||||||
@ -58,6 +58,10 @@ IfSetDirective[ws=' \t']:
|
|||||||
'@ifSet' variable=DottedIdString /\n/+
|
'@ifSet' variable=DottedIdString /\n/+
|
||||||
;
|
;
|
||||||
|
|
||||||
|
IfCondDirective[ws=' \t']:
|
||||||
|
'@if' variable=DottedIdString operator='=' other_value=ValueExpr /\n/+
|
||||||
|
;
|
||||||
|
|
||||||
ResourceEdgeDirectiveKind:
|
ResourceEdgeDirectiveKind:
|
||||||
'@needs' | '@wants' | '@provides'
|
'@needs' | '@wants' | '@provides'
|
||||||
;
|
;
|
||||||
|
@ -94,10 +94,28 @@ class IfSetDirective(IfDirective):
|
|||||||
check_variable: str
|
check_variable: str
|
||||||
|
|
||||||
def condition_true(self, vars: Variables) -> bool:
|
def condition_true(self, vars: Variables) -> bool:
|
||||||
print(f"isset? {self.check_variable} {vars.has_dotted(self.check_variable)}")
|
|
||||||
return vars.has_dotted(self.check_variable)
|
return vars.has_dotted(self.check_variable)
|
||||||
|
|
||||||
|
|
||||||
|
@attr.s(auto_attribs=True)
|
||||||
|
class IfCondDirective(IfDirective):
|
||||||
|
# Name of the variable
|
||||||
|
variable: str
|
||||||
|
|
||||||
|
# The operator that is used
|
||||||
|
operator: str
|
||||||
|
|
||||||
|
# The other value to check for equality against
|
||||||
|
other_value: str
|
||||||
|
|
||||||
|
def condition_true(self, vars: Variables) -> bool:
|
||||||
|
if self.operator == "=":
|
||||||
|
value = vars.get_dotted(self.variable)
|
||||||
|
return value == self.other_value
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(f"operator {self.operator} not understood.")
|
||||||
|
|
||||||
|
|
||||||
@attr.s(auto_attribs=True)
|
@attr.s(auto_attribs=True)
|
||||||
class RecipeEdgeDirective:
|
class RecipeEdgeDirective:
|
||||||
# "after" or "before"
|
# "after" or "before"
|
||||||
@ -154,7 +172,7 @@ class MenuRecipe:
|
|||||||
|
|
||||||
user_directive: Optional[str] = None
|
user_directive: Optional[str] = None
|
||||||
sous_directive: Optional[str] = None
|
sous_directive: Optional[str] = None
|
||||||
control_directives: List[ForDirective] = attr.ib(factory=list)
|
control_directives: List[ControlDirective] = attr.ib(factory=list)
|
||||||
recipe_edges: List[RecipeEdgeDirective] = attr.ib(factory=list)
|
recipe_edges: List[RecipeEdgeDirective] = attr.ib(factory=list)
|
||||||
resource_edges: List[ResourceEdgeDirective] = attr.ib(factory=list)
|
resource_edges: List[ResourceEdgeDirective] = attr.ib(factory=list)
|
||||||
listen_edges: List[ListenEdgeDirective] = attr.ib(factory=list)
|
listen_edges: List[ListenEdgeDirective] = attr.ib(factory=list)
|
||||||
@ -233,6 +251,13 @@ def convert_textx_recipe(txrecipe_or_subblock, parent: Optional[MenuBlock]):
|
|||||||
var = directive.variable
|
var = directive.variable
|
||||||
assert isinstance(var, str)
|
assert isinstance(var, str)
|
||||||
recipe.control_directives.append(IfSetDirective(var))
|
recipe.control_directives.append(IfSetDirective(var))
|
||||||
|
elif isinstance(directive, scoml_classes["IfCondDirective"]):
|
||||||
|
var = directive.variable
|
||||||
|
op = directive.operator
|
||||||
|
other_value = convert_textx_value(directive.other_value)
|
||||||
|
assert isinstance(var, str)
|
||||||
|
assert isinstance(op, str)
|
||||||
|
recipe.control_directives.append(IfCondDirective(var, op, other_value))
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unknown directive {directive}")
|
raise ValueError(f"Unknown directive {directive}")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user