Skip to content

Commit c52da90

Browse files
authored
Merge pull request #16 from PetrPytelka/fix_npe_CommandAnalyzerIf
Fix NPE for CommandAnalyzerIf
2 parents 6127838 + 2d6c423 commit c52da90

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/main/java/com/scriptbasic/syntax/commands/CommandAnalyzerIf.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,24 @@ public EndOfStatementResult consumeEndOfStatement() throws AnalysisException {
4040
state = InlineProcessorState.EXPECTING_ELSE_CLAUSE;
4141
return EndOfStatementResult.CONSUMED;
4242
}
43-
final var nextElement = ctx.lexicalAnalyzer.peek();
44-
45-
// handle multiple statements separated with colon
46-
if(nextElement!=null && nextElement.getLexeme().equals(":")) {
47-
ctx.lexicalAnalyzer.get();
48-
return EndOfStatementResult.CONSUMED;
49-
}
5043

51-
if (!nextElement.isLineTerminator() && !nextElement.getLexeme().equals("'")
52-
&& state == InlineProcessorState.EXPECTING_THEN_CLAUSE) {
53-
// only else statement is allowed
54-
if(nextElement.isSymbol(ScriptBasicKeyWords.KEYWORD_ELSE)) {
55-
state = InlineProcessorState.EXPECTING_ELSE;
44+
final var nextElement = ctx.lexicalAnalyzer.peek();
45+
if(nextElement!=null) {
46+
// handle multiple statements separated with colon
47+
if(nextElement.getLexeme().equals(":")) {
48+
ctx.lexicalAnalyzer.get();
5649
return EndOfStatementResult.CONSUMED;
57-
}
58-
throw new BasicSyntaxException("Unexpexted element: "+nextElement.getLexeme());
50+
}
51+
52+
if (!nextElement.isLineTerminator() && !nextElement.getLexeme().equals("'")
53+
&& state == InlineProcessorState.EXPECTING_THEN_CLAUSE) {
54+
// only else statement is allowed
55+
if (nextElement.isSymbol(ScriptBasicKeyWords.KEYWORD_ELSE)) {
56+
state = InlineProcessorState.EXPECTING_ELSE;
57+
return EndOfStatementResult.CONSUMED;
58+
}
59+
throw new BasicSyntaxException("Unexpexted element: " + nextElement.getLexeme());
60+
}
5961
}
6062

6163
state = InlineProcessorState.CLAUSE_DEFINED;

src/test/java/com/scriptbasic/testprograms/TestPrograms.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void testPrograms() throws Exception {
6464
codeTest("TestIf2.bas", "12568");
6565
codeTest("TestIf3.bas", "12456");
6666
codeTest("TestIf4.bas", "135910");
67+
codeTest("TestIf5.bas", "1");
6768
testSyntaxFail("TestIncorrectIf1.bas");
6869
testSyntaxFail("TestIncorrectIf2.bas");
6970
testSyntaxFail("TestIncorrectIf3.bas");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'
2+
' This program is used in unit testing the jScriptBasic interpreter to test the functionality of the
3+
' single line if-then-else
4+
'
5+
if true then print "1"

0 commit comments

Comments
 (0)