mirror of
https://github.com/louisrubet/rpn
synced 2025-01-19 10:26:22 +01:00
#46: added command default
This commit is contained in:
parent
e375323434
commit
d4a47c32ff
3 changed files with 36 additions and 26 deletions
|
@ -17,6 +17,7 @@ program::keyword_t program::_keywords[] =
|
||||||
{ cmd_keyword, "version", &program::rpn_version, "show rpn version" },
|
{ cmd_keyword, "version", &program::rpn_version, "show rpn version" },
|
||||||
{ cmd_keyword, "uname", &program::rpn_uname, "show rpn complete identification string" },
|
{ cmd_keyword, "uname", &program::rpn_uname, "show rpn complete identification string" },
|
||||||
{ cmd_keyword, "type", &program::type, "show first stack entry type" },
|
{ cmd_keyword, "type", &program::type, "show first stack entry type" },
|
||||||
|
{ cmd_keyword, "default", &program::rpn_default, "setting precision, float representation, float precision, binary mode and verbosity to default" },
|
||||||
|
|
||||||
//REAL
|
//REAL
|
||||||
{ cmd_undef, "", NULL, "\nREAL"},
|
{ cmd_undef, "", NULL, "\nREAL"},
|
||||||
|
|
|
@ -28,8 +28,7 @@ void help()
|
||||||
cout<<endl;
|
cout<<endl;
|
||||||
|
|
||||||
// description
|
// description
|
||||||
cout<<description<<mpfr_get_version()<<description_more;
|
cout<<description<<endl<<endl;
|
||||||
cout<<endl<<endl;
|
|
||||||
|
|
||||||
// syntax
|
// syntax
|
||||||
cout<<syntax<<endl;
|
cout<<syntax<<endl;
|
||||||
|
@ -186,3 +185,8 @@ void type()
|
||||||
symbol* sym = (symbol*)allocate_back(size, cmd_symbol);
|
symbol* sym = (symbol*)allocate_back(size, cmd_symbol);
|
||||||
sym->set(cmd_type_string[type], string_size);
|
sym->set(cmd_type_string[type], string_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void rpn_default()
|
||||||
|
{
|
||||||
|
program::apply_default();
|
||||||
|
}
|
||||||
|
|
53
src/rpn.cpp
53
src/rpn.cpp
|
@ -68,6 +68,7 @@ typedef enum {
|
||||||
ret_unknown_err,
|
ret_unknown_err,
|
||||||
ret_missing_operand,
|
ret_missing_operand,
|
||||||
ret_bad_operand_type,
|
ret_bad_operand_type,
|
||||||
|
ret_out_of_range,
|
||||||
ret_unknown_variable,
|
ret_unknown_variable,
|
||||||
ret_internal,
|
ret_internal,
|
||||||
ret_deadly,
|
ret_deadly,
|
||||||
|
@ -80,7 +81,7 @@ typedef enum {
|
||||||
} ret_value;
|
} ret_value;
|
||||||
|
|
||||||
const char* ret_value_string[ret_max] = {
|
const char* ret_value_string[ret_max] = {
|
||||||
"ok", "unknown command", "missing operand", "bad operand type", "unknown variable", "internal error, aborting",
|
"ok", "unknown command", "missing operand", "bad operand type", "out of range", "unknown variable", "internal error, aborting",
|
||||||
"deadly", "goodbye", "not implemented", "no operation", "syntax", "division by zero"
|
"deadly", "goodbye", "not implemented", "no operation", "syntax", "division by zero"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -152,7 +153,7 @@ struct floating_t
|
||||||
{
|
{
|
||||||
return mpfr_cmp(&mpfr, &right.mpfr) < 0;
|
return mpfr_cmp(&mpfr, &right.mpfr) < 0;
|
||||||
}
|
}
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
class program;
|
class program;
|
||||||
class object;
|
class object;
|
||||||
|
@ -162,7 +163,7 @@ typedef void (program::*program_fn_t)(void);
|
||||||
typedef union
|
typedef union
|
||||||
{
|
{
|
||||||
program_fn_t _fn;
|
program_fn_t _fn;
|
||||||
} __attribute__((packed)) operand;
|
} operand;
|
||||||
typedef int (program::*branch_fn_t)(branch&);
|
typedef int (program::*branch_fn_t)(branch&);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -173,7 +174,7 @@ struct object
|
||||||
|
|
||||||
//
|
//
|
||||||
void show(ostream& stream = cout);
|
void show(ostream& stream = cout);
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
struct number : public object
|
struct number : public object
|
||||||
{
|
{
|
||||||
|
@ -243,7 +244,7 @@ struct number : public object
|
||||||
// precision
|
// precision
|
||||||
static int s_default_precision;
|
static int s_default_precision;
|
||||||
static int s_current_precision;
|
static int s_current_precision;
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
number::mode_enum number::s_default_mode = number::std;
|
number::mode_enum number::s_default_mode = number::std;
|
||||||
number::mode_enum number::s_mode = number::s_default_mode;
|
number::mode_enum number::s_mode = number::s_default_mode;
|
||||||
|
@ -271,7 +272,7 @@ struct binary : public object
|
||||||
} binary_enum;
|
} binary_enum;
|
||||||
static binary_enum s_default_mode;
|
static binary_enum s_default_mode;
|
||||||
static binary_enum s_mode;
|
static binary_enum s_mode;
|
||||||
} __attribute__((packed));
|
};
|
||||||
binary::binary_enum binary::s_default_mode = binary::dec;
|
binary::binary_enum binary::s_default_mode = binary::dec;
|
||||||
binary::binary_enum binary::s_mode = binary::s_default_mode;
|
binary::binary_enum binary::s_mode = binary::s_default_mode;
|
||||||
|
|
||||||
|
@ -296,7 +297,7 @@ struct ostring : public object
|
||||||
//
|
//
|
||||||
unsigned int _len;
|
unsigned int _len;
|
||||||
char _value[0];
|
char _value[0];
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
struct oprogram : public object
|
struct oprogram : public object
|
||||||
{
|
{
|
||||||
|
@ -319,7 +320,7 @@ struct oprogram : public object
|
||||||
//
|
//
|
||||||
unsigned int _len;
|
unsigned int _len;
|
||||||
char _value[0];
|
char _value[0];
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
struct symbol : public object
|
struct symbol : public object
|
||||||
{
|
{
|
||||||
|
@ -344,7 +345,7 @@ struct symbol : public object
|
||||||
bool _auto_eval;
|
bool _auto_eval;
|
||||||
unsigned int _len;
|
unsigned int _len;
|
||||||
char _value[0];
|
char _value[0];
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
struct keyword : public object
|
struct keyword : public object
|
||||||
{
|
{
|
||||||
|
@ -369,7 +370,7 @@ struct keyword : public object
|
||||||
program_fn_t _fn;
|
program_fn_t _fn;
|
||||||
unsigned int _len;
|
unsigned int _len;
|
||||||
char _value[0];
|
char _value[0];
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
struct branch : public object
|
struct branch : public object
|
||||||
{
|
{
|
||||||
|
@ -404,7 +405,7 @@ struct branch : public object
|
||||||
bool arg_bool;
|
bool arg_bool;
|
||||||
unsigned int _len;
|
unsigned int _len;
|
||||||
char _value[0];
|
char _value[0];
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
void object::show(ostream& stream)
|
void object::show(ostream& stream)
|
||||||
{
|
{
|
||||||
|
@ -812,6 +813,21 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void apply_default()
|
||||||
|
{
|
||||||
|
//default float precision, float mode, binary mode, verbosity
|
||||||
|
number::s_mode = number::s_default_mode;
|
||||||
|
number::s_current_precision = number::s_default_precision;
|
||||||
|
binary::s_mode = binary::s_default_mode;
|
||||||
|
|
||||||
|
// format for mpfr_printf
|
||||||
|
stringstream ss;
|
||||||
|
ss << number::s_current_precision;
|
||||||
|
s_mpfr_printf_format = s_mpfr_printf_format_beg + ss.str() + s_mpfr_printf_format_std;
|
||||||
|
|
||||||
|
g_verbose = 0;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ret_value _err;
|
ret_value _err;
|
||||||
string _err_context;
|
string _err_context;
|
||||||
|
@ -883,23 +899,12 @@ private:
|
||||||
#include "rpn-program.h"
|
#include "rpn-program.h"
|
||||||
#include "rpn-trig.h"
|
#include "rpn-trig.h"
|
||||||
#include "rpn-logs.h"
|
#include "rpn-logs.h"
|
||||||
} __attribute__((packed));
|
};
|
||||||
|
|
||||||
//keywords declaration
|
//keywords declaration
|
||||||
#include "rpn-cmd.h"
|
#include "rpn-cmd.h"
|
||||||
|
|
||||||
#include "rpn-test-core.h"
|
#include "rpn-test-core.h"
|
||||||
|
|
||||||
//
|
|
||||||
static void apply_default(void)
|
|
||||||
{
|
|
||||||
//default precision
|
|
||||||
number::s_mode = number::s_default_mode;
|
|
||||||
|
|
||||||
//default binary mode
|
|
||||||
binary::s_mode = binary::s_default_mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
@ -908,7 +913,7 @@ int main(int argc, char* argv[])
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
// apply default configuration
|
// apply default configuration
|
||||||
apply_default();
|
program::apply_default();
|
||||||
|
|
||||||
// run with interactive prompt
|
// run with interactive prompt
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
|
|
Loading…
Reference in a new issue