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