diff src/list/list.c @ 13:a765eb7f3b7c

implement delete, traverse
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Tue, 07 Apr 2015 15:39:14 +0900
parents
children c3314208d7b9
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/list/list.c	Tue Apr 07 15:39:14 2015 +0900
@@ -0,0 +1,103 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "prototype.h"
+
+#include "context.h"
+#include "allocate.h"
+#include "origin_cs.h"
+
+__code code1(Context* context) {
+    goto meta_code1(context, allocate);
+}
+
+__code meta_code1(Context* context, __code (*next)()) {
+    context->next = next;
+    goto context->next(context, (int)sizeof(data1), 1, code2);
+}
+
+__code code2(Context* context, data1* ds) {
+    ds->value = 1024;
+    goto meta_code2(context, ds);
+}
+
+__code meta_code2(Context* context, data1* ds) {
+    mdata* mds = (mdata*)context->mds-1;
+    context->head = mds;
+    mds->next = 0;
+    goto append(context, code3);
+}
+
+__code append(Context* context, __code (*next)()) {
+    goto meta_append(context, (mdata*)context->head, next);
+}
+
+__code meta_append(Context* context, mdata* mds, __code (*next)()) {
+    if (mds->next)
+        goto meta_append(context, mds->next, next);
+    mds->next = context->mds;
+    goto allocate(context, (int)sizeof(data1), 1, next);
+}
+
+__code code3(Context* context, data1* ds) {
+    ds->value = 10;
+    goto meta_code3(context);
+}
+
+__code meta_code3(Context* context) {
+    goto append(context, code4);
+}
+
+__code code4(Context* context, data1* ds) {
+    goto meta_code4(context);
+}
+
+__code meta_code4(Context* context) {
+    goto traverse(context, code5);
+}
+
+__code traverse(Context* context, __code (*next)()) {
+    goto meta_traverse(context, (mdata*)context->head, next);
+}
+
+__code meta_traverse(Context* context, mdata* mds, __code (*next)()) {
+    printf("current value in list is %d\n", ((data1*)mds->ds)->value);
+    if (mds->next)
+        goto meta_traverse(context, mds->next, next);
+    goto next(context);
+}
+
+__code code5(Context* context) {
+    goto meta_code5(context);
+}
+
+__code meta_code5(Context* context) {
+    goto delete(context, code6);
+}
+
+__code delete(Context* context, __code (*next)()) {
+    goto meta_delete(context, (mdata*)context->head, next);
+}
+
+__code meta_delete(Context* context, mdata* head, __code (*next)()) {
+    context->head = (head->next) ? head->next : 0;
+    goto next(context);
+}
+
+__code code6(Context* context) {
+    printf("after delete\n");
+    goto meta_code6(context);
+}
+
+__code meta_code6(Context* context) {
+    goto traverse(context, exit_code);
+}
+
+int main() {
+    Context* context = (Context*)malloc(sizeof(Context));
+    context->ds_heap = malloc(sizeof(data1)*1024);
+    context->mds_heap = malloc(sizeof(mdata)*1024);
+    context->ds = context->ds_heap;
+    context->mds = context->mds_heap;
+    goto start_code(context, code1);
+}