From 8150a68fe68050a1cfef1b4cc6d0e2c7ab118f0a Mon Sep 17 00:00:00 2001 From: Derval Guillaume <gderval@uliege.be> Date: Sat, 3 Dec 2022 23:23:17 +0100 Subject: [PATCH] Expressions --- src/gboml/ast/__init__.py | 3 ++- src/gboml/ast/variables.py | 5 +++++ src/gboml/gboml.lark | 3 ++- src/gboml/parsing.py | 1 + tests/instances/ok/complex_parsing.txt | 2 +- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/gboml/ast/__init__.py b/src/gboml/ast/__init__.py index 550739b..ef2a017 100644 --- a/src/gboml/ast/__init__.py +++ b/src/gboml/ast/__init__.py @@ -6,7 +6,8 @@ __all__ = [ "StdConstraint", "SOSConstraint", "Objective", "VariableDefinition", "Node", "HyperEdge", "NodeDefinition", "NodeImport", "HyperEdgeDefinition", "HyperEdgeImport", "ExpressionOp", "GBOMLGraph", "ImplicitLoop", "RValue", "RValueWithGen", "GeneratedRValue", - "Range", "MultiLoop", "DictEntry", "Dictionary", "NodeGenerator", "HyperEdgeGenerator" + "Range", "MultiLoop", "DictEntry", "Dictionary", "NodeGenerator", "HyperEdgeGenerator", + "DefinitionType" ] from gboml.ast.arrays import * diff --git a/src/gboml/ast/variables.py b/src/gboml/ast/variables.py index fd761ce..87a7927 100644 --- a/src/gboml/ast/variables.py +++ b/src/gboml/ast/variables.py @@ -18,9 +18,14 @@ class VarType(Enum): binary = "binary" +class DefinitionType(Enum): + constant = "=" + expression = "<-" + @dataclass class Definition(GBOMLObject): name: str + type: DefinitionType value: RValue tags: list[str] = field(default_factory=list) diff --git a/src/gboml/gboml.lark b/src/gboml/gboml.lark index 9cad864..9118012 100644 --- a/src/gboml/gboml.lark +++ b/src/gboml/gboml.lark @@ -91,7 +91,8 @@ bool_expression_comparison: expression (COMPARISON_OPERATOR | CTR_OPERATOR) expr COMPARISON_OPERATOR: "<" | ">" | "!=" // DEFINITIONS -definition: ID "=" rvalue tags ";" +definition: ID DEF_TYPE rvalue tags ";" +DEF_TYPE: "=" | "<-" // ARRAYS array_or_dict: "{" separated_maybe_empty_list{_array_or_dict_entry, ","} "}" diff --git a/src/gboml/parsing.py b/src/gboml/parsing.py index 1612dcc..1aa3130 100644 --- a/src/gboml/parsing.py +++ b/src/gboml/parsing.py @@ -114,6 +114,7 @@ def _lark_to_gboml(tree: Tree, filename: Optional[str] = None) -> GBOMLGraph: def COMPARISON_OPERATOR(self, token): return Operator(token.value) def STRING(self, token): return token.value[1:-1].replace('\\"', '"') def VTYPE(self, token): return VarType(token.value) + def DEF_TYPE(self, token): return DefinitionType(token.value) NodesAndHyperEdges = namedtuple("NodesAndHyperEdges", ["nodes", "hyperedges"]) diff --git a/tests/instances/ok/complex_parsing.txt b/tests/instances/ok/complex_parsing.txt index a617270..349e999 100644 --- a/tests/instances/ok/complex_parsing.txt +++ b/tests/instances/ok/complex_parsing.txt @@ -5,7 +5,7 @@ b = 2.2; c = 2e2; d = -1; - e = 2e-2; + e <- 2e-2; f = [0:2]; g = [0:10:2]; #NODE nodeI = import nodeA from "hello.gboml"; -- GitLab