forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegexp.cc
36 lines (29 loc) · 1.05 KB
/
Regexp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "rewriter/Regexp.h"
#include "ast/Helpers.h"
#include "ast/ast.h"
#include "core/Context.h"
#include "core/Names.h"
#include "core/core.h"
#include "rewriter/rewriter.h"
using namespace std;
namespace sorbet::rewriter {
vector<ast::ExpressionPtr> Regexp::run(core::MutableContext ctx, ast::Assign *asgn) {
auto lhs = ast::cast_tree<ast::UnresolvedConstantLit>(asgn->lhs);
if (lhs == nullptr) {
return {};
}
auto send = ast::cast_tree<ast::Send>(asgn->rhs);
if (send == nullptr || send->fun != core::Names::new_()) {
return {};
}
auto recv = ast::cast_tree<ast::ConstantLit>(send->recv);
if (recv == nullptr || recv->symbol != core::Symbols::Regexp()) {
return {};
}
vector<ast::ExpressionPtr> stats;
auto type = ast::MK::Constant(send->loc, core::Symbols::Regexp());
auto newRhs = ast::MK::Let(send->loc, std::move(asgn->rhs), std::move(type));
stats.emplace_back(ast::MK::Assign(asgn->loc, std::move(asgn->lhs), std::move(newRhs)));
return stats;
}
}; // namespace sorbet::rewriter