Skip to content

Commit 9de283c

Browse files
author
Alexander Dreyer
committed
chg: encapsulating newstruct's overloaded '=' and use in pyobject
1 parent 30fc061 commit 9de283c

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

Singular/newstruct.cc

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,37 @@ void * newstruct_Copy(blackbox*b, void *d)
132132
return (void*)lCopy_newstruct(n1);
133133
}
134134

135+
// Used by newstruct_Assign for overloaded '='
136+
BOOLEAN newstruct_equal(int op, leftv l, leftv r)
137+
{
138+
blackbox *ll=getBlackboxStuff(op);
139+
assume(ll->data != NULL);
140+
newstruct_desc nt=(newstruct_desc)ll->data;
141+
newstruct_proc p=nt->procs;
142+
143+
while( (p!=NULL) && ((p->t!='=')||(p->args!=1)) ) p=p->next;
144+
145+
if (p!=NULL)
146+
{
147+
leftv sl;
148+
idrec hh;
149+
memset(&hh,0,sizeof(hh));
150+
hh.id=Tok2Cmdname(p->t);
151+
hh.typ=PROC_CMD;
152+
hh.data.pinf=p->p;
153+
sleftv tmp;
154+
memset(&tmp,0,sizeof(sleftv));
155+
tmp.Copy(r);
156+
sl = iiMake_proc(&hh, NULL, &tmp);
157+
if (sl != NULL)
158+
{
159+
if (sl->Typ() == op) { l->Copy(sl); return FALSE;}
160+
else sl->CleanUp();
161+
}
162+
}
163+
return TRUE;
164+
}
165+
135166
BOOLEAN newstruct_Assign(leftv l, leftv r)
136167
{
137168
if (r->Typ()>MAX_TOK)
@@ -192,30 +223,8 @@ BOOLEAN newstruct_Assign(leftv l, leftv r)
192223
else
193224
{
194225
assume(l->Typ() > MAX_TOK);
195-
blackbox *ll=getBlackboxStuff(l->Typ());
196-
newstruct_desc nt=(newstruct_desc)ll->data;
197-
assume(l->Typ() == nt->id);
198-
newstruct_proc p=nt->procs;
199-
200-
while( (p!=NULL) && ((p->t!='=')||(p->args!=1)) ) p=p->next;
201-
202-
if (p!=NULL)
203-
{
204-
idrec hh;
205-
memset(&hh,0,sizeof(hh));
206-
hh.id=Tok2Cmdname(p->t);
207-
hh.typ=PROC_CMD;
208-
hh.data.pinf=p->p;
209-
sleftv tmp;
210-
memset(&tmp,0,sizeof(sleftv));
211-
tmp.Copy(r);
212-
leftv sl = iiMake_proc(&hh, NULL, &tmp);
213-
if (sl != NULL)
214-
{
215-
if (sl->Typ()==l->Typ()) return newstruct_Assign(l, sl);
216-
else sl->CleanUp();
217-
}
218-
}
226+
sleftv tmp;
227+
if(!newstruct_equal(l->Typ(), &tmp, r)) return newstruct_Assign(l, &tmp);
219228
}
220229
Werror("assign %s(%d) = %s(%d)",
221230
Tok2Cmdname(l->Typ()),l->Typ(),Tok2Cmdname(r->Typ()),r->Typ());

Singular/pyobject.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,20 @@ class PythonCastDynamic:
342342
case LIST_CMD: return PythonCastStatic<lists>(value);
343343
}
344344

345+
sleftv tmp;
346+
BOOLEAN newstruct_equal(int, leftv, leftv); // declaring overloaded '='
347+
if (!newstruct_equal(PythonInterpreter::id(), &tmp, value))
348+
return PythonCastStatic<>(&tmp);
349+
345350
if (typeId > MAX_TOK) // custom types
346351
{
347352
blackbox *bbx = getBlackboxStuff(typeId);
348-
sleftv tmp;
349-
if (! bbx->blackbox_Op1(PythonInterpreter::id(), &tmp, value) )
350-
return PythonCastStatic<>(&tmp);
353+
assume(bbx != NULL);
354+
if (! bbx->blackbox_Op1(PythonInterpreter::id(), &tmp, value))
355+
return PythonCastStatic<>(&tmp);
351356
}
352-
else
353-
Werror("type '%s` incompatible with 'pyobject`", iiTwoOps(typeId));
354357

358+
Werror("type '%s` incompatible with 'pyobject`", iiTwoOps(typeId));
355359
return PythonObject();
356360
}
357361
};

0 commit comments

Comments
 (0)