diff --git a/bjforth/src/main/java/bjforth/primitives/NULL.java b/bjforth/src/main/java/bjforth/primitives/NULL.java new file mode 100644 index 00000000..bedf4abd --- /dev/null +++ b/bjforth/src/main/java/bjforth/primitives/NULL.java @@ -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 . + */ +package bjforth.primitives; + +import bjforth.machine.Machine; + +public class NULL implements Primitive { + @Override + public void execute(Machine machine) { + machine.pushToParameterStack((Object) null); + } +} diff --git a/bjforth/src/main/java/bjforth/primitives/PrimitiveFactory.java b/bjforth/src/main/java/bjforth/primitives/PrimitiveFactory.java index 28a2425c..7bb723d5 100644 --- a/bjforth/src/main/java/bjforth/primitives/PrimitiveFactory.java +++ b/bjforth/src/main/java/bjforth/primitives/PrimitiveFactory.java @@ -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); @@ -308,6 +310,7 @@ static Primitive WORD() { containerNEQU, containerNROT, containerNUMBER, + containerNULL, containerOVER, containerPRINT, containerPRINTLN, diff --git a/bjforth/src/test/java/bjforth/primitives/NULLTest.java b/bjforth/src/test/java/bjforth/primitives/NULLTest.java new file mode 100644 index 00000000..cfba990b --- /dev/null +++ b/bjforth/src/test/java/bjforth/primitives/NULLTest.java @@ -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 . + */ +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()); + } +} diff --git a/docs/Primitives.md b/docs/Primitives.md index 7990afd4..53d9e1e9 100644 --- a/docs/Primitives.md +++ b/docs/Primitives.md @@ -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` | | |