bug-make
[Top][All Lists]
Advanced

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

[patch] Guile conditional


From: Dmitry Bogatov
Subject: [patch] Guile conditional
Date: Mon, 15 Jun 2015 17:36:57 +0300
User-agent: Mutt/1.5.23.1-rc1 (2014-03-12)

Hello!

I would like to propose patch, implementing new
conditional: `ifscm'. It has following syntax:

        ifscm (some s-expression #f)
        MAKE_VAR = foo
        endif

Why? Some complains, that lack of logical operations make conditionals
ugly. Maybe. To me, main is that to check whether file exists requires
voodo like this(real code from some Debian package)

        ifeq ($(shell if [ -f /my/file ] ; then echo 0; else echo 1; fi), 0)

instead of just

        ifscm (file-exists "/my/file")

Patch does not contains documentation, I propose discuss code first.
What do you think?

-- 
Accept: text/plain, text/x-diff
Accept-Language: eo,en,ru
X-Keep-In-CC: yes
From 87123428a53f534611c8266d21c161a1ec951675 Mon Sep 17 00:00:00 2001
From: Dmitry Bogatov <address@hidden>
Date: Fri, 5 Jun 2015 22:33:22 +0300
Subject: [PATCH] Add Guile-based conditional

  * read.c(conditional_line): new conditional `ifscm'
  * read.c: new conditional type -- `c_ifscm'
  * read.c(conditional_scm_eval): new function, that evaluates `ifscm'
    conditional argument, and returns it's truthfulness. Needed to
    comply with `scm_with_guile' function expectations.

Signed-off-by: Dmitry Bogatov <address@hidden>
---
 read.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 88 insertions(+), 1 deletion(-)

diff --git a/read.c b/read.c
index e67d386..a80e48b 100644
--- a/read.c
+++ b/read.c
@@ -29,6 +29,9 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "debug.h"
 #include "hash.h"
 
+#ifdef HAVE_GUILE
+#include <libguile.h>
+#endif /* !HAVE_GUILE */
 
 #ifdef WINDOWS32
 #include <windows.h>
@@ -1556,6 +1559,81 @@ do_define (char *name, enum variable_origin origin, 
struct ebuffer *ebuf)
   return (v);
 }
 
+
+enum conditional_cmdtype {
+  c_ifdef,
+  c_ifndef,
+  c_ifeq,
+#ifdef HAVE_GUILE
+  c_ifscm,
+#endif /* !HAVE_GUILE */
+  c_ifneq,
+  c_else,
+  c_endif
+};
+
+#ifdef HAVE_GUILE
+
+struct conditional_scm_context {
+  const gmk_floc *flocp;
+  char *line;
+  int eval_error;
+};
+
+static SCM
+conditional_scm_eval_handler(void *data, SCM key, SCM args)
+{
+  struct conditional_scm_context *ctx = data;
+
+  /* Due `with_guile' magic we can't call `fatal' here. */
+  ctx->eval_error = 1;
+  scm_print_exception(scm_current_error_port(), SCM_BOOL_F, key, args);
+  return SCM_UNDEFINED;
+}
+
+static SCM
+conditional_scm_eval_body(void *data)
+{
+  const char *line = data;
+  return scm_c_eval_string(line);
+}
+
+static void*
+conditional_scm_eval(void *data)
+{
+  struct conditional_scm_context *ctx = data;
+  SCM value = scm_internal_catch(
+      SCM_BOOL_T,                    /* catch everything */
+      &conditional_scm_eval_body,    /* body:: SCM (*)(void*) */
+      ctx->line,                     /* body argument */
+      &conditional_scm_eval_handler, /* exception handler */
+      ctx);                          /* exception handler argument */
+
+  return (scm_is_true (value) ? data : NULL);
+}
+
+static int
+conditional_line_ifscm(char *line, const gmk_floc *flocp)
+{
+  void *eval_result;
+  struct conditional_scm_context ctx;
+  ctx.flocp = flocp;
+  ctx.line = line;
+  ctx.eval_error = 0;
+
+  eval_result = scm_with_guile(conditional_scm_eval, &ctx);
+
+  if (ctx.eval_error)
+    {
+      OS(fatal, ctx.flocp, _("exception during evaluating Guile expression: 
`%s'"),
+           ctx.line);
+    }
+
+  return (NULL == eval_result);
+}
+
+#endif /* !HAVE_GUILE */
+
 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
    "ifneq", "else" and "endif".
    LINE is the input line, with the command as its first word.
@@ -1572,7 +1650,7 @@ static int
 conditional_line (char *line, int len, const gmk_floc *flocp)
 {
   const char *cmdname;
-  enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_else, c_endif } cmdtype;
+  enum conditional_cmdtype cmdtype;
   unsigned int i;
   unsigned int o;
 
@@ -1584,6 +1662,9 @@ conditional_line (char *line, int len, const gmk_floc 
*flocp)
   chkword ("ifdef", c_ifdef)
   else chkword ("ifndef", c_ifndef)
   else chkword ("ifeq", c_ifeq)
+#ifdef HAVE_GUILE
+  else chkword ("ifscm", c_ifscm)
+#endif /* !HAVE_GUILE */
   else chkword ("ifneq", c_ifneq)
   else chkword ("else", c_else)
   else chkword ("endif", c_endif)
@@ -1724,6 +1805,12 @@ conditional_line (char *line, int len, const gmk_floc 
*flocp)
 
       free (var);
     }
+#ifdef HAVE_GUILE
+  else if (cmdtype == c_ifscm)
+    {
+      conditionals->ignoring[o] = conditional_line_ifscm(line, flocp);
+    }
+#endif /* !HAVE_GUILE */
   else
     {
       /* "ifeq" or "ifneq".  */
-- 
I may be not subscribed. Please, keep me in carbon copy.

Attachment: pgpkXXqQ8u70U.pgp
Description: PGP signature


reply via email to

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