Skip to content

Commit

Permalink
Implement &. in desugar
Browse files Browse the repository at this point in the history
  • Loading branch information
nelhage committed Nov 14, 2017
1 parent 690590a commit 93ae72f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ast/desugar/Desugar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,29 @@ unique_ptr<Expression> node2TreeImpl(core::Context ctx, unique_ptr<parser::Node>
auto send = mkSend(what->loc, rec, a->method, args, flags);
result.swap(send);
},
[&](parser::CSend *a) {
core::NameRef tempRecv =
ctx.state.freshNameUnique(core::UniqueNameKind::Desugar, core::Names::assignTemp());
core::Loc recvLoc = a->receiver->loc;

// NOTE(nelhage): We actually desugar into a call to `nil?`. If an
// object has overridden `nil?`, this technically will not match
// Ruby's behavior.

auto assgn = mkAssign(recvLoc, tempRecv, node2TreeImpl(ctx, a->receiver));
auto cond = mkSend0(a->loc, mkLocal(recvLoc, tempRecv), core::Names::nil_p());

unique_ptr<parser::Node> sendNode = make_unique<parser::Send>(
a->loc, make_unique<parser::LVar>(recvLoc, tempRecv), a->method, move(a->args));
auto send = node2TreeImpl(ctx, sendNode);

unique_ptr<Expression> nil = make_unique<Nil>(a->loc);
auto iff = mkIf(a->loc, cond, nil, send);
InsSeq::STATS_store stats;
stats.emplace_back(move(assgn));
auto res = mkInsSeq(a->loc, stats, move(iff));
result.swap(res);
},
[&](parser::Self *a) {
unique_ptr<Expression> self = make_unique<Self>(what->loc, ctx.state.defn_todo());
result.swap(self);
Expand Down
5 changes: 5 additions & 0 deletions core/Context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ static UTF8Desc ampersand_DESC{(char *)ampersand_str, (int)strlen(ampersand_str)
static const char *lambda_str = "lambda";
static UTF8Desc lambda_DESC{(char *)lambda_str, (int)strlen(lambda_str)};

static const char *nil_p_str = "nil?";
static UTF8Desc nil_p_DESC{(char *)nil_p_str, (int)strlen(nil_p_str)};

static const char *todo_str = "<todo sym>";
static UTF8Desc todo_DESC{(char *)todo_str, (int)strlen(todo_str)};

Expand Down Expand Up @@ -274,6 +277,7 @@ GlobalState::GlobalState(spdlog::logger &logger) : logger(logger), errors(*this)
NameRef ampersand_id = enterNameUTF8(ampersand_DESC);
NameRef block_call_id = enterNameUTF8(block_call_DESC);
NameRef lambda_id = enterNameUTF8(lambda_DESC);
NameRef nil_p_id = enterNameUTF8(nil_p_DESC);

DEBUG_ONLY(Error::check(init_id == Names::initialize()));
DEBUG_ONLY(Error::check(andAnd_id == Names::andAnd()));
Expand Down Expand Up @@ -313,6 +317,7 @@ GlobalState::GlobalState(spdlog::logger &logger) : logger(logger), errors(*this)
DEBUG_ONLY(Error::check(ampersand_id == Names::ampersand()));
DEBUG_ONLY(Error::check(block_call_id == Names::blockCall()));
DEBUG_ONLY(Error::check(lambda_id == Names::lambda()));
DEBUG_ONLY(Error::check(nil_p_id == Names::nil_p()));

SymbolRef no_symbol_id = synthesizeClass(no_symbol_DESC);
SymbolRef top_id = synthesizeClass(top_DESC); // BasicObject
Expand Down
5 changes: 5 additions & 0 deletions core/Names.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ CheckSize(UniqueName, 8, 4)
static inline NameRef lambda() {
return NameRef(38);
}

// nil?
static inline NameRef nil_p() {
return NameRef(39);
}
};

class Name {
Expand Down

0 comments on commit 93ae72f

Please sign in to comment.