Skip to content
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
37 changes: 37 additions & 0 deletions bjforth/src/main/java/bjforth/primitives/AND.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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;
import bjforth.machine.MachineException;
import java.util.NoSuchElementException;

public class AND implements Primitive {
@Override
public void execute(Machine machine) {
try {
var obj1 = (Integer) machine.popFromParameterStack();
var obj2 = (Integer) machine.popFromParameterStack();
var result = (Integer) (obj1 & obj2);
machine.pushToParameterStack(result);
} catch (NoSuchElementException e) {
throw new MachineException("Stack error.");
}
}
}
37 changes: 37 additions & 0 deletions bjforth/src/main/java/bjforth/primitives/OR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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;
import bjforth.machine.MachineException;
import java.util.NoSuchElementException;

public class OR implements Primitive {
@Override
public void execute(Machine machine) {
try {
var obj1 = (Integer) machine.popFromParameterStack();
var obj2 = (Integer) machine.popFromParameterStack();
var result = (Integer) (obj1 | obj2);
machine.pushToParameterStack(result);
} catch (NoSuchElementException e) {
throw new MachineException("Stack error.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class PrimitiveFactory {

private static PrimitiveContainer containerADDSTORE = new PrimitiveContainer(ADDSTORE::new);

private static PrimitiveContainer containerAND = new PrimitiveContainer(AND::new);

private static PrimitiveContainer containerATLANGLE = new PrimitiveContainer(ATLANGLE::new);

private static PrimitiveContainer containerBASE = new PrimitiveContainer(BASE::new);
Expand Down Expand Up @@ -177,6 +179,8 @@ static Primitive NUMBER() {

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

private static PrimitiveContainer containerOR = new PrimitiveContainer(OR::new);

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

private static PrimitiveContainer containerPRINT = new PrimitiveContainer(PRINT::new);
Expand Down Expand Up @@ -263,6 +267,7 @@ static Primitive WORD() {
List.of(
containerADD,
containerADDSTORE,
containerAND,
containerATLANGLE,
containerBASE,
containerBASESTORE,
Expand Down Expand Up @@ -319,6 +324,7 @@ static Primitive WORD() {
containerNROT,
containerNUMBER,
containerNULL,
containerOR,
containerOVER,
containerPRINT,
containerPRINTLN,
Expand Down
81 changes: 81 additions & 0 deletions bjforth/src/test/java/bjforth/primitives/ANDTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import bjforth.machine.MachineException;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;

class ANDTest {

@Test
void worksOk() {
// GIVEN
var ANDaddr = getPrimitiveAddress("AND");
var n1 = RandomUtils.insecure().randomInt(1000, 2000);
var n2 = RandomUtils.insecure().randomInt(1000, 2000);
var actualState =
aMachineState()
.withInstrcutionPointer(ANDaddr)
.withParameterStack(aParameterStack().with(n1, n2).build())
.build();
var machine = aMachine().withState(actualState).build();

// WHEN
machine.step();

// THEN
assertThat(actualState).hasParameterStackEqualTo(aParameterStack().with(n1 & n2).build());
}

@Test
void throwIfEmpty() {
var ANDaddr = getPrimitiveAddress("AND");
var actualState =
aMachineState()
.withInstrcutionPointer(ANDaddr)
.withParameterStack(aParameterStack().build())
.build();
var machine = aMachine().withState(actualState).build();

// EXPECT
assertThatThrownBy(machine::step).isInstanceOf(MachineException.class);
}

@Test
void throwIfOnlyOneElement() {
var ANDaddr = getPrimitiveAddress("AND");
var actualState =
aMachineState()
.withInstrcutionPointer(ANDaddr)
.withParameterStack(aParameterStack().with(10).build())
.build();
var machine = aMachine().withState(actualState).build();

// EXPECT
assertThatThrownBy(machine::step).isInstanceOf(MachineException.class);
}
}
81 changes: 81 additions & 0 deletions bjforth/src/test/java/bjforth/primitives/ORTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import bjforth.machine.MachineException;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;

class ORTest {

@Test
void worksOk() {
// GIVEN
var ORaddr = getPrimitiveAddress("OR");
var n1 = RandomUtils.insecure().randomInt(1000, 2000);
var n2 = RandomUtils.insecure().randomInt(1000, 2000);
var actualState =
aMachineState()
.withInstrcutionPointer(ORaddr)
.withParameterStack(aParameterStack().with(n1, n2).build())
.build();
var machine = aMachine().withState(actualState).build();

// WHEN
machine.step();

// THEN
assertThat(actualState).hasParameterStackEqualTo(aParameterStack().with(n1 | n2).build());
}

@Test
void throwIfEmpty() {
var ORaddr = getPrimitiveAddress("OR");
var actualState =
aMachineState()
.withInstrcutionPointer(ORaddr)
.withParameterStack(aParameterStack().build())
.build();
var machine = aMachine().withState(actualState).build();

// EXPECT
assertThatThrownBy(machine::step).isInstanceOf(MachineException.class);
}

@Test
void throwIfOnlyOneElement() {
var ORaddr = getPrimitiveAddress("OR");
var actualState =
aMachineState()
.withInstrcutionPointer(ORaddr)
.withParameterStack(aParameterStack().with(10).build())
.build();
var machine = aMachine().withState(actualState).build();

// EXPECT
assertThatThrownBy(machine::step).isInstanceOf(MachineException.class);
}
}
2 changes: 2 additions & 0 deletions docs/Primitives.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ In the following table, which lists all the primitives
|-------------|---------------------|----------------|--------------------------------------------------|
| `+` | `y - z` | ADD | |
| `+!` | `p x - ` | ADDSTORE | |
| `AND` | `x y - z` | | |
| `@@` | `... a b - o` | ATAT | Create a new object, `new/0 java.lang.Object @@` |
| `BASE` | ` - x` | | |
| `BRANCH` | ` -` | | |
Expand Down Expand Up @@ -70,6 +71,7 @@ In the following table, which lists all the primitives
| `-ROT` | `a b c - c b a` | NROT | |
| `NUMBER` | ` - x` | | |
| `NULL` | ` - null` | | |
| `OR` | ` x y - z` | | |
| `OVER` | `a b - a b a` | | |
| `PRINT` | `a - a` | | |
| `PRINTLN` | `a - a` | | |
Expand Down