Coverage for skema/program_analysis/CAST/python/util.py: 95%
21 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
1from typing import List
2from skema.program_analysis.CAST2FN.model.cast import AstNode, CASTLiteralValue, SourceRef
5def generate_dummy_source_refs(node: AstNode) -> AstNode:
6 """Walks a tree of AstNodes replacing any null SourceRefs with a dummy value"""
7 if isinstance(node, CASTLiteralValue) and not node.source_code_data_type:
8 node.source_code_data_type = ["Fortran", "Fotran95", "None"]
9 if not node.source_refs:
10 node.source_refs = [SourceRef("", -1, -1, -1, -1)]
12 for attribute_str in node.attribute_map:
13 attribute = getattr(node, attribute_str)
14 if isinstance(attribute, AstNode):
15 generate_dummy_source_refs(attribute)
16 elif isinstance(attribute, List):
17 for element in attribute:
18 if isinstance(element, AstNode):
19 generate_dummy_source_refs(element)
21 return node
23def get_op(operator):
24 ops = {
25 '+': 'ast.Add',
26 '-': 'ast.Sub',
27 '*': 'ast.Mult',
28 '/': 'ast.Div',
29 '==' : 'ast.Eq',
30 '!=' : 'ast.NotEq',
31 '<' : 'ast.Lt',
32 '<=' : 'ast.LtE',
33 '>' : 'ast.Gt',
34 '>=' : 'ast.GtE',
35 '%' : 'ast.Mod',
36 # ast.UAdd: 'ast.UAdd',
37 # ast.USub: 'ast.USub',
38 # ast.FloorDiv: 'ast.FloorDiv',
39 # ast.Pow: 'ast.Pow',
40 # ast.LShift: 'ast.LShift',
41 # ast.RShift: 'ast.RShift',
42 # ast.BitOr: 'ast.BitOr',
43 # ast.BitAnd: 'ast.BitAnd',
44 # ast.BitXor: 'ast.BitXor',
45 # ast.And: 'ast.And',
46 # ast.Or: 'ast.Or',
47 # ast.In: 'ast.In',
48 # ast.NotIn: 'ast.NotIn',
49 # ast.Not: 'ast.Not',
50 # ast.Invert: 'ast.Invert',
51 }
52 if operator in ops.keys():
53 return ops[operator]
54 else:
55 raise NotImplementedError(f"python tree sitter util.py: Operator {operator} isn't in the operations table")