poke-devel
[Top][All Lists]
Advanced

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

[PATCH v2 1/2] pkl: change AST structure for try-catch stmts


From: Mohammad-Reza Nabipoor
Subject: [PATCH v2 1/2] pkl: change AST structure for try-catch stmts
Date: Thu, 14 Jul 2022 23:43:22 +0430

2022-07-14  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * etc/hacking.org (try_stmt): Update the topology.
        * HACKING (try_stmt): Update.
        * libpoke/pkl-ast.h (enum pkl_ast_code): Add
        PKL_AST_TRY_STMT_HANDLER.
        (PKL_AST_TRY_STMT_HANDLER_CODE): New macro.
        (struct pkl_ast_try_stmt_handler): New struct.
        (pkl_ast_make_try_stmt_handler): New function.
        (union pkl_ast_node): Add `try_stmt_handler`.
        * libpoke/pkl-ast.c ():
        * libpoke/pkl-tab.y (stmt): Adapt to use
        `pkl_ast_make_try_stmt_handler` in try-catch statements.
        * libpoke/pkl-pass.c (pkl_do_pass_1): Add case for TRY_STMT_HANDLER.
        * libpoke/pkl-gen.c (pkl_gen_ps_try_stmt_handler): New handler.
        (pkl_phase_gen): Register new handler.
---
 ChangeLog          | 17 +++++++++++++++++
 HACKING            |  5 ++++-
 etc/hacking.org    |  5 ++++-
 libpoke/pkl-ast.c  | 26 ++++++++++++++++++++++++++
 libpoke/pkl-ast.h  | 16 ++++++++++++++++
 libpoke/pkl-gen.c  | 12 ++++++++++++
 libpoke/pkl-pass.c |  3 +++
 libpoke/pkl-tab.y  | 24 +++++++++++++++---------
 8 files changed, 97 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 75893ee0..f3322b03 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2022-07-14  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * etc/hacking.org (try_stmt): Update the topology.
+       * HACKING (try_stmt): Update.
+       * libpoke/pkl-ast.h (enum pkl_ast_code): Add
+       PKL_AST_TRY_STMT_HANDLER.
+       (PKL_AST_TRY_STMT_HANDLER_CODE): New macro.
+       (struct pkl_ast_try_stmt_handler): New struct.
+       (pkl_ast_make_try_stmt_handler): New function.
+       (union pkl_ast_node): Add `try_stmt_handler`.
+       * libpoke/pkl-ast.c ():
+       * libpoke/pkl-tab.y (stmt): Adapt to use
+       `pkl_ast_make_try_stmt_handler` in try-catch statements.
+       * libpoke/pkl-pass.c (pkl_do_pass_1): Add case for TRY_STMT_HANDLER.
+       * libpoke/pkl-gen.c (pkl_gen_ps_try_stmt_handler): New handler.
+       (pkl_phase_gen): Register new handler.
+
 2022-07-14  Jose E. Marchesi  <jemarch@gnu.org>
 
        * libpoke/pkl-ast.h: Update comments.
diff --git a/HACKING b/HACKING
index ecdf4034..35f4665c 100644
--- a/HACKING
+++ b/HACKING
@@ -1330,9 +1330,12 @@ with GNU poke.  If not, see 
<https://www.gnu.org/licenses/>.
   | |   |
   | |   +-- code
   | |
+  | +-- try_stmt_handler
+  | |   |
+  | |   +-- code
+  | |
   | +-- [arg]
   | +-- [exp]
-  | +-- handler
   `----
 
 
diff --git a/etc/hacking.org b/etc/hacking.org
index 8bca1042..ad3ff9c3 100644
--- a/etc/hacking.org
+++ b/etc/hacking.org
@@ -1004,9 +1004,12 @@ have a testsuite in =testsuite/poke.FOO= with a driver
     : |   |
     : |   +-- code
     : |
+    : +-- try_stmt_handler
+    : |   |
+    : |   +-- code
+    : |
     : +-- [arg]
     : +-- [exp]
-    : +-- handler
 
     where ARG and EXP are exclusive.
 
diff --git a/libpoke/pkl-ast.c b/libpoke/pkl-ast.c
index ee7fd800..92b2f936 100644
--- a/libpoke/pkl-ast.c
+++ b/libpoke/pkl-ast.c
@@ -2100,6 +2100,20 @@ pkl_ast_make_try_stmt_body (pkl_ast ast, pkl_ast_node 
code)
   return try_stmt_body;
 }
 
+/* Build and return an AST node for handler of a `try-catch' statement.  */
+
+pkl_ast_node
+pkl_ast_make_try_stmt_handler (pkl_ast ast, pkl_ast_node code)
+{
+  pkl_ast_node try_stmt_handler
+    = pkl_ast_make_node (ast, PKL_AST_TRY_STMT_HANDLER);
+
+  assert (code);
+
+  PKL_AST_TRY_STMT_HANDLER_CODE (try_stmt_handler) = ASTREF (code);
+  return try_stmt_handler;
+}
+
 /* Build and return an AST node for a `print' statement.  */
 
 pkl_ast_node
