From 568634d64f67da9f9dae3a1523f6d030bf149471 Mon Sep 17 00:00:00 2001 From: Sean Geles Date: Mon, 8 Apr 2024 09:51:32 +1000 Subject: [PATCH 1/2] Add string concatenation operator to scripting --- .../behaviortree_cpp/scripting/operators.hpp | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/include/behaviortree_cpp/scripting/operators.hpp b/include/behaviortree_cpp/scripting/operators.hpp index 176fa2df0..5faca77b3 100644 --- a/include/behaviortree_cpp/scripting/operators.hpp +++ b/include/behaviortree_cpp/scripting/operators.hpp @@ -150,6 +150,7 @@ struct ExprBinaryArithmetic : ExprBase minus, times, div, + concat, bit_and, bit_or, @@ -171,6 +172,8 @@ struct ExprBinaryArithmetic : ExprBase return "*"; case div: return "/"; + case concat: + return ".."; case bit_and: return "&"; case bit_or: @@ -276,6 +279,12 @@ struct ExprBinaryArithmetic : ExprBase { return Any(lhs_v.cast() + rhs_v.cast()); } + else if (op == concat && ((rhs_v.isString() && lhs_v.isString()) || + (rhs_v.isString() && lhs_v.isNumber()) || + (rhs_v.isNumber() && lhs_v.isString()))) + { + return Any(lhs_v.cast() + rhs_v.cast()); + } else { throw RuntimeError("Operation not permitted"); @@ -722,6 +731,16 @@ struct Expression : lexy::expression_production using operand = math_product; }; + // x .. y + struct string_concat : dsl::infix_op_left + { + static constexpr auto op = [] { + return dsl::op(LEXY_LIT("..")); + }(); + + using operand = math_sum; + }; + // ~x struct bit_prefix : dsl::prefix_op { @@ -785,7 +804,7 @@ struct Expression : lexy::expression_production dsl::op(LEXY_LIT("||")) / dsl::op(LEXY_LIT("&&")); - using operand = comparison; + using operand = dsl::groups; }; // x ? y : z From 1093b4eec2b73f4aa5b70ac40defd4b5afa8c54a Mon Sep 17 00:00:00 2001 From: Sean Geles <58413041+seanng-1@users.noreply.github.com> Date: Sun, 14 Apr 2024 11:27:52 +1000 Subject: [PATCH 2/2] Linting --- include/behaviortree_cpp/scripting/operators.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/behaviortree_cpp/scripting/operators.hpp b/include/behaviortree_cpp/scripting/operators.hpp index 5faca77b3..14cf04f43 100644 --- a/include/behaviortree_cpp/scripting/operators.hpp +++ b/include/behaviortree_cpp/scripting/operators.hpp @@ -279,9 +279,9 @@ struct ExprBinaryArithmetic : ExprBase { return Any(lhs_v.cast() + rhs_v.cast()); } - else if (op == concat && ((rhs_v.isString() && lhs_v.isString()) || - (rhs_v.isString() && lhs_v.isNumber()) || - (rhs_v.isNumber() && lhs_v.isString()))) + else if(op == concat && ((rhs_v.isString() && lhs_v.isString()) || + (rhs_v.isString() && lhs_v.isNumber()) || + (rhs_v.isNumber() && lhs_v.isString()))) { return Any(lhs_v.cast() + rhs_v.cast()); } @@ -737,7 +737,7 @@ struct Expression : lexy::expression_production static constexpr auto op = [] { return dsl::op(LEXY_LIT("..")); }(); - + using operand = math_sum; };