Coverage for skema/program_analysis/CAST/pythonAST/builtin_map.py: 82%

34 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1""" 

2 builtin_map.py reads python_builtins.yaml into a structure 

3 that we can then query and use 

4""" 

5import yaml 

6from yaml.loader import SafeLoader 

7 

8import os 

9from pathlib import Path 

10 

11BUILTINS_FILENAME = "python_builtins.yaml" 

12BUILTINS = None 

13 

14def build_map(): 

15 global BUILTINS 

16 if BUILTINS == None: 

17 f_path = os.path.join(os.path.dirname(__file__), BUILTINS_FILENAME) 

18 with open(f_path) as f: 

19 BUILTINS = yaml.load(f, Loader=SafeLoader) 

20 return True 

21 else: 

22 return False 

23 

24def dump_map(): 

25 if BUILTINS != None: 

26 print(BUILTINS) 

27 return True 

28 else: 

29 print("Built in map isn't generated yet") 

30 return False 

31 

32def check_builtin(func_name): 

33 # Check if it's in the list of functions 

34 # Then check the actual operators afterwards 

35 

36 if func_name in BUILTINS['Functions']: 

37 return True 

38 for op in BUILTINS['Operators']: 

39 if func_name in op: 

40 return True 

41 

42 return False 

43 

44def retrieve_operator(func_name): 

45 # Returns the function name if it's a builtin function 

46 # Otherwise it returns the operator function name if it exists 

47 # TODO: Vincent double check this functionality 

48 

49 if func_name in BUILTINS['Functions']: 

50 return func_name 

51 

52 for op in BUILTINS['Operators']: 

53 if func_name in op: 

54 return op[func_name] 

55 

56 print(f"{func_name} NOT IMPLEMENTED") 

57 return None