Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/debug/debug.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void dump_stack(rpn_context & ctxt) {

void dump_variables(rpn_context & ctxt) {
Serial.printf("Variables\n--------------------\n");
rpn_variables_foreach(ctxt, [](const String& name, const rpn_value& value) {
Serial.printf("$%s = %.2f\n", name.c_str(), value.toFloat());
rpn_variables_foreach(ctxt, [](const char* name, const rpn_value& value) {
Serial.printf("$%s = %.2f\n", name, value.toFloat());
});

Serial.println();
Expand Down Expand Up @@ -85,7 +85,7 @@ void setup() {
// Last parameter in rpn_process forces variable check,
// the execution will fail if the variable does not exist
if (!rpn_process(ctxt, "$temperatue 18 21 cmp3 1 + 1 $relay 0 3 index", true)) {
rpn_handle_error(ctxt.error, rpn_decode_errors([](const String& decoded) {
rpn_handle_error(ctxt.error, rpn_decode_errors([](const char* decoded) {
Serial.println("rpn_process stopped after an error: ");
Serial.println(decoded);
}));
Expand Down
43 changes: 8 additions & 35 deletions examples/host/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,28 @@ const char* stack_type(rpn_stack_value::Type type) {
}
}

void dump_value(const rpn_value& val) {
switch (val.type) {
case rpn_value::Type::Boolean:
std::cout << (val.toBoolean() ? "true" : "false") << " (Boolean) ";
break;
case rpn_value::Type::Integer:
std::cout << val.toInt() << " (Integer) ";
break;
case rpn_value::Type::Unsigned:
std::cout << val.toUint() << " (Unsigned) ";
break;
case rpn_value::Type::Float:
std::cout << val.toFloat() << " (Float) ";
break;
case rpn_value::Type::String:
std::cout << val.toString().c_str() << " (String) ";
break;
case rpn_value::Type::Error:
std::cout << "error ";
break;
case rpn_value::Type::Null:
std::cout << "null ";
break;
}
}

void dump_variables(rpn_context & ctxt) {
rpn_variables_foreach(ctxt, [](const String& name, rpn_value& value) {
std::cout << "$" << name.c_str() << " is ";
dump_value(value);
std::cout << std::endl;
rpn_variables_foreach(ctxt, [](const char* name, rpn_value& value) {
std::cout << "$" << name << " is ";
std::cout << value.toString() << std::endl;
});
}

void dump_stack(rpn_context & ctxt) {
size_t index = rpn_stack_size(ctxt);
rpn_stack_foreach(ctxt, [&index](rpn_stack_value::Type type, const rpn_value& value) {
std::cout << std::setfill('0') << std::setw(3) << --index << ": ";
dump_value(value);
std::cout << value.toString();
std::cout << " (" << stack_type(type) << ")" << std::endl;
});
std::cout << std::endl;
}

void dump_operators(rpn_context & ctxt) {
size_t index = 0;
rpn_operators_foreach(ctxt, [&index](const String& name, size_t argc, rpn_operator::callback_type) {
rpn_operators_foreach(ctxt, [&index](const char* name, size_t argc, rpn_operator::callback_type) {
std::cout << std::setfill('0') << std::setw(3) << ++index << ": ";
std::cout << name.c_str() << "(...), " << argc << std::endl;
std::cout << name << "(...), " << argc << std::endl;
});
}

Expand Down Expand Up @@ -151,14 +124,14 @@ int main(int argc, char** argv) {
break;
}
if (!rpn_process(ctxt, input.c_str())) {
auto handler = [&ctxt](const String& decoded) {
auto handler = [&ctxt](const char* decoded) {
auto pos = ctxt.error.position;
std::cout << " ";
while (--pos) {
std::cout << ' ';
}
std::cout << "^\n";
std::cout << "ERR: " << decoded.c_str() << std::endl;
std::cout << "ERR: " << decoded << std::endl;
};
rpn_handle_error(ctxt.error, rpn_decode_errors(handler));
}
Expand Down
8 changes: 6 additions & 2 deletions examples/time/time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ along with the rpnlib library. If not, see <http://www.gnu.org/licenses/>.
#include <sys/time.h>
#include <rpnlib.h>

// time_t value on ESP8266 could be either 4 or 8 bytes, but it might not always be able to fit into rpn_int
// (but, it is possible to tweak time funcs to accept time_t as LOW and HIGH parts, requiring special operators to handle such values)
static_assert(sizeof(rpn_int) >= sizeof(time_t), "time_t should be able to fit into a single rpn_value");

void dump_stack(rpn_context & ctxt) {
rpn_value value;
auto index = rpn_stack_size(ctxt) - 1;
Expand Down Expand Up @@ -115,11 +119,11 @@ void setup() {
return 0;
});

rpn_variable_set(ctxt, F("time"),
rpn_variable_set(ctxt, "time",
rpn_value(static_cast<rpn_int>(time(nullptr)))
);

rpn_variables_foreach(ctxt, [](const String& name, const rpn_value& value) {
rpn_variables_foreach(ctxt, [](const char* name, const rpn_value& value) {
Serial.println(name);
Serial.println(value.toInt());
});
Expand Down
4 changes: 2 additions & 2 deletions src/rpnlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ rpn_input_buffer& rpn_input_buffer::write(const char* data, size_t data_length)
return *this;
}

bool rpn_input_buffer::operator==(const String& other) const {
return other.equals(_buffer);
bool rpn_input_buffer::operator==(const std::string& other) const {
return other == _buffer;
}

void rpn_input_buffer::reset() {
Expand Down
4 changes: 1 addition & 3 deletions src/rpnlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ along with the rpnlib library. If not, see <http://www.gnu.org/licenses/>.
#ifndef rpnlib_h
#define rpnlib_h

#include <Arduino.h>

#include "rpnlib_config.h"
#include "rpnlib_error.h"

Expand Down Expand Up @@ -62,7 +60,7 @@ struct rpn_input_buffer {

const char* c_str() const;
size_t length() const;
bool operator==(const String& other) const;
bool operator==(const std::string& other) const;

rpn_input_buffer& operator+=(char c);
rpn_input_buffer& write(const char* data, size_t data_length);
Expand Down
2 changes: 2 additions & 0 deletions src/rpnlib_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ along with the rpnlib library. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <cstdint>

#ifndef RPNLIB_INT_TYPE
#define RPNLIB_INT_TYPE int32_t
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/rpnlib_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ along with the rpnlib library. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <cstddef>

enum class rpn_error_category {
Unknown,
Processing,
Expand Down
16 changes: 4 additions & 12 deletions src/rpnlib_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ extern "C" {
#include "fs_math.h"
}

#include "rpnlib_compat.h"

#include <cmath>
#include <cstdlib>
#include <algorithm>
Expand Down Expand Up @@ -239,8 +237,8 @@ rpn_error _rpn_abs(rpn_context & ctxt) {
}

rpn_value result =
(top.isFloat()) ? rpn_value(rpnlib_abs(top.toFloat())) :
(top.isInt()) ? rpn_value(rpnlib_abs(top.toInt())) :
(top.isFloat()) ? rpn_value(std::abs(top.toFloat())) :
(top.isInt()) ? rpn_value(std::abs(top.toInt())) :
(rpn_value{});

_rpn_stack_eat(ctxt, 1);
Expand Down Expand Up @@ -392,7 +390,7 @@ rpn_error _rpn_index(rpn_context & ctxt) {
return conversion.error();
}

auto offset = rpnlib_round(conversion.value());
auto offset = std::round(conversion.value());
if (offset >= 0.) {
if ((offset + 1) > size) {
return rpn_operator_error::InvalidArgument;
Expand Down Expand Up @@ -539,7 +537,7 @@ rpn_error _rpn_round(rpn_context & ctxt) {
return conversion.error();
}

rpn_float limit = rpnlib_round(conversion.value());
rpn_float limit = std::round(conversion.value());
rpn_float multiplier = 1.0;
for (int i = 0; i < limit; ++i) {
multiplier *= 10.0;
Expand Down Expand Up @@ -742,12 +740,6 @@ rpn_error _rpn_assign(rpn_context & ctxt) {
// Functions methods
// ----------------------------------------------------------------------------

rpn_operator::rpn_operator(const char* name, unsigned char argc, callback_type callback) :
name(name),
argc(argc),
callback(callback)
{}

bool rpn_operator_set(rpn_context & ctxt, const char * name, unsigned char argc, rpn_operator::callback_type callback) {
ctxt.operators.emplace_front(name, argc, callback);
return true;
Expand Down
16 changes: 9 additions & 7 deletions src/rpnlib_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ struct rpn_operator {
using callback_type = rpn_error(*)(rpn_context &);

rpn_operator() = delete;
rpn_operator(const char*, unsigned char, callback_type);

rpn_operator(const rpn_operator&) = default;
rpn_operator(rpn_operator&& other) noexcept :
name(std::move(other.name)),
argc(other.argc),
callback(other.callback)
template <typename T>
rpn_operator(T&& name_, unsigned char argc_, callback_type callback_) :
name(std::forward<T>(name_)),
argc(argc_),
callback(callback_)
{}

String name;
rpn_operator(const rpn_operator&) = default;
rpn_operator(rpn_operator&& other) noexcept = default;

std::string name;
unsigned char argc;
callback_type callback;
};
Expand Down
12 changes: 7 additions & 5 deletions src/rpnlib_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ along with the rpnlib library. If not, see <http://www.gnu.org/licenses/>.
#pragma once

#include "rpnlib.h"

#include <cstdio>
#include <utility>

template <typename Callback>
Expand All @@ -35,14 +37,14 @@ void rpn_stack_foreach(rpn_context & ctxt, Callback callback) {
template <typename Callback>
void rpn_variables_foreach(rpn_context & ctxt, Callback callback) {
for (auto& var : ctxt.variables) {
callback(var.name, *(var.value.get()));
callback(var.name.c_str(), *(var.value.get()));
}
}

template <typename Callback>
void rpn_operators_foreach(rpn_context & ctxt, Callback callback) {
for (auto& op : ctxt.operators) {
callback(op.name, op.argc, op.callback);
callback(op.name.c_str(), op.argc, op.callback);
}
}

Expand All @@ -65,7 +67,6 @@ void rpn_handle_error(const rpn_error& error, Visitor&& visitor) {

template <typename Callback>
struct rpn_error_decoder {

rpn_error_decoder(Callback&& callback) :
callback(callback)
{}
Expand Down Expand Up @@ -152,11 +153,12 @@ struct rpn_error_decoder {
}

void operator()(int code) {
callback(String("Unknown error #") + String(code));
char buffer[33];
snprintf(buffer, sizeof(buffer), "Unknown error #%d", code);
callback(buffer);
}

Callback callback;

};

template <typename Callback>
Expand Down
Loading