test/chimaera.sod: Make `Serpent' tickle tolerance be a slot.
[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 }
49
50 code c : user {
51 /*----- Main driver code --------------------------------------------------*/
52
53 static void tickle_animal(Animal *a)
54 {
55 int i;
56
57 for (i = 0; i < 3; i++) {
58 printf("tickle %s #%d...\n", a->_vt->_class->cls.name, i);
59 Animal_tickle(a);
60 }
61 }
62
63 static void provoke_lion(Lion *l)
64 {
65 printf("provoking %s as a lion\n", l->_vt->_class->cls.name);
66 Lion_bite(l);
67 }
68
69 static void provoke_goat(Goat *g)
70 {
71 printf("provoking %s as a goat\n", g->_vt->_class->cls.name);
72 Goat_butt(g);
73 }
74
75 static void provoke_serpent(Serpent *s)
76 {
77 printf("provoking %s as a serpent\n", s->_vt->_class->cls.name);
78 Serpent_bite(s);
79 }
80
81 int main(void)
82 {
83 {
84 SOD_DECL(Lion, l);
85 provoke_lion(l);
86 tickle_animal(LION__CONV_NML(l));
87 }
88
89 {
90 SOD_DECL(Goat, g);
91 provoke_goat(g);
92 tickle_animal(GOAT__CONV_NML(g));
93 }
94
95 {
96 SOD_DECL(Serpent, s);
97 provoke_serpent(s);
98 tickle_animal(SERPENT__CONV_NML(s));
99 }
100
101 {
102 SOD_DECL(Chimaera, c);
103 provoke_lion(CHIMAERA__CONV_LION(c));
104 provoke_goat(CHIMAERA__CONV_GOAT(c));
105 provoke_serpent(CHIMAERA__CONV_SERPENT(c));
106 tickle_animal(CHIMAERA__CONV_NML(c));
107 }
108
109 return (0);
110 }
111
112 }