Initial commit

This commit is contained in:
Bassem Girgis
2018-12-20 17:34:07 -06:00
parent 7a2d899662
commit 81b4b9e273
34743 changed files with 5940233 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
This directory contains examples for Guile.
constants -- handling #define and %constant literals
class -- classic c++ class example
matrix -- a very simple Matrix example
multimap -- typemaps with multiple sub-types
multivalue -- using the %values_as_list directive
port -- scheme ports as temporary FILE streams
simple -- the simple example from the user manual
std_vector -- C++ STL vector<int> and vector<double>
Note that the examples in this directory build a special version of
Guile which includes the wrapped functions in the top-level module.

View File

@@ -0,0 +1,9 @@
# see top-level Makefile.in
constants
class
port
simple
std_vector
matrix
multimap
multivalue

View File

@@ -0,0 +1,22 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' guile_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile_cpp
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='my-guile' INTERFACE='$(INTERFACE)' guile_static_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_clean

View File

@@ -0,0 +1,28 @@
/* File : example.cxx */
#include "example.h"
#define M_PI 3.14159265358979323846
/* Move the shape to a new location */
void Shape::move(double dx, double dy) {
x += dx;
y += dy;
}
int Shape::nshapes = 0;
double Circle::area() {
return M_PI*radius*radius;
}
double Circle::perimeter() {
return 2*M_PI*radius;
}
double Square::area() {
return width*width;
}
double Square::perimeter() {
return 4*width;
}

View File

@@ -0,0 +1,34 @@
/* File : example.h */
class Shape {
public:
Shape() {
nshapes++;
}
virtual ~Shape() {
nshapes--;
}
double x, y;
void move(double dx, double dy);
virtual double area() = 0;
virtual double perimeter() = 0;
static int nshapes;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { }
virtual double area();
virtual double perimeter();
};
class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { }
virtual double area();
virtual double perimeter();
};

View File

@@ -0,0 +1,9 @@
/* 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,60 @@
; file: runme.scm
; This file illustrates the proxy class C++ interface generated
; by SWIG.
(load-extension "./libexample" "scm_init_example_module")
; Convenience wrapper around the display function
; (which only accepts one argument at the time)
(define (mdisplay-newline . args)
(for-each display args)
(newline))
; ----- Object creation -----
(mdisplay-newline "Creating some objects:")
(define c (new-Circle 10))
(mdisplay-newline " Created circle " c)
(define s (new-Square 10))
(mdisplay-newline " Created square " s)
; ----- Access a static member -----
(mdisplay-newline "\nA total of " (Shape-nshapes) " shapes were created")
; ----- Member data access -----
; Set the location of the object
(Shape-x-set c 20)
(Shape-y-set c 30)
(Shape-x-set s -10)
(Shape-y-set s 5)
(mdisplay-newline "\nHere is their current position:")
(mdisplay-newline " Circle = (" (Shape-x-get c) "," (Shape-y-get c) ")")
(mdisplay-newline " Square = (" (Shape-x-get s) "," (Shape-y-get s) ")")
; ----- Call some methods -----
(mdisplay-newline "\nHere are some properties of the shapes:")
(define (shape-props o)
(mdisplay-newline " " o)
(mdisplay-newline " area = " (Shape-area o))
(mdisplay-newline " perimeter = " (Shape-perimeter o)))
(for-each shape-props (list c s))
(mdisplay-newline "\nGuess I'll clean up now")
; Note: this invokes the virtual destructor
(delete-Shape c)
(delete-Shape s)
(define s 3)
(mdisplay-newline (Shape-nshapes) " shapes remain")
(mdisplay-newline "Goodbye")
(exit 0)

View File

@@ -0,0 +1,17 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS =
TARGET = my-guile
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_augmented_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile_augmented
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_clean

View File

@@ -0,0 +1,27 @@
/* File : example.i */
%module example
/* A few preprocessor macros */
#define ICONST 42
#define FCONST 2.1828
#define CCONST 'x'
#define CCONST2 '\n'
#define SCONST "Hello World"
#define SCONST2 "\"Hello World\""
/* This should work just fine */
#define EXPR ICONST + 3*(FCONST)
/* This shouldn't do anything */
#define EXTERN extern
/* Neither should this (BAR isn't defined) */
#define FOO (ICONST + BAR)
/* The following directives also produce constants */
%constant int iconst = 37;
%constant double fconst = 3.14;
%include guilemain.i

View File

