summaryrefslogtreecommitdiff
path: root/Examples/mzscheme
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/mzscheme')
-rw-r--r--Examples/mzscheme/check.list3
-rw-r--r--Examples/mzscheme/multimap/Makefile13
-rw-r--r--Examples/mzscheme/multimap/example.c53
-rw-r--r--Examples/mzscheme/multimap/example.i91
-rw-r--r--Examples/mzscheme/multimap/example.scm27
-rw-r--r--Examples/mzscheme/simple/Makefile13
-rw-r--r--Examples/mzscheme/simple/README1
-rw-r--r--Examples/mzscheme/simple/example.c24
-rw-r--r--Examples/mzscheme/simple/example.i16
-rw-r--r--Examples/mzscheme/simple/example.scm24
-rw-r--r--Examples/mzscheme/std_vector/Makefile19
-rw-r--r--Examples/mzscheme/std_vector/example.h25
-rw-r--r--Examples/mzscheme/std_vector/example.i17
-rw-r--r--Examples/mzscheme/std_vector/example.scm54
14 files changed, 380 insertions, 0 deletions
diff --git a/Examples/mzscheme/check.list b/Examples/mzscheme/check.list
new file mode 100644
index 0000000..ae728ea
--- /dev/null
+++ b/Examples/mzscheme/check.list
@@ -0,0 +1,3 @@
+# see top-level Makefile.in
+multimap
+simple
diff --git a/Examples/mzscheme/multimap/Makefile b/Examples/mzscheme/multimap/Makefile
new file mode 100644
index 0000000..a3cfb8f
--- /dev/null
+++ b/Examples/mzscheme/multimap/Makefile
@@ -0,0 +1,13 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+all::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' mzscheme
+clean::
+ $(MAKE) -f $(TOP)/Makefile mzscheme_clean
+
+check: all
diff --git a/Examples/mzscheme/multimap/example.c b/Examples/mzscheme/multimap/example.c
new file mode 100644
index 0000000..b8360fa
--- /dev/null
+++ b/Examples/mzscheme/multimap/example.c
@@ -0,0 +1,53 @@
+/* File : example.c */
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+/* 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 gcdmain(int argc, char *argv[]) {
+ int x,y;
+ if (argc != 3) {
+ printf("usage: gcd x y\n");
+ return -1;
+ }
+ x = atoi(argv[1]);
+ y = atoi(argv[2]);
+ printf("gcd(%d,%d) = %d\n", x,y,gcd(x,y));
+ return 0;
+}
+
+int count(char *bytes, int len, char c) {
+ int i;
+ int count = 0;
+ for (i = 0; i < len; i++) {
+ if (bytes[i] == c) count++;
+ }
+ return count;
+}
+
+void capitalize(char *str, int len) {
+ int i;
+ for (i = 0; i < len; i++) {
+ str[i] = (char)toupper(str[i]);
+ }
+}
+
+void circle(double x, double y) {
+ double a = x*x + y*y;
+ if (a > 1.0) {
+ printf("Bad points %g, %g\n", x,y);
+ } else {
+ printf("Good points %g, %g\n", x,y);
+ }
+}
diff --git a/Examples/mzscheme/multimap/example.i b/Examples/mzscheme/multimap/example.i
new file mode 100644
index 0000000..515948a
--- /dev/null
+++ b/Examples/mzscheme/multimap/example.i
@@ -0,0 +1,91 @@
+/* File : example.i */
+%module example
+
+%{
+extern int gcd(int x, int y);
+extern int gcdmain(int argc, char *argv[]);
+extern int count(char *bytes, int len, char c);
+extern void capitalize (char *str, int len);
+extern void circle (double cx, double cy);
+extern int squareCubed (int n, int *OUTPUT);
+%}
+
+%include exception.i
+%include typemaps.i
+
+extern int gcd(int x, int y);
+
+%typemap(in) (int argc, char *argv[]) {
+ int i;
+ Scheme_Object **elms;
+ if (!SCHEME_VECTORP($input)) {
+ scheme_wrong_type("$name","vector",$argnum,argc,argv);
+ }
+ $1 = SCHEME_VEC_SIZE($input);
+ elms = SCHEME_VEC_ELS($input);
+ if ($1 == 0) {
+ scheme_wrong_type("$name","vector",$argnum,argc,argv);
+ }
+ $2 = (char **) malloc(($1+1)*sizeof(char *));
+ for (i = 0; i < $1; i++) {
+ if (!SCHEME_STRINGP(elms[i])) {
+ free($2);
+ scheme_wrong_type("$name","vector",$argnum,argc,argv);
+ }
+ $2[i] = SCHEME_STR_VAL(elms[i]);
+ }
+ $2[i] = 0;
+}
+
+%typemap(freearg) (int argc, char *argv[]) {
+ free($2);
+}
+extern int gcdmain(int argc, char *argv[]);
+
+%typemap(in) (char *bytes, int len) {
+ if (!SCHEME_STRINGP($input)) {
+ scheme_wrong_type("$name","string",1,argc,argv);
+ }
+ $1 = SCHEME_STR_VAL($input);
+ $2 = SCHEME_STRLEN_VAL($input);
+}
+
+extern int count(char *bytes, int len, char c);
+
+
+/* This example shows how to wrap a function that mutates a string */
+
+%typemap(in) (char *str, int len) {
+ if (!SCHEME_STRINGP($input)) {
+ scheme_wrong_type("$name","string",1,argc,argv);
+ }
+ $2 = SCHEME_STRLEN_VAL($input);
+ $1 = (char *) malloc($2+1);
+ memmove($1,SCHEME_STR_VAL($input),$2);
+}
+
+/* Return the mutated string as a new object. */
+
+%typemap(argout) (char *str, int len) {
+ Scheme_Object *s;
+ s = scheme_make_sized_string($1,$2,1);
+ SWIG_APPEND_VALUE(s);
+ free($1);
+}
+
+extern void capitalize(char *str, int len);
+
+/* A multi-valued constraint. Force two arguments to lie
+ inside the unit circle */
+
+%typemap(check) (double cx, double cy) {
+ double a = $1*$1 + $2*$2;
+ if (a > 1.0) {
+ SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
+ return NULL;
+ }
+}
+
+extern void circle(double cx, double cy);
+
+
diff --git a/Examples/mzscheme/multimap/example.scm b/Examples/mzscheme/multimap/example.scm
new file mode 100644
index 0000000..ac9f642
--- /dev/null
+++ b/Examples/mzscheme/multimap/example.scm
@@ -0,0 +1,27 @@
+;; run with mzscheme -r example.scm
+
+(load-extension "example.so")
+
+; Call the GCD function
+
+(define x 42)
+(define y 105)
+(define g (gcd x y))
+
+(display "The gcd of ")
+(display x)
+(display " and ")
+(display y)
+(display " is ")
+(display g)
+(newline)
+
+; Call the gcdmain() function
+(gcdmain #("gcdmain" "42" "105"))
+
+
+(display (count "Hello World" #\l))
+(newline)
+
+(display (capitalize "hello world"))
+(newline) \ No newline at end of file
diff --git a/Examples/mzscheme/simple/Makefile b/Examples/mzscheme/simple/Makefile
new file mode 100644
index 0000000..a3cfb8f
--- /dev/null
+++ b/Examples/mzscheme/simple/Makefile
@@ -0,0 +1,13 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+all::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' mzscheme
+clean::
+ $(MAKE) -f $(TOP)/Makefile mzscheme_clean
+
+check: all
diff --git a/Examples/mzscheme/simple/README b/Examples/mzscheme/simple/README
new file mode 100644
index 0000000..07e8da0
--- /dev/null
+++ b/Examples/mzscheme/simple/README
@@ -0,0 +1 @@
+Simple example from users manual.
diff --git a/Examples/mzscheme/simple/example.c b/Examples/mzscheme/simple/example.c
new file mode 100644
index 0000000..f2b0747
--- /dev/null
+++ b/Examples/mzscheme/simple/example.c
@@ -0,0 +1,24 @@
+/* Simple example from documentation */
+/* File : example.c */
+
+#include <time.h>
+
+double My_variable = 3.0;
+
+/* Compute factorial of n */
+int fact(int n) {
+ if (n <= 1) return 1;
+ else return n*fact(n-1);
+}
+
+/* Compute n mod m */
+int my_mod(int n, int m) {
+ return (n % m);
+}
+
+
+char *get_time() {
+ long ltime;
+ time(&ltime);
+ return ctime(&ltime);
+}
diff --git a/Examples/mzscheme/simple/example.i b/Examples/mzscheme/simple/example.i
new file mode 100644
index 0000000..5b3e955
--- /dev/null
+++ b/Examples/mzscheme/simple/example.i
@@ -0,0 +1,16 @@
+/* File : example.i */
+%module example
+%{
+/* Put headers and other declarations here */
+%}
+
+%include typemaps.i
+
+%rename(mod) my_mod;
+
+%inline %{
+extern double My_variable;
+extern int fact(int);
+extern int my_mod(int n, int m);
+extern char *get_time();
+%}
diff --git a/Examples/mzscheme/simple/example.scm b/Examples/mzscheme/simple/example.scm
new file mode 100644
index 0000000..8e20345
--- /dev/null
+++ b/Examples/mzscheme/simple/example.scm
@@ -0,0 +1,24 @@
+;; run with mzscheme -r example.scm
+
+(load-extension "example.so")
+
+(display (get-time))
+
+(printf "My-variable = ~a~n" (my-variable))
+
+(let loop ((i 0))
+ (when (< i 14) (begin (display i)
+ (display " factorial is ")
+ (display (fact i))
+ (newline)
+ (loop (+ i 1)))))
+
+(let loop ((i 1))
+ (when (< i 250)
+ (begin
+ (let loopi ((j 1))
+ (when (< j 250) (begin (my-variable (+ (my-variable) (mod i j)))
+ (loopi (+ j 1)))))
+ (loop (+ i 1)))))
+
+(printf "My-variable = ~a~n" (my-variable))
diff --git a/Examples/mzscheme/std_vector/Makefile b/Examples/mzscheme/std_vector/Makefile
new file mode 100644
index 0000000..e187269
--- /dev/null
+++ b/Examples/mzscheme/std_vector/Makefile
@@ -0,0 +1,19 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS =
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+GPP = `which g++`
+MZC = test -n "/usr/bin/mzc" && /usr/bin/mzc
+
+all::
+ $(SWIG) -mzscheme -c++ $(SWIGOPT) $(INTERFACE)
+ $(MZC) --compiler $(GPP) ++ccf "-I." --cc example_wrap.cxx
+ $(MZC) --linker $(GPP) --ld $(TARGET).so example_wrap.o
+
+clean:
+ $(MAKE) -f $(TOP)/Makefile mzscheme_clean
+
+check: all
diff --git a/Examples/mzscheme/std_vector/example.h b/Examples/mzscheme/std_vector/example.h
new file mode 100644
index 0000000..4f0dac7
--- /dev/null
+++ b/Examples/mzscheme/std_vector/example.h
@@ -0,0 +1,25 @@
+/* File : example.h */
+
+#include <vector>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+
+double average(std::vector<int> v) {
+ return std::accumulate(v.begin(),v.end(),0.0)/v.size();
+}
+
+std::vector<double> half(const std::vector<double>& v) {
+ std::vector<double> w(v);
+ for (unsigned int i=0; i<w.size(); i++)
+ w[i] /= 2.0;
+ return w;
+}
+
+void halve_in_place(std::vector<double>& v) {
+ // would you believe this is the same as the above?
+ std::transform(v.begin(),v.end(),v.begin(),
+ std::bind2nd(std::divides<double>(),2.0));
+}
+
+
diff --git a/Examples/mzscheme/std_vector/example.i b/Examples/mzscheme/std_vector/example.i
new file mode 100644
index 0000000..aa58b66
--- /dev/null
+++ b/Examples/mzscheme/std_vector/example.i
@@ -0,0 +1,17 @@
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+%include stl.i
+/* instantiate the required template specializations */
+namespace std {
+ %template(IntVector) vector<int>;
+ %template(DoubleVector) vector<double>;
+}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+
diff --git a/Examples/mzscheme/std_vector/example.scm b/Examples/mzscheme/std_vector/example.scm
new file mode 100644
index 0000000..0e4ac3f
--- /dev/null
+++ b/Examples/mzscheme/std_vector/example.scm
@@ -0,0 +1,54 @@
+;; run with mzscheme -r example.scm
+
+(load-extension "example.so")
+
+; repeatedly invoke a procedure with v and an index as arguments
+(define (with-vector v proc size-proc)
+ (let ((size (size-proc v)))
+ (define (with-vector-item v i)
+ (if (< i size)
+ (begin
+ (proc v i)
+ (with-vector-item v (+ i 1)))))
+ (with-vector-item v 0)))
+
+(define (with-intvector v proc)
+ (with-vector v proc intvector-length))
+(define (with-doublevector v proc)
+ (with-vector v proc doublevector-length))
+
+(define (print-doublevector v)
+ (with-doublevector v (lambda (v i) (display (doublevector-ref v i))
+ (display " ")))
+ (newline))
+
+
+; Call average with a Scheme list...
+
+(display (average '(1 2 3 4)))
+(newline)
+
+; ... or a wrapped std::vector<int>
+(define v (new-intvector 4))
+(with-intvector v (lambda (v i) (intvector-set! v i (+ i 1))))
+(display (average v))
+(newline)
+(delete-intvector v)
+
+; half will return a Scheme vector.
+; Call it with a Scheme vector...
+
+(display (half #(1 1.5 2 2.5 3)))
+(newline)
+
+; ... or a wrapped std::vector<double>
+(define v (new-doublevector))
+(map (lambda (i) (doublevector-push! v i)) '(1 2 3 4))
+(display (half v))
+(newline)
+
+; now halve a wrapped std::vector<double> in place
+(halve-in-place v)
+(print-doublevector v)
+(delete-doublevector v)
+