mirror of https://github.com/exaloop/codon.git
Property setters (#499)
* Fix __from_gpu_new__ * Fix GPU tests * Update GPU debug codegen * Add will-return attribute for GPU compilation * Fix isinstance on unresolved types * Fix union type instantiation and pendingRealizations placement * Add float16, bfloat16 and float128 IR types * Add float16, bfloat16 and float128 types * Mark complex64 as no-python * Fix float methods * Add float tests * Disable some float tests * Fix bitset in reaching definitions analysis * Fix static bool unification * Add property setters * Remove log --------- Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>pull/500/head^2
parent
a257678c4f
commit
7fdbc7f21e
codon/parser/visitors
simplify
typecheck
test/parser
|
@ -221,8 +221,20 @@ void SimplifyVisitor::visit(ClassStmt *stmt) {
|
|||
// Add class methods
|
||||
for (const auto &sp : getClassMethods(stmt->suite))
|
||||
if (sp && sp->getFunction()) {
|
||||
if (sp.get() != autoDeducedInit.second)
|
||||
if (sp.get() != autoDeducedInit.second) {
|
||||
auto &ds = sp->getFunction()->decorators;
|
||||
for (auto &dc : ds) {
|
||||
if (auto d = dc->getDot()) {
|
||||
if (d->member == "setter" and d->expr->isId(sp->getFunction()->name) &&
|
||||
sp->getFunction()->args.size() == 2) {
|
||||
sp->getFunction()->name = format(".set_{}", sp->getFunction()->name);
|
||||
dc = nullptr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
fnStmts.push_back(transform(sp));
|
||||
}
|
||||
}
|
||||
|
||||
// After popping context block, record types and nested classes will disappear.
|
||||
|
|
|
@ -113,6 +113,8 @@ void SimplifyVisitor::visit(FunctionStmt *stmt) {
|
|||
|
||||
// Parse attributes
|
||||
for (auto i = stmt->decorators.size(); i-- > 0;) {
|
||||
if (!stmt->decorators[i])
|
||||
continue;
|
||||
auto [isAttr, attrName] = getDecorator(stmt->decorators[i]);
|
||||
if (!attrName.empty()) {
|
||||
stmt->attributes.set(attrName);
|
||||
|
|
|
@ -163,7 +163,14 @@ void TypecheckVisitor::visit(AssignMemberStmt *stmt) {
|
|||
if (auto lhsClass = stmt->lhs->getType()->getClass()) {
|
||||
auto member = ctx->findMember(lhsClass, stmt->member);
|
||||
|
||||
if (!member && stmt->lhs->isType()) {
|
||||
if (!member) {
|
||||
// Case: setters
|
||||
auto setters = ctx->findMethod(lhsClass.get(), format(".set_{}", stmt->member));
|
||||
if (!setters.empty()) {
|
||||
resultStmt = transform(N<ExprStmt>(
|
||||
N<CallExpr>(N<IdExpr>(setters[0]->ast->name), stmt->lhs, stmt->rhs)));
|
||||
return;
|
||||
}
|
||||
// Case: class variables
|
||||
if (auto cls = in(ctx->cache->classes, lhsClass->name))
|
||||
if (auto var = in(cls->classVars, stmt->member)) {
|
||||
|
|
|
@ -1276,3 +1276,30 @@ def f(x: int) -> int:
|
|||
return g(y - 1) * z
|
||||
return g(4)
|
||||
print(f(3)) #: 1296
|
||||
|
||||
#%% class_setter,barebones
|
||||
class Foo:
|
||||
_x: int
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
print('getter')
|
||||
return self._x
|
||||
|
||||
@x.setter
|
||||
def x(self, v):
|
||||
print('setter')
|
||||
self._x = v
|
||||
|
||||
f = Foo(1)
|
||||
print(f.x)
|
||||
#: getter
|
||||
#: 1
|
||||
|
||||
f.x = 99
|
||||
print(f.x)
|
||||
print(f._x)
|
||||
#: setter
|
||||
#: getter
|
||||
#: 99
|
||||
#: 99
|
||||
|
|
Loading…
Reference in New Issue