From a3dc23cd377704a4abd730f74cc6e4660b2228ab Mon Sep 17 00:00:00 2001 From: Derval Guillaume <gderval@uliege.be> Date: Tue, 2 May 2023 11:55:15 +0200 Subject: [PATCH] Add variable bounds --- src/gboml/ast/variables.py | 3 +++ src/gboml/gboml.lark | 2 +- src/gboml/parsing.py | 7 ++++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/gboml/ast/variables.py b/src/gboml/ast/variables.py index 1ca9e80..6a1f094 100644 --- a/src/gboml/ast/variables.py +++ b/src/gboml/ast/variables.py @@ -2,6 +2,7 @@ from dataclasses import dataclass, field from enum import Enum from typing import Optional +from gboml.ast.expressions import Expression from gboml.ast.functions import Function from gboml.ast.arrays import Array, Range from gboml.ast.base import GBOMLObject, NamedGBOMLObject @@ -57,6 +58,8 @@ class VariableDefinition(NamedGBOMLObject): indices: list[str] scope: VarScope type: VarType + bound_lower: Optional[Expression] + bound_upper: Optional[Expression] import_from: Optional[VarOrParam] = None tags: set[str] = field(default_factory=set) diff --git a/src/gboml/gboml.lark b/src/gboml/gboml.lark index bf7ff85..a7e96bf 100644 --- a/src/gboml/gboml.lark +++ b/src/gboml/gboml.lark @@ -64,7 +64,7 @@ hyperedge_import: "#HYPEREDGE" ID "=" "import" var_or_param "from" STRING hypere hyperedge_redefs: "with" definition* | ";" // VARIABLES -variable_definition: SCOPE [VTYPE] ":" separated_list{variable_name,","} [_LARROW separated_list{var_or_param, ","}] tags ";" +variable_definition: SCOPE [VTYPE] ":" separated_list{variable_name,","} [_LARROW separated_list{var_or_param, ","}] ["in" "[" [expression] ":" [expression] "]"] tags ";" variable_name: ID olist{index} SCOPE: "internal" | "external" VTYPE: "binary" | "continuous" | "integer" diff --git a/src/gboml/parsing.py b/src/gboml/parsing.py index 0d1f0df..ab703ad 100644 --- a/src/gboml/parsing.py +++ b/src/gboml/parsing.py @@ -193,12 +193,14 @@ def _lark_to_gboml(tree: Tree, filename: Optional[str] = None) -> GBOMLGraph: return GBOMLGraph(time_horizon, global_defs, nodes_hyperedges.nodes, nodes_hyperedges.hyperedges, meta=meta) def variable_definition(self, meta: Meta, scope: VarScope, type: Optional[VarType], names: list[(str, list[str])], - imports_from: Optional[list[VarOrParam]], tags: set[str]): + imports_from: Optional[list[VarOrParam]], + bound_lower: Optional[Expression], bound_upper: Optional[Expression], tags: set[str]): if imports_from is not None and len(imports_from) != len(names): raise Exception("Invalid variable import, numbers of variables on the left and on the right-side of " "`<-` don't match") for name, import_from in zip(names, imports_from or repeat(None, len(names))): - yield VariableDefinition(name[0], name[1], scope, type or VarType.continuous, import_from, tags, meta=meta) + yield VariableDefinition(name[0], name[1], scope, type or VarType.continuous, + bound_lower, bound_upper, import_from, tags, meta=meta) def variables_block(self, _: Meta, *defs: Tuple[Iterable[VariableDefinition]]): return [vd for iterable in defs for vd in iterable] @@ -223,5 +225,4 @@ def _lark_to_gboml(tree: Tree, filename: Optional[str] = None) -> GBOMLGraph: else: return ConstantDefinition(name, val, tags, meta=meta) - return GBOMLLarkTransformer().transform(tree) -- GitLab