Skip to content

Commit

Permalink
fixed a scope issue in repeat and while, see hneemann#1320
Browse files Browse the repository at this point in the history
  • Loading branch information
hneemann committed Sep 6, 2024
1 parent 3d6bf91 commit 2e9a8ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main/java/de/neemann/digital/hdl/hgs/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,19 @@ 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();
expect(UNTIL);
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();
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/de/neemann/digital/hdl/hgs/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <? n:=0;i:=1; while (i<=9) { a:=i*2; n=n+a; i++; } print(n); ?> World!");
assertEquals("Hello 90 World!", c.toString());
}

public void testParseTemplateRepeat() throws IOException, ParserException, HGSEvalException {
Context c = exec("Hello <? i:=0; repeat { =i; i++; } until i=10; ?> World!");
assertEquals("Hello 0123456789 World!", c.toString());
}

public void testParseTemplateRepeat2() throws IOException, ParserException, HGSEvalException {
Context c = exec("Hello <? n:=0;i:=1; repeat { a:=i*2;n=n+a; i++; } until i=10; print(n); ?> World!");
assertEquals("Hello 90 World!", c.toString());
}

public void testParseTemplateArray() throws IOException, ParserException, HGSEvalException {
Context c = exec("<? a:=[]; a[0]:=1; a[1]:=7; print(a[1], \",\" ,sizeOf(a)); ?>;");
assertEquals("7,2;", c.toString());
Expand Down

0 comments on commit 2e9a8ad

Please sign in to comment.