From 0deb546a2575330918137ed79894f94c8301bf8f Mon Sep 17 00:00:00 2001 From: Sean Geles <58413041+seanng-1@users.noreply.github.com> Date: Wed, 24 Apr 2024 19:53:58 +1000 Subject: [PATCH] Add string concatenation operator to scripting (#802) * Add string concatenation operator to scripting * Linting --- .../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..14cf04f43 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