+/* -*-sod-*- */
+
+code c: includes {
+#include <assert.h>
+#include <stdio.h>
+
+#include "rat.h"
+}
+
+code h: includes {
+#include "sod.h"
+}
+
+typename FILE;
+
+code c: early_user {
+ static int gcd(int x, int y)
+ {
+ int t;
+ if (x < 0) x = -x;
+ if (y < 0) y = -y;
+ while (y) { t = x%y; x = y; y = t; }
+ return (x);
+ }
+}
+
+[nick = rat, link = SodObject]
+class Rational: SodObject {
+ int num, den;
+ initarg int num = 0, den = 1;
+ init {
+ int g;
+ assert(den);
+ g = gcd(num, den);
+ me->rat.num = num/g;
+ me->rat.den = den/g;
+ }
+
+ void print(FILE *fp) { fprintf(fp, "%d/%d\n", me->rat.num, me->rat.den); }
+}
+
+code c: user {
+ int main(void)
+ {
+ SOD_DECL(Rational, r0, KWARGS(K(den, 9)));
+ SOD_DECL(Rational, r1, KWARGS(K(num, 6)));
+ SOD_DECL(Rational, r2, KWARGS(K(num, 6) K(den, 9)));
+ Rational_print(r0, stdout);
+ Rational_print(r1, stdout);
+ Rational_print(r2, stdout);
+ return (0);
+ }
+}