-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loki string parser based on pymbolic parser
- Loading branch information
1 parent
0a15267
commit 6489ed5
Showing
3 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# (C) Copyright 2018- ECMWF. | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
|
||
from sys import intern | ||
import pytools.lex | ||
from pymbolic.parser import Parser as ParserBase | ||
from pymbolic.mapper import Mapper | ||
import pymbolic.primitives as pmbl | ||
|
||
from loki.expression import symbols as sym | ||
|
||
__all__ = ['LokiParser', 'loki_parse'] | ||
|
||
|
||
class PymbolicMapper(Mapper): | ||
""" | ||
A visitor for expressions that returns the combined result of a specified callback function. | ||
""" | ||
# pylint: disable=abstract-method | ||
|
||
def __init__(self, scope=None): | ||
self.scope = scope | ||
super().__init__() | ||
|
||
def map_product(self, expr, *args, **kwargs): | ||
Check failure on line 29 in loki/expression/parser.py GitHub Actions / code checks (3.11)
|
||
return sym.Product(expr.children) | ||
map_Mul = map_product | ||
|
||
def map_sum(self, expr, *args, **kwargs): | ||
return sym.Sum(tuple(self.rec(child, *args, **kwargs) for child in expr.children)) | ||
map_Add = map_sum | ||
|
||
def map_power(self, expr, *args, **kwargs): | ||
Check failure on line 37 in loki/expression/parser.py GitHub Actions / code checks (3.11)
|
||
return sym.Power(base=expr.base, exponent=expr.exponent) | ||
|
||
def map_quotient(self, expr, *args, **kwargs): | ||
return sym.Quotient(numerator=expr.numerator, denominator=expr.denominator) | ||
|
||
def map_comparison(self, expr, *args, **kwargs): | ||
Check failure on line 43 in loki/expression/parser.py GitHub Actions / code checks (3.11)
|
||
# return sym.Comparison(tuple(self.rec(child, *args, **kwargs) for child in expr.children)) | ||
return sym.Comparison(left=self.rec(expr.left), operator=expr.operator, right=self.rec(expr.right)) | ||
|
||
def map_logical_and(self, expr, *args, **kwargs): | ||
Check failure on line 47 in loki/expression/parser.py GitHub Actions / code checks (3.11)
|
||
return sym.LogicalAnd(expr.children) | ||
|
||
def map_logical_or(self, expr, *args, **kwargs): | ||
return sym.LogicalOr(expr.children) | ||
|
||
def map_logical_not(self, expr, *args, **kwargs): | ||
return sym.LogicalNot(expr.children) | ||
|
||
def map_constant(self, expr, *args, **kwargs): | ||
return sym.Literal(expr) | ||
map_logic_literal = map_constant | ||
map_string_literal = map_constant | ||
map_intrinsic_literal = map_constant | ||
|
||
map_int_literal = map_constant | ||
map_float_literal = map_int_literal | ||
map_variable_symbol = map_constant | ||
map_deferred_type_symbol = map_constant | ||
|
||
def map_meta_symbol(self, expr, *args, **kwargs): | ||
return sym.Variable(str(expr.name)) | ||
map_Symbol = map_meta_symbol | ||
map_scalar = map_meta_symbol | ||
map_array = map_meta_symbol | ||
|
||
def map_slice(self, expr, *args, **kwargs): | ||
return sym.RangeIndex(expr.children) | ||
|
||
map_range = map_slice | ||
map_range_index = map_slice | ||
map_loop_range = map_slice | ||
|
||
def map_variable(self, expr, *args, **kwargs): | ||
return sym.Variable(name=expr.name, scope=self.scope) | ||
|
||
def map_algebraic_leaf(self, expr, *args, **kwargs): | ||
if str(expr).isnumeric(): | ||
return self.map_constant(expr) | ||
if isinstance(expr, pmbl.Call): | ||
if self.scope is not None: | ||
if expr.function.name in self.scope.symbol_attrs: | ||
return sym.Variable(name=expr.function.name, scope=self.scope, dimensions=self.rec(expr.parameters)) | ||
return expr | ||
else: | ||
try: | ||
return self.map_variable(expr) | ||
except: | ||
return expr | ||
|
||
def map_tuple(self, expr, *args, **kwargs): | ||
return tuple(self.rec(elem) for elem in expr) | ||
|
||
|
||
class LokiParser(ParserBase): | ||
|
||
_f_lessequal = intern('_f_lessequal') | ||
_f_less = intern('_f_less') | ||
_f_greaterequal = intern('_f_greaterequal') | ||
_f_greater = intern('_f_greater') | ||
_f_equal = intern('_f_equal') | ||
_f_notequal = intern('_f_notequal') | ||
_f_and = intern("and") | ||
_f_or = intern("or") | ||
# _f_not = intern("not") | ||
|
||
lex_table = [ | ||
(_f_lessequal, pytools.lex.RE(r"\.le\.")), | ||
(_f_less, pytools.lex.RE(r"\.lt\.")), | ||
(_f_greaterequal, pytools.lex.RE(r"\.ge\.")), | ||
(_f_greater, pytools.lex.RE(r"\.gt\.")), | ||
(_f_equal, pytools.lex.RE(r"\.eq\.")), | ||
(_f_notequal, pytools.lex.RE(r"\.ne\.")), | ||
(_f_and, pytools.lex.RE(r"\.and\.")), | ||
(_f_and, pytools.lex.RE(r"\.or\.")), | ||
# (_f_and, pytools.lex.RE(r"\.not\.")), | ||
] + ParserBase.lex_table | ||
|
||
ParserBase._COMP_TABLE.update({ | ||
_f_lessequal: "<=", | ||
_f_less: "<", | ||
_f_greaterequal: ">=", | ||
_f_greater: ">", | ||
_f_equal: "==", | ||
_f_notequal: "!=" | ||
}) | ||
|
||
def __call__(self, expr_str, scope=None, min_precedence=0): | ||
result = super().__call__(expr_str, min_precedence) | ||
return PymbolicMapper(scope=scope)(result) | ||
|
||
loki_parse = LokiParser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters