Improved code

This commit is contained in:
Marco Cetica 2024-03-13 09:08:08 +01:00
parent de558064c8
commit dbecebce07
Signed by: marco
GPG Key ID: 45060A949E90D0FD
7 changed files with 54 additions and 50 deletions

View File

@ -10,9 +10,11 @@ jobs:
steps:
- name: Checkout repo
uses: actions/checkout@main
- name: Build dc
- name: Install dependencies
run: |
apt -y update && apt -y install cmake ninja-build build-essential
- name: Build DC
run: |
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release .
cmake --build build
- name: Run unit tests

View File

@ -10,9 +10,11 @@ jobs:
steps:
- name: Checkout repo
uses: actions/checkout@main
- name: Build dc
- name: Install dependencies
run: |
sudo apt -y update && sudo apt -y install cmake ninja-build build-essential
- name: Build DC
run: |
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release .
cmake --build build
- name: Run unit tests

View File

@ -1,4 +1,4 @@
# dc ![](https://github.com/ceticamarco/dc/actions/workflows/dc.yml/badge.svg)
# dc ![](https://github.com/ceticamarco/dc/actions/workflows/dc.yml/badge.svg) [![CodeFactor](https://www.codefactor.io/repository/github/ceticamarco/dc/badge/master)](https://www.codefactor.io/repository/github/ceticamarco/dc/overview/master)
**dc** is an advanced, scientific and programmable RPN desktop calculator with macro support (re)written in C++.
By default, dc supports a wide range of arithmetical, trigonometrical and numeric functions.

View File

@ -34,7 +34,7 @@ std::optional<std::string> Math::exec(dc::Stack<std::string> &stack, dc::Paramet
return err;
}
std::optional<std::string> Math::fn_add(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_add(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'+' requires two operands";
@ -62,7 +62,7 @@ std::optional<std::string> Math::fn_add(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_sub(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_sub(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'-' requires two operands";
@ -100,7 +100,7 @@ std::optional<std::string> Math::fn_sub(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_mul(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_mul(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'*' requires two operands";
@ -128,7 +128,7 @@ std::optional<std::string> Math::fn_mul(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_div(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_div(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'/' requires two operands";
@ -161,7 +161,7 @@ std::optional<std::string> Math::fn_div(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_mod(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_mod(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'%' requires two operands";
@ -194,7 +194,7 @@ std::optional<std::string> Math::fn_mod(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_div_mod(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_div_mod(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'~' requires two operands";
@ -229,7 +229,7 @@ std::optional<std::string> Math::fn_div_mod(dc::Stack<std::string> &stack, dc::P
return std::nullopt;
}
std::optional<std::string> Math::fn_mod_exp(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_mod_exp(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.size() < 3) {
return "'|' requires three operands";
@ -278,7 +278,7 @@ std::optional<std::string> Math::fn_mod_exp(dc::Stack<std::string> &stack, dc::P
return std::nullopt;
}
std::optional<std::string> Math::fn_exp(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_exp(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'^' requires two operands";
@ -306,7 +306,7 @@ std::optional<std::string> Math::fn_exp(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_sqrt(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_sqrt(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.empty()) {
return "'v' requires one operand";
@ -335,7 +335,7 @@ std::optional<std::string> Math::fn_sqrt(dc::Stack<std::string> &stack, dc::Para
return std::nullopt;
}
std::optional<std::string> Math::fn_sin(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_sin(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.empty()) {
return "'sin' requires one operand";
@ -360,7 +360,7 @@ std::optional<std::string> Math::fn_sin(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_cos(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_cos(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.empty()) {
return "'cos' requires one operand";
@ -385,7 +385,7 @@ std::optional<std::string> Math::fn_cos(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_tan(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_tan(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.empty()) {
return "'tan' requires one operand";
@ -410,7 +410,7 @@ std::optional<std::string> Math::fn_tan(dc::Stack<std::string> &stack, dc::Param
return std::nullopt;
}
std::optional<std::string> Math::fn_asin(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_asin(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.empty()) {
return "'asin' requires one operand";
@ -435,7 +435,7 @@ std::optional<std::string> Math::fn_asin(dc::Stack<std::string> &stack, dc::Para
return std::nullopt;
}
std::optional<std::string> Math::fn_acos(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_acos(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.empty()) {
return "'acos' requires one operand";
@ -460,7 +460,7 @@ std::optional<std::string> Math::fn_acos(dc::Stack<std::string> &stack, dc::Para
return std::nullopt;
}
std::optional<std::string> Math::fn_atan(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_atan(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.empty()) {
return "'atan' requires one operand";
@ -485,7 +485,7 @@ std::optional<std::string> Math::fn_atan(dc::Stack<std::string> &stack, dc::Para
return std::nullopt;
}
std::optional<std::string> Math::fn_fact(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_fact(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
// Check if stack has enough elements
if(stack.empty()) {
return "'!' requires one operand";
@ -519,13 +519,13 @@ std::optional<std::string> Math::fn_fact(dc::Stack<std::string> &stack, dc::Para
return std::nullopt;
}
std::optional<std::string> Math::fn_pi(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_pi(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
stack.push(trim_digits(std::numbers::pi, parameters.precision));
return std::nullopt;
}
std::optional<std::string> Math::fn_e(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Math::fn_e(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
stack.push(trim_digits(std::numbers::e, parameters.precision));
return std::nullopt;

View File

@ -8,24 +8,24 @@ public:
std::optional<std::string> exec(dc::Stack<std::string> &stack, dc::Parameters &parameters, std::unordered_map<char, dc::Register> &regs) override;
private:
static std::optional<std::string> fn_add(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_sub(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_mul(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_div(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_mod(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_div_mod(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_mod_exp(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_exp(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_sqrt(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_sin(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_cos(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_tan(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_asin(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_acos(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_atan(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_fact(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_pi(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_e(dc::Stack<std::string> &stack, dc::Parameters &parameters) ;
static std::optional<std::string> fn_add(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_sub(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_mul(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_div(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_mod(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_div_mod(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_mod_exp(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_exp(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_sqrt(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_sin(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_cos(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_tan(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_asin(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_acos(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_atan(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_fact(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_pi(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_e(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::string trim_digits(double number, unsigned int precision);
OPType op_type;

View File

@ -46,7 +46,7 @@ std::optional<std::string> Stack::exec(dc::Stack<std::string> &stack, dc::Parame
return err;
}
std::optional<std::string> Stack::fn_print(dc::Stack<std::string> &stack, dc::Parameters &parameters, bool new_line) {
std::optional<std::string> Stack::fn_print(dc::Stack<std::string> &stack, dc::Parameters &parameters, bool new_line) {
// Check if the stack is empty
if(stack.empty()) {
return "Cannot print empty stack";
@ -129,30 +129,30 @@ std::optional<std::string> Stack::fn_dup_head(dc::Stack<std::string> &stack) {
return std::nullopt;
}
std::optional<std::string> Stack::fn_print_stack(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Stack::fn_print_stack(const dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
const auto& const_ref = stack.get_ref();
switch(parameters.oradix) {
case dc::radix_base::DEC: {
for(auto & it : std::ranges::reverse_view(const_ref)) {
for(const auto& it : std::ranges::reverse_view(const_ref)) {
std::cout << it << std::endl;
}
break;
}
case dc::radix_base::BIN: {
for(auto & it : std::ranges::reverse_view(const_ref)) {
for(const auto& it : std::ranges::reverse_view(const_ref)) {
std::cout << to_bin(std::stol(it)) << 'b' << std::endl;
}
break;
}
case dc::radix_base::OCT: {
for(auto & it : std::ranges::reverse_view(const_ref)) {
for(const auto& it : std::ranges::reverse_view(const_ref)) {
std::cout << std::oct << std::stol(it) << 'o' << std::dec << std::endl;
}
break;
}
case dc::radix_base::HEX: {
for(auto & it : std::ranges::reverse_view(const_ref)) {
for(const auto& it : std::ranges::reverse_view(const_ref)) {
std::cout << std::hex << std::uppercase << std::stol(it) << 'h'
<< std::dec << std::nouppercase << std::endl;
}
@ -224,7 +224,7 @@ std::optional<std::string> Stack::fn_set_precision(dc::Stack<std::string> &stack
return std::nullopt;
}
std::optional<std::string> Stack::fn_get_precision(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Stack::fn_get_precision(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
stack.push(std::to_string(parameters.precision));
return std::nullopt;
@ -283,7 +283,7 @@ std::optional<std::string> Stack::fn_set_iradix(dc::Stack<std::string> &stack, d
return std::nullopt;
}
std::optional<std::string> Stack::fn_get_iradix(dc::Stack<std::string> &stack, dc::Parameters &parameters) {
std::optional<std::string> Stack::fn_get_iradix(dc::Stack<std::string> &stack, const dc::Parameters &parameters) {
stack.push(std::to_string(parameters.iradix));
return std::nullopt;

View File

@ -12,15 +12,15 @@ private:
static std::optional<std::string> fn_pop_head(dc::Stack<std::string> &stack);
static std::optional<std::string> fn_swap_xy(dc::Stack<std::string> &stack);
static std::optional<std::string> fn_dup_head(dc::Stack<std::string> &stack);
std::optional<std::string> fn_print_stack(dc::Stack<std::string> &stack, dc::Parameters &parameters);
std::optional<std::string> fn_print_stack(const dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_head_size(dc::Stack<std::string> &stack);
static std::optional<std::string> fn_stack_size(dc::Stack<std::string> &stack);
static std::optional<std::string> fn_set_precision(dc::Stack<std::string> &stack, dc::Parameters &parameters);
static std::optional<std::string> fn_get_precision(dc::Stack<std::string> &stack, dc::Parameters &parameters);
static std::optional<std::string> fn_get_precision(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
static std::optional<std::string> fn_set_oradix(dc::Stack<std::string> &stack, dc::Parameters &parameters);
static std::optional<std::string> fn_get_oradix(dc::Stack<std::string> &stack, dc::Parameters &parameters);
static std::optional<std::string> fn_set_iradix(dc::Stack<std::string> &stack, dc::Parameters &parameters);
static std::optional<std::string> fn_get_iradix(dc::Stack<std::string> &stack, dc::Parameters &parameters);
static std::optional<std::string> fn_get_iradix(dc::Stack<std::string> &stack, const dc::Parameters &parameters);
std::optional<std::string> fn_get_lastx(dc::Stack<std::string> &stack);
std::optional<std::string> fn_get_lasty(dc::Stack<std::string> &stack);
std::optional<std::string> fn_get_lastz(dc::Stack<std::string> &stack);