]> git.draconx.ca Git - cdecl99.git/blobdiff - src/explain.c
Port to use getline.h from dxcommon.
[cdecl99.git] / src / explain.c
index a22b4d05a3d233ebc50fc9d15a30cee28f4e3873..10a7064419dc885e945d067180f997e8d2f72ce1 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Render C declarations as English.
- *  Copyright © 2011, 2021, 2023 Nick Bowler
+ *  Copyright © 2011, 2021, 2023-2024 Nick Bowler
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -17,9 +17,7 @@
  */
 
 #include <config.h>
-#include <stdio.h>
 #include <inttypes.h>
-#include <assert.h>
 
 #include "cdecl.h"
 #include "cdecl-internal.h"
@@ -69,17 +67,12 @@ explain_array(struct output_state *dst, struct cdecl_array *a)
 {
        size_t rc = 0;
 
-       if (a->vla)
-               cdecl__emit(dst, "variable-length ");
-       cdecl__emit(dst, "array ");
-
+       cdecl__emit(dst, "variable-length array " + (a->vla ? 0 : 16));
        if (a->vla) {
                rc = cdecl__emit(dst, a->vla);
        } else {
-               rc = snprintf(dst->dst, dst->dstlen, "%.0" PRIuMAX, a->length);
-               cdecl__advance(dst, rc);
+               rc = cdecl__emit_uint(dst, a->length);
        }
-
        cdecl__emit(dst, " of " + !rc);
 }
 
@@ -102,8 +95,9 @@ static void explain_decl(struct output_state *dst, struct cdecl *decl)
 static void
 explain_function(struct output_state *dst, struct cdecl_function *f)
 {
-       cdecl__emit(dst, "function ");
+       int tail_offset = 7;
 
+       cdecl__emit(dst, "function ");
        if (f->parameters) {
                struct cdecl *p;
 
@@ -115,22 +109,16 @@ explain_function(struct output_state *dst, struct cdecl_function *f)
                                cdecl__emit(dst, ", ");
                }
 
-               if (f->variadic)
-                       cdecl__emit(dst, ", ...) ");
-               else
-                       cdecl__emit(dst, ") ");
+               tail_offset = f->variadic ? 0 : 5;
        }
-
-       cdecl__emit(dst, "returning ");
+       cdecl__emit(dst, ", ...) returning " + tail_offset);
 }
 
 static void
 explain_declarators(struct output_state *dst, struct cdecl_declarator *d)
 {
-       if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
-               return;
-
-       explain_declarators(dst, d->child);
+       if (d->child)
+               explain_declarators(dst, d->child);
 
        switch (d->type) {
        case CDECL_DECL_POINTER:
@@ -142,8 +130,6 @@ explain_declarators(struct output_state *dst, struct cdecl_declarator *d)
        case CDECL_DECL_FUNCTION:
                explain_function(dst, &d->u.function);
                return;
-       default:
-               assert(0);
        }
 }