Skip to content

Commit be675af

Browse files
author
Andrew
committed
Code cleanup and minor additions to notebook
1 parent bbfd08f commit be675af

File tree

3 files changed

+65
-42
lines changed

3 files changed

+65
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,4 @@ dmypy.json
133133

134134
# Pyre type checker
135135
.pyre/
136+
output/main.cpp

PyPlus Documentation.ipynb

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
"These classes (classes prepended with cpp) hold definitions for object representations of C++ files, functions, variables, and code lines. This helps keep converted code organized and makes code export much easier.\n",
3838
"\n",
3939
"#### modules/pyplusexceptions\n",
40-
"This class holds exceptions specific to pyplus. This is to help make exiting a recursive function call easier when code translation for a certain line isn't supported."
40+
"This class holds exceptions specific to pyplus. This is to help make exiting a recursive function call easier when code translation for a certain line isn't supported.\n",
41+
"\n",
42+
"### Note on Code Style\n",
43+
"PEP8 recommends using snake case for function names, however for several of my functions beginning with 'parse_', the following word is capitalized. This was a design decision, not an oversight. To help reduce the amount of code needed to call the appropriate functions to parse various nodes, the node's class name was used as part of the function name. This allows me to use getattr to find the correct function based on the name. As I am using the class name to find the correct function, this results in the capital letters in the function declarations."
4144
]
4245
},
4346
{

modules/pyanalyzer.py

+60-41
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,10 @@ def find_else_lineno(self, search_index):
356356
357357
Returns
358358
-------
359-
tuple : (int, int)
360-
Tuple containing the line number of the else statement and the
361-
index of the last character of the else in the original python code
359+
search_index : int
360+
Line number of the else statement
361+
end_col_offset : int
362+
Index of the last character of the else in the original python code
362363
363364
Raises
364365
------
@@ -378,7 +379,8 @@ def find_else_lineno(self, search_index):
378379
end_col_offset += 4
379380

380381
# Line number is 1+index in list
381-
return search_index + 1, end_col_offset
382+
search_index += 1
383+
return search_index, end_col_offset
382384

383385
def parse_While(self, node, file_index, function_key, indent):
384386
"""
@@ -700,9 +702,10 @@ def parse_Call(self, node, file_index, function_key):
700702
701703
Returns
702704
-------
703-
tuple : (str, [str])
704-
Tuple with the string representation of the call and the return
705-
type in a list of a string
705+
return_str : str
706+
The call represented as a string
707+
return_type : list of str
708+
The return type of the call
706709
707710
Raises
708711
------
@@ -791,9 +794,10 @@ def parse_ported_function(self, file_index, function_key, function, args,
791794
792795
Returns
793796
-------
794-
tuple : (str, [str])
795-
Tuple with the string representation of the ported function and the
796-
return type in a list of a string
797+
return_str : str
798+
The ported function represented as a string
799+
return_type : list of str
800+
The return type of the ported function
797801
798802
Raises
799803
------
@@ -829,21 +833,27 @@ def parse_Constant(self, node, file_index, function_key):
829833
830834
Returns
831835
-------
832-
tuple : (str, [str])
833-
Tuple with the string representation of the constant and the return
834-
type in a list of a string
836+
return_str : str
837+
The constant value represented as a string
838+
return_type : list of str
839+
The type of the constant
835840
"""
836841
# Strings need to be wrapped in quotes
837842
if type(node.value) is str:
838-
return ("\"" + node.value + "\""), ["str"]
843+
return_str = ("\"" + node.value + "\"")
844+
return_type = ["str"]
839845

840846
# Python booleans are capital while C++ is lowercase, so we need to
841847
# translate it
842848
elif type(node.value) is bool:
843-
return cvar.CPPVariable.bool_map[str(node.value)], ["bool"]
849+
return_str = cvar.CPPVariable.bool_map[str(node.value)]
850+
return_type = ["bool"]
844851

845852
else:
846-
return str(node.value), [type(node.value).__name__]
853+
return_str = str(node.value)
854+
return_type = [type(node.value).__name__]
855+
856+
return return_str, return_type
847857

848858
# Operators
849859
def parse_BoolOp(self, node, file_index, function_key):
@@ -861,9 +871,10 @@ def parse_BoolOp(self, node, file_index, function_key):
861871
862872
Returns
863873
-------
864-
tuple : (str, [str])
865-
Tuple with the string representation of the BoolOp and the return
866-
type in a list of a string
874+
return_str : str
875+
The BoolOp represented as a string
876+
return_type : list of str
877+
The return type of the BoolOp
867878
868879
Raises
869880
------
@@ -906,7 +917,8 @@ def parse_BoolOp(self, node, file_index, function_key):
906917
else:
907918
return_type = compare_nodes[0][1]
908919

909-
return "(" + return_str + ")", return_type
920+
return_str = "(" + return_str + ")"
921+
return return_str, return_type
910922

911923
def parse_BinOp(self, node, file_index, function_key):
912924
"""
@@ -923,9 +935,10 @@ def parse_BinOp(self, node, file_index, function_key):
923935
924936
Returns
925937
-------
926-
tuple : (str, [str])
927-
Tuple with the string representation of the BinOp and the return
928-
type in a list of a string
938+
return_str : str
939+
The BinOp represented as a string
940+
return_type : list of str
941+
The return type of the BinOp
929942
930943
Raises
931944
------
@@ -945,32 +958,33 @@ def parse_BinOp(self, node, file_index, function_key):
945958
if operator in PyAnalyzer.operator_map:
946959
if operator == "Pow":
947960
self.output_files[file_index].add_include_file("math.h")
948-
return_expr = "pow(" + left_str + ", " + right_str + ")"
961+
return_str = "pow(" + left_str + ", " + right_str + ")"
949962
return_type = ["float"]
950963

951964
elif operator == "FloorDiv":
952-
return_expr = left_str + " / " + right_str
965+
return_str = left_str + " / " + right_str
953966
# If they aren't both ints, we need to cast to int to truncate
954967
if left_type[0] != "int" or right_type[0] != "int":
955-
return_expr = "(int)(" + return_expr + ")"
968+
return_str = "(int)(" + return_str + ")"
956969
return_type = ["int"]
957970

958971
elif operator == "Div":
959-
return_expr = left_str + " / " + right_str
972+
return_str = left_str + " / " + right_str
960973
# We need to cast one to a double or it will perform integer
961974
# math
962975
if left_type[0] != "float" or right_type[0] != "float":
963-
return_expr = "(double)" + return_expr
976+
return_str = "(double)" + return_str
964977
return_type = ["float"]
965978

966979
else:
967-
return_expr = left_str \
980+
return_str = left_str \
968981
+ PyAnalyzer.operator_map[operator] \
969982
+ right_str
970983

971984
return_type = self.type_precedence(left_type, right_type)
972985

973-
return "(" + return_expr + ")", return_type
986+
return_str = "(" + return_str + ")"
987+
return return_str, return_type
974988

975989
def type_precedence(self, type_a, type_b):
976990
"""
@@ -987,7 +1001,7 @@ def type_precedence(self, type_a, type_b):
9871001
9881002
Returns
9891003
-------
990-
list of str
1004+
return_type : list of str
9911005
The list that holds the type that should take precedence
9921006
"""
9931007
if type_a[0] in PyAnalyzer.type_precedence_dict and type_b[0] in PyAnalyzer.type_precedence_dict:
@@ -1020,9 +1034,10 @@ def parse_UnaryOp(self, node, file_index, function_key):
10201034
10211035
Returns
10221036
-------
1023-
tuple : (str, [str])
1024-
Tuple with the string representation of the UnaryOp and the return
1025-
type in a list of a string
1037+
return_str : str
1038+
The UnaryOp represented as a string
1039+
return_type : list of str
1040+
The return type of the UnaryOp
10261041
10271042
Raises
10281043
------
@@ -1039,11 +1054,12 @@ def parse_UnaryOp(self, node, file_index, function_key):
10391054

10401055
# Not operation becomes a bool no matter what type it operated on
10411056
if operator is ast.Not:
1042-
return_type = "bool"
1057+
return_type = ["bool"]
10431058
else:
1044-
return_type = "int"
1059+
return_type = ["int"]
10451060

1046-
return "(" + PyAnalyzer.operator_map[operator.__name__] + return_str + ")", [return_type]
1061+
return_str = "(" + PyAnalyzer.operator_map[operator.__name__] + return_str + ")"
1062+
return return_str, return_type
10471063

10481064
def parse_Compare(self, node, file_index, function_key):
10491065
"""
@@ -1060,9 +1076,10 @@ def parse_Compare(self, node, file_index, function_key):
10601076
10611077
Returns
10621078
-------
1063-
tuple : (str, [str])
1064-
Tuple with the string representation of the comparison and the
1065-
return type in a list of a string
1079+
return_str : str
1080+
The Compare operation represented as a string
1081+
return_type : list of str
1082+
The return type of the Compare operation
10661083
10671084
Raises
10681085
------
@@ -1101,7 +1118,9 @@ def parse_Compare(self, node, file_index, function_key):
11011118
PyAnalyzer.comparison_map[node.ops[-1].__class__.__name__] \
11021119
+ comparator + ")"
11031120

1104-
return return_str, ["bool"]
1121+
# All comparisons come back as a bool
1122+
return_type = ["bool"]
1123+
return return_str, return_type
11051124

11061125
def recurse_operator(self, node, file_index, function_key):
11071126
"""

0 commit comments

Comments
 (0)