Skip to content

Commit

Permalink
Merge pull request #43 from YungLee/gsoc2012-scilab
Browse files Browse the repository at this point in the history
Gsoc2012 scilab
  • Loading branch information
sylvestre committed May 29, 2013
2 parents 6138b43 + d5b08c7 commit 0390648
Show file tree
Hide file tree
Showing 14 changed files with 629 additions and 53 deletions.
6 changes: 6 additions & 0 deletions Examples/scilab/contract/runme.sci
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Foo_set (3.1415926);
// See if the change took effect
printf("Foo = %f\n", Foo_get());

// Check error message if violate contract
g = gcd(-42,105);
fact(-4);



exit


Expand Down
9 changes: 6 additions & 3 deletions Examples/scilab/matrix2/matrixlib.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
double sumitems(double *first, int nbRow, int nbCol) {
int i;
double total;
Expand All @@ -14,14 +15,16 @@ void sumitems_argoutput(double *first, int nbRow, int nbCol,double** result,int*
*result=malloc(nbRow*nbCol*sizeof(double));
for (i=0; i<(nbRow*nbCol); i++) {
(*result)[i]=first[i]+first[i];
}
}
return;
}

double* getValues(int *numberOfRow, int *numberOfCol) {
*numberOfRow=23; *numberOfCol=3;
double *tempMatrix = (double*)malloc(sizeof(double) * *numberOfRow * *numberOfCol);
double *tempMatrix ;
int i;
*numberOfRow=23; *numberOfCol=3;
tempMatrix= (double*)malloc(sizeof(double )* *numberOfRow * *numberOfCol);

for (i=0; i<((*numberOfRow)*(*numberOfCol)); i++) {
tempMatrix[i]=i*2;
}
Expand Down
37 changes: 33 additions & 4 deletions Examples/scilab/matrix2/matrixlib.i
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,41 @@
%include matrix.i




%apply (double* matrixAsInput,int rows,int cols){(double *first, int nbRow, int nbCol)}
%apply (double** matrixAsArgOutput,int* rows,int* cols){(double **result,int* nbRowOut,int* nbColOut)}

%typemap(out) (double*)(int *nRow, int *nCol)
{
SciErr sciErr;
sciErr = createMatrixOfDouble(pvApiCtx, Rhs+$result, *nRow, *nCol, (double *)$1);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
AssignOutputVariable(pvApiCtx, outputPosition) = Rhs+$result;
free($1);
}
%typemap (in,numinputs=0) (int *numberOfRow, int *numberOfCol)
{
}
%typemap(arginit) (int *numberOfRow, int *numberOfCol)
{
$1 =(int*)malloc(sizeof(int));
$2 =(int*)malloc(sizeof(int));
nRow =$1;
nCol =$2;
}

%typemap(freearg) (int *numberOfRow, int *numberOfCol)
{
free($1);
free($2);
}



%inline {
extern void sumitems_argoutput(double *first, int nbRow, int nbCol,double **result,int* nbRowOut,int* nbColOut);
extern double* getValues(int *numberOfRow, int *numberOfCol);
extern void sumitems_argoutput(double *first, int nbRow, int nbCol,double **result,int* nbRowOut,int* nbColOut);
extern double* getValues(int *numberOfRow, int *numberOfCol);
}

3 changes: 1 addition & 2 deletions Examples/scilab/matrix2/runme.sci
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
exec loader.sce

myMatrix=[ 103 3 1 12;0 0 2043 1];
sumitems(myMatrix)

m=sumitems_argoutput(myMatrix)
myOtherMatrix=getValues();
size(myOtherMatrix)
disp(myOtherMatrix);
Expand Down
10 changes: 5 additions & 5 deletions Lib/scilab/matrix.i
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%typemap(in) (double* matrixAsInput, int rows, int cols) {
int *piAddr = NULL;
SciErr sciErr;
sciErr = getVarAddressFromPosition(pvApiCtx, $input, &piAddr);
if (sciErr.iErr) {
printError(&sciErr, 0);
Expand All @@ -12,9 +13,9 @@
}
}


%typemap(in,numinputs=0) (double** matrixAsArgOutput,int* rows, int* cols)
{

}

%typemap(arginit) (double** matrixAsArgOutput,int* rows, int* cols)
Expand All @@ -34,15 +35,14 @@

%typemap(argout) (double** matrixAsArgOutput,int* rows, int* cols)
{
sciErr = createMatrixOfDouble(pvApiCtx, iVarOut, *$2, *$3, (double *)*$1);
SciErr sciErr;
sciErr = createMatrixOfDouble(pvApiCtx, Rhs +$result, *$2, *$3, (double *)*$1);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
AssignOutputVariable(pvApiCtx, outputPosition) = Rhs +$result;

AssignOutputVariable(pvApiCtx, iOutNum) = iVarOut;
iOutNum++;
iVarOut++;
}


24 changes: 12 additions & 12 deletions Lib/scilab/scichar.swg
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ SwigScilabStringToChar(void *_pvApiCtx, int _iVar, char *_pcValue, char *_fname)
return SWIG_ERROR;
}

