[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[COMMITTED] pkl: allow re-definition of variables, functions and units
From: |
Jose E. Marchesi |
Subject: |
[COMMITTED] pkl: allow re-definition of variables, functions and units |
Date: |
Wed, 18 Nov 2020 11:43:12 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
In order to ease interactive usage of poke, this patch makes it
possible to re-define variables, functions and units at the toplevel.
So this is now allowed:
(poke) var foo = 10;
(poke) var foo = 20;
But this of course is not:
fun f = void:
{
var foo = 10;
var foo = 20;
...
}
2020-11-18 Jose E. Marchesi <jemarch@gnu.org>
* libpoke/pkl-env.c (pkl_env_register): Adjust call to
register_decl accordingly.
(register_decl): Allow "redefining" variables, functions and
types.
* testsuite/poke.pkl/redef-1.pk: New test.
* testsuite/poke.pkl/redef-2.pk: Likewise.
* testsuite/poke.pkl/redef-3.pk: Likewise.
* testsuite/poke.pkl/redef-diag-1.pk: Likewise.
* testsuite/poke.pkl/redef-diag-2.pk: Likewise.
* testsuite/poke.pkl/redef-diag-3.pk: Likewise.
* testsuite/Makefile.am (EXTRA_DIST): Add new tests.
* etc/poke.rec (Allow re-definitions of variables and units at the
top-level): Remove as done.
---
ChangeLog | 16 +++++++++++++++
etc/poke.rec | 9 --------
libpoke/pkl-env.c | 33 +++++++++++++++++++++++++-----
testsuite/Makefile.am | 6 ++++++
testsuite/poke.pkl/redef-1.pk | 11 ++++++++++
testsuite/poke.pkl/redef-2.pk | 14 +++++++++++++
testsuite/poke.pkl/redef-3.pk | 9 ++++++++
testsuite/poke.pkl/redef-diag-1.pk | 8 ++++++++
testsuite/poke.pkl/redef-diag-2.pk | 8 ++++++++
testsuite/poke.pkl/redef-diag-3.pk | 8 ++++++++
10 files changed, 108 insertions(+), 14 deletions(-)
create mode 100644 testsuite/poke.pkl/redef-1.pk
create mode 100644 testsuite/poke.pkl/redef-2.pk
create mode 100644 testsuite/poke.pkl/redef-3.pk
create mode 100644 testsuite/poke.pkl/redef-diag-1.pk
create mode 100644 testsuite/poke.pkl/redef-diag-2.pk
create mode 100644 testsuite/poke.pkl/redef-diag-3.pk
diff --git a/ChangeLog b/ChangeLog
index 6dcfbd59..29b83483 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2020-11-18 Jose E. Marchesi <jemarch@gnu.org>
+
+ * libpoke/pkl-env.c (pkl_env_register): Adjust call to
+ register_decl accordingly.
+ (register_decl): Allow "redefining" variables, functions and
+ types.
+ * testsuite/poke.pkl/redef-1.pk: New test.
+ * testsuite/poke.pkl/redef-2.pk: Likewise.
+ * testsuite/poke.pkl/redef-3.pk: Likewise.
+ * testsuite/poke.pkl/redef-diag-1.pk: Likewise.
+ * testsuite/poke.pkl/redef-diag-2.pk: Likewise.
+ * testsuite/poke.pkl/redef-diag-3.pk: Likewise.
+ * testsuite/Makefile.am (EXTRA_DIST): Add new tests.
+ * etc/poke.rec (Allow re-definitions of variables and units at the
+ top-level): Remove as done.
+
2020-11-18 Jose E. Marchesi <jemarch@gnu.org>
* etc/poke.rec (Allow re-definitions of variables and units at the
diff --git a/etc/poke.rec b/etc/poke.rec
index 5212de4a..ec2558d4 100644
--- a/etc/poke.rec
+++ b/etc/poke.rec
@@ -2252,15 +2252,6 @@ Description:
+ Support for mapping functions is a requirement for this.
Requires: Support mapping and constructing functions
-Summary: Allow re-definitions of variables and units at the top-level
-Component: Compiler
-Kind: ENH
-Priority: 3
-Description:
-+ Re-defining variable and units at the top-level will ease interactive
-+ usage of poke by a lot.
-Target: 1.0
-
Summary: Factorize promotion of expressions in promo
Component: Compiler
Kind: ENH
diff --git a/libpoke/pkl-env.c b/libpoke/pkl-env.c
index d8a3a8c6..00486b4b 100644
--- a/libpoke/pkl-env.c
+++ b/libpoke/pkl-env.c
@@ -118,15 +118,38 @@ get_registered (pkl_hash hash_table, const char *name)
}
static int
-register_decl (pkl_hash hash_table,
+register_decl (int top_level_p,
+ pkl_hash hash_table,
const char *name,
pkl_ast_node decl)
{
int hash;
+ pkl_ast_node found_decl;
- if (get_registered (hash_table, name) != NULL)
- /* Already registered. */
- return 0;
+ /* Check if DECL is already registered in the given hash table.
+
+ If we are in the global environment and the declaration is for a
+ variable, funcion, or an unit, then we allow "redefining" by
+ changing the name of the previous declaration to "".
+
+ Otherwise we don't register DECL, as it is already defined. */
+
+ found_decl = get_registered (hash_table, name);
+ if (found_decl != NULL)
+ {
+ int decl_kind = PKL_AST_DECL_KIND (decl);
+
+ if (top_level_p
+ && (decl_kind == PKL_AST_DECL_KIND_VAR
+ || decl_kind == PKL_AST_DECL_KIND_FUNC
+ || decl_kind == PKL_AST_DECL_KIND_UNIT))
+ {
+ pkl_ast_node_free (PKL_AST_DECL_NAME (found_decl));
+ PKL_AST_DECL_NAME (found_decl) = NULL;
+ }
+ else
+ return 0;
+ }
/* Add the declaration to the hash table. */
hash = hash_string (name);
@@ -206,7 +229,7 @@ pkl_env_register (pkl_env env,
{
pkl_hash *table = get_ns_table (env, namespace);
- if (register_decl (*table, name, decl))
+ if (register_decl (env->up == NULL, *table, name, decl))
{
switch (PKL_AST_DECL_KIND (decl))
{
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index c4fbb11a..f2c1d065 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -1285,6 +1285,12 @@ EXTRA_DIST = \
poke.pkl/raise-6.pk \
poke.pkl/raise-diag-1.pk \
poke.pkl/rand-1.pk \
+ poke.pkl/redef-diag-1.pk \
+ poke.pkl/redef-diag-2.pk \
+ poke.pkl/redef-diag-3.pk \
+ poke.pkl/redef-1.pk \
+ poke.pkl/redef-2.pk \
+ poke.pkl/redef-3.pk \
poke.pkl/return-1.pk \
poke.pkl/return-2.pk \
poke.pkl/return-diag-1.pk \
diff --git a/testsuite/poke.pkl/redef-1.pk b/testsuite/poke.pkl/redef-1.pk
new file mode 100644
index 00000000..2240ff70
--- /dev/null
+++ b/testsuite/poke.pkl/redef-1.pk
@@ -0,0 +1,11 @@
+/* { dg-do run } */
+
+var N = 2;
+fun f = int: { return N + 1; }
+
+/* { dg-command { var N = 10; } } */
+/* { dg-command { f } } */
+/* { dg-output "3" } */
+/* { dg-command { N + 1 } } */
+/* { dg-output "\n11" } */
+
diff --git a/testsuite/poke.pkl/redef-2.pk b/testsuite/poke.pkl/redef-2.pk
new file mode 100644
index 00000000..42545b29
--- /dev/null
+++ b/testsuite/poke.pkl/redef-2.pk
@@ -0,0 +1,14 @@
+/* { dg-do run } */
+
+var N = 2;
+fun f = int: { return N + 1; }
+
+/* { dg-command { var oldf = f } } */
+/* { dg-command { fun f = int: { return 60 + N; } } } */
+/* { dg-command { f } } */
+/* { dg-output "62" } */
+/* { dg-command {fun f = int: { return 666; } } } */
+/* { dg-command {f} } */
+/* { dg-output "\n666" } */
+/* { dg-command { oldf } } */
+/* { dg-output "\n3" } */
diff --git a/testsuite/poke.pkl/redef-3.pk b/testsuite/poke.pkl/redef-3.pk
new file mode 100644
index 00000000..11c31489
--- /dev/null
+++ b/testsuite/poke.pkl/redef-3.pk
@@ -0,0 +1,9 @@
+/* { dg-do run } */
+
+unit Foo = 5;
+
+/* { dg-command { 2#Foo } } */
+/* { dg-output "2#5" } */
+/* { dg-command {unit Foo = 666} } */
+/* { dg-command {3#Foo} } */
+/* { dg-output "\n3#666" } */
diff --git a/testsuite/poke.pkl/redef-diag-1.pk
b/testsuite/poke.pkl/redef-diag-1.pk
new file mode 100644
index 00000000..3e484e78
--- /dev/null
+++ b/testsuite/poke.pkl/redef-diag-1.pk
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+
+fun foo = void:
+{
+ var a = 10;
+ var b = 20;
+ var a = 30; /* { dg-error "" } */
+}
diff --git a/testsuite/poke.pkl/redef-diag-2.pk
b/testsuite/poke.pkl/redef-diag-2.pk
new file mode 100644
index 00000000..aef2fe2f
--- /dev/null
+++ b/testsuite/poke.pkl/redef-diag-2.pk
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+
+fun foo = void:
+{
+ unit a = 10;
+ unit b = 20;
+ unit a = 30; /* { dg-error "" } */
+}
diff --git a/testsuite/poke.pkl/redef-diag-3.pk
b/testsuite/poke.pkl/redef-diag-3.pk
new file mode 100644
index 00000000..60590a3f
--- /dev/null
+++ b/testsuite/poke.pkl/redef-diag-3.pk
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+
+fun foo = void:
+{
+ fun a = void: {}
+ fun b = void: {}
+ fun a = void: {} ; /* { dg-error "" } */
+}
--
2.25.0.2.g232378479e
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [COMMITTED] pkl: allow re-definition of variables, functions and units,
Jose E. Marchesi <=