c2a4061997-02-06Fredrik Hübinette (Hubbe) #define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) ) class stack { int ptr; mixed *arr=allocate(32); void push(mixed val) { if(ptr==sizeof(arr)) arr+=allocate(ptr); arr[ptr++]=val; }
1bbfe11997-06-20Henrik Grubbström (Grubba)  mixed top() { if (ptr) { return(arr[ptr-1]); } error("Stack underflow\n"); }
3cda061997-03-08Henrik Grubbström (Grubba)  mixed pop(void|int val)
c2a4061997-02-06Fredrik Hübinette (Hubbe)  { mixed foo;
3cda061997-03-08Henrik Grubbström (Grubba)  if (val) { if (ptr <= 0) { error("Stack underflow\n"); } if (ptr < val) { val = ptr; } ptr -= val; foo = arr[ptr..ptr + val - 1]; for (int i=0; i < val; i++) { arr[ptr + i] = 0; /* Don't waste references */ } } else { if(--ptr < 0) error("Stack underflow\n");
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
3cda061997-03-08Henrik Grubbström (Grubba)  foo=arr[ptr]; arr[ptr]=0; /* Don't waste references */ }
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return foo; } void reset() { arr=allocate(32); ptr=0; } };