add support for Enums

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11288 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Baozeng Ding 2009-06-20 02:44:06 +00:00
commit ed84d6b162
19 changed files with 865 additions and 222 deletions

View file

@ -4,7 +4,7 @@ example
# Call our gcd() function
x = 42;
x = -2;
y = 105;
g = example.gcd(x,y);
printf("The gcd of %d and %d is %d\n",x,y,g);

View file

@ -1,32 +1,22 @@
// builder the *.so
exec builder.sce;
exec example.sce;
//loader the *.so
exec loader.sce;
printf("ICONST = %i (should be 42)\n", ICONST());
printf("FCONST = %f (should be 2.1828)\n", FCONST());
printf("CCONST = %c (should be x)\n", CCONST());
printf("CCONST2 = %s (this should be on a new line)\n", CCONST2());
printf("SCONST = %s (should be Hello World)\n", SCONST());
printf("SCONST2 = %s (should be Hello World)\n", SCONST2());
printf("EXPR = %f (should be 48.5484)\n", EXPR());
printf("iconst = %i (should be 37)\n", iconst());
printf("fconst = %f (should be 3.14)\n", fconst());
printf("ICONST = %i (should be 42)\n", example.ICONST);
printf("FCONST = %f (should be 2.1828)\n",example. FCONST);
printf("CCONST = %c (should be ''x'')\n", example.CCONST);
printf("CCONST2 = %s (this should be on a new line)\n", example.CCONST2);
printf("SCONST = %s (should be ''Hello World'')\n", example.SCONST);
printf("SCONST2 = %s (should be "'""Hello World"""')\n", example.SCONST2);
printf("EXPR = %f (should be 48.5484)\n",example.EXPR);
printf("iconst = %i (should be 37)\n", example.iconst);
printf("fconst = %f (should be 3.14)\n", example.fconst);
try
printf("EXTERN = %s (Arg! This should not printf(anything)\n", EXTERN());
printf("EXTERN = %s (Arg! This should not printf(anything)\n", example.EXTERN);
catch
printf("EXTERN is not defined (good)\n");
printf("EXTERN is not defined (good)\n");
end
try
printf("FOO = %i (Arg! This should not printf(anything)\n", FOO());
printf("FOO = %i (Arg! This should not printf(anything)\n", example.FOO);
catch
printf("FOO is not defined (good)\n");
printf("FOO is not defined (good)\n");
end

View file

@ -0,0 +1,23 @@
/* File : example.c */
/* A global variable */
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
int fact(int n) {
if (n <= 0) return 1;
return n*fact(n-1);
}

View file

@ -0,0 +1,21 @@
/* File : example.i */
%module example
%contract gcd(int x, int y) {
require:
x >= 0;
y >= 0;
}
%contract fact(int n) {
require:
n >= 0;
ensure:
fact >= 1;
}
%inline %{
extern int gcd(int x, int y);
extern int fact(int n);
extern double Foo;
%}

View file

@ -0,0 +1,15 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.c
TARGET = example
INTERFACE = example.i
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab
clean::
$(MAKE) -f $(TOP)/Makefile scilab_clean
rm -f *.sce *.so lib*lib.c
check: all

View file

@ -0,0 +1,34 @@
// builder the *.so
exec builder.sce;
// loader the *.so
exec loader.sce;
// Call our gcd() function
x = 42;
y = 105;
g = gcd(x,y);
printf("The gcd of %d and %d is %d\n",x,y,g);
// Call our fact() function
x=5;
g=fact(x);
printf("The fact of %d is %d",x,g);
// Manipulate the Foo global variable
// Output its current value
printf("Foo = %f\n",Foo_get());
// Change its value
Foo_set (3.1415926);
// See if the change took effect
printf("Foo = %f\n", Foo_get());
//Call our gcd() function to test the contract conditon
x=-42;
y=105;
g=gcd(x,y);
printf("The gcd of %d and %d is %d\n",x,y,g);

View file

@ -0,0 +1,16 @@
/* File : example.c */
#include "example.h"
#include <stdio.h>
void enum_test(color c) {
if (c == RED) {
sciprint("color = RED, ");
} else if (c == BLUE) {
sciprint("color = BLUE, ");
} else if (c == GREEN) {
sciprint("color = GREEN, ");
} else {
sciprint("color = Unknown color!, ");
}
}

View file

@ -0,0 +1,6 @@
/* File : example.h */
typedef enum { RED, BLUE, GREEN } color;
void enum_test(color c);

View file

@ -0,0 +1,11 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Let's just grab the original header file here */
%include "example.h"

View file

@ -0,0 +1,15 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.c
TARGET = example
INTERFACE = example.i
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab
clean::
$(MAKE) -f $(TOP)/Makefile scilab_clean
rm -f *.sce *.so lib*lib.c
check: all

View file

@ -0,0 +1,22 @@
// builder the *.so
exec builder.sce;
// loader the *.so
exec loader.sce;
exec example.sce;
// Print out the value of some enums
printf("*** color ***\n");
printf(" RED = %i\n", color.RED);
printf(" BLUE = %i\n", color.BLUE);
printf(" GREEN = %i\n", color.GREEN);
printf("\nTesting use of enums with functions\n");
enum_test(color.RED);
enum_test(color.BLUE);
enum_test(color.GREEN);
enum_test(1234);

View file

@ -1,7 +1,7 @@
// builder the *.so
exec builder.sce;
//loader the *.so
// loader the *.so
exec loader.sce;
// Call our gcd() function
@ -11,7 +11,7 @@ y = 105;
g = gcd(x,y);
printf("The gcd of %d and %d is %d\n",x,y,g);
//Manipulate the Foo global variable
// Manipulate the Foo global variable
// Output its current value
Foo_get()
@ -19,6 +19,6 @@ Foo_get()
// Change its value
Foo_set(3.1415926)
//See if the change took effect
// See if the change took effect
Foo_get()

View file

@ -28,18 +28,18 @@ char *strvar=0;
/* A debugging function to print out their values */
void print_vars() {
sciprint("ivar = %d\n", ivar);
sciprint("svar = %d\n", svar);
sciprint("lvar = %ld\n", lvar);
sciprint("uivar = %u\n", uivar);
sciprint("usvar = %u\n", usvar);
sciprint("ulvar = %lu\n", ulvar);
sciprint("scvar = %d\n", scvar);
sciprint("ucvar = %u\n", ucvar);
sciprint("fvar = %g\n", fvar);
sciprint("dvar = %g\n", dvar);
sciprint("cvar = %c\n", cvar);
sciprint("strvar = %s\n",strvar);
printf("ivar = %d\n", ivar);
printf("svar = %d\n", svar);
printf("lvar = %ld\n", lvar);
printf("uivar = %u\n", uivar);
printf("usvar = %u\n", usvar);
printf("ulvar = %lu\n", ulvar);
printf("scvar = %d\n", scvar);
printf("ucvar = %u\n", ucvar);
printf("fvar = %g\n", fvar);
printf("dvar = %g\n", dvar);
printf("cvar = %c\n", cvar);
printf("strvar = %s\n",strvar);
}