Skip to content

Instantly share code, notes, and snippets.

@lindenb

lindenb/Makefile Secret

Created May 24, 2024 07:10
Show Gist options
  • Save lindenb/7b7793ced81fc3a4a566333f5149d65a to your computer and use it in GitHub Desktop.
Save lindenb/7b7793ced81fc3a4a566333f5149d65a to your computer and use it in GitHub Desktop.
guile program scanning integers
#include <stdio.h>
#include <libguile.h>
struct Global {
/** inputstream */
FILE* in;
/** currrent value */
int value;
/** contains script as string */
char buffer[2048];
};
static struct Global global;
/** retrieve the next record, return #t on success */
static SCM _next () {
int ret = fscanf(global.in,"%d", &(global.value));
if(ret!=1) return SCM_BOOL_F;
return SCM_BOOL_T;
}
/** get current number */
static SCM _get () {
return scm_from_int(global.value);
}
/** output current number */
static SCM _emit () {
fprintf(stderr,"%d\n",global.value);
return SCM_BOOL_T;
}
/** dispose memory associated */
static SCM _dispose () {
return SCM_BOOL_T;
}
static void*
inner_main (void *data)
{
struct Global* g= (struct Global*)data;
scm_c_define_gsubr("_next", 0, 0, 0, _next);
scm_c_define_gsubr("_get", 0, 0, 0, _get);
scm_c_define_gsubr("_emit", 0, 0, 0, _emit);
scm_c_define_gsubr("_dispose", 0, 0, 0, _dispose);
scm_c_eval_string(g->buffer);
return 0;
}
int main(int argc,char** argv) {
if(argc!=2) return -1;
global.value=1;
global.in = stdin;
sprintf(global.buffer,"(while (_next) (if %s (_emit) ) (_dispose) ) ",argv[1]);
scm_with_guile(inner_main,(void*)&global);
return 0;
}
GUILE_VERSION=2.2
# Tell the C compiler where to find <libguile.h>
CFLAGS=`pkg-config --cflags guile-$(GUILE_VERSION)`
# Tell the linker what libraries to use and where to find them.
LIBS=`pkg-config --libs guile-$(GUILE_VERSION)`
test: a.out
seq 1 100 | ./a.out '(= (modulo (_get) 7) 0)'
a.out: filter.c
$(CC) $(CFLAGS) -o $@ $< $(LIBS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment