-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored master branch #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,8 +292,7 @@ def visit_Module(self, node: TypedModule) -> plt.AST: | |
raise RuntimeError( | ||
"The contract can not always detect if it was passed three or two parameters on-chain." | ||
) | ||
cp = plt.Program("1.0.0", validator) | ||
return cp | ||
return plt.Program("1.0.0", validator) | ||
|
||
def visit_Constant(self, node: TypedConstant) -> plt.AST: | ||
plt_type = ConstantMap.get(type(node.value)) | ||
|
@@ -796,10 +795,7 @@ def visit_ListComp(self, node: TypedListComp) -> plt.AST: | |
lst = plt.Apply(self.visit(gen.iter), plt.Var(STATEMONAD)) | ||
ifs = None | ||
for ifexpr in gen.ifs: | ||
if ifs is None: | ||
ifs = self.visit(ifexpr) | ||
else: | ||
ifs = plt.And(ifs, self.visit(ifexpr)) | ||
ifs = self.visit(ifexpr) if ifs is None else plt.And(ifs, self.visit(ifexpr)) | ||
Comment on lines
-799
to
+798
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
map_fun = plt.Lambda( | ||
["x"], | ||
plt.Apply( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,11 @@ def compare(a: int, b: int) -> int: | |
# a == b: 0 | ||
# a > b: -1 | ||
if a < b: | ||
result = 1 | ||
return 1 | ||
elif a == b: | ||
result = 0 | ||
return 0 | ||
else: | ||
result = -1 | ||
return result | ||
return -1 | ||
Comment on lines
-9
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def compare_extended_helper(time: ExtendedPOSIXTime) -> int: | ||
|
@@ -31,21 +30,15 @@ def compare_extended(a: ExtendedPOSIXTime, b: ExtendedPOSIXTime) -> int: | |
# a > b: -1 | ||
a_val = compare_extended_helper(a) | ||
b_val = compare_extended_helper(b) | ||
if a_val == 0 and b_val == 0: | ||
a_finite: FinitePOSIXTime = a | ||
b_finite: FinitePOSIXTime = b | ||
result = compare(a_finite.time, b_finite.time) | ||
else: | ||
result = compare(a_val, b_val) | ||
return result | ||
if a_val != 0 or b_val != 0: | ||
return compare(a_val, b_val) | ||
a_finite: FinitePOSIXTime = a | ||
b_finite: FinitePOSIXTime = b | ||
return compare(a_finite.time, b_finite.time) | ||
Comment on lines
-34
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def get_bool(b: BoolData) -> bool: | ||
if isinstance(b, TrueData): | ||
result = True | ||
else: | ||
result = False | ||
return result | ||
return isinstance(b, TrueData) | ||
Comment on lines
-44
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def compare_upper_bound(a: UpperBoundPOSIXTime, b: UpperBoundPOSIXTime) -> int: | ||
|
@@ -76,7 +69,7 @@ def contains(a: POSIXTimeRange, b: POSIXTimeRange) -> bool: | |
# Returns True if the interval `b` is entirely contained in `a`. | ||
lower = compare_lower_bound(a.lower_bound, b.lower_bound) | ||
upper = compare_upper_bound(a.upper_bound, b.upper_bound) | ||
return (lower == 1 or lower == 0) and (upper == 0 or upper == -1) | ||
return lower in [1, 0] and upper in [0, -1] | ||
Comment on lines
-79
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def make_range( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,10 +66,7 @@ class OptimizeRemoveDeadvars(CompilingNodeTransformer): | |
|
||
def guaranteed(self, name: str) -> bool: | ||
name = name | ||
for scope in reversed(self.guaranteed_avail_names): | ||
if name in scope: | ||
return True | ||
return False | ||
return any(name in scope for scope in reversed(self.guaranteed_avail_names)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def enter_scope(self): | ||
self.guaranteed_avail_names.append([]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ def visit_FunctionDef(self, node: FunctionDef): | |
def bs_from_int(i: int): | ||
hex_str = f"{i:x}" | ||
if len(hex_str) % 2 == 1: | ||
hex_str = "0" + hex_str | ||
hex_str = f"0{hex_str}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return bytes.fromhex(hex_str) | ||
|
||
|
||
|
@@ -47,11 +47,8 @@ def visit_Module(self, node: Module) -> Module: | |
# collect all variable names | ||
collector = NameCollector() | ||
collector.visit(node) | ||
# sort by most used | ||
varmap = {} | ||
varnames = sorted(collector.vars.items(), key=lambda x: x[1], reverse=True) | ||
for i, (v, _) in enumerate(varnames): | ||
varmap[v] = bs_from_int(i) | ||
varmap = {v: bs_from_int(i) for i, (v, _) in enumerate(varnames)} | ||
Comment on lines
-50
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
self.varmap = varmap | ||
node_cp = copy(node) | ||
node_cp.body = [self.visit(s) for s in node.body] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -495,11 +495,11 @@ def all_tokens_unlocked_from_address( | |
) -> int: | ||
"""Returns how many tokens of specified type are unlocked from given address""" | ||
return sum( | ||
[ | ||
txi.resolved.value.get(token.policy_id, {b"": 0}).get(token.token_name, 0) | ||
for txi in txins | ||
if txi.resolved.address == address | ||
] | ||
txi.resolved.value.get(token.policy_id, {b"": 0}).get( | ||
token.token_name, 0 | ||
) | ||
for txi in txins | ||
if txi.resolved.address == address | ||
Comment on lines
-498
to
+502
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
|
||
|
||
|
@@ -508,11 +508,9 @@ def all_tokens_locked_at_address_with_datum( | |
) -> int: | ||
"""Returns how many tokens of specified type are locked at then given address with the specified datum""" | ||
return sum( | ||
[ | ||
txo.value.get(token.policy_id, {b"": 0}).get(token.token_name, 0) | ||
for txo in txouts | ||
if txo.address == address and txo.datum == output_datum | ||
] | ||
txo.value.get(token.policy_id, {b"": 0}).get(token.token_name, 0) | ||
for txo in txouts | ||
if txo.address == address and txo.datum == output_datum | ||
Comment on lines
-511
to
+513
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
|
||
|
||
|
@@ -521,11 +519,9 @@ def all_tokens_locked_at_address( | |
) -> int: | ||
"""Returns how many tokens of specified type are locked at the given address""" | ||
return sum( | ||
[ | ||
txo.value.get(token.policy_id, {b"": 0}).get(token.token_name, 0) | ||
for txo in txouts | ||
if txo.address == address | ||
] | ||
txo.value.get(token.policy_id, {b"": 0}).get(token.token_name, 0) | ||
for txo in txouts | ||
if txo.address == address | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,11 @@ class RewriteAugAssign(CompilingNodeTransformer): | |
def visit_AugAssign(self, node: AugAssign) -> Assign: | ||
target_cp = copy(node.target) | ||
target_cp.ctx = Load() | ||
a = Assign( | ||
return Assign( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
[self.visit(node.target)], | ||
BinOp( | ||
self.visit(target_cp), | ||
self.visit(node.op), | ||
self.visit(node.value), | ||
), | ||
) | ||
return a |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ def visit_ImportFrom( | |
node.names[0].name == "*" | ||
), "The import must have the form 'from <pkg> import *'" | ||
assert ( | ||
node.names[0].asname == None | ||
node.names[0].asname is None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
), "The import must have the form 'from <pkg> import *'" | ||
# TODO set anchor point according to own package | ||
if self.filename: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,7 +223,6 @@ def visit_If(self, node: If) -> TypedIf: | |
typed_if = copy(node) | ||
if ( | ||
isinstance(typed_if.test, Call) | ||
and (typed_if.test.func, Name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
and typed_if.test.func.id == "isinstance" | ||
): | ||
tc = typed_if.test | ||
|
@@ -246,7 +245,7 @@ def visit_If(self, node: If) -> TypedIf: | |
assert isinstance(target_class, RecordType), "Can only cast to PlutusData" | ||
assert ( | ||
target_class in target_inst_class.typ.typs | ||
), f"Trying to cast an instance of Union type to non-instance of union type" | ||
), "Trying to cast an instance of Union type to non-instance of union type" | ||
typed_if.test = self.visit( | ||
Compare( | ||
left=Attribute(tc.args[0], "CONSTR_ID"), | ||
|
@@ -475,7 +474,7 @@ def visit_Subscript(self, node: Subscript) -> TypedSubscript: | |
elif isinstance(ts.value.typ.typ, DictType): | ||
# TODO could be implemented with potentially just erroring. It might be desired to avoid this though. | ||
raise TypeInferenceError( | ||
f"Could not infer type of subscript of dict. Use 'get' with a default value instead." | ||
"Could not infer type of subscript of dict. Use 'get' with a default value instead." | ||
Comment on lines
-478
to
+477
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
else: | ||
raise TypeInferenceError( | ||
|
@@ -517,8 +516,7 @@ def visit_Call(self, node: Call) -> TypedCall: | |
raise TypeInferenceError("Could not infer type of call") | ||
|
||
def visit_Pass(self, node: Pass) -> TypedPass: | ||
tp = copy(node) | ||
return tp | ||
return copy(node) | ||
Comment on lines
-520
to
+519
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def visit_Return(self, node: Return) -> TypedReturn: | ||
tp = copy(node) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1041,7 +1041,7 @@ def empty_list(p: Type): | |
), | ||
) | ||
) | ||
if isinstance(p.typ, RecordType) or isinstance(p.typ, AnyType): | ||
if isinstance(p.typ, (RecordType, AnyType)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return plt.EmptyDataList() | ||
raise NotImplementedError(f"Empty lists of type {p} can't be constructed yet") | ||
|
||
|
@@ -1113,7 +1113,7 @@ def visit(self, node): | |
node_class_name = node.__class__.__name__ | ||
if node_class_name.startswith("Typed"): | ||
node_class_name = node_class_name[len("Typed") :] | ||
method = "visit_" + node_class_name | ||
method = f"visit_{node_class_name}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
visitor = getattr(self, method, self.generic_visit) | ||
return visitor(node) | ||
|
||
|
@@ -1124,6 +1124,6 @@ def visit(self, node): | |
node_class_name = node.__class__.__name__ | ||
if node_class_name.startswith("Typed"): | ||
node_class_name = node_class_name[len("Typed") :] | ||
method = "visit_" + node_class_name | ||
method = f"visit_{node_class_name}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
visitor = getattr(self, method, self.generic_visit) | ||
return visitor(node) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,2 @@ | ||
def validator(a: int, b: int) -> int: | ||
# trivial implementation of c = a * b | ||
c = 0 | ||
for k in range(b): | ||
c += a | ||
return c | ||
return sum(a for _ in range(b)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
def validator(a: int, b: int) -> int: | ||
# trivial implementation of c = a * b | ||
c = 0 | ||
while 0 < b: | ||
while b > 0: | ||
c += a | ||
b -= 1 | ||
return c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
UPLCCompiler.visit_Module
refactored with the following changes:inline-immediately-returned-variable
)