[Issue 24181] dmd compiled tinylisp.c runtime error

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Oct 18 15:18:32 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=24181

Dennis <dkorpel at live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel at live.nl

--- Comment #4 from Dennis <dkorpel at live.nl> ---
I tried reducing it with dustmite, but it's still pretty large:

```
int printf();
int sscanf();
char *strcpy();
int strcmp();
long strlen();

unsigned hp, sp = 1024, ATOM, PRIM, CONS = 0x7ffa, NIL;
double cell[1024], nil, tru, err, env;
double box(unsigned t, unsigned i)
{
        double x;
        *(long *)&x = (long)t << 48 | i;
        return x;
}
unsigned ord(double x) { return *(unsigned *)&x; }
unsigned equ(double x, double y) { return *(unsigned *)&x == *(long *)&y; }
double atom(char *s)
{
        unsigned i = 0;
        while (i < hp && strcmp((char *)cell + i, s))
                i += 1;
        hp += strlen(strcpy((char *)cell + i, s)) + 1;
        return box(ATOM, i);
}
double cons(double x, double y)
{
        cell[--sp] = x;
        cell[--sp] = y;
        return box(CONS, sp);
}
double car(double p)
{
        return p ? cell[ord(p) + 1] : err;
}
double cdr(double p)
{
        return p ? cell[ord(p)] : err;
}
double pair(double v, double x, double e)
{
        return cons(cons(v, x), e);
}
double assoc(double v, double e)
{
        while (e && !equ(v, car(car(e))))
                e = cdr(e);
        return e ? cdr(car(e)) : err;
}
unsigned not(double x) { return x == NIL; }
double f_add(double t)
{
        double n;
        while (!not(t = cdr(t)))
                n += car(t);
        return n;
}
struct
{
        char *s;
        double (*f)();
} prim[] = {
    "-", 0,
    "+", f_add,
};
double apply(double f, double t)
{
        return f ? prim[ord(f)].f(t) : f;
}
double eval(double x, double e)
{
        return *(long *)&x >> 48 == ATOM ? assoc(x, e) : x ? apply(eval(car(x),
e), x) : x;
}
char buf[40], see;
char* input = "(+ 4 4)";
int inputI = 0;
void look()
{
        see = input[inputI++];
}
unsigned seeing(char c) { return c ? see <= c : see; }
char get()
{
        char c = see;
        look();
        return c;
}
char scan()
{
        int i = 0;
        while (seeing(' '))
                look();
        return buf[i++] = get();
}
double parse();
double list()
{
        double x;
        return scan() == ')' ? nil : (x = parse(), cons(x, list()));
}
double atomic()
{
        double n;
        int i;
        return sscanf(buf, "%lg%n", &n, &i) ? n : atom(buf);
}
double parse()
{
        return *buf == '(' ? list() : atomic();
}
void print(double x)
{
        // printf("%016lX\n", *(unsigned long*)&x);
        if (x == ATOM)
                printf("ERR");
        else
                printf("%.10lg", x);
}
int main()
{
        int i;
        for (i = 0; prim[i].s; ++i)
                env = pair(atom(prim[i].s), box(PRIM, i), env);

        double result = eval((scan(), parse()), env);
        print(result);
}
```

This program prints 8 with gcc, but ERR with dmd on linux 64-bit.

--


More information about the Digitalmars-d-bugs mailing list