@@ -0,0 +1,19 @@
(or (= (ICONST) 42) (exit 1))
(display "ICONST = ")(display (ICONST))(display " (should be 42)\n")
(or (< (abs (- (FCONST) 2.1828)) 0.00001) (exit 1))
(display "FCONST = ")(display (FCONST))(display " (should be 2.1828)\n")
(or (char=? (CCONST) #\x) (exit 1))
(display "CCONST = ")(display (CCONST))(display " (should be 'x')\n")
(or (char=? (CCONST2) #\newline) (exit 1))
(display "CCONST2 = ")(display (CCONST2))(display " (this should be on a new line)\n")
(or (string=? (SCONST) "Hello World") (exit 1))
(display "SCONST = ")(display (SCONST))(display " (should be 'Hello World')\n")
(or (string=? (SCONST2) "\"Hello World\"") (exit 1))
(display "SCONST2 = ")(display (SCONST2))(display " (should be \"Hello World\")\n")
(or (< (abs (- (EXPR) (+ (ICONST) (* 3 (FCONST))))) 0.00001) (exit 1))
(display "EXPR = ")(display (abs (- (EXPR) (+ (ICONST) (* 3 (FCONST))))))(display " (should round to 0.0)\n")
(or (= (iconst) 37) (exit 1))
(display "iconst = ")(display (iconst))(display " (should be 37)\n")
(or (< (abs (- (fconst) 3.14)) 0.00001) (exit 1))
(display "fconst = ")(display (fconst))(display " (should be 3.14)\n")
(exit 0)

View File

@@ -0,0 +1,17 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS = matrix.c vector.c
TARGET = my-guile
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' GUILE_RUNOPTIONS='-e do-test' guile_augmented_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' LIBS='-lm' guile_augmented
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_clean

View File

@@ -0,0 +1,13 @@
Matrix example. To run the example, execute the program 'matrix' and
type the following :
(load "runme.scm")
(do-test 0)
Alternatively, use the command-line:
./my-guile -e do-test -s runme.scm
Or, if your operating system is spiffy enough:
./runme.scm

View File

@@ -0,0 +1,17 @@
/*** Matrix and vector example ***/
%module Matrix
%{
#include <math.h>
%}
%include guilemain.i
%include matrix.i
%include vector.i
// Include the math library so we can get some random numbers and
// other stuff
%include math.i
extern double drand48();

View File

@@ -0,0 +1,61 @@
/* FILE : matrix.c : some simple 4x4 matrix operations */
#include <stdlib.h>
#include <stdio.h>
double **new_matrix() {
int i;
double **M;
M = (double **) malloc(4*sizeof(double *));
M[0] = (double *) malloc(16*sizeof(double));
for (i = 0; i < 4; i++) {
M[i] = M[0] + 4*i;
}
return M;
}
void destroy_matrix(double **M) {
free(M[0]);
free(M);
}
void print_matrix(double **M) {
int i,j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%10g ", M[i][j]);
}
printf("\n");
}
}
void mat_mult(double **m1, double **m2, double **m3) {
int i,j,k;
double temp[4][4];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
temp[i][j] = 0;
for (k = 0; k < 4; k++)
temp[i][j] += m1[i][k]*m2[k][j];
}
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
m3[i][j] = temp[i][j];
}

View File

@@ -0,0 +1,36 @@
//
// FILE : matrix.i
%{
void set_m(double **M, int i, int j, double val) {
M[i][j] = val;
}
double get_m(double **M, int i, int j) {
return M[i][j];
}
%}
%inline {
/*** Matrix Operations ***/
extern double **new_matrix();
/* Creates a new matrix and returns a pointer to it */
extern void destroy_matrix(double **M);
/* Destroys the matrix M */
extern void print_matrix(double **M);
/* Prints out the matrix M */
extern void set_m(double **M, int i, int j, double val);
/* Sets M[i][j] = val*/
extern double get_m(double **M, int i, int j);
/* Returns M[i][j] */
extern void mat_mult(double **a, double **b, double **c);
/* Multiplies matrix a by b and places the result in c*/
}

View File

@@ -0,0 +1,210 @@
;;; Authors: David Beazley <beazley@cs.uchicago.edu>, 1999
;;; Martin Froehlich <MartinFroehlich@ACM.org>, 2000
;;;
;;; PURPOSE OF THIS FILE: This file is an example for how to use the guile
;;; scripting options with a little more than trivial script. Example
;;; derived from David Beazley's matrix evaluation example. David
;;; Beazley's annotation: >>Guile script for testing out matrix
;;; operations. Disclaimer : I'm not a very good scheme
;;; programmer<<. Martin Froehlich's annotation: >>I'm not a very good
;;; scheme programmer, too<<.
;;;
;;; Explanation: The three lines at the beginning of this script are
;;; telling the kernel to load the enhanced guile interpreter named
;;; "matrix"; to execute the function "do-test" (-e option) after loading
;;; this script (-s option). There are a lot more options wich allow for
;;; even finer tuning. SEE ALSO: Section "Guile Scripts" in the "Guile
;;; reference manual -- Part I: Preliminaries".
;;;
;;;
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;;; Create a zero matrix
(define (zero M)
(define (zero-loop M i j)
(if (< i 4)
(if (< j 4) (begin
(set-m M i j 0.0)
(zero-loop M i (+ j 1)))
(zero-loop M (+ i 1) 0))))
(zero-loop M 0 0))
;;; Create an identity matrix
(define (identity M)
(define (iloop M i)
(if (< i 4) (begin
(set-m M i i 1.0)
(iloop M (+ i 1)))))
(zero M)
(iloop M 0))
;;; Rotate around x axis
(define (rotx M r)
(define temp (new-matrix))
(define rd (/ (* r 3.14159) 180.0))
(zero temp)
(set-m temp 0 0 1.0)
(set-m temp 1 1 (cos rd))
(set-m temp 1 2 (- 0 (sin rd)))
(set-m temp 2 1 (sin rd))
(set-m temp 2 2 (cos rd))
(set-m temp 3 3 1.0)
(mat-mult M temp M)
(destroy-matrix temp))
;;; Rotate around y axis
(define (roty M r)
(define temp (new-matrix))
(define rd (/ (* r 3.14159) 180.0))
(zero temp)
(set-m temp 1 1 1.0)
(set-m temp 0 0 (cos rd))
(set-m temp 0 2 (sin rd))
(set-m temp 2 0 (- 0 (sin rd)))
(set-m temp 2 2 (cos rd))
(set-m temp 3 3 1.0)
(mat-mult M temp M)
(destroy-matrix temp))
;;; Rotate around z axis
(define (rotz M r)
(define temp (new-matrix))
(define rd (/ (* r 3.14159) 180.0))
(zero temp)
(set-m temp 0 0 (cos rd))
(set-m temp 0 1 (- 0 (sin rd)))
(set-m temp 1 0 (sin rd))
(set-m temp 1 1 (cos rd))
(set-m temp 2 2 1.0)
(set-m temp 3 3 1.0)
(mat-mult M temp M)
(destroy-matrix temp))
;;; Scale a matrix
(define (scale M s)
(define temp (new-matrix))
(define (sloop m i s)
(if (< i 4) (begin
(set-m m i i s)
(sloop m (+ i 1) s))))
(zero temp)
(sloop temp 0 s)
(mat-mult M temp M)
(destroy-matrix temp))
;;; Make a matrix with random elements
(define (randmat M)
(define (rand-loop M i j)
(if (< i 4)
(if (< j 4)
(begin
(set-m M i j (drand48))
(rand-loop M i (+ j 1)))
(rand-loop M (+ i 1) 0))))
(rand-loop M 0 0))
;;; stray definitions collected here
(define (rot-test M v t i)
(if (< i 360) (begin
(rotx M 1)
(rotz M -0.5)
(transform M v t)
(rot-test M v t (+ i 1)))))
(define (create-matrix) ; Create some matrices
(let loop ((i 0) (result '()))
(if (< i 200)
(loop (+ i 1) (cons (new-matrix) result))
result)))
(define (add-mat M ML)
(define (add-two m1 m2 i j)
(if (< i 4)
(if (< j 4)
(begin
(set-m m1 i j (+ (get-m m1 i j) (get-m m2 i j)))
(add-two m1 m2 i (+ j 1)))
(add-two m1 m2 (+ i 1) 0))))
(if (null? ML) *unspecified*
(begin
(add-two M (car ML) 0 0)
(add-mat M (cdr ML)))))
(define (cleanup ML)
(if (null? ML) *unspecified*
(begin
(destroy-matrix (car ML))
(cleanup (cdr ML)))))
(define (make-random ML) ; Put random values in them
(if (null? ML) *unspecified*
(begin
(randmat (car ML))
(make-random (cdr ML)))))
(define (mul-mat m ML)
(if (null? ML) *unspecified*
(begin
(mat-mult m (car ML) m)
(mul-mat m (cdr ML)))))
;;; Now we'll hammer on things a little bit just to make
;;; sure everything works.
(define M1 (new-matrix)) ; a matrix
(define v (createv 1 2 3 4)) ; a vector
(define t (createv 0 0 0 0)) ; the zero-vector
(define M-list (create-matrix)) ; get list of marices
(define M (new-matrix)) ; yet another matrix
(display "variables defined\n")
(define (do-test x)
(display "Testing matrix program...\n")
(identity M1)
(print-matrix M1)
(display "Rotate-x 45 degrees\n")
(rotx M1 45)
(print-matrix M1)
(display "Rotate y 30 degrees\n")
(roty M1 30)
(print-matrix M1)
(display "Rotate z 15 degrees\n")
(rotz M1 15)
(print-matrix M1)
(display "Scale 0.5\n")
(scale M1 0.5)
(print-matrix M1)
;; Rotating ...
(display "Rotating...\n")
(rot-test M1 v t 0)
(printv t)
(make-random M-list)
(zero M1)
(display "Adding them together (in Guile)\n")
(add-mat M1 M-list)
(print-matrix M1)
(display "Doing 200 multiplications (mostly in C)\n")
(randmat M)
(mul-mat M M-list)
(display "Cleaning up\n")
(cleanup M-list))

View File

@@ -0,0 +1,44 @@
/* File : vector.c */
#include <stdlib.h>
#include <stdio.h>
#include "vector.h"
Vector *createv(double x, double y, double z, double w) {
Vector *n;
n = (Vector *) malloc(sizeof(Vector));
n->x = x;
n->y = y;
n->z = z;
n->w = w;
return n;
}
/* Destroy vector */
void destroyv(Vector *v) {
free(v);
}
/* Print a vector */
void printv(Vector *v) {
printf("x = %g, y = %g, z = %g, w = %g\n", v->x, v->y, v->z, v->w);
}
/* Do a transformation */
void transform(double **m, Vector *v, Vector *r) {
r->x = m[0][0]*v->x + m[0][1]*v->y + m[0][2]*v->z + m[0][3]*v->w;
r->y = m[1][0]*v->x + m[1][1]*v->y + m[1][2]*v->z + m[1][3]*v->w;
r->z = m[2][0]*v->x + m[2][1]*v->y + m[2][2]*v->z + m[2][3]*v->w;
r->w = m[3][0]*v->x + m[3][1]*v->y + m[3][2]*v->z + m[3][3]*v->w;
}

View File

@@ -0,0 +1,10 @@
#include <math.h>
typedef struct {
double x;
double y;
double z;
double w;
} Vector;

View File

@@ -0,0 +1,22 @@
//
// FILE : vector.i
%{
#include "vector.h"
%}
%inline {
extern Vector *createv(double x,double y,double z,double w);
/* Creates a new vector v(x,y,z,w) */
extern void destroyv(Vector *v);
/* Destroys the vector v */
extern void printv(Vector *v);
/* Prints out the vector v */
extern void transform(double **T, Vector *v, Vector *t);
/* Transforms vector c to vector t by M*v --> t */
}

View File

@@ -0,0 +1,22 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS = example.c
TARGET = example
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' guile_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='my-guile' INTERFACE='$(INTERFACE)' guile_static
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_clean

View File

@@ -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);
}
}

