diff --git a/src/main/java/com/scriptbasic/syntax/commands/CommandAnalyzerIf.java b/src/main/java/com/scriptbasic/syntax/commands/CommandAnalyzerIf.java index ac87015c..8e9a9be4 100644 --- a/src/main/java/com/scriptbasic/syntax/commands/CommandAnalyzerIf.java +++ b/src/main/java/com/scriptbasic/syntax/commands/CommandAnalyzerIf.java @@ -40,22 +40,24 @@ public EndOfStatementResult consumeEndOfStatement() throws AnalysisException { state = InlineProcessorState.EXPECTING_ELSE_CLAUSE; return EndOfStatementResult.CONSUMED; } - final var nextElement = ctx.lexicalAnalyzer.peek(); - - // handle multiple statements separated with colon - if(nextElement!=null && nextElement.getLexeme().equals(":")) { - ctx.lexicalAnalyzer.get(); - return EndOfStatementResult.CONSUMED; - } - if (!nextElement.isLineTerminator() && !nextElement.getLexeme().equals("'") - && state == InlineProcessorState.EXPECTING_THEN_CLAUSE) { - // only else statement is allowed - if(nextElement.isSymbol(ScriptBasicKeyWords.KEYWORD_ELSE)) { - state = InlineProcessorState.EXPECTING_ELSE; + final var nextElement = ctx.lexicalAnalyzer.peek(); + if(nextElement!=null) { + // handle multiple statements separated with colon + if(nextElement.getLexeme().equals(":")) { + ctx.lexicalAnalyzer.get(); return EndOfStatementResult.CONSUMED; - } - throw new BasicSyntaxException("Unexpexted element: "+nextElement.getLexeme()); + } + + if (!nextElement.isLineTerminator() && !nextElement.getLexeme().equals("'") + && state == InlineProcessorState.EXPECTING_THEN_CLAUSE) { + // only else statement is allowed + if (nextElement.isSymbol(ScriptBasicKeyWords.KEYWORD_ELSE)) { + state = InlineProcessorState.EXPECTING_ELSE; + return EndOfStatementResult.CONSUMED; + } + throw new BasicSyntaxException("Unexpexted element: " + nextElement.getLexeme()); + } } state = InlineProcessorState.CLAUSE_DEFINED; diff --git a/src/test/java/com/scriptbasic/testprograms/TestPrograms.java b/src/test/java/com/scriptbasic/testprograms/TestPrograms.java index 7defc576..269e3426 100644 --- a/src/test/java/com/scriptbasic/testprograms/TestPrograms.java +++ b/src/test/java/com/scriptbasic/testprograms/TestPrograms.java @@ -64,6 +64,7 @@ public void testPrograms() throws Exception { codeTest("TestIf2.bas", "12568"); codeTest("TestIf3.bas", "12456"); codeTest("TestIf4.bas", "135910"); + codeTest("TestIf5.bas", "1"); testSyntaxFail("TestIncorrectIf1.bas"); testSyntaxFail("TestIncorrectIf2.bas"); testSyntaxFail("TestIncorrectIf3.bas"); diff --git a/src/test/resources/com/scriptbasic/testprograms/TestIf5.bas b/src/test/resources/com/scriptbasic/testprograms/TestIf5.bas new file mode 100644 index 00000000..ed53ff41 --- /dev/null +++ b/src/test/resources/com/scriptbasic/testprograms/TestIf5.bas @@ -0,0 +1,5 @@ +' +' This program is used in unit testing the jScriptBasic interpreter to test the functionality of the +' single line if-then-else +' +if true then print "1" \ No newline at end of file