@@ -2527,6 +2541,11 @@ pkl_ast_node_free (pkl_ast_node ast)
       pkl_ast_node_free (PKL_AST_TRY_STMT_BODY_CODE (ast));
       break;
 
+    case PKL_AST_TRY_STMT_HANDLER:
+
+      pkl_ast_node_free (PKL_AST_TRY_STMT_HANDLER_CODE (ast));
+      break;
+
     case PKL_AST_FORMAT_ARG:
       free (PKL_AST_FORMAT_ARG_SUFFIX (ast));
       free (PKL_AST_FORMAT_ARG_BEGIN_SC (ast));
@@ -3339,6 +3358,13 @@ pkl_ast_print_1 (FILE *fp, pkl_ast_node ast, int indent)
       PRINT_AST_SUBAST (try_stmt_body_code, TRY_STMT_BODY_CODE);
       break;
 
+    case PKL_AST_TRY_STMT_HANDLER:
+      IPRINTF ("TRY_STMT_HANDLER::\n");
+
+      PRINT_COMMON_FIELDS;
+      PRINT_AST_SUBAST (try_stmt_handler_code, TRY_STMT_HANDLER_CODE);
+      break;
+
     case PKL_AST_TRY_STMT:
       IPRINTF ("TRY_STMT::\n");
 
diff --git a/libpoke/pkl-ast.h b/libpoke/pkl-ast.h
index 2ba2b3f9..8ebd6dad 100644
--- a/libpoke/pkl-ast.h
+++ b/libpoke/pkl-ast.h
@@ -85,6 +85,7 @@ enum pkl_ast_code
   PKL_AST_EXP_STMT,
   PKL_AST_TRY_STMT,
   PKL_AST_TRY_STMT_BODY,
+  PKL_AST_TRY_STMT_HANDLER,
   PKL_AST_PRINT_STMT,
   PKL_AST_BREAK_STMT,
   PKL_AST_CONTINUE_STMT,
@@ -1834,6 +1835,20 @@ struct pkl_ast_try_stmt_body
 
 pkl_ast_node pkl_ast_make_try_stmt_body (pkl_ast ast, pkl_ast_node code);
 