View File

@@ -0,0 +1,98 @@
/* 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[]) %{
scm_t_array_handle handle;
size_t i;
size_t lenp;
ssize_t inc;
const SCM *v;
if (!(SCM_NIMP($input) && scm_is_vector($input))) {
SWIG_exception(SWIG_ValueError, "Expecting a vector");
return 0;
}
v = scm_vector_elements($input, &handle, &lenp, &inc);
$1 = (int)lenp;
if ($1 == 0) {
SWIG_exception(SWIG_ValueError, "Vector must contain at least 1 element");
}
$2 = (char **) malloc(($1+1)*sizeof(char *));
for (i = 0; i < $1; i++, v +=inc ) {
if (!(SCM_NIMP(*v) && scm_is_string(*v))) {
free($2);
SWIG_exception(SWIG_ValueError, "Vector items must be strings");
return 0;
}
$2[i] = scm_to_locale_string(*v);
}
$2[i] = 0;
scm_array_handle_release (&handle);
%}
%typemap(freearg) (int argc, char *argv[]) %{
for (i = 0; i < $1; i++) {
free($2[i]);
}
free($2);
%}
extern int gcdmain(int argc, char *argv[]);
%typemap(in) (char *bytes, int len) %{
if (!(SCM_NIMP($input) && scm_is_string($input))) {
SWIG_exception(SWIG_ValueError, "Expecting a string");
}
$1 = scm_to_locale_string($input);
$2 = scm_c_string_length($input);
%}
%typemap(freearg) (char *bytes, int len) %{
free($1);
%}
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) {
size_t temp;
$1 = SWIG_Guile_scm2newstr($input,&temp);
$2 = temp;
}
/* Return the mutated string as a new object. */
%typemap(argout) (char *str, int len) {
SWIG_APPEND_VALUE(scm_from_locale_stringn($1,$2));
if ($1) SWIG_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");
}
}
extern void circle(double cx, double cy);

