Add LastX command

The `LastX` implementation is not part of any RPL implementation that
I am aware of, but it is a staple of RPN. Implementing it in RPL might
make it easier to backport a few RPN programs.

Fixes: #384

Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
This commit is contained in:
Christophe de Dinechin 2023-09-30 17:55:01 +02:00
parent a0bff89b06
commit 257773267f
10 changed files with 57 additions and 5 deletions

View file

@ -35,6 +35,19 @@ Duplicate the same object twice on the stack
## DuplicateN (DUPN)
Duplicate a group of N objects, N being given in stack level 1
## LastArguments (LASTARG)
Put the last arguments back on the stack
## LastX
Put the last first argument on the stack.
This command does not exist on HP RPL calculators, and is here to make it easier
to adapt RPN programs that use LastX a bit more often.
## Undo
Restore the stack to its last state before executing an interactive command.
Note that this command can be used from a program, but it will restore the state
prior to program execution.
## NDUPN
Replicate one object N times and return N

View file

@ -7,7 +7,7 @@ extern const unsigned char EditorFont_sparse_font_data[];
const unsigned char EditorFont_sparse_font_data[73676] FONT_QSPI =
{
0xBC, 0x02, 0xC7, 0xBF, 0x04, 0x36, 0x00, 0x01, 0x00, 0x2C, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01,
0xBD, 0x02, 0xC7, 0xBF, 0x04, 0x36, 0x00, 0x01, 0x00, 0x2C, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01,
0x00, 0x2C, 0x01, 0x01, 0x08, 0x00, 0x20, 0x5F, 0x00, 0x2C, 0x01, 0x01, 0x08, 0x00, 0x02, 0x0B,
0x05, 0x21, 0x09, 0xEF, 0xBD, 0xF7, 0xDE, 0x7B, 0xEF, 0xBD, 0xF7, 0xDE, 0x7B, 0xEF, 0xBD, 0xF7,
0x1E, 0x00, 0x00, 0xB8, 0xFF, 0xFF, 0xFF, 0x0E, 0x03, 0x0B, 0x0B, 0x0F, 0x11, 0x8F, 0x7F, 0xFC,

View file

@ -7,7 +7,7 @@ extern const unsigned char HelpFont_sparse_font_data[];
const unsigned char HelpFont_sparse_font_data[15010] FONT_QSPI =
{
0xBC, 0x02, 0x9E, 0x75, 0x14, 0x00, 0x01, 0x00, 0x11, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01, 0x00,
0xBD, 0x02, 0x9E, 0x75, 0x14, 0x00, 0x01, 0x00, 0x11, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01, 0x00,
0x11, 0x01, 0x01, 0x03, 0x00, 0x20, 0x5F, 0x00, 0x11, 0x01, 0x01, 0x03, 0x00, 0x01, 0x04, 0x02,
0x0D, 0x04, 0xFF, 0xFF, 0xF3, 0x03, 0x01, 0x04, 0x04, 0x05, 0x06, 0xFF, 0xFF, 0x0F, 0x01, 0x04,
0x08, 0x0D, 0x0A, 0x12, 0x12, 0x14, 0x7F, 0x7F, 0x24, 0x24, 0x24, 0xFE, 0xFE, 0x28, 0x48, 0x48,

View file

@ -7,7 +7,7 @@ extern const unsigned char StackFont_sparse_font_data[];
const unsigned char StackFont_sparse_font_data[36409] FONT_QSPI =
{
0xBC, 0x02, 0xB4, 0x9C, 0x02, 0x24, 0x00, 0x01, 0x00, 0x1C, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01,
0xBD, 0x02, 0xB4, 0x9C, 0x02, 0x24, 0x00, 0x01, 0x00, 0x1C, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01,
0x00, 0x1C, 0x01, 0x01, 0x06, 0x00, 0x20, 0x5F, 0x00, 0x1C, 0x01, 0x01, 0x06, 0x00, 0x02, 0x05,
0x03, 0x17, 0x06, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0xC0, 0xFF, 0x1F, 0x02, 0x05, 0x08, 0x0A,
0x0C, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0x01, 0x05, 0x0E, 0x17, 0x10,

View file

@ -664,6 +664,17 @@ COMMAND_BODY(LastArg)
}
COMMAND_BODY(LastX)
// ----------------------------------------------------------------------------
// Return the last first argument
// ----------------------------------------------------------------------------
{
if (!rt.last(0))
return ERROR;
return OK;
}
COMMAND_BODY(Undo)
// ----------------------------------------------------------------------------
// Return the undo stack

View file

@ -146,6 +146,7 @@ COMMAND_DECLARE(SystemSetup); // Select the system menu
COMMAND_DECLARE(Version); // Return a version string
COMMAND_DECLARE(Help); // Activate online help
COMMAND_DECLARE(LastArg); // Return last arguments
COMMAND_DECLARE(LastX); // Return last X argument
COMMAND_DECLARE(Undo); // Revert to the Undo stack
COMMAND_DECLARE(ToolsMenu); // Automatic selection of the right menu
COMMAND_DECLARE(LastMenu); // Return to

View file

@ -280,7 +280,6 @@ CMD(ForStep)
//
// ============================================================================
NAMED(LastArg, "LastArguments")
CMD(Depth)
NAMED(ToList, "→List")
CMD(Get)
@ -547,6 +546,8 @@ CMD(SaveState)
CMD(SystemSetup)
CMD(Help)
CMD(Undo)
NAMED(LastArg, "LastArguments")
CMD(LastX)
CMD(Unimplemented)

View file

@ -948,7 +948,8 @@ MENU(LastThingsMenu,
// ----------------------------------------------------------------------------
// Menu with the last things
// ----------------------------------------------------------------------------
"Arg", ID_LastArg,
"Args", ID_LastArg,
"X", ID_LastX,
"Stack", ID_Undo,
"Menu", ID_LastMenu,
"Cmd", ID_Unimplemented);

View file

@ -978,6 +978,26 @@ bool runtime::last()
}
bool runtime::last(uint index)
// ----------------------------------------------------------------------------
// Push back the last argument on the stack
// ----------------------------------------------------------------------------
{
size_t nargs = args();
if (index >= nargs)
{
rt.missing_argument_error();
return false;
}
size_t sz = sizeof(object_p);
if (available(sz) < sz)
return false;
*--Stack = Args[index];
return true;
}
bool runtime::save()
// ----------------------------------------------------------------------------
// Save the stack in the undo area

View file

@ -561,6 +561,11 @@ struct runtime
// Push back last arguments
// ------------------------------------------------------------------------
bool last(uint index);
// ------------------------------------------------------------------------
// Push back last argument
// ------------------------------------------------------------------------
bool save();
// ------------------------------------------------------------------------