Commits

Dan Buch  committed 02e19d1

so it runs, but things are not looking pretty

  • Participants
  • Parent commits f87fd6c

Comments (0)

Files changed (14)

 	find -name '*.o' -exec rm -f {} ';'
 	rm -f $(BINDIR)/*
 
-%.o: %.c
-	$(QUIET_CC)$(CC) $(CFLAGS) -g -c -o $@ $^
+%.o: %.c %.h
+	$(QUIET_CC)$(CC) $(CFLAGS) -g -c -o $@ $<
 
 
 $(BINDIR)/ch1: $(CH1_OBJECTS)

File src/ch1/Set.c

 }
 
 
-/* vim:filetype=c:fileencoding=utf-8
+/* vim:filetype=c
  */

File src/ch1/Set.h

 
 #include <stdarg.h>
 
-struct Set { unsigned count; };
-struct Object { unsigned count; struct Set * in; };
+struct Set {
+    unsigned count;
+};
+struct Object {
+    unsigned count;
+    struct Set * in;
+};
 
 static const size_t _Set = sizeof(struct Set);
 static const size_t _Object = sizeof(struct Object);
 
 #endif
 
-/* vim:filetype=c:fileencoding=utf-8
+/* vim:filetype=c
  */

File src/ch1/main.c

 }
 
 
-/* vim:filetype=c:fileencoding=utf-8
+/* vim:filetype=c
  */

File src/ch1/new.h

 
 #endif
 
-/* vim:filetype=c:fileencoding=utf-8
+/* vim:filetype=c
  */

File src/ch2/Class.c

+#include "common.h"
+
+/* void * new (const void * _class, ...)
+ * {
+ *     const struct Class * class = _class;
+ *     void * p = calloc(1, class -> size);
+
+ *     assert(p);
+
+ *     * (const struct Class **) p = class;
+
+ *     if (class->ctor)
+ *     {
+ *         va_list ap;
+
+ *         va_start(ap, _class);
+ *         p = class->ctor(p, &ap);
+ *         va_end(ap);
+ *     }
+ *     return p;
+ * } */
+
+void *
+new(const void * _class, ...)
+{
+    const struct Class * class = _class;
+    void * p = calloc(1, class -> size);
+
+    assert(p);
+    * (const struct Class **) p = class;
+
+    if (class -> ctor)
+    {
+        va_list ap;
+        va_start(ap, _class);
+        p = class -> ctor(p, & ap);
+        va_end(ap);
+    }
+    return p;
+}
+
+
+
+void
+delete(void * self)
+{
+    const struct Class ** cp = self;
+
+    if (self && *cp && (*cp)->dtor) {
+        self = (*cp)->dtor(self);
+    }
+    free(self);
+}
+
+
+void *
+clone(const void * self)
+{
+    const struct Class * const * cp = self;
+
+    assert(self && *cp);
+    void * new_inst = new(self);
+    return new_inst;
+}
+
+
+int
+differ(const void * self, const void * b)
+{
+    const struct Class * const * cp = self;
+
+    assert(self && *cp && (*cp)->differ);
+    return (*cp)->differ(self, b);
+}
+
+
+size_t
+sizeOf(const void * self)
+{
+    const struct Class * const * cp = self;
+
+    assert(self && *cp);
+    return (*cp)->size;
+}
+
+
+/* vim:filetype=c
+ */

File src/ch2/Class.h

+#ifndef CLASS_H
+#define CLASS_H
+
+struct Class {
+    size_t size;
+    void * (* ctor) (void * self, va_list * app);
+    void * (* dtor) (void * self);
+    void * (* clone) (const void * self);
+    int (* differ) (const void * self, const void * b);
+};
+
+
+void * new (const void * _class, ...);
+void delete (void * self);
+void * clone (const void * self);
+int differ (const void * self, const void * b);
+size_t sizeOf (const void * self);
+
+#endif
+
+/* vim:filetype=c
+ */

File src/ch2/Set.c

 }
 
 
-/* vim:filetype=c:fileencoding=utf-8
+/* vim:filetype=c
  */

