diff --git a/src/main/java/de/neemann/digital/hdl/hgs/Parser.java b/src/main/java/de/neemann/digital/hdl/hgs/Parser.java index ed4312782..4fe4c461a 100644 --- a/src/main/java/de/neemann/digital/hdl/hgs/Parser.java +++ b/src/main/java/de/neemann/digital/hdl/hgs/Parser.java @@ -266,8 +266,9 @@ private Statement parseStatement(boolean isRealStatement) throws IOException, Pa expect(CLOSE); inner = parseStatement(); return c -> { - Context iC = new Context(c, false); - while ((boolean) whileCond.value(iC)) inner.execute(iC); + while ((boolean) whileCond.value(c)) { + inner.execute(new Context(c, false)); + } }; case REPEAT: final Statement repeatInner = parseStatement(); @@ -275,10 +276,9 @@ private Statement parseStatement(boolean isRealStatement) throws IOException, Pa final Expression repeatCond = toBool(parseExpression()); if (isRealStatement) expect(SEMICOLON); return c -> { - Context iC = new Context(c, false); do { - repeatInner.execute(iC); - } while (!(boolean) repeatCond.value(iC)); + repeatInner.execute(new Context(c, false)); + } while (!(boolean) repeatCond.value(c)); }; case OPENBRACE: Statements s = new Statements(); diff --git a/src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java b/src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java index d9e05c52a..984de88d4 100644 --- a/src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java +++ b/src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java @@ -319,11 +319,21 @@ public void testParseTemplateWhile() throws IOException, ParserException, HGSEva assertEquals("Hello 0123456789 World!", c.toString()); } + public void testParseTemplateWhile2() throws IOException, ParserException, HGSEvalException { + Context c = exec("Hello World!"); + assertEquals("Hello 90 World!", c.toString()); + } + public void testParseTemplateRepeat() throws IOException, ParserException, HGSEvalException { Context c = exec("Hello World!"); assertEquals("Hello 0123456789 World!", c.toString()); } + public void testParseTemplateRepeat2() throws IOException, ParserException, HGSEvalException { + Context c = exec("Hello World!"); + assertEquals("Hello 90 World!", c.toString()); + } + public void testParseTemplateArray() throws IOException, ParserException, HGSEvalException { Context c = exec(";"); assertEquals("7,2;", c.toString());