mirror of
https://github.com/louisrubet/rpn
synced 2025-02-01 07:57:52 +01:00
some c++ better practices
This commit is contained in:
parent
328bdf3c58
commit
06a103fa8f
7 changed files with 28 additions and 44 deletions
28
src/main.cpp
28
src/main.cpp
|
@ -1,10 +1,6 @@
|
|||
#include <linux/limits.h>
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <iostream>
|
||||
|
@ -14,7 +10,7 @@ using namespace std;
|
|||
#include "program.hpp"
|
||||
|
||||
static heap _global_heap;
|
||||
static stack _global_stack;
|
||||
static rpnstack _global_stack;
|
||||
static program* _prog_to_interrupt = NULL;
|
||||
|
||||
/// @brief actions to be done at rpn exit
|
||||
|
@ -30,9 +26,9 @@ static void exit_interactive_rpn() {
|
|||
history.close();
|
||||
|
||||
// save it
|
||||
if (linenoiseHistorySave(history_path.str()) != 0)
|
||||
cerr << "warning, could not save " << history_path.str() << " (errno=" << errno << " '"
|
||||
<< strerror(errno) "')" << endl;
|
||||
if (linenoiseHistorySave(history_path.str().c_str()) != 0)
|
||||
cerr << "warning, could not save " << history_path.str() << " [errno=" << errno << ' ' << strerror(errno)
|
||||
<< "']" << endl;
|
||||
linenoiseHistoryFree();
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +43,7 @@ static void init_interactive_rpn() {
|
|||
|
||||
// don't care about errors
|
||||
linenoiseHistorySetMaxLen(HISTORY_FILE_MAX_LINES);
|
||||
linenoiseHistoryLoad(history_path.str());
|
||||
linenoiseHistoryLoad(history_path.str().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,12 +86,12 @@ static void catch_signals(program* prog) {
|
|||
act.sa_sigaction = &ctrlc_handler;
|
||||
act.sa_flags = SA_SIGINFO;
|
||||
if (sigaction(SIGINT, &act, NULL) < 0)
|
||||
(void)fprintf(stderr, "Warning, Ctrl-C cannot be catched [errno=%d '%s']", errno, strerror(errno));
|
||||
cerr << "Warning, Ctrl-C cannot be caught [errno=" << errno << ' ' << strerror(errno) << "']" << endl;
|
||||
|
||||
act.sa_sigaction = &segv_handler;
|
||||
act.sa_flags = SA_SIGINFO;
|
||||
if (sigaction(SIGSEGV, &act, NULL) < 0)
|
||||
(void)fprintf(stderr, "Warning, SIGSEGV cannot be catched [errno=%d '%s']", errno, strerror(errno));
|
||||
cerr << "Warning, SIGSEGV cannot be caught [errno=" << errno << ' ' << strerror(errno) << "']" << endl;
|
||||
}
|
||||
|
||||
/// @brief rpn entry point
|
||||
|
@ -119,7 +115,7 @@ int main(int argc, char* argv[]) {
|
|||
// entry loop
|
||||
while (go_on) {
|
||||
// make program from interactive entry
|
||||
program prog;
|
||||
program prog(&_global_stack, &_global_heap);
|
||||
switch (program::entry(prog)) {
|
||||
case ret_good_bye:
|
||||
go_on = false;
|
||||
|
@ -131,7 +127,7 @@ int main(int argc, char* argv[]) {
|
|||
catch_signals(&prog);
|
||||
|
||||
// run it
|
||||
if (prog.run(_global_stack, _global_heap) == ret_good_bye)
|
||||
if (prog.run() == ret_good_bye)
|
||||
go_on = false;
|
||||
else
|
||||
program::show_stack(_global_stack);
|
||||
|
@ -144,7 +140,7 @@ int main(int argc, char* argv[]) {
|
|||
}
|
||||
// run with cmd line arguments
|
||||
else {
|
||||
program prog;
|
||||
program prog(&_global_stack, &_global_heap);
|
||||
string entry;
|
||||
int i;
|
||||
|
||||
|
@ -157,13 +153,11 @@ int main(int argc, char* argv[]) {
|
|||
// make program
|
||||
ret = program::parse(entry, prog);
|
||||
if (ret == ret_ok) {
|
||||
string separator = "";
|
||||
|
||||
// user could stop prog with CtrlC
|
||||
catch_signals(&prog);
|
||||
|
||||
// run it
|
||||
ret = prog.run(_global_stack, _global_heap);
|
||||
ret = prog.run();
|
||||
program::show_stack(_global_stack, false);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -209,21 +209,13 @@ program::keyword_t program::s_keywords[] = {
|
|||
|
||||
/// @brief run a program on a stack and a heap
|
||||
///
|
||||
/// @param stk the stack, storing prog result
|
||||
/// @param hp the heap, storing variables
|
||||
/// @return ret_value see this type
|
||||
///
|
||||
ret_value program::run(rpnstack& stk, heap& hp) {
|
||||
ret_value program::run() {
|
||||
bool go_out = false;
|
||||
ret_value ret = ret_ok;
|
||||
cmd_type_t type;
|
||||
|
||||
// stack comes from outside
|
||||
_stack = &stk;
|
||||
|
||||
// global heap comes from outside
|
||||
_heap = &hp;
|
||||
|
||||
_err = ret_ok;
|
||||
_err_context = "";
|
||||
|
||||
|
@ -299,7 +291,7 @@ ret_value program::run(rpnstack& stk, heap& hp) {
|
|||
default:
|
||||
// not a command, but a stack entry, manage it
|
||||
// copy the program stack entry to the running stack
|
||||
stk.push_front(o->clone());
|
||||
_stack->push_front(o->clone());
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -44,8 +44,7 @@ struct if_layout_t {
|
|||
//< program class: the class containing a string parser, all the programs keywords, a stack for running the program
|
||||
class program : public deque<object*> {
|
||||
public:
|
||||
program(program* parent_prog = NULL) {
|
||||
_parent_prog = parent_prog;
|
||||
program(rpnstack* stk, heap* hp, program* parent = nullptr):_stack(stk),_heap(hp),_parent(parent) {
|
||||
interrupt_now = false;
|
||||
}
|
||||
virtual ~program() {
|
||||
|
@ -60,9 +59,8 @@ class program : public deque<object*> {
|
|||
static ret_value get_fn(const char* fn_name, program_fn_t& fn, cmd_type_t& type);
|
||||
|
||||
// running
|
||||
ret_value run(rpnstack& stk, heap& hp);
|
||||
ret_value run();
|
||||
void stop();
|
||||
// bool compare_keyword(keyword* k, const char* str_to_compare, int len);
|
||||
bool compare_branch(branch* b, const char* str_to_compare, int len);
|
||||
ret_value preprocess(void);
|
||||
|
||||
|
@ -93,7 +91,7 @@ class program : public deque<object*> {
|
|||
heap _local_heap;
|
||||
|
||||
// parent prog for inheriting heaps
|
||||
program* _parent_prog;
|
||||
program* _parent;
|
||||
|
||||
int stack_size() { return _stack->size(); }
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
///
|
||||
bool program::find_variable(string& variable, object*& obj) {
|
||||
bool found = false;
|
||||
program* parent = _parent_prog;
|
||||
program* parent = _parent;
|
||||
|
||||
if (_local_heap.get(variable, obj))
|
||||
found = true;
|
||||
|
@ -19,7 +19,7 @@ bool program::find_variable(string& variable, object*& obj) {
|
|||
found = true;
|
||||
break;
|
||||
}
|
||||
parent = parent->_parent_prog;
|
||||
parent = parent->_parent;
|
||||
}
|
||||
if (!found) {
|
||||
if (_heap->get(variable, obj)) found = true;
|
||||
|
@ -64,12 +64,12 @@ void program::rpn_eval(void) {
|
|||
|
||||
// run prog if any
|
||||
if (run_prog) {
|
||||
program prog(this);
|
||||
program prog(_stack, _heap, this);
|
||||
|
||||
// make program from entry
|
||||
if (program::parse(prog_text, prog) == ret_ok) {
|
||||
// run it
|
||||
prog.run(*_stack, *_heap);
|
||||
prog.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -135,12 +135,12 @@ int program::rpn_inprog(branch& myobj) {
|
|||
|
||||
// run the program
|
||||
string entry(((oprogram*)(*this)[myobj.arg1 + count_symbols + 1])->value);
|
||||
program prog(this);
|
||||
program prog(_stack, _heap, this);
|
||||
|
||||
// make the program from entry
|
||||
if (program::parse(entry, prog) == ret_ok) {
|
||||
// run it
|
||||
prog.run(*_stack, *_heap);
|
||||
prog.run();
|
||||
}
|
||||
|
||||
// point on next command
|
||||
|
|
|
@ -187,7 +187,7 @@ void program::rpn_purge(void) {
|
|||
///
|
||||
void program::rpn_vars(void) {
|
||||
object* obj;
|
||||
program* parent = _parent_prog;
|
||||
program* parent = _parent;
|
||||
string name;
|
||||
|
||||
// heap variables
|
||||
|
@ -206,7 +206,7 @@ void program::rpn_vars(void) {
|
|||
obj->show(cout);
|
||||
cout<<endl;
|
||||
}
|
||||
parent = parent->_parent_prog;
|
||||
parent = parent->_parent;
|
||||
}
|
||||
|
||||
// local variables
|
||||
|
|
|
@ -23,13 +23,13 @@ void program::rpn_strout() {
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_string);
|
||||
|
||||
string entry(_stack->value<ostring>(0));
|
||||
program prog;
|
||||
program prog(_stack, _heap);
|
||||
_stack->pop();
|
||||
|
||||
// make program from string in stack level 1
|
||||
if (program::parse(entry, prog) == ret_ok)
|
||||
// run it
|
||||
prog.run(*_stack, *_heap);
|
||||
prog.run();
|
||||
}
|
||||
|
||||
/// @brief chr keyword implementation
|
||||
|
|
|
@ -227,11 +227,11 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail
|
|||
// parse entry and run line
|
||||
entry = regex_replace(entry, regex("`"), "");
|
||||
if (!entry.empty()) {
|
||||
program prog;
|
||||
program prog(&stk, &hp);
|
||||
ret = program::parse(entry, prog);
|
||||
if (ret == ret_ok) {
|
||||
// run it
|
||||
(void)prog.run(stk, hp);
|
||||
(void)prog.run();
|
||||
last_err = (int)prog.get_err();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue