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


struct {
    int type;
    int valeur;} stack[100];
int memoire[26]={0};
int top=0;

void dump(void){
    int i;
    printf("--------------------\n");
    for(i=0;i<top;i++){
        if(stack[i].type==0){
            printf("[%3d]: %10d\n",i,stack[i].valeur);}
        else{
            printf("[%3d]: %c\n",i,'a'+stack[i].valeur);}}
    printf("--------------------\n");
    for(i=0;i<sizeof(memoire)/sizeof(int);i++){
        printf("[%3c]: %10d\n",'a'+i,memoire[i]);}
    printf("--------------------\n");}

void push(int n){
    if(top<100){
        stack[top].type=0;
        stack[top++].valeur=n;}
    else{
        fprintf(stderr,"La pile est pleine !\n");
        exit(1);}}

void pushv(int n){
    if(top<100){
        stack[top].type=1;
        stack[top++].valeur=n;}
    else{
        fprintf(stderr,"La pile est pleine !\n");
        exit(1);}}

void store(int d,int x){
    if((0<=d)&&(d<sizeof(memoire)/sizeof(int))){
        memoire[d]=x;}
    else{
        fprintf(stderr,"Adresse hors memoire %d !\n",d);
        dump();
        exit(1);}}

int retrieve(int d){
    if((0<=d)&&(d<sizeof(memoire)/sizeof(int))){
        return(memoire[d]);}
    else{
        fprintf(stderr,"Adresse hors memoire %d !\n",d);
        dump();
        exit(1);}}

int pop(void){
    if(top>0){
        --top;
        if(stack[top].type==0){
            return(stack[top].valeur);}
        else{
            return(retrieve(stack[top].valeur));}}
    else{
        fprintf(stderr,"La pile est vide !\n");
        exit(1);}}

int popv(void){
    if(top>0){
        --top;
        if(stack[top].type==1){
            return(stack[top].valeur);}
        else{
            fprintf(stderr,"Mauvais type sur la pile !\n");
            dump();
            exit(1);}}
    else{
        fprintf(stderr,"La pile est vide !\n");
        exit(1);}}

void interprete(void){
    int c;
    while((c=getc(stdin))>1){
        switch(c){
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            push(c-'0');
            break;
        case 'a':
        case 'b':
        case 'c':
        case 'd':
        case 'e':
        case 'f':
        case 'g':
        case 'h':
        case 'i':
        case 'j':
        case 'k':
        case 'l':
        case 'm':
        case 'n':
        case 'o':
        case 'p':
        case 'q':
        case 'r':
        case 's':
        case 't':
        case 'u':
        case 'v':
        case 'w':
        case 'x':
        case 'y':
        case 'z':
            pushv(c-'a');
            break;
        case '!':
            {
                int d=popv();
                store(d,pop());}
            break;
        case '~':
            push(-pop());
            break;
        case '+':
            push(pop()+pop());
            break;
        case '*':
            push(pop()*pop());
            break;
        case '-':
            {
                int b=pop();
                int a=pop();
                push(a-b);}
            break;
        case '/':
            {
                int b=pop();
                int a=pop();
                if(b==0){
                    fprintf(stderr,"Division par zero !\n");
                    exit(1);}
                push(a/b);}
            break;
        case '=':
            printf("%d\n",pop());
            break;
        case '&':
            dump();
            break;
        case '\n':
        case '\t':
        case ' ':
            break;
        default:
            fprintf(stderr,"Mauvais caractere : %d '%c'.\n",c,c);
            break;}}}


int main(void){
    interprete();
    exit(0);
    return(0);}


//// THE END ////
ViewGit