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

Fix semantics of empty blocks #18

Merged
merged 2 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/compiler/GenerationContexts.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,7 @@ uint8_t method_genc_compute_stack_depth(method_generation_context* mgenc) {

return max_depth;
}

bool method_genc_has_bytecodes(method_generation_context* mgenc) {
return mgenc->bp != 0;
}
1 change: 1 addition & 0 deletions src/compiler/GenerationContexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ bool method_genc_find_var(
bool method_genc_find_field(method_generation_context* mgenc, pString field);
uint8_t method_genc_compute_stack_depth(method_generation_context* mgenc);

bool method_genc_has_bytecodes(method_generation_context* mgenc);

#endif // GENERATIONCONTEXTS_H_
15 changes: 13 additions & 2 deletions src/compiler/Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,17 @@ void blockBody(Lexer* l, method_generation_context* mgenc, bool seen_period) {
if(accept(l, Exit))
result(l, mgenc);
else if(l->sym == EndBlock) {
if(seen_period)
if (seen_period) {
// a POP has been generated which must be elided (blocks always
// return the value of the last expression, regardless of whether it
// was terminated with a . or not)
mgenc->bp--;
}
if (mgenc->block_method && !method_genc_has_bytecodes(mgenc)) {
pVMSymbol nilSym = Universe_symbol_for_cstr("nil");
SEND(mgenc->literals, addIfAbsent, nilSym);
emit_PUSH_GLOBAL(mgenc, nilSym);
}
emit_RETURN_LOCAL(mgenc);
mgenc->finished = true;
} else if(l->sym == EndTerm) {
Expand Down Expand Up @@ -1118,7 +1124,12 @@ void nestedBlock(Lexer* l, method_generation_context* mgenc) {

// if no return has been generated, we can be sure that the last expression
// in the block was not terminated by ., and can generate a return
if(!mgenc->finished) {
if (!mgenc->finished) {
if (!method_genc_has_bytecodes(mgenc)) {
pVMSymbol nilSym = Universe_symbol_for_cstr("nil");
SEND(mgenc->literals, addIfAbsent, nilSym);
emit_PUSH_GLOBAL(mgenc, nilSym);
}
emit_RETURN_LOCAL(mgenc);
mgenc->finished = true;
}
Expand Down
7 changes: 6 additions & 1 deletion tests/BasicInterpreterTests.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ static const Test tests[] = {
{"Blocks", "testArg2", (void*) 77, INTEGER},
{"Blocks", "testArgAndLocal", (void*) 8, INTEGER},
{"Blocks", "testArgAndContext", (void*) 8, INTEGER},
{"Blocks", "testEmptyZeroArg", 1, INTEGER},
{"Blocks", "testEmptyOneArg", 1, INTEGER},
{"Blocks", "testEmptyTwoArg", 1, INTEGER},

{"Return", "testReturnSelf", "Return", CLASS},
{"Return", "testReturnSelfImplicitly", "Return", CLASS},
Expand Down Expand Up @@ -98,10 +101,12 @@ static const Test tests[] = {

{"Regressions", "testSymbolEquality", (void*) 1, INTEGER},
{"Regressions", "testSymbolReferenceEquality", (void*) 1, INTEGER},
{"Regressions", "testUninitializedLocal", 1, INTEGER},
{"Regressions", "testUninitializedLocalInBlock", 1, INTEGER},

{"BinaryOperation", "test", (void*) 11, INTEGER},

{"NumberOfTests", "numberOfTests", (void*) 52, INTEGER},
{"NumberOfTests", "numberOfTests", (void*) 57, INTEGER},

{NULL}
};
Expand Down