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 empty return statements not being emitted in bytecode. #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions lib/src/compiler/ast/block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'stat.dart';
// explist ::= exp {‘,’ exp}
class Block extends Node {
List<Stat> stats;
List<Exp> retExps;
List<Exp>? retExps;

Block({required this.stats,required this.retExps});

Expand All @@ -26,9 +26,9 @@ class Block extends Node {
}
sb.write(']');
}
if(retExps.isNotEmpty){
if(retExps != null){
sb.write(',\nRetExps:[');
for(var exp in retExps){
for(var exp in retExps!){
_expToStr(exp,sb);
}
sb.writeln(']');
Expand Down
4 changes: 2 additions & 2 deletions lib/src/compiler/codegen/block_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class BlockProcessor {
StatProcessor.processStat(fi, stat);
}

if (node.retExps.isNotEmpty) {
processRetStat(fi, node.retExps, node.lastLine);
if (node.retExps != null) {
processRetStat(fi, node.retExps!, node.lastLine);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/compiler/parser/block_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class BlockParser {

// retstat ::= return [explist] [‘;’]
// explist ::= exp {‘,’ exp}
static List<Exp> parseRetExps(Lexer lexer) {
static List<Exp>? parseRetExps(Lexer lexer) {
if (lexer.LookAhead() != TokenKind.TOKEN_KW_RETURN) {
return List.empty();
return null;
}

lexer.nextToken();
Expand Down
10 changes: 10 additions & 0 deletions test/block/empty_return.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
x = true

function empty_return()
if true then
return
end

-- Should not get here.
x = false
end
21 changes: 21 additions & 0 deletions test/block/empty_return_statement_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:io';

import 'package:lua_dardo/lua.dart';
import 'package:test/test.dart';

void main() {
test('Empty return statement exits function early', () {
Directory.current = './test/block/';

final ls = LuaState.newState();
ls.openLibs();
ls.doFile("empty_return.lua");

ls.getGlobal("empty_return");
ls.pCall(0, 0, 1);

ls.getGlobal("x");
final result = ls.toBoolean(-1);
expect(result, true);
});
}