From 4eede74f7f4b84cad08dd4378388e71f4a97ab77 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Wed, 23 Jun 2021 19:35:28 +0100 Subject: [PATCH] Add @if with equality comparison --- scone/head/grammar/scoml.tx | 6 +++++- scone/head/menu_reader.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/scone/head/grammar/scoml.tx b/scone/head/grammar/scoml.tx index 8090227..16e0aa6 100644 --- a/scone/head/grammar/scoml.tx +++ b/scone/head/grammar/scoml.tx @@ -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' ; diff --git a/scone/head/menu_reader.py b/scone/head/menu_reader.py index 7f7ca03..96fa9d7 100644 --- a/scone/head/menu_reader.py +++ b/scone/head/menu_reader.py @@ -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}")