View File

@@ -0,0 +1,30 @@
;;; Test out some multi-argument typemaps
(load-extension "./libexample" "scm_init_example_module")
; 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"))
; Call the count function
(display (count "Hello World" #\l))
(newline)
; Call the capitalize function
(display (capitalize "hello world"))
(newline)
(exit 0)

View File

@@ -0,0 +1,22 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS = example.c
TARGET = example
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' guile_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='my-guile' INTERFACE='$(INTERFACE)' guile_static
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_clean

View File

@@ -0,0 +1,18 @@
void divide_l(int a, int b, int *quotient_p, int *remainder_p)
{
*quotient_p = a/b;
*remainder_p = a%b;
}
void divide_v(int a, int b, int *quotient_p, int *remainder_p)
{
*quotient_p = a/b;
*remainder_p = a%b;
}
void divide_mv(int a, int b, int *quotient_p, int *remainder_p)
{
*quotient_p = a/b;
*remainder_p = a%b;
}

View File

@@ -0,0 +1,32 @@
/* -*- c -*- */
%module example;
%{
void divide_l(int a, int b, int *quotient_p, int *remainder_p);
void divide_v(int a, int b, int *quotient_p, int *remainder_p);
void divide_mv(int a, int b, int *quotient_p, int *remainder_p);
%}
/* Multiple values as lists. By default, if more than one value is to
be returned, a list of the values is created and returned; to switch
back to this behavior, use: */
%values_as_list;
void divide_l(int a, int b, int *OUTPUT, int *OUTPUT);
/* Multiple values as vectors. By issueing: */
%values_as_vector;
/* vectors instead of lists will be used. */
void divide_v(int a, int b, int *OUTPUT, int *OUTPUT);
/* Multiple values for multiple-value continuations.
(This is the most elegant way.) By issueing: */
%multiple_values;
/* multiple values are passed to the multiple-value
continuation, as created by `call-with-values' or the
convenience macro `receive'. (See the Scheme file.) */
void divide_mv(int a, int b, int *OUTPUT, int *OUTPUT);

View File

@@ -0,0 +1,67 @@
;;;; Show the three different ways to deal with multiple return values
(load-extension "./libexample" "scm_init_example_module")
;;; Multiple values as lists. By default, if more than one value is to
;;; be returned, a list of the values is created and returned. The
;;; procedure divide-l does so:
(let* ((quotient/remainder (divide-l 37 5))
;; divide-l returns a list of the two values, so get them:
(quotient (car quotient/remainder))
(remainder (cadr quotient/remainder)))
(display "37 divided by 5 is ")
(display quotient)
(display ", remainder ")
(display remainder)
(newline))
;;; Multiple values as vectors. You can get vectors instead of lists
;;; if you want:
(let* ((quotient-remainder-vector (divide-v 40 7))
;; divide-v returns a vector of two values, so get them:
(quotient (vector-ref quotient-remainder-vector 0))
(remainder (vector-ref quotient-remainder-vector 1)))
(display "40 divided by 7 is ")
(display quotient)
(display ", remainder ")
(display remainder)
(newline))
;;; Multiple values for multiple-value continuations. (The most
;;; elegant way.) You can get multiple values passed to the
;;; multiple-value continuation, as created by `call-with-values'.
(call-with-values (lambda ()
;; the "producer" procedure
(divide-mv 91 13))
(lambda (quotient remainder)
;; the "consumer" procedure
(display "91 divided by 13 is ")
(display quotient)
(display ", remainder ")
(display remainder)
(newline)))
;;; SRFI-8 has a very convenient macro for this construction:
(use-modules (srfi srfi-8))
;;; If your Guile is too old, you can define the receive macro yourself:
;;;
;;; (define-macro (receive vars vals . body)
;;; `(call-with-values (lambda () ,vals)
;;; (lambda ,vars ,@body)))
(receive (quotient remainder)
(divide-mv 111 19) ; the "producer" form
;; In the body, `quotient' and `remainder' are bound to the two
;; values.
(display "111 divided by 19 is ")
(display quotient)
(display ", remainder ")
(display remainder)
(newline))
(exit 0)

View File

@@ -0,0 +1,18 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS = example.c
TARGET = my-guile
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_augmented_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile_augmented
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_clean
rm -f test.out

View File

@@ -0,0 +1,2 @@
This example illustrates the translation from Scheme file ports to
temporary FILE streams. Read the source and run ./my-guile -s runme.scm

View File

@@ -0,0 +1,18 @@
#include <stdio.h>
#include <errno.h>
void print_int(FILE *f, int i)
{
if (fprintf(f, "%d\n", i)<0)
perror("print_int");
}
int read_int(FILE *f)
{
int i;
if (fscanf(f, "%d", &i)!=1) {
fprintf(stderr, "read_int: error reading from file\n");
perror("read_int");
}
return i;
}

View File

@@ -0,0 +1,15 @@
%module port
%include guilemain.i
/* Include the required FILE * typemaps */
%include ports.i
%{
#include <stdio.h>
%}
%inline %{
void print_int(FILE *f, int i);
int read_int(FILE *f);
%}

View File

@@ -0,0 +1,35 @@
;; Call with standard output
(print-int (current-output-port) 314159)
;; Redirection to a file. Note that the port is automatically flushed
;; (via force-output) before calling the C function, and that the C
;; function gets a temporary "FILE" stream, which is closed after the
;; call. So you can simply mix Scheme and C output.
(with-output-to-file "test.out"
(lambda ()
(display 4711)
(newline)
(print-int (current-output-port) 314159)
(display 815)
(newline)))
;; Redirection to a string or soft port won't work --
;; we can only handle file ports.
(catch #t
(lambda ()
(with-output-to-string
(lambda ()
(print-int (current-output-port) 314159))))
(lambda args
(display "Below shows that attempting to write to a string or soft port will result in a wrong-type-error...")
(newline)
(write args) (newline)))
;; Read from a file port. Note that it is a bad idea to mix Scheme and
;; C input because of buffering, hence the call to seek to rewind the file.
(with-input-from-file "test.out"
(lambda ()
(seek (current-input-port) 0 SEEK_SET)
(display (read-int (current-input-port)))
(newline)))

View File

@@ -0,0 +1,17 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS = example.c
TARGET = my-guile
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_augmented_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile_augmented
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_clean

View File

@@ -0,0 +1,9 @@
A very simple example.
To run it, start the program 'my-guile' and type:
(load "runme.scm")
Alternatively, you can use the shell command:
./my-guile -s runme.scm

View File

@@ -0,0 +1,21 @@
/* Simple example from documentation */
/* File : example.c */
#include <time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int mod(int n, int m) {
return (n % m);
}
char *get_time() {
long ltime;
time(&ltime);
return ctime(&ltime);
}

