Skip to content

Commit

Permalink
Make sure the empty blocks return nil
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Aug 25, 2020
1 parent 7b2697d commit ea9f341
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/som/compiler/MethodGenerationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ public void addLocal(final String local) {
locals.add(local);
}

public boolean hasBytecodes() {
return !bytecode.isEmpty();
}

public void removeLastBytecode() {
bytecode.remove(bytecode.size() - 1);
}
Expand Down
12 changes: 12 additions & 0 deletions src/som/compiler/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ private void blockBody(final MethodGenerationContext mgenc, final boolean seenPe
// was terminated with a . or not)
mgenc.removeLastBytecode();
}
if (mgenc.isBlockMethod() && !mgenc.hasBytecodes()) {
// if the block is empty, we need to return nil
SSymbol nilSym = universe.symbolFor("nil");
mgenc.addLiteralIfAbsent(nilSym);
bcGen.emitPUSHGLOBAL(mgenc, nilSym);
}
bcGen.emitRETURNLOCAL(mgenc);
mgenc.setFinished();
} else if (sym == EndTerm) {
Expand Down Expand Up @@ -878,6 +884,12 @@ private void nestedBlock(final MethodGenerationContext mgenc) throws ProgramDefi
// expression
// in the block was not terminated by ., and can generate a return
if (!mgenc.isFinished()) {
if (!mgenc.hasBytecodes()) {
// if the block is empty, we need to return nil
SSymbol nilSym = universe.symbolFor("nil");
mgenc.addLiteralIfAbsent(nilSym);
bcGen.emitPUSHGLOBAL(mgenc, nilSym);
}
bcGen.emitRETURNLOCAL(mgenc);
mgenc.setFinished(true);
}
Expand Down

0 comments on commit ea9f341

Please sign in to comment.