File src/ch2/Set.h

 
 #endif
 
-/* vim:filetype=c:fileencoding=utf-8
+/* vim:filetype=c
  */

File src/ch2/String.c

+#include "common.h"
+
+const void * String = & _String;
+
+
+/*
+static void * String_ctor (void * _self, va_list * app)
+{
+    struct String * self = _self;
+    const char * text = va_arg(* app, const char *);
+
+    self->text = malloc(strlen(text) + 1);
+    assert(self->text);
+    strcpy(self->text, text);
+    return self;
+}
+*/
+
+
+/* vim:filetype=c
+ */

File src/ch2/String.h

+#ifndef STRING_H
+#define STRING_H
+
+struct String {
+    const void * class;
+    char * text;
+};
+
 extern const void * String;
+static const size_t _String = sizeof(struct String);
 
-/* vim:filetype=c:fileencoding=utf-8
+#endif
+
+/* vim:filetype=c
  */

File src/ch2/common.c

-#include "common.h"
-
-
-/* void * new (const void * _class, ...)
- * {
- *     const struct Class * class = _class;
- *     void * p = calloc(1, class -> size);
-
- *     assert(p);
-
- *     * (const struct Class **) p = class;
-
- *     if (class->ctor)
- *     {
- *         va_list ap;
-
- *         va_start(ap, _class);
- *         p = class->ctor(p, &ap);
- *         va_end(ap);
- *     }
- *     return p;
- * } */
-
-void * new (const void * _class, ...)
-{
-    const struct Class * class = _class;
-    void * p = calloc(1, class -> size);
-
-    assert(p);
-    * (const struct Class **) p = class;
-
-    if (class -> ctor)
-    {
-        va_list ap;
-        va_start(ap, _class);
-        p = class -> ctor(p, & ap);
-        va_end(ap);
-    }
-    return p;
-}
-
-
-
-void delete (void * self)
-{
-    const struct Class ** cp = self;
-
-    if (self && *cp && (*cp)->dtor) {
-        self = (*cp)->dtor(self);
-    }
-    free(self);
-}
-
-
-int differ (const void * self, const void * b)
-{
-    const struct Class * const * cp = self;
-
-    assert(self && *cp && (*cp)->differ);
-    return (*cp)->differ(self, b);
-}
-
-
-size_t sizeOf (const void * self)
-{
-    const struct Class * const * cp = self;
-
-    assert(self && *cp);
-    return (*cp)->size;
-}
-
-
-/* vim:filetype=c:fileencoding=utf-8
- */

File src/ch2/common.h

+#ifndef HAS_COMMON
+
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <assert.h>
 
 #include "Set.h"
+#include "Class.h"
 #include "String.h"
 
 #define HAS_COMMON 1
+#endif
 
-struct Class {
-    size_t size;
-    void * (* ctor) (void * self, va_list * app);
-    void * (* dtor) (void * self);
-    void * (* clone) (const void * self);
-    int (* differ) (const void * self, const void * b);
-};
+/* vim:filetype=c
+ */
 
-void * new (const void * _class, ...);
-void delete (void * self);
-void * clone (const void * self);
-int differ (const void * self, const void * b);
-size_t sizeOf (const void * self);
-
-/* vim:filetype=c:fileencoding=utf-8
- */

File src/ch2/main.c

 #include "common.h"
 
-const void * String;
-
-
 int main()
 {
-    void * a = new(String, "a"), * aa = clone(a);
+    printf("Begin!\n");
+    printf("making \"* a\"\n");
+    void * a = new(String, "a");
+    printf("cloning \"* a\" to \"* aa\"\n");
+    void * aa = clone(a);
+    printf("making \"* b\"\n");
     void * b = new(String, "b");
 
     printf("sizeOf(a) == %u\n", sizeOf(a));
         puts("clone?");
     }
 
-    delete(a), delete(aa), delete(b);
+    delete(a);
+    delete(aa);
+    delete(b);
     return 0;
 }
 
 
-/* vim:filetype=c:fileencoding=utf-8
+/* vim:filetype=c
  */