Minor code improvements
All checks were successful
dc / build (push) Successful in 42s

This commit is contained in:
Marco Cetica 2024-03-07 16:16:17 +01:00
parent 531454ca17
commit 207c387001
Signed by: marco
GPG Key ID: 45060A949E90D0FD
9 changed files with 80 additions and 81 deletions

View File

@ -22,9 +22,9 @@ void helper() {
int main(int argc, char **argv) {
int opt;
const char *short_opts = "e:f:hV";
std::string cli_expression = "";
std::string file_name = "";
std::string stdin_expression = "";
std::string cli_expression;
std::string file_name;
std::string stdin_expression;
bool execute_expression = false;
bool execute_file = false;
dc_stack_t stack;

View File

@ -21,7 +21,7 @@
std::optional<std::string> Evaluate::eval() {
for(size_t idx = 0; idx < this->expr.size(); idx++) {
auto val = this->expr.at(idx);
std::optional<std::string> err = std::nullopt;
std::optional<std::string> err;
//
// NUMERICAL OPERATIONS
@ -287,7 +287,7 @@ std::optional<std::string> Evaluate::handle_special(std::string val, size_t &idx
return err;
}
std::optional<std::string> Evaluate::parse_base_n(std::string val) {
std::optional<std::string> Evaluate::parse_base_n(const std::string& val) {
// Discard values that are neither integers neither within "ABCDEF"
if(!is_num<long>(val) && !X_CONTAINS_Y("ABCDEF", val)) {
return "This input base supports integers only";
@ -307,7 +307,7 @@ std::optional<std::string> Evaluate::parse_base_n(std::string val) {
std::optional<std::string> Evaluate::parse_macro(size_t &idx) {
// A macro is any string surrounded by square brackets
std::string dc_macro = "";
std::string dc_macro;
bool closing_bracket = false;
// Scan next token
@ -356,7 +356,7 @@ std::optional<std::string> Evaluate::parse_macro_command(std::string val) {
// If command has length equal to three, then it's either '<=', '>=' or '!='
// Check if command is >=, <= or !=
std::string operation = "";
std::string operation;
char dc_register = 0;
if(val.length() == 3) {
operation = val.substr(0, 2);
@ -492,10 +492,10 @@ std::optional<std::string> Evaluate::parse_register_command(std::string val) {
// If the register is empty, push '0' to the stack
auto reg_name = val.at(1);
// If register does not exists or its stack is empty, push '0' onto the main stack
// If register does not exist or its stack is empty, push '0' onto the main stack
auto it = this->regs.find(reg_name);
if(it == this->regs.end() || it->second.stack.empty()) {
this->stack.push_back("0");
this->stack.emplace_back("0");
return std::nullopt;
}

View File

@ -8,9 +8,9 @@
class Evaluate {
public:
Evaluate(const std::vector<std::string> e, std::unordered_map<char, Register> &r,
Evaluate(const std::vector<std::string>& e, std::unordered_map<char, Register> &r,
dc_stack_t &s, Parameters &p)
: expr(std::move(e)), regs(r), stack(s), parameters(p) {}
: expr(e), regs(r), stack(s), parameters(p) {}
Evaluate(std::unordered_map<char, Register> &r, dc_stack_t &s, Parameters &p)
: regs(r), stack(s), parameters(p) {}
std::optional<std::string> eval();
@ -21,7 +21,7 @@ private:
std::optional<std::string> parse_macro_command(std::string val);
std::optional<std::string> parse_register_command(std::string val);
std::optional<std::string> parse_array_command(std::string val);
std::optional<std::string> parse_base_n(std::string val);
std::optional<std::string> parse_base_n(const std::string& val);
std::optional<std::string> fn_set_precision();
std::optional<std::string> fn_get_precision();
std::optional<std::string> fn_set_oradix();

View File

@ -147,7 +147,7 @@ std::optional<std::string> Macro::fn_evaluate_macro(dc_stack_t &stack) {
std::optional<std::string> Macro::fn_read_input(dc_stack_t &stack) {
// Read user input from stdin
std::string user_input = "";
std::string user_input;
std::cin >> user_input;
if(std::cin.fail()) {
@ -166,7 +166,7 @@ std::optional<std::string> Macro::fn_read_input(dc_stack_t &stack) {
return std::nullopt;
}
std::vector<std::string> Macro::split(std::string str) {
std::vector<std::string> Macro::split(const std::string& str) {
std::stringstream ss(str);
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;

View File

@ -8,14 +8,12 @@ enum class Operator {
class Macro : public IOperation {
public:
Macro(const OPType op_t, const Operator o,
const char dc_r, std::unordered_map<char, Register> &r, Parameters &p)
: op_type(std::move(op_t)), op(std::move(o)),
dc_register(std::move(dc_r)), regs(r), parameters(p) {}
Macro(const OPType op_t, const Operator o, const char dc_r, std::unordered_map<char, Register> &r, Parameters &p)
: op_type(op_t), op(o), dc_register(dc_r), regs(r), parameters(p) {}
Macro(const OPType op_t, std::unordered_map<char, Register> &r, Parameters &p)
: op_type(std::move(op_t)), regs(r), parameters(p) {}
: op_type(op_t), regs(r), parameters(p) {}
std::optional<std::string> exec(dc_stack_t &stack) override;
static std::vector<std::string> split(std::string str);
static std::vector<std::string> split(const std::string& str);
private:
std::optional<std::string> fn_execute(dc_stack_t &stack);
@ -23,8 +21,8 @@ private:
std::optional<std::string> fn_read_input(dc_stack_t &stack);
OPType op_type;
Operator op;
char dc_register;
Operator op{};
char dc_register{};
std::unordered_map<char, Register> &regs;
Parameters &parameters;
};

View File

@ -33,7 +33,7 @@ std::optional<std::string> Math::exec(dc_stack_t &stack) {
return err;
}
std::optional<std::string> Math::fn_add(dc_stack_t &stack) {
std::optional<std::string> Math::fn_add(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'+' requires two operands";
@ -63,7 +63,7 @@ std::optional<std::string> Math::fn_add(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_sub(dc_stack_t &stack) {
std::optional<std::string> Math::fn_sub(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'-' requires two operands";
@ -103,7 +103,7 @@ std::optional<std::string> Math::fn_sub(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_mul(dc_stack_t &stack) {
std::optional<std::string> Math::fn_mul(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'*' requires two operands";
@ -133,7 +133,7 @@ std::optional<std::string> Math::fn_mul(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_div(dc_stack_t &stack) {
std::optional<std::string> Math::fn_div(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'/' requires two operands";
@ -168,7 +168,7 @@ std::optional<std::string> Math::fn_div(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_mod(dc_stack_t &stack) {
std::optional<std::string> Math::fn_mod(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'%' requires two operands";
@ -203,7 +203,7 @@ std::optional<std::string> Math::fn_mod(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_div_mod(dc_stack_t &stack) {
std::optional<std::string> Math::fn_div_mod(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'~' requires two operands";
@ -240,7 +240,7 @@ std::optional<std::string> Math::fn_div_mod(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_mod_exp(dc_stack_t &stack) {
std::optional<std::string> Math::fn_mod_exp(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.size() < 3) {
return "'|' requires three operands";
@ -270,7 +270,7 @@ std::optional<std::string> Math::fn_mod_exp(dc_stack_t &stack) {
stack.pop_back();
if(modulus == 1) {
stack.push_back("0");
stack.emplace_back("0");
return std::nullopt;
} else if(modulus == 0) {
return "Modulus cannot be zero";
@ -293,7 +293,7 @@ std::optional<std::string> Math::fn_mod_exp(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_exp(dc_stack_t &stack) {
std::optional<std::string> Math::fn_exp(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.size() < 2) {
return "'^' requires two operands";
@ -323,7 +323,7 @@ std::optional<std::string> Math::fn_exp(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_sqrt(dc_stack_t &stack) {
std::optional<std::string> Math::fn_sqrt(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.empty()) {
return "'v' requires one operand";
@ -352,7 +352,7 @@ std::optional<std::string> Math::fn_sqrt(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_sin(dc_stack_t &stack) {
std::optional<std::string> Math::fn_sin(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.empty()) {
return "'sin' requires one operand";
@ -377,7 +377,7 @@ std::optional<std::string> Math::fn_sin(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_cos(dc_stack_t &stack) {
std::optional<std::string> Math::fn_cos(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.empty()) {
return "'cos' requires one operand";
@ -402,7 +402,7 @@ std::optional<std::string> Math::fn_cos(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_tan(dc_stack_t &stack) {
std::optional<std::string> Math::fn_tan(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.empty()) {
return "'tan' requires one operand";
@ -427,7 +427,7 @@ std::optional<std::string> Math::fn_tan(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_asin(dc_stack_t &stack) {
std::optional<std::string> Math::fn_asin(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.empty()) {
return "'asin' requires one operand";
@ -452,7 +452,7 @@ std::optional<std::string> Math::fn_asin(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_acos(dc_stack_t &stack) {
std::optional<std::string> Math::fn_acos(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.empty()) {
return "'acos' requires one operand";
@ -477,7 +477,7 @@ std::optional<std::string> Math::fn_acos(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_atan(dc_stack_t &stack) {
std::optional<std::string> Math::fn_atan(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.empty()) {
return "'atan' requires one operand";
@ -502,7 +502,7 @@ std::optional<std::string> Math::fn_atan(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_fact(dc_stack_t &stack) {
std::optional<std::string> Math::fn_fact(dc_stack_t &stack) const {
// Check if stack has enough elements
if(stack.empty()) {
return "'!' requires one operand";
@ -528,7 +528,7 @@ std::optional<std::string> Math::fn_fact(dc_stack_t &stack) {
}
// Push back the result as a string
stack.push_back(trim_digits(factorial, this->precision));
stack.push_back(trim_digits(static_cast<double>(factorial), this->precision));
} else {
return "'!' requires numeric values";
}
@ -536,13 +536,13 @@ std::optional<std::string> Math::fn_fact(dc_stack_t &stack) {
return std::nullopt;
}
std::optional<std::string> Math::fn_pi(dc_stack_t &stack) {
std::optional<std::string> Math::fn_pi(dc_stack_t &stack) const {
stack.push_back(trim_digits(std::numbers::pi, this->precision));
return std::nullopt;
}
std::optional<std::string> Math::fn_e(dc_stack_t &stack) {
std::optional<std::string> Math::fn_e(dc_stack_t &stack) const {
stack.push_back(trim_digits(std::numbers::e, this->precision));
return std::nullopt;
@ -556,7 +556,7 @@ std::string Math::trim_digits(double number, unsigned int precision) {
precision = 2;
}
oss << std::fixed << std::setprecision(precision) << number;
oss << std::fixed << std::setprecision(static_cast<int>(precision)) << number;
std::string s = oss.str();
return s;

View File

@ -4,28 +4,28 @@
class Math : public IOperation {
public:
Math(const OPType op_t, const unsigned int p) : op_type(std::move(op_t)), precision(std::move(p)) {}
Math(const OPType op_t, const unsigned int p) : op_type(op_t), precision(p) {}
std::optional<std::string> exec(dc_stack_t &stack) override;
private:
std::optional<std::string> fn_add(dc_stack_t &stack);
std::optional<std::string> fn_sub(dc_stack_t &stack);
std::optional<std::string> fn_mul(dc_stack_t &stack);
std::optional<std::string> fn_div(dc_stack_t &stack);
std::optional<std::string> fn_mod(dc_stack_t &stack);
std::optional<std::string> fn_div_mod(dc_stack_t &stack);
std::optional<std::string> fn_mod_exp(dc_stack_t &stack);
std::optional<std::string> fn_exp(dc_stack_t &stack);
std::optional<std::string> fn_sqrt(dc_stack_t &stack);
std::optional<std::string> fn_sin(dc_stack_t &stack);
std::optional<std::string> fn_cos(dc_stack_t &stack);
std::optional<std::string> fn_tan(dc_stack_t &stack);
std::optional<std::string> fn_asin(dc_stack_t &stack);
std::optional<std::string> fn_acos(dc_stack_t &stack);
std::optional<std::string> fn_atan(dc_stack_t &stack);
std::optional<std::string> fn_fact(dc_stack_t &stack);
std::optional<std::string> fn_pi(dc_stack_t &stack);
std::optional<std::string> fn_e(dc_stack_t &stack);
std::optional<std::string> fn_add(dc_stack_t &stack) const;
std::optional<std::string> fn_sub(dc_stack_t &stack) const;
std::optional<std::string> fn_mul(dc_stack_t &stack) const;
std::optional<std::string> fn_div(dc_stack_t &stack) const;
std::optional<std::string> fn_mod(dc_stack_t &stack) const;
std::optional<std::string> fn_div_mod(dc_stack_t &stack) const;
std::optional<std::string> fn_mod_exp(dc_stack_t &stack) const;
std::optional<std::string> fn_exp(dc_stack_t &stack) const;
std::optional<std::string> fn_sqrt(dc_stack_t &stack) const;
std::optional<std::string> fn_sin(dc_stack_t &stack) const;
std::optional<std::string> fn_cos(dc_stack_t &stack) const;
std::optional<std::string> fn_tan(dc_stack_t &stack) const;
std::optional<std::string> fn_asin(dc_stack_t &stack) const;
std::optional<std::string> fn_acos(dc_stack_t &stack) const;
std::optional<std::string> fn_atan(dc_stack_t &stack) const;
std::optional<std::string> fn_fact(dc_stack_t &stack) const;
std::optional<std::string> fn_pi(dc_stack_t &stack) const;
std::optional<std::string> fn_e(dc_stack_t &stack) const;
static std::string trim_digits(double number, unsigned int precision);
OPType op_type;

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <algorithm>
#include <ranges>
#include "stack.h"
#include "is_num.h"
@ -119,28 +120,28 @@ std::optional<std::string> Stack::fn_dup_head(dc_stack_t &stack) {
std::optional<std::string> Stack::fn_print_stack(dc_stack_t &stack) {
switch(this->oradix) {
case radix_base::DEC: {
for(auto it = stack.rbegin(); it != stack.rend(); it++) {
std::cout << *it << std::endl;
for(auto & it : std::ranges::reverse_view(stack)) {
std::cout << it << std::endl;
}
break;
}
case radix_base::BIN: {
for(auto it = stack.rbegin(); it != stack.rend(); it++) {
std::cout << to_bin(std::stol(*it)) << 'b' << std::endl;
for(auto & it : std::ranges::reverse_view(stack)) {
std::cout << to_bin(std::stol(it)) << 'b' << std::endl;
}
break;
}
case radix_base::OCT: {
for(auto it = stack.rbegin(); it != stack.rend(); it++) {
std::cout << std::oct << std::stol(*it) << 'o' << std::dec << std::endl;
for(auto & it : std::ranges::reverse_view(stack)) {
std::cout << std::oct << std::stol(it) << 'o' << std::dec << std::endl;
}
break;
}
case radix_base::HEX: {
for(auto it = stack.rbegin(); it != stack.rend(); it++) {
std::cout << std::hex << std::uppercase << std::stol(*it) << 'h'
<< std::dec << std::nouppercase << std::endl;
}
for(auto & it : std::ranges::reverse_view(stack)) {
std::cout << std::hex << std::uppercase << std::stol(it) << 'h'
<< std::dec << std::nouppercase << std::endl;
}
break;
}
}
@ -190,7 +191,7 @@ constexpr std::string Stack::to_bin(auto num) {
return "0";
}
std::string res = "";
std::string res;
while(num > 0) {
res = (std::to_string(num % 2) + res);
num /= 2;

View File

@ -4,20 +4,20 @@
class Stack : public IOperation {
public:
Stack(const OPType op_t) : op_type(std::move(op_t)) {}
Stack(const OPType op_t, const radix_base base) : op_type(std::move(op_t)), oradix(base) {}
explicit Stack(const OPType op_t) : op_type(op_t) {}
Stack(const OPType op_t, const radix_base base) : op_type(op_t), oradix(base) {}
std::optional<std::string> exec(dc_stack_t &stack) override;
private:
std::optional<std::string> fn_print(dc_stack_t &stack, bool new_line);
std::optional<std::string> fn_pop_head(dc_stack_t &stack);
std::optional<std::string> fn_swap_xy(dc_stack_t &stack);
std::optional<std::string> fn_dup_head(dc_stack_t &stack);
static std::optional<std::string> fn_pop_head(dc_stack_t &stack);
static std::optional<std::string> fn_swap_xy(dc_stack_t &stack);
static std::optional<std::string> fn_dup_head(dc_stack_t &stack);
std::optional<std::string> fn_print_stack(dc_stack_t &stack);
std::optional<std::string> fn_head_size(dc_stack_t &stack);
std::optional<std::string> fn_stack_size(dc_stack_t &stack);
static std::optional<std::string> fn_head_size(dc_stack_t &stack);
static std::optional<std::string> fn_stack_size(dc_stack_t &stack);
constexpr std::string to_bin(auto num);
OPType op_type;
radix_base oradix;
radix_base oradix{};
};