1
0

right idea, wrong implementation

This commit is contained in:
dtb 2022-07-19 18:24:19 -04:00
parent 9a95b64618
commit 4221781ee6

View File

@ -1,121 +0,0 @@
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include "libstrnum.h"
#include "noargvzero.h"
#include <stdio.h>
typedef long int pile_t;
struct Stack{
size_t s;
size_t dot;
pile_t *pile;
};
/* probably could be optimized */
pile_t
Stack_pop(struct Stack *s){
pile_t n;
n = s->pile[(s->dot)-1];
if(s->dot >= 0)
--(s->dot);
return n;
}
struct Stack *
Stack_push(pile_t n, struct Stack *s){
pile_t *new;
if(s->s == s->dot && (new = realloc(s->pile, sizeof(*(s->pile)) * ++(s->s))) == NULL)
return NULL;
s->pile[(s->dot)++] = n;
return s;
}
int main(int argc, char *argv[]){
char *argv0;
int i;
//int ir, or; /* input radix, output radix */
int op;
struct Stack s;
pile_t x, y;
unsigned short int retval;
NOARGVZERO(argv);
retval = EX_OK;
/* stack initialization */
if((s.pile = malloc(sizeof(pile_t))) == NULL)
return EX_OSERR;
*s.pile = 0;
s.dot = 0;
s.s = 1;
Stack_push(0, &s);
argv0 = argv[0];
/* Most operations are 1 char, all are two max, so no sense using
* strcmp or a dictionary */
while(*++argv != NULL){
/* parse out operation */
op = '\0';
switch(**argv){
case '+': case '-': case '%': case '^': case '<': case '>':
case '=': case '!': case '?': case 'q': case 'x':
if((*argv)[1] == '\0')
op = **argv;
break;
case '*':
if((*argv)[1] == '\0')
op = **argv; /* "*" */
else if((*argv)[1] == **argv && (*argv)[2] == '\0')
op = '^'; /* "**" */
break;
default:
op = 'a' + !stris(STRIS_TYPE_UINT, *argv) * ('h' - 'a');
break;
}
if(op == '\0'){
retval = EX_SOFTWARE;
goto quit;
}
/* conduct operation */
switch(op){
case 'a':
if(Stack_push(atoi(*argv), &s) == NULL){
retval = EX_OSERR;
goto quit;
}
break;
case 'h':
goto usage;
case 'q': case 'x':
goto quit;
case '+':
Stack_push(Stack_pop(&s) + Stack_pop(&s), &s);
write(1,"0",1);
break;
case '-':
y = Stack_pop(&s);
x = Stack_pop(&s);
Stack_push(x - y, &s);
break;
}
}
printf("%li\n", s.pile[s.dot - 1]);
goto quit;
usage:
write(2, "Usage: ", 7);
for(i = 0; argv0[i] != '\0'; ++i);
write(2, argv0, i);
write(2, "(-C [program])|(program...)\n", 28);
retval = EX_USAGE;
quit:
free(s.pile);
return retval;
}