summaryrefslogtreecommitdiff
path: root/c/examples/dynamic_typing/dietpass.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/examples/dynamic_typing/dietpass.c')
-rw-r--r--c/examples/dynamic_typing/dietpass.c63
1 files changed, 25 insertions, 38 deletions
diff --git a/c/examples/dynamic_typing/dietpass.c b/c/examples/dynamic_typing/dietpass.c
index 62c0f9e..6c32f00 100644
--- a/c/examples/dynamic_typing/dietpass.c
+++ b/c/examples/dynamic_typing/dietpass.c
@@ -3,10 +3,10 @@
#include <stdlib.h>
#include <string.h>
#include <libdietc.h>
+#include <string_helpers.c>
-static void insert_line(struct instruction **insertion_point, char *line);
-static int is_void_pointer(struct type *type);
-static char *describe_type(struct instruction **insertion_point, struct type *type);
+static int is_void_pointer(struct type *);
+static char *describe_type(struct instruction *, struct type *);
int main(int argc, char **argv) {
assert(argc == 2);
@@ -23,12 +23,9 @@ int main(int argc, char **argv) {
char *rhs = libdietc_tokdup(libdietc_nth_token(i->line, 5));
struct type *rhs_type = libdietc_object_type(program, rhs);
- struct instruction *insertion_point = i;
- char *descriptor = describe_type(&insertion_point, rhs_type);
- char *line = malloc(strlen(descriptor) + strlen(rhs) + 512);
- sprintf(line, "\tannotate_type((void*)%s, %s);", rhs, descriptor);
- insert_line(&insertion_point, line);
- i = insertion_point;
+ char *descriptor = describe_type(i, rhs_type);
+ char *line = sbuild("\tannotate_type((void*)%s, %s);", rhs, descriptor);
+ libdietc_insert_before(i, line);
}
}
libdietc_print(program);
@@ -42,32 +39,31 @@ static int is_void_pointer(struct type *type) {
return 1;
}
-static char *describe_type(struct instruction **insertion_point, struct type *type) {
+static char *describe_type(struct instruction *before, struct type *type) {
assert(type);
static unsigned long count = 0;
- char *name = calloc(512, sizeof(char));
- sprintf(name, "dtype_%d", count++);
+ char *name = sbuild("dtype_%d", count++);
- char *line = calloc(strlen(name) + 1024, sizeof(char));
+ char *line = 0;
char *base_ty = NULL;
switch (type->kind) {
case TYPE_BASIC:
- sprintf(line, "\tvoid *%s = make_basic(\"%s\", sizeof(%s));",
- name, type->basic, type->basic);
+ line = sbuild("\tvoid *%s = make_basic(\"%s\", sizeof(%s));",
+ name, type->basic, type->basic);
break;
case TYPE_ARRAY:
- base_ty = describe_type(insertion_point, type->base);
- sprintf(line, "\tvoid *%s = make_array(%s, %ld);",
- name, base_ty, type->length);
+ base_ty = describe_type(before, type->base);
+ line = sbuild("\tvoid *%s = make_array(%s, %ld);",
+ name, base_ty, type->length);
break;
case TYPE_POINTER:
- base_ty = describe_type(insertion_point, type->base);
- sprintf(line, "\tvoid *%s = make_pointer(%s);", name, base_ty);
+ base_ty = describe_type(before, type->base);
+ line = sbuild("\tvoid *%s = make_pointer(%s);", name, base_ty);
break;
case TYPE_STRUCT:
case TYPE_UNION: {
- sprintf(line, "\tvoid *%s = make_struct(sizeof(Type_%ld));", name, type->id);
- insert_line(insertion_point, line);
+ line = sbuild("\tvoid *%s = make_struct(sizeof(Type_%ld));", name, type->id);
+ libdietc_insert_before(before, line);
unsigned long n = 0;
for (struct type *m = type->members; m; m = m->next_member) n++;
struct type **fields = calloc(n, sizeof(*fields));
@@ -75,13 +71,12 @@ static char *describe_type(struct instruction **insertion_point, struct type *ty
for (struct type *m = type->members; m; m = m->next_member) fields[--i] = m;
for (i = 0; i < n; i++) {
- char *prepend_buffer = calloc(2048, sizeof(char));
- char *fd = describe_type(insertion_point, fields[i]);
- sprintf(prepend_buffer,
- "\tprepend_member(%s, %s, \"%s\", __builtin_offsetof(Type_%ld, %s));",
- name, fd, fields[i]->field_name, type->id,
- fields[i]->field_name);
- insert_line(insertion_point, prepend_buffer);
+ char *fd = describe_type(before, fields[i]);
+ char *prepend_buffer = sbuild(
+ "\tprepend_member(%s, %s, \"%s\", __builtin_offsetof(Type_%ld, %s));",
+ name, fd, fields[i]->field_name, type->id,
+ fields[i]->field_name);
+ libdietc_insert_before(before, prepend_buffer);
}
return name;
}
@@ -89,14 +84,6 @@ static char *describe_type(struct instruction **insertion_point, struct type *ty
assert(0); // not supported yet
break;
}
- insert_line(insertion_point, line);
+ libdietc_insert_before(before, line);
return name;
}
-
-static void insert_line(struct instruction **insertion_point, char *line) {
- struct instruction *new = calloc(1, sizeof(*new));
- new->line = line;
- new->next = (*insertion_point)->next;
- (*insertion_point)->next = new;
- *insertion_point = new;
-}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback