Add @if with equality comparison

This commit is contained in:
Olivier 'reivilibre' 2021-06-23 19:35:28 +01:00
parent f83c624b08
commit 4eede74f7f
2 changed files with 32 additions and 3 deletions

View File

@ -28,7 +28,7 @@ SubBlock[ws=' \t']:
Directive:
UserDirective | SousDirective | ForDirective | ImportDirective |
RecipeEdgeDirective | ResourceEdgeDirective | ListenEdgeDirective |
IfSetDirective
IfSetDirective | IfCondDirective
;
UserDirective[ws=' \t']:
@ -58,6 +58,10 @@ IfSetDirective[ws=' \t']:
'@ifSet' variable=DottedIdString /\n/+
;
IfCondDirective[ws=' \t']:
'@if' variable=DottedIdString operator='=' other_value=ValueExpr /\n/+
;
ResourceEdgeDirectiveKind:
'@needs' | '@wants' | '@provides'
;

View File

@ -94,10 +94,28 @@ class IfSetDirective(IfDirective):
check_variable: str
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)
@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)
class RecipeEdgeDirective:
# "after" or "before"
@ -154,7 +172,7 @@ class MenuRecipe:
user_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)
resource_edges: List[ResourceEdgeDirective] = 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
assert isinstance(var, str)
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:
raise ValueError(f"Unknown directive {directive}")