_pstStrings = (char *)MALLOC(sizeof(char));
_pstStrings = (char *)malloc(sizeof(char));
sciErr = getMatrixOfString(pvApiCtx, piAddrVar, &iRows, &iCols, &_piLength, (char **)&_pstStrings);
if (sciErr.iErr) {
printError(&sciErr, 0);
Expand All @@ -44,7 +44,7 @@ SwigScilabStringToChar(void *_pvApiCtx, int _iVar, char *_pcValue, char *_fname)
}
*_pcValue = _pstStrings[0];

FREE(_pstStrings);
free(_pstStrings);

return SWIG_OK;
}
Expand All @@ -58,7 +58,7 @@ SWIGINTERN int
SwigScilabStringFromChar(void *_pvApiCtx, int _iVarOut, char _chValue) {
SciErr sciErr;

char *pchValue = (char*)MALLOC(sizeof(char) * 2);
char *pchValue = (char*)malloc(sizeof(char) * 2);
pchValue[0] = _chValue;
pchValue[1] = '\0';

Expand All @@ -68,7 +68,7 @@ SwigScilabStringFromChar(void *_pvApiCtx, int _iVarOut, char _chValue) {
return SWIG_ERROR;
}

FREE(pchValue);
free(pchValue);

return Rhs + _iVarOut;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ SwigScilabStringToCharPtr(void *_pvApiCtx, int _iVar, char *_pcValue, int _iLeng
return SWIG_ERROR;
}

pstStrings = (char *)MALLOC(sizeof(char) * (piLength + 1));
pstStrings = (char *)malloc(sizeof(char) * (piLength + 1));
sciErr = getMatrixOfString(pvApiCtx, piAddrVar, &iRows, &iCols, &piLength, (char **)&pstStrings);
if (sciErr.iErr) {
printError(&sciErr, 0);
Expand All @@ -135,7 +135,7 @@ SwigScilabStringToCharPtr(void *_pvApiCtx, int _iVar, char *_pcValue, int _iLeng

strcpy(_pcValue, pstStrings);

FREE(pstStrings);
free(pstStrings);

return SWIG_OK;
}
Expand Down Expand Up @@ -177,7 +177,7 @@ SwigScilabStringToCharPtrAndSize(void *_pvApiCtx, int _iVar, char **_pcValue, si
return SWIG_ERROR;
}

_pstStrings = (char *)MALLOC(sizeof(char) * (piLength + 1));
_pstStrings = (char *)malloc(sizeof(char) * (piLength + 1));
sciErr = getMatrixOfString(pvApiCtx, piAddrVar, &iRows, &iCols, &piLength, (char **)&_pstStrings);
if (sciErr.iErr) {
printError(&sciErr, 0);
Expand All @@ -191,7 +191,7 @@ SwigScilabStringToCharPtrAndSize(void *_pvApiCtx, int _iVar, char **_pcValue, si
*_pcValue = _pstStrings;
}

FREE(_pstStrings);
free(_pstStrings);

if (_piLength != NULL) {
*_piLength = (size_t) piLength;
Expand All @@ -206,7 +206,7 @@ SwigScilabStringFromCharPtr(void *_pvApiCtx, int _iVarOut, const char *_pchValue
SciErr sciErr;
char **pstData = NULL;

pstData = (char **)MALLOC(sizeof(char *));
pstData = (char **)malloc(sizeof(char *));
pstData[0] = strdup(_pchValue);

sciErr = createMatrixOfString(_pvApiCtx, Rhs + _iVarOut, 1, 1, (char **)pstData);
Expand All @@ -215,7 +215,7 @@ SwigScilabStringFromCharPtr(void *_pvApiCtx, int _iVarOut, const char *_pchValue
return SWIG_ERROR;
}

freeArrayOfString(pstData, 1);
free(pstData[0]);

return Rhs + _iVarOut;
}
Expand All @@ -226,7 +226,7 @@ SwigScilabStringFromCharPtrAndSize(void *_pvApiCtx, int _iVarOut, const char *_p
SciErr sciErr;
char **pstData = NULL;

pstData = (char **)MALLOC(sizeof(char *));
pstData = (char **)malloc(sizeof(char *));
pstData[0] = strdup(_pchValue);

sciErr = createMatrixOfString(_pvApiCtx, Rhs + _iVarOut, 1, 1, (char **)pstData);
Expand All @@ -235,7 +235,7 @@ SwigScilabStringFromCharPtrAndSize(void *_pvApiCtx, int _iVarOut, const char *_p
return SWIG_ERROR;
}

FREE(pstData);
free(pstData[0]);

return Rhs + _iVarOut;
}
Expand Down
2 changes: 2 additions & 0 deletions Lib/scilab/scirun.swg
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ static void SWIG_Scilab_SetOutputPosition(int _outputPosition) {
#define Scilab_Error_Occurred() 0
#define SWIG_Scilab_AddErrorMsg(msg) {;}

#ifdef __cplusplus
namespace std {
class SciObject {
public:
SciObject();
};
}
#endif
53 changes: 47 additions & 6 deletions Lib/scilab/sciruntime.swg
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

#define %scilabcode %insert("scilab")

// Error message will be displayed inside Scilab fragment functions
// and the following line Will not work because code is not an int
//#define SWIG_Error(code, msg) Scierror(code, _("%s\n"), msg);

%insert(runtime) %{
/* Scilab standard headers */
Expand All @@ -28,8 +25,52 @@ extern "C" {

typedef int SciObject;


/* -----------------------------------------------------------------------------
* error manipulation
* ----------------------------------------------------------------------------- */

SWIGINTERN const char*
SWIG_Scilab_ErrorType(int code) {
switch(code) {
case SWIG_MemoryError:
return "MemoryError";
case SWIG_IOError:
return "IOError";
case SWIG_RuntimeError:
return "RuntimeError";
case SWIG_IndexError:
return "IndexError";
case SWIG_TypeError:
return "TypeError";
case SWIG_DivisionByZero:
return "ZeroDivisionError";
case SWIG_OverflowError:
return "OverflowError";
case SWIG_SyntaxError:
return "SyntaxError";
case SWIG_ValueError:
return "ValueError";
case SWIG_SystemError:
return "SystemError";
case SWIG_AttributeError:
return "AttributeError";
default:
return "RuntimeError";
}
}

SWIGINTERN void
SWIG_Scilab_ErrorMsg(int code, const char *mesg)
{
sciprint(_("SWIG/Scilab Error : %s\n%s"),SWIG_Scilab_ErrorType(code),mesg);
}



#define SWIG_fail return SWIG_ERROR;
#define SWIG_Error return SWIG_ERROR;
#define SWIG_ErrorType(code) SWIG_Scilab_ErrorType(code)
#define SWIG_Error(code, msg) SWIG_Scilab_ErrorMsg(code,msg)

/* Used for C++ enums */
//#define SWIG_AsVal_int(scilabValue, valuePointer) SWIG_SciDouble_AsInt(pvApiCtx, scilabValue, valuePointer, fname)
Expand Down Expand Up @@ -153,7 +194,7 @@ SWIG_Scilab_NewMemberObj(void *_pvApiCtx, int _iVarOut, void *_ptr, int _sz, swi
r = SWIG_PackData(r, _ptr, _sz);
strcpy(r, _type->name);

pstData = (char **)MALLOC(sizeof(char *));
pstData = (char **)malloc(sizeof(char *));
pstData[0] = strdup(r);

sciErr = createMatrixOfString(_pvApiCtx, Rhs + _iVarOut, 1, 1, (char **)pstData);
Expand All @@ -162,7 +203,7 @@ SWIG_Scilab_NewMemberObj(void *_pvApiCtx, int _iVarOut, void *_ptr, int _sz, swi
return SWIG_ERROR;
}

freeArrayOfString(pstData, 1);
free(pstData[0]);

return Rhs + _iVarOut;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Modules/d.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3760,7 +3760,7 @@ class D : public Language {
String *nspace = getNSpace();
if (nspace) {
// Check the root package/outermost namespace (a class A in module
// A.B leads to problems if another module A.C is also imported)
// A.B leads to problems if another module A.C is also imported)
if (Len(package) > 0) {
String *dotless_package = NewStringWithSize(package, Len(package) - 1);
if (Cmp(class_name, dotless_package) == 0) {
Expand Down
14 changes: 14 additions & 0 deletions Source/Modules/scilab.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ class SCILAB : public Language {
}
}
/* Add cleanup code */
for (param = functionParamsList; param;) {
String *tm;
if ((tm = Getattr(param, "tmap:freearg"))) {
if (tm && (Len(tm) != 0)) {
Replaceall(tm, "$source", Getattr(param, "lname"));
Printf(wrapper->code, "%s\n", tm);
Delete(tm);
}
param= Getattr(param, "tmap:freearg:next");
} else {
param = nextSibling(param);
}
}


/* Close the function(ok) */
Printv(wrapper->code, "return SWIG_OK;\n", NIL);
Expand Down
5 changes: 5 additions & 0 deletions swigwin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/Debug/
/swigwin.opensdf
/swigwin.sdf
/swigwin.vcxproj.user
/ipch/
Loading

0 comments on commit 0390648

Please sign in to comment.