Skip to content

Commit

Permalink
chg: encapsulating newstruct's overloaded '=' and use in pyobject
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Dreyer committed Jun 27, 2012
1 parent 30fc061 commit 9de283c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
57 changes: 33 additions & 24 deletions Singular/newstruct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,37 @@ void * newstruct_Copy(blackbox*b, void *d)
return (void*)lCopy_newstruct(n1);
}

// Used by newstruct_Assign for overloaded '='
BOOLEAN newstruct_equal(int op, leftv l, leftv r)
{
blackbox *ll=getBlackboxStuff(op);
assume(ll->data != NULL);
newstruct_desc nt=(newstruct_desc)ll->data;
newstruct_proc p=nt->procs;

while( (p!=NULL) && ((p->t!='=')||(p->args!=1)) ) p=p->next;

if (p!=NULL)
{
leftv sl;
idrec hh;
memset(&hh,0,sizeof(hh));
hh.id=Tok2Cmdname(p->t);
hh.typ=PROC_CMD;
hh.data.pinf=p->p;
sleftv tmp;
memset(&tmp,0,sizeof(sleftv));
tmp.Copy(r);
sl = iiMake_proc(&hh, NULL, &tmp);
if (sl != NULL)
{
if (sl->Typ() == op) { l->Copy(sl); return FALSE;}
else sl->CleanUp();
}
}
return TRUE;
}

BOOLEAN newstruct_Assign(leftv l, leftv r)
{
if (r->Typ()>MAX_TOK)
Expand Down Expand Up @@ -192,30 +223,8 @@ BOOLEAN newstruct_Assign(leftv l, leftv r)
else
{
assume(l->Typ() > MAX_TOK);
blackbox *ll=getBlackboxStuff(l->Typ());
newstruct_desc nt=(newstruct_desc)ll->data;
assume(l->Typ() == nt->id);
newstruct_proc p=nt->procs;

while( (p!=NULL) && ((p->t!='=')||(p->args!=1)) ) p=p->next;

if (p!=NULL)
{
idrec hh;
memset(&hh,0,sizeof(hh));
hh.id=Tok2Cmdname(p->t);
hh.typ=PROC_CMD;
hh.data.pinf=p->p;
sleftv tmp;
memset(&tmp,0,sizeof(sleftv));
tmp.Copy(r);
leftv sl = iiMake_proc(&hh, NULL, &tmp);
if (sl != NULL)
{
if (sl->Typ()==l->Typ()) return newstruct_Assign(l, sl);
else sl->CleanUp();
}
}
sleftv tmp;
if(!newstruct_equal(l->Typ(), &tmp, r)) return newstruct_Assign(l, &tmp);
}
Werror("assign %s(%d) = %s(%d)",
Tok2Cmdname(l->Typ()),l->Typ(),Tok2Cmdname(r->Typ()),r->Typ());
Expand Down
14 changes: 9 additions & 5 deletions Singular/pyobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,20 @@ class PythonCastDynamic:
case LIST_CMD: return PythonCastStatic<lists>(value);
}

sleftv tmp;
BOOLEAN newstruct_equal(int, leftv, leftv); // declaring overloaded '='
if (!newstruct_equal(PythonInterpreter::id(), &tmp, value))
return PythonCastStatic<>(&tmp);

if (typeId > MAX_TOK) // custom types
{
blackbox *bbx = getBlackboxStuff(typeId);
sleftv tmp;
if (! bbx->blackbox_Op1(PythonInterpreter::id(), &tmp, value) )
return PythonCastStatic<>(&tmp);
assume(bbx != NULL);
if (! bbx->blackbox_Op1(PythonInterpreter::id(), &tmp, value))
return PythonCastStatic<>(&tmp);
}
else
Werror("type '%s` incompatible with 'pyobject`", iiTwoOps(typeId));

Werror("type '%s` incompatible with 'pyobject`", iiTwoOps(typeId));
return PythonObject();
}
};
Expand Down

0 comments on commit 9de283c

Please sign in to comment.