lisp toy: strangest warning I've seen

R

Richard Tobin

(lp=malloc(sizeof*lp)) || error("poof!");
[/QUOTE]
Not answering your question here, but... C is not PERL. Blech-an'a-half.

But that style is perfectly idiomatic in Lisp, so given the context
it's strange to attribute it to Perl.

-- Richard
 
L

luserXtrog

I'm having difficulty with the termination indicator for
6bit encoded characters packed into longs, as described in
the McCarthy paper. I'm trying to use -1U for all bits set,
but I'm missing something somewhere.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int error(char *s){ fprintf(stderr, "%s\n", s); exit(EXIT_FAILURE); }

union word {
long i;
struct sexpr *link;
};

struct sexpr {
bool atom;
union word car;
struct { struct sexpr *link; } cdr;
};

bool atom(struct sexpr *sp) { return (sp && sp->atom); }


char *charset = "0123456789"
"abcdefghijklmnopqrstuvwxyz" ;

struct sexpr *assc(char *pnom) {
struct sexpr *ascl, *asct;
char *code, *cp = pnom;
int c=24;
for ( ((ascl=malloc(sizeof*ascl)) || error("poof!")), asct=ascl,
asct->car.i=-1U;
*cp && (code=strchr(charset, *cp));
cp++, c-=6 )
{
if ( (cp-pnom) && !((cp-pnom)%5) ) { //5 6bit codes fit in
32bits
(asct->cdr.link=malloc(sizeof*asct)) || error("poof!");
asct=asct->cdr.link;
asct->car.i=0;
}
asct->car.i&= (code-charset)<<c;;
}
asct->cdr.link=NULL;
return ascl;
}

char *pname(struct sexpr *ascl) {
struct sexpr *asct=ascl;
char *pnom, *cp;
size_t sz;
((pnom=malloc(sz=6)) || error("poof"));
cp=pnom;
while (asct) {
for ( int i=0, c=24;
i<5;
i++, c-=6, cp++ )
{
int in;
in= (asct->car.i>>c)%64
if(in==-1U) *cp=0, goto ret;
*cp= charset[ in ];
}
((pnom=realloc(pnom,sz+=5)) || error("poof"));
asct=asct->cdr.link;
}
ret:return pnom;
}

bool print(FILE *f, struct sexpr *sp) {
if (sp) {
if (sp->atom) {
fprintf(f, "%d %s\n", sp->car.i, pname(sp->cdr.link));
} else {
print(f,sp->car.link);
print(f,sp->cdr.link);
}
return true;
} else return false;
}




struct sexpr *globl;

bool init() {
struct sexpr *lp, *h;
(lp=malloc(sizeof*lp)) || error("poof!");
h=lp;
for(int *i = (int[]){ 42, 69, 613, 0 }; *i; i++) {
struct sexpr *neu;
lp->atom = false;
(neu=malloc(sizeof*neu)) || error("poof!");
neu->atom = true;
neu->car.i = *i;
neu->cdr.link = assc("foo");
lp->car.link = neu;
(lp->cdr.link=malloc(sizeof*lp)) || error("poof!");
lp = lp->cdr.link;
}
free(lp);
lp=NULL;
globl=h;
return true;
}

bool run() {
//while (print(stdout,eval(read(stdin)))) ;
print(stdout,globl);
return true;
}

int main() {
return init()&&run()?0:EXIT_FAILURE;
}

/*eof*/
 
B

Barry Schwarz

I'm having difficulty with the termination indicator for
6bit encoded characters packed into longs, as described in
the McCarthy paper. I'm trying to use -1U for all bits set,
but I'm missing something somewhere.
snip

            in= (asct->car.i>>c)%64

0 <= in <= 63
            if(in==-1U) *cp=0, goto ret;

-1U == UINT_MAX >= 32767U (or is it 65535)

In either case, your if condition can never be true.

snip
 
L

luserXtrog

0 <= in <= 63


-1U == UINT_MAX >= 32767U (or is it 65535)

In either case, your if condition can never be true.

snip

Oh, right. I guess 63 would serve better.
I'm sure there are other issues with signed shifting;
and the whole malloc madness. I'm starting over with
a big array of unsigned longs, and I'm thinking of
packing four signed ascii bytes in each for the list,
last byte negative. Real pointers are getting
disasterous.

This is all just a horribly extended tangent from
what I'm actually trying to solve which is a decent
way to implement the postscript save/restore mechanism.
It appears to have been borrowed from lisp as a dynamic
scoping mechanism, but for postscript, it initially
had to double as the only means of freeing memory.

All off-topic, of course, unless it involves some
actual c-code illustrating the problem. Chicken <> Egg.

Thanks again. I hope someday to resurrect your NeWS
programs, once this interpreter functions sufficiently.
(Assuming you're the same Barry Schwarz)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top