| 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] |
| 20 | void tickle(); |
| 21 | |
| 22 | [role = before] |
| 23 | void nml.tickle() { me->nml.tickles++; } |
| 24 | } |
| 25 | |
| 26 | class Lion : Animal { |
| 27 | void bite() { puts("Munch!"); } |
| 28 | void nml.tickle() { me->_vt->lion.bite(me); } |
| 29 | } |
| 30 | |
| 31 | class Goat : Animal { |
| 32 | void butt() { puts("Bonk!"); } |
| 33 | void nml.tickle() { me->_vt->goat.butt(me); } |
| 34 | } |
| 35 | |
| 36 | class Serpent : Animal { |
| 37 | void hiss() { puts("Sssss!"); } |
| 38 | void bite() { puts("Nom!"); } |
| 39 | void nml.tickle() { |
| 40 | if (SERPENT__CONV_NML(me)->nml.tickles > 2) me->_vt->serpent.bite(me); |
| 41 | else me->_vt->serpent.hiss(me); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | [nick = sir, link = Animal] |
| 46 | class Chimaera : Lion, Goat, Serpent { |
| 47 | } |
| 48 | |
| 49 | code c : user [classes end, user, epilogue] { |
| 50 | /*----- Main driver code --------------------------------------------------*/ |
| 51 | |
| 52 | static 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); |
| 58 | a->_vt->nml.tickle(a); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static void provoke_lion(Lion *l) |
| 63 | { |
| 64 | printf("provoking %s as a lion\n", l->_vt->_class->cls.name); |
| 65 | l->_vt->lion.bite(l); |
| 66 | } |
| 67 | |
| 68 | static void provoke_goat(Goat *g) |
| 69 | { |
| 70 | printf("provoking %s as a goat\n", g->_vt->_class->cls.name); |
| 71 | g->_vt->goat.butt(g); |
| 72 | } |
| 73 | |
| 74 | static void provoke_serpent(Serpent *s) |
| 75 | { |
| 76 | printf("provoking %s as a serpent\n", s->_vt->_class->cls.name); |
| 77 | s->_vt->serpent.bite(s); |
| 78 | } |
| 79 | |
| 80 | int main(void) |
| 81 | { |
| 82 | { |
| 83 | SOD_DECL(Lion, l); |
| 84 | provoke_lion(l); |
| 85 | tickle_animal(LION__CONV_NML(l)); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | SOD_DECL(Goat, g); |
| 90 | provoke_goat(g); |
| 91 | tickle_animal(GOAT__CONV_NML(g)); |
| 92 | } |
| 93 | |
| 94 | { |
| 95 | SOD_DECL(Serpent, s); |
| 96 | provoke_serpent(s); |
| 97 | tickle_animal(SERPENT__CONV_NML(s)); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | SOD_DECL(Chimaera, c); |
| 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 | } |