Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ejemplo En C De Un Motor De Scripting O Maquina Virtual

DZone's Guide to

Ejemplo En C De Un Motor De Scripting O Maquina Virtual

·
Free Resource
El codigo es una implementacion de un lenguaje de scripting sin lenguaje, por lo tanto queda limitado a la interpretacion de codigos por lo que se parece mas a una maquina virtual


#include 
  
   


typedef struct _operation{
        unsigned int op;
        int arg1;
        int arg2;
        int arg3;
} operation;

typedef struct _scriptEngine{
        int var[255];
        int ip;
        int halt;
        operation code[255];        
} scriptEngine;

void operation_run(const operation *p, scriptEngine *s){
     switch (p->op){
            case 0: //add
                 s->var[p->arg1] = s->var[p->arg2]+s->var[p->arg3];
                 s->ip++;
                 printf("Realizando suma\n");
                 break;
            case 1: //sup
                 s->var[p->arg1] = s->var[p->arg2]-s->var[p->arg3];
                 s->ip++;
                 printf("Realizando resta\n");
                 break;
            case 3: //mul
                 s->var[p->arg1] = s->var[p->arg2]*s->var[p->arg3];
                 s->ip++;
                 printf("Realizando multiplicacion\n");                 
                 break;
            case 4: //div
                 s->var[p->arg1] = s->var[p->arg2]/s->var[p->arg3];
                 s->ip++;
                 printf("Realizando division\n");                 
                 break;
            case 5: //mod
                 s->var[p->arg1] = s->var[p->arg2]%s->var[p->arg3];
                 s->ip++;
                 printf("Realizando modulo\n");                 
                 break;
            case 6: //set
                 s->var[p->arg1] = p->arg2;
                 s->ip++;
                 printf("Realizando asignacion\n");                 
                 break;
            case 7: //halt
                 s->halt=1;
                 break;
            case 8: //jump
                 s->ip=s->var[p->arg1];
                 break;                 
            default:
                 break;            
     }

}

scriptEngine* scriptEngine_new(){
              scriptEngine *new_script;
              new_script = (scriptEngine*) malloc(sizeof(scriptEngine));
              new_script->ip=0;
              new_script->halt=0;              
              return new_script;
}

void scriptEngine_delete(scriptEngine *script){
     free(script);
}


void scriptEngine_run(scriptEngine *script){
    
     int j;
     
     while(script->halt==0){
         printf("Inicio de operacion\n");
         operation_run(&(script->code[script->ip]),script);
         printf("Estado de la maquina:\n");
         for (j=0;j<5;j++){
             printf("Posicion de la memoria %i: %i\n",j,script->var[j]);
         }         
     }     
};

void scriptEngine_create_example(scriptEngine *script, int example){
     switch (example){
            case 0:
                     script->code[0].op=6; //set
                     script->code[0].arg1=0;
                     script->code[0].arg2=10;
                     
                     script->code[1].op=6; //set
                     script->code[1].arg1=1;
                     script->code[1].arg2=20;
                     
                     script->code[2].op=0; //set
                     script->code[2].arg1=2;
                     script->code[2].arg2=0;     
                     script->code[2].arg3=1;    
                     
                     script->code[3].op=7;
                     break;
     }
}

int main(int argc, char **argv){
    
    scriptEngine* script;
    script = scriptEngine_new();
    scriptEngine_create_example(script,0);
    scriptEngine_run(script);
    scriptEngine_delete(script);
    
    system("PAUSE");
    return 0;    
}



  
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}