Skip to content
Snippets Groups Projects
Commit 0261aa86 authored by Derval Guillaume's avatar Derval Guillaume
Browse files

rvalues

parent a550b567
No related branches found
No related tags found
No related merge requests found
Pipeline #10533 failed
Showing with 99 additions and 57 deletions
__all__ = [
"Meta", "GBOMLObject", "VarScope", "VarType", "SOSType", "ObjType",
"Operator", "ExpressionObj", "Expression", "BoolExpression", "VarOrParamLeaf",
"VarOrParam", "GeneratedExpression", "Array", "Loop", "Function", "BoolExpressionOp",
"BoolExpressionComparison", "ScopeChange", "Import", "Definition", "Constraint",
"VarOrParam", "Array", "Loop", "Function", "BoolExpressionOp",
"BoolExpressionComparison", "ScopeChange", "ImportFile", "Definition", "Constraint",
"StdConstraint", "SOSConstraint", "Objective", "VariableDefinition", "Node",
"HyperEdge", "NodeDefinition", "NodeImport", "HyperEdgeDefinition", "HyperEdgeImport",
"ExpressionOp", "GBOMLGraph", "ImplicitLoop"
"ExpressionOp", "GBOMLGraph", "ImplicitLoop", "RValue", "RValueWithGen", "GeneratedRValue"
]
from gboml.ast.arrays import *
from gboml.ast.base import *
from gboml.ast.constraints import *
from gboml.ast.expression_operators import *
from gboml.ast.expressions import *
from gboml.ast.graph import GBOMLGraph
from gboml.ast.functions import *
from gboml.ast.graph import *
from gboml.ast.hyperedges import *
from gboml.ast.import_file import *
from gboml.ast.loops import *
from gboml.ast.nodes import *
from gboml.ast.objectives import *
from gboml.ast.variables import *
\ No newline at end of file
from gboml.ast.path import *
from gboml.ast.rvalue import *
from gboml.ast.variables import *
import typing
if typing.TYPE_CHECKING:
from gboml.ast.rvalue import RValueWithGen
Array = list["RValueWithGen"]
......@@ -2,10 +2,11 @@ from dataclasses import dataclass
from enum import Enum
from typing import Optional
from gboml.ast.arrays import Array
from gboml.ast.base import GBOMLObject
from gboml.ast.expression_operators import Operator
from gboml.ast.expressions import Expression
from gboml.ast.loops import Loop, GeneratedExpression
from gboml.ast.loops import Loop
class SOSType(Enum):
......@@ -29,5 +30,5 @@ class StdConstraint(Constraint):
@dataclass
class SOSConstraint(Constraint):
type: SOSType
content: list[GeneratedExpression | Expression]
content: Array
loop: Optional[Loop]
from dataclasses import dataclass
from enum import Enum
from gboml.ast.expressions import ExpressionObj, Expression, Array, BoolExpression
from gboml.ast.loops import GeneratedExpression
from gboml.ast.expressions import ExpressionObj, Expression, BoolExpression
class Operator(Enum):
......@@ -29,16 +28,13 @@ class ExpressionOp(ExpressionObj):
operator: Operator
operands: list[Expression]
@dataclass
class Function(ExpressionObj):
name: str
operands: list[GeneratedExpression | Expression | Array | str]
@dataclass
class BoolExpressionOp(BoolExpression):
operator: Operator
operands: list[BoolExpression]
@dataclass
class BoolExpressionComparison(BoolExpression):
lhs: Expression
......
from dataclasses import dataclass, field
from dataclasses import dataclass
from gboml.ast.base import GBOMLObject
......@@ -9,19 +9,10 @@ class ExpressionObj(GBOMLObject):
Expression = int | float | ExpressionObj
Array = list["GeneratedExpression | Expression | Array | str"]
@dataclass
class BoolExpression(GBOMLObject):
pass
@dataclass
class VarOrParamLeaf(GBOMLObject):
name: str
indices: list[Expression] = field(default_factory=list)
@dataclass
class VarOrParam(ExpressionObj):
path: list[VarOrParamLeaf]
from dataclasses import dataclass
from gboml.ast.expressions import ExpressionObj
from gboml.ast.rvalue import RValueWithGen
@dataclass
class Function(ExpressionObj):
name: str
operands: list[RValueWithGen]
\ No newline at end of file
from dataclasses import dataclass
from typing import Optional
from lark.load_grammar import Definition
from gboml.ast.variables import Definition
from gboml.ast.base import GBOMLObject
from gboml.ast.hyperedges import HyperEdge
from gboml.ast.nodes import Node
......
......@@ -2,7 +2,7 @@ from dataclasses import dataclass
from gboml.ast.base import GBOMLObject
from gboml.ast.constraints import Constraint
from gboml.ast.expressions import VarOrParam
from gboml.ast.path import VarOrParam
from gboml.ast.variables import Definition
......
from dataclasses import dataclass
from gboml.ast import GBOMLObject
@dataclass
class ImportFile(GBOMLObject):
filename: str
......@@ -2,7 +2,8 @@ from dataclasses import dataclass, field
from typing import Optional
from gboml.ast.base import GBOMLObject
from gboml.ast.expressions import BoolExpression, Expression, VarOrParam, VarOrParamLeaf
from gboml.ast.expressions import BoolExpression, Expression
from gboml.ast.path import VarOrParam, VarOrParamLeaf
@dataclass
......@@ -15,15 +16,9 @@ class Loop(GBOMLObject):
@dataclass
class ImplicitLoop(GBOMLObject):
class ImplicitLoop(Loop):
varid: str = field(default="t", init=False)
start: Expression = field(default=0, init=False)
end: Expression = field(default_factory=lambda: VarOrParam([VarOrParamLeaf("T")]), init=False)
step: Optional[Expression] = field(default=None, init=False)
condition: BoolExpression
@dataclass
class GeneratedExpression(GBOMLObject):
expression: Expression
loop: Loop
......@@ -2,7 +2,7 @@ from dataclasses import dataclass
from gboml.ast.base import GBOMLObject
from gboml.ast.constraints import Constraint
from gboml.ast.expressions import VarOrParam
from gboml.ast.path import VarOrParam
from gboml.ast.hyperedges import HyperEdge
from gboml.ast.objectives import Objective
from gboml.ast.variables import Definition, VariableDefinition, ScopeChange
......
import typing
from dataclasses import dataclass, field
from gboml.ast.base import GBOMLObject
from gboml.ast.expressions import ExpressionObj
if typing.TYPE_CHECKING:
from gboml.ast.rvalue import RValue
@dataclass
class VarOrParamLeaf(GBOMLObject):
name: str
indices: list["RValue"] = field(default_factory=list)
@dataclass
class VarOrParam(ExpressionObj):
path: list[VarOrParamLeaf]
\ No newline at end of file
from dataclasses import dataclass
from gboml.ast.arrays import Array
from gboml.ast.base import GBOMLObject
from gboml.ast.loops import Loop
from gboml.ast.expressions import Expression
from gboml.ast.import_file import ImportFile
RValue = Expression | Array | ImportFile | str
@dataclass
class GeneratedRValue(GBOMLObject):
value: RValue
loop: Loop
RValueWithGen = RValue | GeneratedRValue
......@@ -3,7 +3,8 @@ from enum import Enum
from typing import Optional
from gboml.ast.base import GBOMLObject
from gboml.ast.expressions import Expression, Array, VarOrParam
from gboml.ast.path import VarOrParam
from gboml.ast.rvalue import RValue
class VarScope(Enum):
......@@ -17,15 +18,10 @@ class VarType(Enum):
binary = "binary"
@dataclass
class Import(GBOMLObject):
filename: str
@dataclass
class Definition(GBOMLObject):
name: str
value: Expression | Array | Import | str
value: RValue
@dataclass
......
......@@ -83,13 +83,10 @@ bool_expression_comparison: expression (COMPARISON_OPERATOR | CTR_OPERATOR) expr
COMPARISON_OPERATOR: "<" | ">" | "!="
// DEFINITIONS
definition: ID "=" _definition_rhs ";"
_definition_rhs: import | STRING | expression | array
import: "import" STRING
definition: ID "=" rvalue ";"
// ARRAYS
array: "{" _separated_list{_array_entry, ","} "}"
_array_entry: generated_expression | STRING | array
array: "{" _separated_list{generated_rvalue, ","} "}"
// EXPRESSIONS
?expression: substraction
......@@ -104,13 +101,14 @@ unary_minus: "-" value
?value: function | var_or_param | INT | FLOAT | ("(" expression ")")
// FUNCTIONS
function: ID "(" separated_list{_expression_or_array, ","} ")"
_expression_or_array: generated_expression | array
// GENERATOR
?generated_expression: expression loop?
function: ID "(" separated_list{generated_rvalue, ","} ")"
// VAR OR PARAM LINK
var_or_param: separated_list{var_or_param_leaf, "."}
var_or_param_leaf: ID plist{index}
?index: "[" (expression | STRING) "]"
\ No newline at end of file
?index: "[" rvalue "]"
//rvalue: anything that can be assigned to a parameter
?rvalue: import | array | expression | STRING
import: "import" STRING
?generated_rvalue: rvalue loop?
\ No newline at end of file
......@@ -14,7 +14,8 @@ def parse_file(filename: str) -> GBOMLGraph:
filename: path to the GBOML-formatted text to parse
Returns: a GBOMLGraph
"""
return parse(open(filename).read(), filename)
with open(filename) as f:
return parse(f.read(), filename)
def parse(text: str, filename: Optional[str] = None) -> GBOMLGraph:
......@@ -82,9 +83,9 @@ def _lark_to_gboml(tree: Tree, filename: Optional[str] = None) -> GBOMLGraph:
"bool_expression_not": bool_op_transform(Operator.b_not),
"bool_expression_comparison": BoolExpressionComparison,
"function": Function,
"generated_expression": GeneratedExpression,
"import": Import,
"variable_scope_change": ScopeChange
"import": ImportFile,
"variable_scope_change": ScopeChange,
"generated_rvalue": GeneratedRValue
}
def __default__(self, data, children, _):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment