[BACK]Return to sample.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/sample.c, Revision 1.2

1.2     ! takayama    1: /* $OpenXM$ */
1.1       maekawa     2: #include <stdio.h>
                      3: #include "../Kan/datatype.h"
                      4: #include "../Kan/stackm.h"
                      5: #include "../Kan/kclass.h"
                      6:
                      7: #include "sample.h"
                      8:
                      9: /*
                     10:   options for Kan/Makefile
                     11: PLUGIN = ../plugin
                     12: PLUGIN_PROTOTYPE  = $(PLUGIN)/sample.h
                     13: PLUGIN_LIB  = $(PLUGIN)/sample.a
                     14: PLUGIN_EXT = $(PLUGIN)/sample.hh
                     15: PLUGIN_SM1 =$(PLUGIN)/sample.sm1
                     16: PLUGIN_LINKFLAG =
                     17: */
                     18:
                     19: static struct operandStack *SharedStack = NULL;
                     20: static struct object Kplugin_NullObject;
                     21:
                     22: /*  Sample is a very simple stack machine.
                     23:     There are only one operation "add" on the shared stack.
                     24:     Kplugin_sample(KPLUGIN_SAMPLE_INIT,
                     25:                    [ new-operand-stack-for-the-shared-stack ]);
                     26:     Pplugin_sample(KPLUGIN_SAMPLE_ADD, [ ] ); push the result on the shared
                     27:                                               stack.
                     28: */
                     29:
                     30: int Kplugin_sample(int opcode, struct object obj) {
                     31:   /* obj is assumed to be an array. */
                     32:   struct object ob1,ob2,ob3;
                     33:   static initialized = 0;
                     34:   if (opcode != KPLUGIN_SAMPLE_INIT && !initialized) {
                     35:     errorSample("This component is not initialized.");
                     36:     return(-1);
                     37:   }
                     38:   switch(opcode) {
                     39:   case KPLUGIN_SAMPLE_INIT:
                     40:     if (getoaSize(obj) < 1) {
                     41:       errorSample("No argument for opcode=0 (initialization).");
                     42:       return(-1);
                     43:     }
                     44:     ob1 = getoa(obj,0);
                     45:     if (ectag(ob1) != CLASSNAME_OPERANDSTACK) {
                     46:       errorSample("Argument must be an operand stack.");
                     47:       return(-1);
                     48:     }
                     49:     SharedStack = (struct operandStack *) ecbody(ob1);
                     50:     fprintf(stderr,"Initializing plugin_sample\n");
                     51:     initialized = 1;
                     52:     Kplugin_NullObject.tag = Snull;
                     53:     break;
                     54:   case KPLUGIN_SAMPLE_SELECT:
                     55:     return(1); /* finished. */
                     56:     break;
                     57:   case KPLUGIN_SAMPLE_WAIT:
                     58:     return(0);
                     59:     break;
                     60:   case KPLUGIN_SAMPLE_ADD:
                     61:     ob2 = Kplugin_pop(SharedStack);  printf("%d\n",ob2.lc.ival);
                     62:     ob1 = Kplugin_pop(SharedStack);  printf("%d\n",ob1.lc.ival);
                     63:     ob1.lc.ival += ob2.lc.ival;
                     64:     Kplugin_push(ob1,SharedStack);
                     65:     break;
                     66:   default:
                     67:     errorSample("plugin_sample: unknown op-code.");
                     68:     return(-1);
                     69:     break;
                     70:   }
                     71:   return(0);
                     72: }
                     73:
                     74: /* Should we use functions to handle operand stacks defined in
                     75:    ../Kan/  or we should have our own functions to handle operand stack?
                     76:   We should copy functions to handle stacks from ../Kan.
                     77: */
                     78: int Kplugin_push(struct object ob,struct operandStack *operandstack)
                     79: {
                     80:   (operandstack->ostack)[(operandstack->sp)++] = ob;
                     81:   if ((operandstack->sp) >= (operandstack->size)) {
                     82:     errorSample("Operand stack overflow.");
                     83:     (operandstack->sp)--;
                     84:     return(-1);
                     85:   }
                     86:   return(0);
                     87: }
                     88:
                     89: struct object Kplugin_pop(struct operandStack *operandstack)
                     90: {
                     91:   if ((operandstack->sp) <= 0) {
                     92:     return( Kplugin_NullObject );
                     93:   }else{
                     94:     return( (operandstack->ostack)[--(operandstack->sp)]);
                     95:   }
                     96: }
                     97:
                     98: struct object Kplugin_peek(int k,struct operandStack *operandstack)
                     99: {
                    100:   if (((operandstack->ostack)-k-1) < 0) {
                    101:     return( Kplugin_NullObject );
                    102:   }else{
                    103:     return( (operandstack->ostack)[(operandstack->sp)-k-1]);
                    104:   }
                    105: }
                    106:
                    107:
                    108: static void errorSample(char *s) {
                    109:   fprintf(stderr,"Error in plugin/sample: %s\n");
                    110:   /* or push error message in the sharedStack. */
                    111: }
                    112:
                    113:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>