diff --git a/20210204/hp-20210204.pdf b/20210204/hp-20210204.pdf index f45a2f65a35783420b9a5467d44682759293c739..12b377190ab0b04487433a8feba26b2c14f0f5e3 100644 Binary files a/20210204/hp-20210204.pdf and b/20210204/hp-20210204.pdf differ diff --git a/20210204/hp-20210204.tex b/20210204/hp-20210204.tex index ecfb230808f6d7dc05704a8cc72c54c95ef36ff9..dc4a31a69d54f224e4f274e0e09ba5489146d84a 100644 --- a/20210204/hp-20210204.tex +++ b/20210204/hp-20210204.tex @@ -433,7 +433,9 @@ \begin{center} \begin{minipage}{8.5cm} \begin{lstlisting}[gobble=8] - ¡if (this->base.type == T_INTEGER) + ¡tobject *this; + + if (this->base.type == T_INTEGER) printf ("Integer: %d\n", this->integer.content); else if (this->base.type == T_STRING) printf ("String: \"%s\"\n", this->string.content);¿ diff --git a/20210204/objects-10.c b/20210204/objects-10.c new file mode 100644 index 0000000000000000000000000000000000000000..ac47c0ed5479be655b7d76aba558526b2dc774a3 --- /dev/null +++ b/20210204/objects-10.c @@ -0,0 +1,73 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; + void (* print) (t_object *this); +} t_base; + +typedef struct +{ + int type; + void (* print) (t_object *this); + int content; +} t_integer; + +typedef struct +{ + int type; + void (* print) (t_object *this); + char *content; +} t_string; + +typedef union +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.print = print_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.print = print_string; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->base.print (object[i]); + + return 0; +} diff --git a/20210204/objects-11.c b/20210204/objects-11.c new file mode 100644 index 0000000000000000000000000000000000000000..a15c8eb239b2315845f24f3b4f111d3670d7ea33 --- /dev/null +++ b/20210204/objects-11.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +union t_object; + +typedef struct +{ + int type; + void (* print) (union t_object *this); +} t_base; + +typedef struct +{ + int type; + void (* print) (union t_object *this); + int content; +} t_integer; + +typedef struct +{ + int type; + void (* print) (union t_object *this); + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.print = print_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.print = print_string; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->base.print (object[i]); + + return 0; +} diff --git a/20210204/objects-12.c b/20210204/objects-12.c new file mode 100644 index 0000000000000000000000000000000000000000..5d605e1b295e252b2d947a1d4ac04c9fc805d36d --- /dev/null +++ b/20210204/objects-12.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> + +union t_object; + +typedef struct +{ + void (* print) (union t_object *this); +} t_base; + +typedef struct +{ + void (* print) (union t_object *this); + int content; +} t_integer; + +typedef struct +{ + void (* print) (union t_object *this); + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.print = print_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.print = print_string; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->base.print (object[i]); + + return 0; +} diff --git a/20210204/objects-13.c b/20210204/objects-13.c new file mode 100644 index 0000000000000000000000000000000000000000..81ef279b060e0b6290194fdeda8c3330cb716cdd --- /dev/null +++ b/20210204/objects-13.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <stdlib.h> + +union t_object; +struct t_vmt; + +typedef struct +{ + struct t_vmt *vmt; +} t_base; + +typedef struct +{ + struct t_vmt *vmt; + int content; +} t_integer; + +typedef struct +{ + struct t_vmt *vmt; + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +typedef struct t_vmt +{ + void (* print) (union t_object *this); +} t_vmt; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_vmt vmt_integer = { print_integer }; +t_vmt vmt_string = { print_string }; + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.vmt = &vmt_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->integer.vmt = &vmt_string; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->base.vmt->print (object[i]); + + return 0; +}