Static instance support.
[sod] / test / chimaera.sod
1 /* -*-sod-*-
2 *
3 * A simple SOD module for testing.
4 */
5
6 code c: includes {
7 #include <stdio.h>
8 #include "chimaera.h"
9 }
10
11 code h: includes {
12 #include "sod.h"
13 }
14
15 [nick = nml, link = SodObject]
16 class Animal: SodObject {
17 int tickles = 0;
18
19 [combination = progn] void tickle();
20 [role = before] void nml.tickle() { me->nml.tickles++; }
21 }
22
23 class Lion: Animal {
24 void bite() { puts("Munch!"); }
25 void nml.tickle() { Lion_bite(me); }
26 }
27
28 class Goat: Animal {
29 void butt() { puts("Bonk!"); }
30 void nml.tickle() { Goat_butt(me); }
31 }
32
33 class Serpent: Animal {
34 int limit = 2;
35
36 void hiss() { puts("Sssss!"); }
37 void bite() { puts("Nom!"); }
38 void nml.tickle() {
39 if (SERPENT__CONV_NML(me)->nml.tickles <= me->serpent.limit)
40 Serpent_hiss(me);
41 else
42 Serpent_bite(me);
43 }
44 }
45
46 [nick = sir, link = Animal]
47 class Chimaera: Lion, Goat, Serpent {
48 serpent.limit = 1;
49 }
50
51 code c: user {
52 /*----- Main driver code --------------------------------------------------*/
53
54 static void tickle_animal(Animal *a)
55 {
56 int i;
57
58 for (i = 0; i < 3; i++) {
59 printf("tickle %s #%d...\n", a->_vt->_class->cls.name, i);
60 Animal_tickle(a);
61 }
62 }
63
64 static void provoke_lion(Lion *l)
65 {
66 printf("provoking %s as a lion\n", l->_vt->_class->cls.name);
67 Lion_bite(l);
68 }
69
70 static void provoke_goat(Goat *g)
71 {
72 printf("provoking %s as a goat\n", g->_vt->_class->cls.name);
73 Goat_butt(g);
74 }
75
76 static void provoke_serpent(Serpent *s)
77 {
78 printf("provoking %s as a serpent\n", s->_vt->_class->cls.name);
79 Serpent_bite(s);
80 }
81
82 int main(void)
83 {
84 {
85 SOD_DECL(Lion, l, NO_KWARGS);
86 provoke_lion(l);
87 tickle_animal(LION__CONV_NML(l));
88 }
89
90 {
91 SOD_DECL(Goat, g, NO_KWARGS);
92 provoke_goat(g);
93 tickle_animal(GOAT__CONV_NML(g));
94 }
95
96 {
97 SOD_DECL(Serpent, s, NO_KWARGS);
98 provoke_serpent(s);
99 tickle_animal(SERPENT__CONV_NML(s));
100 }
101
102 {
103 SOD_DECL(Chimaera, c, NO_KWARGS);
104 provoke_lion(CHIMAERA__CONV_LION(c));
105 provoke_goat(CHIMAERA__CONV_GOAT(c));
106 provoke_serpent(CHIMAERA__CONV_SERPENT(c));
107 tickle_animal(CHIMAERA__CONV_NML(c));
108 }
109
110 return (0);
111 }
112
113 }