From 3268027e46fb783c7f203435da52d02d2cc100ff Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 12 Mar 2017 11:05:00 +0100 Subject: [PATCH] mescc: Support do .. while. * module/language/c99/compiler.mes (ast->info): Support do-while. * doc/examples/t.c (test): Test it. --- module/language/c99/compiler.mes | 24 ++++++++++++++++++++++++ scaffold/t.c | 13 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index 5cba1d78..e007fd97 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -1303,6 +1303,30 @@ jump-text) #:globals (.globals body-info)))) + ((do-while ,body ,test) + (let* ((text-length (length text)) + + (body-info ((ast->info info) body)) + (body-text (list-tail (.text body-info) text-length)) + (body-length (length (text->list body-text))) + + (empty (clone info #:text '())) + (test-jump->info ((test->jump->info empty) test)) + (test+jump-info (test-jump->info 0)) + (test-length (length (text->list (.text test+jump-info)))) + + (jump-text (list (lambda (f g ta t d) + (i386:Xjump (- (+ body-length test-length)))))) + (jump-length (length (text->list jump-text))) + + (test-text (.text (test-jump->info jump-length)))) + (clone info #:text + (append + (.text body-info) + test-text + jump-text) + #:globals (.globals body-info)))) + ((labeled-stmt (ident ,label) ,statement) (let ((info (clone info #:text (append text (list label))))) ((ast->info info) statement))) diff --git a/scaffold/t.c b/scaffold/t.c index cfed3505..7ba9cc84 100644 --- a/scaffold/t.c +++ b/scaffold/t.c @@ -395,6 +395,19 @@ test (char *p) char *x = arena; char *y = g_chars; + puts ("t: for (i=1; i<5; ++i)\n"); + for (i=1; i<5; ++i); + if (i != 5) return i; + + puts ("t: while (i<3) i++\n"); + i = 1; + while (i<3) i++; + if (i != 3) return i; + + puts ("t: do i-- while (i>0)\n"); + do i--; while (i>0); + if (i != 0) return 1; + puts ("t: if (0)\n"); if (0) return 1;