Last active
August 29, 2015 13:56
-
-
Save waywardmonkeys/9068820 to your computer and use it in GitHub Desktop.
Embedding Dylan - An example of calling Dylan from C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Library: embed-dylan | |
Target-Type: dll | |
Files: library | |
embed-dylan |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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