コード例 #1
0
ファイル: forth.c プロジェクト: tyru/forth-interp
// NOTE: it's ok to destruct interp more than twice.
void
forth_destruct(ForthInterp *interp)
{
    // free the all stack.
    forth_clear_stack(interp);
    // free the pointer.
    stack_destruct(interp->word_stack);
    FREE(interp->word_stack);

    FREE(interp->src);
    FREE(interp->word_def);
}
コード例 #2
0
ファイル: maze.c プロジェクト: syslover33/ctank
/*
 * maze function defination
 * */
static int maze_findAllway(int **arry_maze, int nRow, int nColm)
{
    int iRet=0;
    stack stackM;
    stack_elem selemCur;
    printf_dbg("[F:%s, L:%d]\n", __FUNCTION__, __LINE__);

    if(arry_maze==NULL || nRow<1 || nColm<1) {
        return -1;
    }

    //1. construct stack
    iRet = stack_construct(&stackM, nRow*nColm);

    //2. init current stack element
    selemCur.x = 0;
    selemCur.y = 0;
    selemCur.nDir = -1;

    //3. Deepth-First-Search(DFS) search path
    do {
        iRet = maze_goNextStep(&stackM, &selemCur);

        if(iRet == 1) {
#ifdef DEBUG
            printf_dbg("[F:%s, L:%d] goNextStep success\n", __FUNCTION__, __LINE__);
	        stack_print(&stackM);
#endif
            if(selemCur.x==g_nRow-1 && selemCur.y==g_nColm-1 && g_arry_maze[selemCur.x][selemCur.y]==0) {
	           stack_print(&stackM);
	           iRet = maze_goLastStep(&stackM, &selemCur);
	        }
        } else if(iRet == 0) {
            iRet = maze_goLastStep(&stackM, &selemCur);
        }
    }while(iRet==1);


    //4. search all complete; destruct stack
    stack_destruct(&stackM);

    return iRet;
}
コード例 #3
0
ファイル: proc.c プロジェクト: olegrok/iLab
int main()
{
    int sizestr = 1, size = 0, comdat = 0, j = 0, i = 0;
    mystack_type buf1 = 0, buf2 = 0, buf3 = 0;


    int* strcommand = (int*)calloc(j + 1,sizeof(int));
    assert(strcommand);
    FILE *product = 0;
    assembler();                            //Перевод команд из текста в цифры
    product = fopen("Product.txt","r");
    /*
        Открываем файл, считываем из него команды и числа,
        помещаем всё это в массив.
        Распределяем введенные данные по массивам меток, команд, чисел.
        Выполняем программу.
    */

    while(!feof(product))                   //Считывание команд
    {
        fscanf(product, "%d", &strcommand[j]);
        j++;
        strcommand = (int*)realloc(strcommand, sizeof(int)*(j + 1));
    }

    fclose(product);

    mystack stk = stack_construct(1);       //Рабочий стек
    mystack call_ret = stack_construct(1);  //Стек вызова


    for(i = 0; strcommand[i] && i <= j; i++)
    {
        if(strcommand[i] < 0)
            continue;
        switch (strcommand[i])
        {

            case 0: return 0;break;             //End

            case 1:                             //Push
                i++;
                stack_push(stk, strcommand[i]);
                break;

            case 2:                             //Pop
                stack_pop(stk);
                break;

            case 3:                             //Add
                stack_push(stk, stack_pop(stk) + stack_pop(stk));
                break;

            case 4:                             //Jump
                    i++;
                    i = strcommand[i] - 1;
                break;

            case 5:                             //Mul
                stack_push(stk, stack_pop(stk) * stack_pop(stk));
                break;

            case 6:                             //Sub
                stack_push(stk, -stack_pop(stk) + stack_pop(stk));
                break;

            case 7:                             //Div
                buf1 = stack_pop(stk);
                buf2 = stack_pop(stk);
                stack_push(stk, buf2 / buf1);
                break;
            case 8:                             //Push_Ax
                Ax = stack_top(stk);
                break;
            case 9:                             //Push_Bx
                Bx = stack_top(stk);
                break;
            case 10:                            //Push_Cx
                Cx = stack_top(stk);
                break;
            case 11:                            //Push_Dx
                Dx = stack_top(stk);
                break;
            case 12:                            //Call
                i++;
                stack_push(call_ret, i);
                i = strcommand[i] - 1;
                break;
            case 13:                            //Ret
                if(stack_check(stk) != 0)
                    i = stack_pop(call_ret);
                break;
            case 14:                            //Jnz
                i++;
                if(stack_top(stk) != 0)
                    i = strcommand[i] - 1;
                break;
            case 15:                            //Jz
                i++;
                if(stack_top(stk) == 0)
                    i = strcommand[i] - 1;
                break;

            case 16:                            //Cmp if (last > penultimate) := 1 if (last < penultimate) :=  0
                buf1 = stack_pop(stk);
                if(buf1 > stack_top(stk))
                    buf3 = 1;
                if(buf1 > stack_top(stk))
                    buf3 = 0;
                stack_push(stk, buf1);
                stack_push(stk, buf3);
                buf3 = 0;
                break;

            case 17:                            //Je JumpIF==
                i++;
                buf1 = stack_pop(stk);
                if(buf1 == stack_top(stk))
                    buf3 = 0;
                else
                    buf3 = -1;
                stack_push(stk, buf1);
                if(!buf3)
                    i = strcommand[i] - 1;
                buf3 = 0;
                break;

            case 18:                            //Jg last >
                i++;
                buf1 = stack_pop(stk);
                if(buf1 > stack_top(stk))
                    buf3 = 0;
                else
                    buf3 = -1;
                stack_push(stk, buf1);
                if(!buf3)
                    i = strcommand[i] - 1;
                buf3 = 0;
                break;

            case 19:                            //Jl last <
                i++;
                buf1 = stack_pop(stk);
                if(buf1 < stack_top(stk))
                    buf3 = 0;
                else
                    buf3 = -1;
                stack_push(stk, buf1);
                if(!buf3)
                    i = strcommand[i] - 1;
                buf3 = 0;
                break;

            case 20:                            //Jng last >=
                i++;
                buf1 = stack_pop(stk);
                if(buf1 >= stack_top(stk))
                    buf3 = 0;
                else
                    buf3 = -1;
                stack_push(stk, buf1);
                if(!buf3)
                    i = strcommand[i] - 1;
                buf3 = 0;
                break;

            case 21:                            //Jnl jast <=
                i++;
                buf1 = stack_pop(stk);
                if(buf1 <= stack_top(stk))
                    buf3 = 0;
                else
                    buf3 = -1;
                stack_push(stk, buf1);
                if(!buf3)
                    i = strcommand[i] - 1;
                buf3 = 0;
                break;

            case 22:                            //Sqr
                buf1 = stack_pop(stk);
                stack_push(stk, buf1 * buf1);
                break;

            case 23:                            //Sqrt
                buf1 = stack_pop(stk);
                buf1 = (mystack_type)sqrt(buf1);
                stack_push(stk, buf1);

                break;


        }

    }
    proc_dump(stk);
    stack_destruct(stk);
    stack_destruct(call_ret);
    free(strcommand);
    return 0;
}