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

Implement the NULL primitive #277

Merged
merged 1 commit into from
Dec 26, 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
28 changes: 28 additions & 0 deletions bjforth/src/main/java/bjforth/primitives/NULL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 Bahman Movaqar
*
* This file is part of bjForth.
*
* bjForth is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* bjForth is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with bjForth. If not, see <https://www.gnu.org/licenses/>.
*/
package bjforth.primitives;

import bjforth.machine.Machine;

public class NULL implements Primitive {
@Override
public void execute(Machine machine) {
machine.pushToParameterStack((Object) null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ static Primitive NUMBER() {
return containerNUMBER.get();
}

private static PrimitiveContainer containerNULL = new PrimitiveContainer(NULL::new);

private static PrimitiveContainer containerOVER = new PrimitiveContainer(OVER::new);

private static PrimitiveContainer containerPRINT = new PrimitiveContainer(PRINT::new);
Expand Down Expand Up @@ -308,6 +310,7 @@ static Primitive WORD() {
containerNEQU,
containerNROT,
containerNUMBER,
containerNULL,
containerOVER,
containerPRINT,
containerPRINTLN,
Expand Down
49 changes: 49 additions & 0 deletions bjforth/src/test/java/bjforth/primitives/NULLTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 Bahman Movaqar
*
* This file is part of bjForth.
*
* bjForth is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* bjForth is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with bjForth. If not, see <https://www.gnu.org/licenses/>.
*/
package bjforth.primitives;

import static bjforth.machine.BootstrapUtils.getPrimitiveAddress;
import static bjforth.machine.MachineAssertions.assertThat;
import static bjforth.machine.MachineBuilder.aMachine;
import static bjforth.machine.MachineStateBuilder.aMachineState;
import static bjforth.machine.ParameterStackBuilder.aParameterStack;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class NULLTest {

@Test
void workOk() {
// GIVEN
var NULLaddr = getPrimitiveAddress("NULL");
var actualState =
aMachineState()
.withInstrcutionPointer(NULLaddr)
.withParameterStack(aParameterStack().build())
.build();
var machine = aMachine().withState(actualState).build();

// WHEN
machine.step();

// THEN
assertThat(actualState).hasParameterStackEqualTo(aParameterStack().with((Object) null).build());
}
}
1 change: 1 addition & 0 deletions docs/Primitives.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ In the following table, which lists all the primitives
| `<>` | `x y - z` | NEQU | |
| `-ROT` | `a b c - c b a` | NROT | |
| `NUMBER` | ` - x` | | |
| `NULL` | ` - null` | | |
| `OVER` | `a b - a b a` | | |
| `PRINT` | `a - a` | | |
| `PRINTLN` | `a - a` | | |
Expand Down