Skip to content

Instantly share code, notes, and snippets.

@waywardmonkeys
Last active August 29, 2015 13:56
Show Gist options
  • Save waywardmonkeys/9068820 to your computer and use it in GitHub Desktop.
Save waywardmonkeys/9068820 to your computer and use it in GitHub Desktop.
Embedding Dylan - An example of calling Dylan from C
Module: embed-dylan
Synopsis:
Author:
Copyright:
define function adder (a :: <integer>, b :: <integer>) => (c :: <integer>)
a + b
end;
define c-callable-wrapper of adder
parameter a :: <C-int>;
parameter b :: <C-int>;
result c :: <C-int>;
c-name: "adder";
end;
define function string-size (s :: <string>) => (size :: <integer>)
s.size
end;
define c-callable-wrapper of string-size
parameter s :: <C-string>;
result size :: <C-int>;
c-name: "string_size";
end;
Library: embed-dylan
Target-Type: dll
Files: library
embed-dylan
Module: dylan-user
define library embed-dylan
use dylan;
use c-ffi;
end library;
define module embed-dylan
use dylan;
use c-ffi;
end module;
#include <stdio.h>
extern int adder(int, int);
extern int string_size(const char *);
int main ()
{
printf("2 + 2 = %d\n", adder(2, string_size("ab")));
return 0;
}
all: build
.PHONY: build test
LIB_SOURCES = $(wildcard *.dylan) \
embed-dylan.lid
build: $(LIB_SOURCES)
mkdir -p _build/bin
dylan-compiler -build embed-dylan.lid
cc -arch i386 -o _build/bin/embed-dylan main.c -lembed-dylan -L_build/lib -Wl,-rpath,@executable_path/../lib/
clean:
rm -rf _build/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment