From: christoph Date: Mon, 2 Jan 2012 00:58:07 +0000 (+0100) Subject: withMulklib working the first time X-Git-Url: http://uxul.de/gitweb/?a=commitdiff_plain;p=lazyeval.git withMulklib working the first time --- diff --git a/lists.h b/lists.h new file mode 100644 index 0000000..9f9b781 --- /dev/null +++ b/lists.h @@ -0,0 +1,58 @@ +#ifndef __LISTS_H +#define __LISTS_H + +#include + +typedef int listElt; + +typedef struct { + listElt car; + void* cdr; /* cons ** cdr */ +} cons; + +typedef cons** list; + +list getNil () { + static cons nil1; + static cons * nil2 = &nil1; + return &nil2; +} + +bool isNil (list c) { + return getNil () == c; +} + +listElt car(list l) { + if (isNil(l)) { + printf("Called car of NIL!\n"); + fsync(0); + exit(-1); + } + return (*l)->car; +} + +list cdr (list l) { + if (isNil(l)) { + printf("Called cdr of NIL!\n"); + fsync(0); + exit(-1); + } + list rt = (list) (*l)->cdr; + return rt; +} + +listElt elt (list l, int eltnum) { + return (eltnum == 0) ? car(l) : elt (cdr(l), eltnum - 1); +} + +int len (list l) { + if (isNil(l)) return 0; + return len(cdr(l)); +} + +list doCons (int car, list cdr, cons * newCons) { + newCons->cdr = (void*) cdr; + newCons->car = car; +} + +#endif diff --git a/withMulklib.c b/withMulklib.c index f8c4b34..1da0e37 100644 --- a/withMulklib.c +++ b/withMulklib.c @@ -10,6 +10,8 @@ #define __USE_GNU #include +#include "lists.h" + /* This code is only for x86_64 gcc linux. It is NOT threadsafe. */ void* slice_alloc (size_t length) { @@ -105,6 +107,8 @@ void*** lazy_alloc (void* (*calculate) (void*), void* opt) { return ret; } +void initializeSignalHandler(); + void handle_segv(int segv, siginfo_t* siginfo, void* ucontext) { ucontext_t* uc = (ucontext_t*) ucontext; @@ -213,7 +217,7 @@ void handle_segv(int segv, siginfo_t* siginfo, void* ucontext) { intptr_t address = uc->uc_mcontext.gregs[setreg]; if (!bpt_has_key(pointer_tree, address)) { - printf("Address not found in Patricia tree.\n"); + printf("Address not found in Patricia tree: %d.\n", address); exit(-1); } @@ -229,20 +233,46 @@ inline void initializeSignalHandler () { struct sigaction q; bzero(&q, sizeof(q)); q.sa_sigaction = handle_segv; - q.sa_flags = SA_SIGINFO; + q.sa_flags = SA_SIGINFO | SA_NODEFER; sigaction(11, &q, NULL); } -void *calculateLazy24 (void* bla) { - int* ret = (int*) slice_alloc (sizeof(int)); - *ret = 42; +typedef struct { + int i; + int (*fun) (int, list); + list initial; +} listComprehensionRecursorR; + +void * listComprehensionRecursor (void* x) { + listComprehensionRecursorR* lcr = (listComprehensionRecursorR*) x; + int value = (lcr->fun)(lcr->i, lcr->initial); + lcr->i++; + + list cdr = (list) lazy_alloc(listComprehensionRecursor, (void*)lcr); + cons * ret = (cons*) slice_alloc(sizeof(cons)); + doCons(value, cdr, ret); return (void*) ret; } +list listComprehension (int (*fun) (int, list)) { + listComprehensionRecursorR* lcr = + (listComprehensionRecursorR*) slice_alloc + (sizeof(listComprehensionRecursorR)); + lcr->i = 0; + lcr->fun=fun; + lcr->initial = (list) lazy_alloc(listComprehensionRecursor, (void*)lcr); + return lcr->initial; +} + +int fib (int x, list l) { + return x == 0 ? 0 : (x == 1 ? 1 : elt(l, x-1) + elt(l, x-2)); +} + int main (void) { initializePointerTree (); initializeSignalHandler (); - void *** lazy42 = lazy_alloc(calculateLazy24, NULL); - printf("%d\n", **((int**)lazy42)); - + list lst = listComprehension(fib); + int i; + for (i = 0; i < 10; i++) + printf("%d\n", elt(lst, i)); }