[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] std.pk: add convenient functions to print values in different ba
From: |
Mohammad-Reza Nabipoor |
Subject: |
[PATCH] std.pk: add convenient functions to print values in different bases |
Date: |
Thu, 26 Sep 2024 00:01:41 +0200 |
2024-09-26 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* std.pk (hex): New function to print value in base 16.
(dec): New function to print value in base 10.
(bin): New function to print value in base 2.
---
Hi Jose.
First I wanted to add the following dot-commands:
- .hex VALUE
- .dec VALUE
- .bin VALUE
To show the VALUE in the specified base.
Imagine the current obase is 16, but you want to know the value of a
field in base 10:
.dec whatever_value
without changing the current obase.
Then I realized I can implement this as Poke functions:
hex :val 342
If the names are too general, I'd suggest:
- obhex
- obdec
- obbin
These function are only useful for interactive sessions, not
scripting.
Regards,
Mohammad-Reza
ChangeLog | 6 ++++++
libpoke/std.pk | 29 +++++++++++++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/ChangeLog b/ChangeLog
index e8f99c49..d33fc7b0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-09-26 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
+
+ * std.pk (hex): New function to print value in base 16.
+ (dec): New function to print value in base 10.
+ (bin): New function to print value in base 2.
+
2024-09-25 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* doc/learn-poke-language-in-y-minutes.pk: Update and add a few
diff --git a/libpoke/std.pk b/libpoke/std.pk
index c5fc64cd..6d323765 100644
--- a/libpoke/std.pk
+++ b/libpoke/std.pk
@@ -939,3 +939,32 @@ fun popcount = (any v) int<32>:
return popcount (asm any: ("ogetm; nip" : v));
raise E_inval;
}
+
+/* Convenient functions to print values with different base (2, 10, 16). */
+
+fun hex = (any val) void:
+{
+ var ob = vm_obase;
+
+ vm_set_obase (16);
+ printf ("%v\n", val);
+ vm_set_obase (ob);
+}
+
+fun dec = (any val) void:
+{
+ var ob = vm_obase;
+
+ vm_set_obase (10);
+ printf ("%v\n", val);
+ vm_set_obase (ob);
+}
+
+fun bin = (any val) void:
+{
+ var ob = vm_obase;
+
+ vm_set_obase (2);
+ printf ("%v\n", val);
+ vm_set_obase (ob);
+}
--
2.46.2
- [PATCH] std.pk: add convenient functions to print values in different bases,
Mohammad-Reza Nabipoor <=
- Re: [PATCH] std.pk: add convenient functions to print values in different bases, Jose E. Marchesi, 2024/09/26
- [PATCH] poke: add new dot-command .bases, Mohammad-Reza Nabipoor, 2024/09/26
- Re: [PATCH] poke: add new dot-command .bases, Jose E. Marchesi, 2024/09/26
- [PATCH v2] poke: add new dot-command .bases, Mohammad-Reza Nabipoor, 2024/09/26
- Re: [PATCH v2] poke: add new dot-command .bases, Jose E. Marchesi, 2024/09/27
- [PATCH v3] poke: add new dot-command .bases, Mohammad-Reza Nabipoor, 2024/09/28
- Re: [PATCH v3] poke: add new dot-command .bases, Jose E. Marchesi, 2024/09/29
- Re: [PATCH v3] poke: add new dot-command .bases, Mohammad-Reza Nabipoor, 2024/09/29