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

Revert changes RE parentheses #205

Merged
merged 2 commits into from
Dec 17, 2024
Merged
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
14 changes: 0 additions & 14 deletions bjforth/src/main/java/bjforth/primitives/WORD.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static bjforth.primitives.PrimitiveFactory.KEY;

import bjforth.machine.Machine;
import bjforth.machine.MachineException;

class WORD implements Primitive {

Expand All @@ -36,17 +35,13 @@ private static enum State {
public void execute(Machine machine) {
var state = State.BEGIN;
var result = new StringBuilder();
int parenthesesCount = 0;
while (state != State.END) {
KEY().execute(machine);
var ch = (int) machine.popFromParameterStack();
switch (state) {
case BEGIN:
if (ch == '\\') {
state = State.IN_COMMENT;
} else if (ch == '(') {
state = State.IN_COMMENT;
parenthesesCount += 1;
} else if (ch != ' ' && ch != '\n') {
result.appendCodePoint(ch);
state = State.IN_WORD;
Expand All @@ -55,15 +50,6 @@ public void execute(Machine machine) {
case IN_COMMENT:
if (ch == '\n') {
state = State.BEGIN;
} else if (ch == ')') {
if (parenthesesCount > 0) {
parenthesesCount -= 1;
} else {
throw new MachineException("Imbalanced parentheses.");
}
if (parenthesesCount == 0) {
state = State.BEGIN;
}
}
break;
case IN_WORD:
Expand Down
4 changes: 4 additions & 0 deletions bjforth/src/test/e2e/e2e-tests.forth
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
\ a b - c
: MULTIPLY * ;

\ a - b
: TIMES-10 10 MULTIPLY ;

20 TIMES-10 TOSTRING
50 50 + TOSTRING
21 changes: 0 additions & 21 deletions bjforth/src/test/java/bjforth/primitives/WORDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,4 @@ void endsWordWhenNewLine() {
// THEN
assertThat(actualState).hasParameterStackEqualTo(aParameterStack().with(wordStr).build());
}

@DisplayName("Treat balanced pairs of parentheses and the content between them as comments.")
@Test
void parensAsComments() {
// GIVEN
var wordStr = RandomStringUtils.randomAlphanumeric(10);
var str = wordStr + " ( this is a comment)" + '\n';
var inputStream = new ByteArrayInputStream(str.getBytes());
System.setIn(inputStream);

var WORDaddr = getPrimitiveAddress("WORD");
var actualState = aMachineState().withInstrcutionPointer(WORDaddr).build();
var machine = aMachine().withState(actualState).build();
var referenceState = aMachineState().copyFrom(actualState).build();

// WHEN
machine.step();

// THEN
assertThat(actualState).hasParameterStackEqualTo(aParameterStack().with(wordStr).build());
}
}