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