Annotation of OpenXM/src/kan96xx/plugin/sample.c, Revision 1.4
1.4 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/plugin/sample.c,v 1.3 2005/06/16 05:07:24 takayama Exp $ */
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;
1.3 takayama 20: static struct object Kplugin_NullObject = OINIT;
1.1 maekawa 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. */
1.3 takayama 32: struct object ob1 = OINIT;
33: struct object ob2 = OINIT;
34: struct object ob3 = OINIT;
1.4 ! takayama 35: static int initialized = 0;
1.1 maekawa 36: if (opcode != KPLUGIN_SAMPLE_INIT && !initialized) {
37: errorSample("This component is not initialized.");
38: return(-1);
39: }
40: switch(opcode) {
41: case KPLUGIN_SAMPLE_INIT:
42: if (getoaSize(obj) < 1) {
43: errorSample("No argument for opcode=0 (initialization).");
44: return(-1);
45: }
46: ob1 = getoa(obj,0);
47: if (ectag(ob1) != CLASSNAME_OPERANDSTACK) {
48: errorSample("Argument must be an operand stack.");
49: return(-1);
50: }
51: SharedStack = (struct operandStack *) ecbody(ob1);
52: fprintf(stderr,"Initializing plugin_sample\n");
53: initialized = 1;
54: Kplugin_NullObject.tag = Snull;
55: break;
56: case KPLUGIN_SAMPLE_SELECT:
57: return(1); /* finished. */
58: break;
59: case KPLUGIN_SAMPLE_WAIT:
60: return(0);
61: break;
62: case KPLUGIN_SAMPLE_ADD:
63: ob2 = Kplugin_pop(SharedStack); printf("%d\n",ob2.lc.ival);
64: ob1 = Kplugin_pop(SharedStack); printf("%d\n",ob1.lc.ival);
65: ob1.lc.ival += ob2.lc.ival;
66: Kplugin_push(ob1,SharedStack);
67: break;
68: default:
69: errorSample("plugin_sample: unknown op-code.");
70: return(-1);
71: break;
72: }
73: return(0);
74: }
75:
76: /* Should we use functions to handle operand stacks defined in
77: ../Kan/ or we should have our own functions to handle operand stack?
78: We should copy functions to handle stacks from ../Kan.
79: */
80: int Kplugin_push(struct object ob,struct operandStack *operandstack)
81: {
82: (operandstack->ostack)[(operandstack->sp)++] = ob;
83: if ((operandstack->sp) >= (operandstack->size)) {
84: errorSample("Operand stack overflow.");
85: (operandstack->sp)--;
86: return(-1);
87: }
88: return(0);
89: }
90:
91: struct object Kplugin_pop(struct operandStack *operandstack)
92: {
93: if ((operandstack->sp) <= 0) {
94: return( Kplugin_NullObject );
95: }else{
96: return( (operandstack->ostack)[--(operandstack->sp)]);
97: }
98: }
99:
100: struct object Kplugin_peek(int k,struct operandStack *operandstack)
101: {
102: if (((operandstack->ostack)-k-1) < 0) {
103: return( Kplugin_NullObject );
104: }else{
105: return( (operandstack->ostack)[(operandstack->sp)-k-1]);
106: }
107: }
108:
109:
110: static void errorSample(char *s) {
1.4 ! takayama 111: fprintf(stderr,"Error in plugin/sample: %s\n",s);
1.1 maekawa 112: /* or push error message in the sharedStack. */
113: }
114:
115:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>