View File

@@ -0,0 +1,14 @@
/* File : example.i */
%module Example
%{
/* Put headers and other declarations here */
%}
%inline %{
extern double My_variable;
extern int fact(int);
extern int mod(int n, int m);
extern char *get_time();
%}
%include guile/guilemain.i

View File

@@ -0,0 +1,26 @@
(define (mdisplay-newline . args) ; does guile-1.3.4 have `format #t'?
(for-each display args)
(newline))
(mdisplay-newline (get-time) "My variable = " (My-variable))
(do ((i 0 (1+ i)))
((= 14 i))
(mdisplay-newline i " factorial is " (fact i)))
(define (mods i imax j jmax)
(if (< i imax)
(if (< j jmax)
(begin
(My-variable (+ (My-variable) (mod i j)))
(mods i imax (+ j 1) jmax))
(mods (+ i 1) imax 1 jmax))))
(mods 1 150 1 150)
(mdisplay-newline "My-variable = " (My-variable))
(exit (and (= 1932053504 (fact 13))
(= 745470.0 (My-variable))))

View File

@@ -0,0 +1,22 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
CXXSRCS =
TARGET = example
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' guile_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile_cpp
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='my-guile' INTERFACE='$(INTERFACE)' guile_static_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' guile_clean

View File

@@ -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));
}

View File

@@ -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"

View File

@@ -0,0 +1,54 @@
(load-extension "./libexample" "scm_init_example_module")
; 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)
(exit 0)