Added options (display, tree type...)

This commit is contained in:
2021-01-25 14:59:32 +01:00
parent 804aa7d6b5
commit cfde68dd96
6 changed files with 184 additions and 74 deletions

64
stack.c
View File

@@ -1,13 +1,13 @@
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <signal.h>
#include "lceb.h"
static STACK *stacks=NULL;
static STACK *allstacks=NULL;
static int nstacks=0;
static int totalstacks=0;
static int laststack=0;
int totalstacks=0;
int laststack=0;
void print_stack(stack, details)
STACK *stack;
@@ -44,33 +44,31 @@ void print_stacks()
}
}
int keep_stack(stack)
STACK *stack;
{
stack->next=stacks;
stacks=stack;
nstacks++;
return 1;
}
STACK *new_stack(size, name, keep)
int size; /* 0 if empty */
int size; /* unused */
char *name;
int keep;
{
STACK *stack;
int *pelt;
int i;
# ifdef DEBUG_STACK
printf("new_stack(size=%d, name=[%s] last=%d total=%d)\n",
size, name, laststack, totalstacks);
# endif
if (!allstacks) {
totalstacks=ALLOCSIZE;
printf("new_stack %d: allocating %d stacks\n", laststack, totalstacks);
# ifdef DEBUG_MEM
printf("new_stack: allocating %d stacks\n", totalstacks);
# endif
allstacks=malloc(totalstacks*sizeof (STACK));
}
if (laststack==totalstacks) {
totalstacks+=ALLOCSIZE;
printf("new_stack %d: resizing stacks array to %d\n", laststack, totalstacks);
allstacks=realloc(stacks, totalstacks*sizeof (STACK));
# ifdef DEBUG_MEM
printf("new_stack: resizing stacks array to %d\n", totalstacks);
# endif
allstacks=realloc(allstacks, totalstacks*sizeof (STACK));
}
stack=allstacks+laststack;
@@ -86,8 +84,8 @@ STACK *new_stack(size, name, keep)
//pelt=malloc(size*sizeof(int));
stack->size=MAXINPUT;
//stack->stack=pelt;
if (keep)
keep_stack(stack);
//if (keep)
// keep_stack(stack);
for (i=0; i<MAXINPUT; ++i) {
//(pelt+i)->nop=0;
//(pelt+i)->curop=0;
@@ -107,13 +105,15 @@ int *push_stack(stack, val)
int size=stack->size;
int *pelt=stack->stack+stack->last;
#ifdef DEBUG_STACK
printf("push_stack(%d): last=%d size=%d\n", val, stack->last, stack->size);
#endif
# ifdef DEBUG_STACK
printf("push_stack(%d:[%s]): last=%d size=%d\n",
val, stack->name, stack->last, stack->size);
# endif
if (pos >= size) {
fprintf(stderr, "stack overflow: size=%d last=%d\n", size, pos);
return NULL;
fprintf(stderr, "stack overflow: size=%d last=%d. generating core.\n", size, pos);
raise(SIGABRT);
return NULL; /* useless, we died */
}
//pelt->nop=0;
//pelt->curop=0;
@@ -127,11 +127,12 @@ int *pop_stack(stack)
STACK *stack;
{
int pos=stack->last+1;
int size=stack->size;
int *pelt=stack->stack+stack->last;
if (pos==0) {
fprintf(stderr, "stack empty: size=%d last=%d\n", size, pos);
# ifdef DEBUG_STACK
printf("pop: empty stack [%s]: size=%d last=%d\n", stack->name, stack->size, pos);
# endif
return NULL;
}
stack->last--;
@@ -143,7 +144,7 @@ STACK *dup_stack(stack, name)
char *name;
{
STACK *new;
int *src=stack->stack, *dst;
int *dst;
int size=stack->size, last=stack->last, i;
new=new_stack(size, name, 0);
@@ -192,24 +193,25 @@ int permute_stack(array, n)
return 0;
}
int gen_stacks(stack)
void gen_stacks(stack)
STACK *stack;
{
char name[80];
int last=stack->last;
int n=1;
STACK *new;
int exists=1;
printf("sorting stack...\n");
printf("last=%d laststack=%d totalstacks=%d\n", last, laststack, totalstacks);
//printf("before sort: ");
//print_stack(stack, 0);
mergesort_stack(stack->stack, 0, last-1);
printf("last=%d total=%d\n", laststack, totalstacks);
//printf("after sort: ");
//print_stack(stack, 0);
// push initial stack
//printf("++++++++++++++++ Adding main stack... ");
new=dup_stack(stack, "Main stack");
dup_stack(stack, "Main stack");
//keep_stack(new);
//print_stacks();
//print_stack(stack, 1);
@@ -221,7 +223,7 @@ int gen_stacks(stack)
//print_stack(stack, 0);
if (exists) {
//printf("++++++++++++++++ Adding stack... ");
new=dup_stack(stack, name);
dup_stack(stack, name);
// print_stack(new, 0);
//keep_stack(new);
}