|
|
|
@ -17,7 +17,7 @@
|
|
|
|
|
"""Resources for ast tree parse."""
|
|
|
|
|
import ast
|
|
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from mindspore import RowTensor, SparseTensor, COOTensor, CSRTensor
|
|
|
|
|
from mindspore.ops import functional as F, composite as C
|
|
|
|
|
from mindspore.ops.composite import multitype_ops
|
|
|
|
@ -25,16 +25,16 @@ from mindspore._c_expression import security
|
|
|
|
|
from . import standard_method as M
|
|
|
|
|
from . import trope as T
|
|
|
|
|
from .namespace import CellNamespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# namespace define
|
|
|
|
|
functional_ns = CellNamespace('mindspore.ops.functional')
|
|
|
|
|
composite_ns = CellNamespace('mindspore.ops.composite')
|
|
|
|
|
trope_ns = CellNamespace('mindspore._extends.parse.trope')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NO_IMPLEMENT = None # not implemented
|
|
|
|
|
SYMBOL_UNDEFINE = 0xFF # Undefined var and function
|
|
|
|
|
|
|
|
|
|
# Some space set aside for readability of code
|
|
|
|
|
|
|
|
|
|
# 一些空间设置以提高代码可读性
|
|
|
|
|
parse_object_map = {
|
|
|
|
|
# ast grammar
|
|
|
|
|
ast.Add: (trope_ns, 'add'),
|
|
|
|
@ -64,17 +64,17 @@ parse_object_map = {
|
|
|
|
|
ast.IsNot: (trope_ns, 'is_not'),
|
|
|
|
|
ast.In: (trope_ns, 'contains'),
|
|
|
|
|
ast.NotIn: (trope_ns, 'not_contains'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# operation symbol type
|
|
|
|
|
'getitem': (composite_ns, 'getitem'),
|
|
|
|
|
'ms_iter': (composite_ns, 'ms_iter'),
|
|
|
|
|
'ms_next': (composite_ns, 'ms_next'),
|
|
|
|
|
'hasnext': (composite_ns, 'hasnext'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# undefined type
|
|
|
|
|
SYMBOL_UNDEFINE: (None, 'undefine'),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Operation symbols corresponding to ast grammar
|
|
|
|
|
ops_symbol_map = {
|
|
|
|
|
# ast grammar
|
|
|
|
@ -88,13 +88,13 @@ ops_symbol_map = {
|
|
|
|
|
ast.LShift: '<<',
|
|
|
|
|
ast.RShift: '>>',
|
|
|
|
|
ast.BitXor: '^',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# undefined type
|
|
|
|
|
SYMBOL_UNDEFINE: '',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Escape an object to another object, eg: system function(len,xxx)
|
|
|
|
|
# Some space set aside for readability of code
|
|
|
|
|
|
|
|
|
|
# 将一个对象转为另一个对象,例如:系统函数(len,xxx)
|
|
|
|
|
# 一些空间设置以提高代码可读性
|
|
|
|
|
convert_object_map = {
|
|
|
|
|
T.add: multitype_ops.add,
|
|
|
|
|
T.sub: multitype_ops.sub,
|
|
|
|
@ -124,7 +124,7 @@ convert_object_map = {
|
|
|
|
|
T.is_not: F.is_not,
|
|
|
|
|
T.contains: multitype_ops.in_,
|
|
|
|
|
T.not_contains: multitype_ops.not_in_,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# system function
|
|
|
|
|
T.len: M.ms_len,
|
|
|
|
|
T.bool_: M.bool_,
|
|
|
|
@ -134,7 +134,7 @@ convert_object_map = {
|
|
|
|
|
T.zip: C.zip_operation,
|
|
|
|
|
T.enumerate: M.enumerate_,
|
|
|
|
|
T.isinstance: M.isinstance_,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# custom define operation
|
|
|
|
|
T.iter: M.ms_iter,
|
|
|
|
|
T.next: M.ms_next,
|
|
|
|
@ -145,7 +145,7 @@ convert_object_map = {
|
|
|
|
|
T.make_slice: F.make_slice,
|
|
|
|
|
T.range: F.make_range,
|
|
|
|
|
T.while_cond: M.while_cond,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# lib function
|
|
|
|
|
math.floor: NO_IMPLEMENT,
|
|
|
|
|
math.trunc: NO_IMPLEMENT,
|
|
|
|
@ -154,13 +154,14 @@ convert_object_map = {
|
|
|
|
|
math.sin: NO_IMPLEMENT,
|
|
|
|
|
math.cos: NO_IMPLEMENT,
|
|
|
|
|
math.tan: NO_IMPLEMENT,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# user defined
|
|
|
|
|
RowTensor: F.make_row_tensor,
|
|
|
|
|
SparseTensor: F.make_sparse_tensor,
|
|
|
|
|
COOTensor: F.make_coo_tensor,
|
|
|
|
|
CSRTensor: F.make_csr_tensor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 如果不启用安全性,则将 T.print 映射到 F.print_
|
|
|
|
|
if not security.enable_security():
|
|
|
|
|
convert_object_map[T.print] = F.print_
|
|
|
|
|
convert_object_map[T.print] = F.print_
|