Add support for the Go programming language.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12108 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Ian Lance Taylor 2010-06-10 01:13:31 +00:00
commit 5af2978f77
259 changed files with 16159 additions and 14 deletions

View file

@ -0,0 +1,18 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.c
TARGET = example
INTERFACE = example.i
SWIGOPT =
all:: go
go::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
clean::
$(MAKE) -f $(TOP)/Makefile go_clean
check: all
$(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run

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,55 @@
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.1
*
* This file is not intended to be easily readable and contains a number of
* coding conventions designed to improve portability and efficiency. Do not make
* changes to this file unless you know what you are doing--modify the SWIG
* interface file instead.
* ----------------------------------------------------------------------------- */
package example
type _swig_fnptr *byte
type _swig_memberptr *byte
func _swig_allocatememory(int) *byte
func _swig_internal_allocate(len int) *byte {
return _swig_allocatememory(len)
}
func _swig_allocatestring(*byte, int) string
func _swig_internal_makegostring(p *byte, l int) string {
return _swig_allocatestring(p, l)
}
func _swig_internal_gopanic(p *byte, l int) {
panic(_swig_allocatestring(p, l))
}
func Gcd(int, int) int
func Gcdmain([]string) int
func Count(string, byte) int
func _swig_wrap_capitalize([]string)
func Capitalize(arg1 []string) {
_swig_wrap_capitalize(arg1)
}
func _swig_wrap_circle(float64, float64)
func Circle(arg1 float64, arg2 float64) {
_swig_wrap_circle(arg1, arg2)
}
type SwigcptrVoid uintptr
type Void interface {
Swigcptr() uintptr;
}
func (p SwigcptrVoid) Swigcptr() uintptr {
return uintptr(p)
}

View file

@ -0,0 +1,110 @@
/* 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);
%}
extern int gcd(int x, int y);
%typemap(go) (int argc, char *argv[]) "[]string"
%typemap(in) (int argc, char *argv[])
%{
{
int i;
_gostring_* a;
$1 = $input.len;
a = (_gostring_*) $input.array;
$2 = (char **) malloc (($1 + 1) * sizeof (char *));
for (i = 0; i < $1; i++) {
_gostring_ *ps = &a[i];
$2[i] = (char *) ps->p;
}
$2[i] = NULL;
}
%}
%typemap(argout) (int argc, char *argv[]) "" /* override char *[] default */
%typemap(freearg) (int argc, char *argv[])
%{
free($2);
%}
extern int gcdmain(int argc, char *argv[]);
%typemap(go) (char *bytes, int len) "string"
%typemap(in) (char *bytes, int len)
%{
$1 = $input.p;
$2 = $input.n;
%}
extern int count(char *bytes, int len, char c);
/* This example shows how to wrap a function that mutates a c string. A one
* element Go string slice is used so that the string can be returned
* modified.
*/
%typemap(go) (char *str, int len) "[]string"
%typemap(in) (char *str, int len)
%{
{
_gostring_ *a;
char *p;
int n;
a = (_gostring_*) $input.array;
p = a[0].p;
n = a[0].n;
$1 = malloc(n + 1);
$2 = n;
memcpy($1, p, n);
}
%}
/* Return the mutated string as a modified element in the array. */
%typemap(argout) (char *str, int len)
%{
{
_gostring_ *a;
a = (_gostring_*) $input.array;
a[0] = _swig_makegostring($1, $2);
}
%}
%typemap(freearg) (char *str, int len)
%{
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_gopanic("$1_name and $2_name must be in unit circle");
return;
}
}
%}
extern void circle(double cx, double cy);

View file

@ -0,0 +1,26 @@
package main
import (
"fmt"
. "./example"
)
func main() {
// Call our gcd() function
x := 42
y := 105
g := Gcd(x, y)
fmt.Println("The gcd of ", x, " and ", y, " is ", g)
// Call the gcdmain() function
args := []string{"gcdmain", "42", "105"}
Gcdmain(args)
// Call the count function
fmt.Println(Count("Hello World", 'l'))
// Call the capitalize function
capitalizeMe := []string{"hello world"}
Capitalize(capitalizeMe)
fmt.Println(capitalizeMe[0])
}