poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] libpoke: Move auto-completion global state to compiler instance


From: Mohammad-Reza Nabipoor
Subject: [PATCH] libpoke: Move auto-completion global state to compiler instance
Date: Tue, 22 Mar 2022 15:51:35 +0430

2022-03-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * libpoke/libpoke.c (struct _pk_compiler): Add auto completion
        machinery data.
        (pk_compiler_new_with_flags): s/malloc/calloc/.
        (complete_attribute): Remove global state, use `pkc`.
        (complete_struct): Likewise.
        (complete_decl): Likewise.
        (pk_ios_completion_function): Likewise.
---

Hi, Jose.

Instead of having `pk_{make,free}_completion_state` functions, I decided
to move global state to compiler instance.

WDYT?

Regards,
Mohammad-Reza



 ChangeLog         | 10 ++++++++++
 libpoke/libpoke.c | 47 ++++++++++++++++++++++++++++-------------------
 2 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cd95242d..73572766 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2022-03-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * libpoke/libpoke.c (struct _pk_compiler): Add auto completion
+       machinery data.
+       (pk_compiler_new_with_flags): s/malloc/calloc/.
+       (complete_attribute): Remove global state, use `pkc`.
+       (complete_struct): Likewise.
+       (complete_decl): Likewise.
+       (pk_ios_completion_function): Likewise.
+
 2022-03-22  Jose E. Marchesi  <jemarch@gnu.org>
 
        * emacs/poke.el (poke-repl-end-of-iteration): Echo output in the
diff --git a/libpoke/libpoke.c b/libpoke/libpoke.c
index afd7a220..a7ec27bc 100644
--- a/libpoke/libpoke.c
+++ b/libpoke/libpoke.c
@@ -36,8 +36,12 @@ struct _pk_compiler
   pkl_compiler compiler;
   pvm vm;
 
-  pkl_ast_node complete_type;
   int status;  /* Status of last API function call. Initialized with PK_OK */
+  /* Data for completion machinery.  */
+  pkl_ast_node complete_type;
+  ios completion_ios;
+  int completion_idx;
+  struct pkl_ast_node_iter completion_iter;
 };
 
 struct pk_term_if libpoke_term_if;
@@ -57,7 +61,7 @@ pk_compiler_new_with_flags (struct pk_term_if *term_if, 
uint32_t flags)
       || !term_if->hyperlink_fn || !term_if->end_hyperlink_fn)
     return NULL;
 
-  pkc = malloc (sizeof (struct _pk_compiler));
+  pkc = calloc (1, sizeof (struct _pk_compiler));
   if (pkc)
     {
       uint32_t pkl_flags = 0;
@@ -200,11 +204,12 @@ pk_set_alien_token_fn (pk_compiler pkc, 
pk_alien_token_handler_fn cb)
 static char *
 complete_attribute (pk_compiler pkc, const char *x, int state)
 {
-  static int idx = 0;
+#define idx pkc->completion_idx
+
   size_t trunk_len;
   char *ret = NULL;
   size_t len = strlen (x);
-  static char *attr_names[] =
+  static const char *attr_names[] =
     {
 #define PKL_DEF_ATTR(CODE,NAME) NAME,
 #include "pkl-attrs.def"
@@ -220,7 +225,7 @@ complete_attribute (pk_compiler pkc, const char *x, int 
state)
   int i;
   for (i = idx; attr_names[i] != NULL; ++i)
     {
-      char *attr_name = attr_names[i];
+      const char *attr_name = attr_names[i];
 
       if (strncmp (x + trunk_len, attr_name, len - trunk_len) == 0)
         {
@@ -240,12 +245,15 @@ complete_attribute (pk_compiler pkc, const char *x, int 
state)
 
  exit:
   return ret;
+
+#undef idx
 }
 
 static char *
 complete_struct (pk_compiler pkc, const char *x, int state)
 {
-  static int idx = 0;
+#define idx pkc->completion_idx
+
   char *ret = NULL;
   pkl_ast_node type = pkc->complete_type;
   pkl_ast_node t;
@@ -328,13 +336,16 @@ complete_struct (pk_compiler pkc, const char *x, int 
state)
  exit:
   pkc->complete_type = type;
   return ret;
+
+#undef idx
 }
 
 static char *
 complete_decl (pk_compiler pkc, const char *x, int state)
 {
-  static int idx = 0;
-  static struct pkl_ast_node_iter iter;
+#define idx pkc->completion_idx
+#define iter pkc->completion_iter
+
   pkl_env env = pkl_get_env (pkc->compiler);
   if (state == 0)
     {
@@ -351,6 +362,9 @@ complete_decl (pk_compiler pkc, const char *x, int state)
 
   size_t len = strlen (x);
   return pkl_env_get_next_matching_decl (env, &iter, x, len);
+
+#undef iter
+#undef idx
 }
 
 /* This function is called repeatedly by the readline library, when
@@ -388,20 +402,13 @@ pk_completion_function (pk_compiler pkc,
    indicate that there are no more such tags.
  */
 char *
-pk_ios_completion_function (pk_compiler pkc __attribute__ ((unused)),
-                            const char *text, int state)
+pk_ios_completion_function (pk_compiler pkc, const char *text, int state)
 {
-  static ios io;
-  if (state == 0)
-    {
-      io = ios_begin ();
-    }
-  else
-    {
-      io = ios_next (io);
-    }
+#define io pkc->completion_ios
 
   int len  = strlen (text);
+
+  io = state == 0 ?  ios_begin () : ios_next (io);
   while (1)
     {
       if (ios_end (io))
@@ -417,6 +424,8 @@ pk_ios_completion_function (pk_compiler pkc __attribute__ 
((unused)),
     }
 
   return NULL;
+
+#undef io
 }
 
 int
-- 
2.35.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]