Add CONSTANTS as a way to select mathematical constants menu

On the HP50G, the `CONSTANTS` menu selects "CAS constants".
This seems to approximately match DB48X mathematical constants menus.

Fixes: #1183

Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
This commit is contained in:
Christophe de Dinechin 2024-09-13 23:55:49 +02:00
parent e791180d14
commit e6d582818b
3 changed files with 67 additions and 4 deletions

View file

@ -260,6 +260,19 @@ COMMAND_BODY(Const)
}
COMMAND_BODY(Constants)
// ----------------------------------------------------------------------------
// Select the mathematics constants menu
// ----------------------------------------------------------------------------
{
if (object_p menu = constant::lookup_menu(constant::constants,
"Mathematics"))
return menu->evaluate();
rt.invalid_constant_error();
return ERROR;
}
// ============================================================================
//
@ -950,3 +963,52 @@ object::result constant::lookup_command(config_r cfg, bool numerical)
cfg.error();
return ERROR;
}
object_p constant::lookup_menu(config_r cfg, utf8 name, size_t len)
// ----------------------------------------------------------------------------
// Find the menu in the current configuratoin
// ----------------------------------------------------------------------------
{
unit_file cfile(cfg.file);
size_t maxb = cfg.nbuiltins;
auto builtins = cfg.builtins;
uint idx = cfg.first_menu;
// Check in-file constants
if (cfile.valid())
{
cfile.seek(0);
while (symbol_g category = cfile.next(true))
{
size_t clen = 0;
utf8 ctxt = category->value(&clen);
if (len == clen && memcmp(name, ctxt, len) == 0)
return object::static_object(id(idx));
idx++;
}
}
// Check built-in constants
for (size_t b = 0; b < maxb; b += 2)
{
if (!builtins[b+1] || !*builtins[b+1])
{
cstring ctxt = builtins[b];
if (ctxt[len] == 0 && memcmp(ctxt, name, len) == 0)
return object::static_object(id(idx));
idx++;
}
}
return nullptr;
}
object_p constant::lookup_menu(config_r cfg, cstring name)
// ----------------------------------------------------------------------------
// Find the menu in the current configuratoin
// ----------------------------------------------------------------------------
{
return lookup_menu(cfg, utf8(name), strlen(name));
}

View file

@ -140,7 +140,9 @@ struct constant : algebraic
return len == nlen && memcmp(ref, txt, len) == 0;
}
static result lookup_command(config_r cfg, bool numerical);
static result lookup_command(config_r cfg, bool numerical);
static object_p lookup_menu(config_r cfg, utf8 name, size_t len);
static object_p lookup_menu(config_r cfg, cstring name);
protected:
static result do_parsing(config_r cfg, parser &p);
@ -195,5 +197,6 @@ public:
COMMAND_DECLARE_INSERT_HELP(ConstantName,-1);
COMMAND_DECLARE_INSERT_HELP(ConstantValue,-1);
COMMAND_DECLARE(Const, 1);
COMMAND_DECLARE(Constants, 0);
#endif // CONSTANT_H

View file

@ -704,6 +704,7 @@ CMD(EquationSolver)
NAMED(XLib, "LibraryItem")
CMD(XlibName)
CMD(XlibValue)
CMD(Constants)
// Last things and stack
NAMED(LastArg, "LastArguments")
@ -1208,9 +1209,6 @@ CONSTANT_MENUS(ConstantsMenu7)
CONSTANT_MENUS(ConstantsMenu8)
CONSTANT_MENUS(ConstantsMenu9)
ALIAS(ConstantsMenu01, "Constants")
#define EQUATION_MENUS(Base) \
EQUATION_MENU(Base##0) \
EQUATION_MENU(Base##1) \