+/* PKL_AST_TRY_STMT_HANDLER nodes represent the handler part of the
+   try-catch statements.  */
+
+#define PKL_AST_TRY_STMT_HANDLER_CODE(AST) ((AST)->try_stmt_handler.code)
+
+struct pkl_ast_try_stmt_handler
+{
+  struct pkl_ast_common common;
+
+  union pkl_ast_node *code;
+};
+
+pkl_ast_node pkl_ast_make_try_stmt_handler (pkl_ast ast, pkl_ast_node code);
+
 /* PKL_AST_PRINT_STMT nodes represent `print' and `printf' statements.
 
    STR_EXP, if not NULL, is an expression node of type string.  In
@@ -1970,6 +1985,7 @@ union pkl_ast_node
   struct pkl_ast_exp_stmt exp_stmt;
   struct pkl_ast_try_stmt try_stmt;
   struct pkl_ast_try_stmt_body try_stmt_body;
+  struct pkl_ast_try_stmt_body try_stmt_handler;
   struct pkl_ast_break_stmt break_stmt;
   struct pkl_ast_continue_stmt continue_stmt;
   struct pkl_ast_raise_stmt raise_stmt;
diff --git a/libpoke/pkl-gen.c b/libpoke/pkl-gen.c
index 6ecd322a..e5f1abf3 100644
--- a/libpoke/pkl-gen.c
+++ b/libpoke/pkl-gen.c
@@ -1762,6 +1762,17 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_ps_try_stmt_body)
 }
 PKL_PHASE_END_HANDLER
 
+/*
+ * | CODE
+ * TRY_STMT_HANDLER
+ */
+
+PKL_PHASE_BEGIN_HANDLER (pkl_gen_ps_try_stmt_handler)
+{
+  /* Nothing to do here.  */
+}
+PKL_PHASE_END_HANDLER
+
 /*
  * | BODY
  * | [HANDLER]
@@ -4639,6 +4650,7 @@ struct pkl_phase pkl_phase_gen =
    PKL_PHASE_PS_HANDLER (PKL_AST_RAISE_STMT, pkl_gen_ps_raise_stmt),
    PKL_PHASE_PR_HANDLER (PKL_AST_TRY_STMT, pkl_gen_pr_try_stmt),
    PKL_PHASE_PS_HANDLER (PKL_AST_TRY_STMT_BODY, pkl_gen_ps_try_stmt_body),
+   PKL_PHASE_PS_HANDLER (PKL_AST_TRY_STMT_HANDLER, 
pkl_gen_ps_try_stmt_handler),
    PKL_PHASE_PS_HANDLER (PKL_AST_FUNCALL_ARG, pkl_gen_ps_funcall_arg),
    PKL_PHASE_PR_HANDLER (PKL_AST_FUNCALL, pkl_gen_pr_funcall),
    PKL_PHASE_PR_HANDLER (PKL_AST_FUNC, pkl_gen_pr_func),
diff --git a/libpoke/pkl-pass.c b/libpoke/pkl-pass.c
index a6fabd65..ed7daf8c 100644
--- a/libpoke/pkl-pass.c
+++ b/libpoke/pkl-pass.c
@@ -548,6 +548,9 @@ pkl_do_pass_1 (pkl_compiler compiler,
     case PKL_AST_TRY_STMT_BODY:
       PKL_PASS (PKL_AST_TRY_STMT_BODY_CODE (node));
       break;
+    case PKL_AST_TRY_STMT_HANDLER:
+      PKL_PASS (PKL_AST_TRY_STMT_HANDLER_CODE (node));
+      break;
     case PKL_AST_TRY_STMT:
       PKL_PASS (PKL_AST_TRY_STMT_BODY (node));
       if (PKL_AST_TRY_STMT_HANDLER (node))
diff --git a/libpoke/pkl-tab.y b/libpoke/pkl-tab.y
index 334c3ec0..c12bb0f6 100644
--- a/libpoke/pkl-tab.y
+++ b/libpoke/pkl-tab.y
@@ -2507,34 +2507,40 @@ stmt:
                 }
         | TRY stmt CATCH comp_stmt
                 {
-                  pkl_ast_node body = pkl_ast_make_try_stmt_body 
(pkl_parser->ast,
-                                                                  $2);
+                  pkl_ast_node body
+                    = pkl_ast_make_try_stmt_body (pkl_parser->ast, $2);
+                  pkl_ast_node handler
+                    = pkl_ast_make_try_stmt_handler (pkl_parser->ast, $4);
 
                   $$ = pkl_ast_make_try_stmt (pkl_parser->ast,
                                               PKL_AST_TRY_STMT_KIND_CATCH,
-                                              body, $4, NULL, NULL);
+                                              body, handler, NULL, NULL);
                   PKL_AST_LOC (body) = @2;
                   PKL_AST_LOC ($$) = @$;
                 }
         | TRY stmt CATCH IF expression comp_stmt
                 {
-                  pkl_ast_node body = pkl_ast_make_try_stmt_body 
(pkl_parser->ast,
-                                                                  $2);
+                  pkl_ast_node body
+                    = pkl_ast_make_try_stmt_body (pkl_parser->ast, $2);
+                  pkl_ast_node handler
+                    = pkl_ast_make_try_stmt_handler (pkl_parser->ast, $6);
 
                   $$ = pkl_ast_make_try_stmt (pkl_parser->ast,
                                               PKL_AST_TRY_STMT_KIND_CATCH,
-                                              body, $6, NULL, $5);
+                                              body, handler, NULL, $5);
                   PKL_AST_LOC (body) = @2;
                   PKL_AST_LOC ($$) = @$;
                 }
         | TRY stmt CATCH  '(' pushlevel function_arg ')' comp_stmt
                 {
-                  pkl_ast_node body = pkl_ast_make_try_stmt_body 
(pkl_parser->ast,
-                                                                  $2);
+                  pkl_ast_node body
+                    = pkl_ast_make_try_stmt_body (pkl_parser->ast, $2);
+                  pkl_ast_node handler
+                    = pkl_ast_make_try_stmt_handler (pkl_parser->ast, $8);
 
                   $$ = pkl_ast_make_try_stmt (pkl_parser->ast,
                                               PKL_AST_TRY_STMT_KIND_CATCH,
-                                              body, $8, $6, NULL);
+                                              body, handler, $6, NULL);
                   PKL_AST_LOC (body) = @2;
                   PKL_AST_LOC ($$) = @$;
 
-- 
2.36.1




reply via email to

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