Skip to content

Commit

Permalink
Add string concatenation operator to scripting (#802)
Browse files Browse the repository at this point in the history
* Add string concatenation operator to scripting

* Linting
  • Loading branch information
seanng-1 authored Apr 24, 2024
1 parent db89b3d commit 0deb546
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion include/behaviortree_cpp/scripting/operators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ struct ExprBinaryArithmetic : ExprBase
minus,
times,
div,
concat,

bit_and,
bit_or,
Expand All @@ -171,6 +172,8 @@ struct ExprBinaryArithmetic : ExprBase
return "*";
case div:
return "/";
case concat:
return "..";
case bit_and:
return "&";
case bit_or:
Expand Down Expand Up @@ -276,6 +279,12 @@ struct ExprBinaryArithmetic : ExprBase
{
return Any(lhs_v.cast<std::string>() + rhs_v.cast<std::string>());
}
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<std::string>() + rhs_v.cast<std::string>());
}
else
{
throw RuntimeError("Operation not permitted");
Expand Down Expand Up @@ -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<Ast::ExprBinaryArithmetic::concat>(LEXY_LIT(".."));
}();

using operand = math_sum;
};

// ~x
struct bit_prefix : dsl::prefix_op
{
Expand Down Expand Up @@ -785,7 +804,7 @@ struct Expression : lexy::expression_production
dsl::op<Ast::ExprBinaryArithmetic::logic_or>(LEXY_LIT("||")) /
dsl::op<Ast::ExprBinaryArithmetic::logic_and>(LEXY_LIT("&&"));

using operand = comparison;
using operand = dsl::groups<string_concat, comparison>;
};

// x ? y : z
Expand Down

0 comments on commit 0deb546

Please sign in to comment.