Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string concatenation operator to scripting #802

Merged
merged 3 commits into from
Apr 24, 2024
Merged
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
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
Loading