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

Java inter-op mark .<, ,< and @< and as immediate #260

Merged
merged 5 commits into from
Dec 25, 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
13 changes: 12 additions & 1 deletion bjforth/src/main/java/bjforth/primitives/ATLANGLE.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bjforth.machine.Machine;
import bjforth.machine.MachineException;
import bjforth.primitives.lib.ClassCache;
import bjforth.variables.Variables;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -129,11 +130,21 @@ public void execute(Machine machine) {
break;
}
}
machine.pushToParameterStack(result);

var HEREaddr = Variables.get("HERE").getAddress();
var HEREvalue = (Integer) machine.getMemoryAt(HEREaddr);
machine.setMemoryAt(HEREvalue, machine.getDictionaryItem("LIT").get().getAddress());
machine.setMemoryAt(HEREvalue + 1, result);
machine.setMemoryAt(HEREaddr, (Integer) machine.getMemoryAt(HEREaddr) + 2);
}

@Override
public String getName() {
return "@<";
}

@Override
public Boolean isImmediate() {
return true;
}
}
13 changes: 12 additions & 1 deletion bjforth/src/main/java/bjforth/primitives/COMMALANGLE.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bjforth.machine.Machine;
import bjforth.machine.MachineException;
import bjforth.primitives.lib.ClassCache;
import bjforth.variables.Variables;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -139,11 +140,21 @@ public void execute(Machine machine) {
break;
}
}
machine.pushToParameterStack(result);

var HEREaddr = Variables.get("HERE").getAddress();
var HEREvalue = (Integer) machine.getMemoryAt(HEREaddr);
machine.setMemoryAt(HEREvalue, machine.getDictionaryItem("LIT").get().getAddress());
machine.setMemoryAt(HEREvalue + 1, result);
machine.setMemoryAt(HEREaddr, (Integer) machine.getMemoryAt(HEREaddr) + 2);
}

@Override
public String getName() {
return ",<";
}

@Override
public Boolean isImmediate() {
return true;
}
}
13 changes: 12 additions & 1 deletion bjforth/src/main/java/bjforth/primitives/DOTLANGLE.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bjforth.machine.Machine;
import bjforth.machine.MachineException;
import bjforth.primitives.lib.ClassCache;
import bjforth.variables.Variables;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -122,11 +123,21 @@ public void execute(Machine machine) {
break;
}
}
machine.pushToParameterStack(result);

var HEREaddr = Variables.get("HERE").getAddress();
var HEREvalue = (Integer) machine.getMemoryAt(HEREaddr);
machine.setMemoryAt(HEREvalue, machine.getDictionaryItem("LIT").get().getAddress());
machine.setMemoryAt(HEREvalue + 1, result);
machine.setMemoryAt(HEREaddr, (Integer) machine.getMemoryAt(HEREaddr) + 2);
}

@Override
public String getName() {
return ".<";
}

@Override
public Boolean isImmediate() {
return true;
}
}
2 changes: 1 addition & 1 deletion bjforth/src/main/java/bjforth/primitives/INTERPRET.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void execute(Machine machine) {
"Pushing unknown word or invalid number onto stack: <%s>".formatted(obj.toString()));
if (STATE == 1) { // Compiling mode
machine.setMemoryAt(HEREvalue, machine.getDictionaryItem("LIT").get().getAddress());
machine.setMemoryAt(HEREvalue + 1, obj.toString());
machine.setMemoryAt(HEREvalue + 1, obj);
machine.setMemoryAt(HEREaddr, (Integer) machine.getMemoryAt(HEREaddr) + 2);
} else { // Immediate mode
machine.pushToParameterStack(obj.toString());
Expand Down
26 changes: 22 additions & 4 deletions bjforth/src/test/e2e/e2e-tests.forth
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,37 @@ PRINT-ME
###################################################################################################

# Integer.valueOf("12")
." 12 ". ,< Integer/valueOf(String)/1 >, PRINTLN
: INTEGER-VALUE-OF
,< Integer/valueOf(String)/1 >,
PRINTLN
;
." 12 ". INTEGER-VALUE-OF

# String.join(", ", "Hello", "world")
." world ". ." Hello ". ." , ". ,< String/join(CharSequence, CharSequence...)/3 >, PRINTLN
: STRING-JOIN
,< String/join(CharSequence, CharSequence...)/3 >,
PRINTLN
;
." world ". ." Hello ". ." , ". STRING-JOIN

###################################################################################################
# Java inter-op - Instance methods
###################################################################################################

24 .< longValue()/0 >. PRINTLN
: LONG-VALUE
.< longValue()/0 >.
PRINTLN
;

24 LONG-VALUE

###################################################################################################
# Java inter-op - Constructor
###################################################################################################

." Hello, world ". @< String(String)/1 >@ PRINTLN
: NEW-STRING
@< String(String)/1 >@
PRINTLN
;

." Hello, world ". NEW-STRING
15 changes: 13 additions & 2 deletions bjforth/src/test/java/bjforth/primitives/ATLANGLETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import bjforth.primitives.ATLANGLE.MethodDescriptor;
import bjforth.variables.Variables;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
Expand Down Expand Up @@ -56,16 +57,21 @@ void worksOk() {
var ATLANGLEaddr = getPrimitiveAddress("@<");
var actualState = aMachineState().withInstrcutionPointer(ATLANGLEaddr).build();
var machine = aMachine().withState(actualState).build();
var HEREvalue = (Integer) machine.getMemoryAt(Variables.get("HERE").getAddress());

// WHEN
machine.step();

// THEN
var actualResult = (MethodDescriptor) machine.popFromParameterStack();
assertThat(machine.getMemoryAt(HEREvalue)).isEqualTo(getPrimitiveAddress("LIT"));

var actualResult = (MethodDescriptor) machine.getMemoryAt(HEREvalue + 1);
assertThat(actualResult.clazz).isEqualTo(String.class);
assertThat(actualResult.arity).isEqualTo(1);
assertThat(actualResult.parameterTypes).isEqualTo(List.of(String.class));
assertThat(actualResult.varargFromArgumentNo).isEqualTo(-1);

assertThat(machine.getMemoryAt(Variables.get("HERE").getAddress())).isEqualTo(HEREvalue + 2);
}

@Test
Expand All @@ -78,16 +84,21 @@ void worksOkUnqualified() {
var ATLANGLEaddr = getPrimitiveAddress("@<");
var actualState = aMachineState().withInstrcutionPointer(ATLANGLEaddr).build();
var machine = aMachine().withState(actualState).build();
var HEREvalue = (Integer) machine.getMemoryAt(Variables.get("HERE").getAddress());

// WHEN
machine.step();

// THEN
var actualResult = (MethodDescriptor) machine.popFromParameterStack();
assertThat(machine.getMemoryAt(HEREvalue)).isEqualTo(getPrimitiveAddress("LIT"));

var actualResult = (MethodDescriptor) machine.getMemoryAt(HEREvalue + 1);
assertThat(actualResult.clazz).isEqualTo(String.class);
assertThat(actualResult.arity).isEqualTo(24);
assertThat(actualResult.parameterTypes)
.isEqualTo(List.of(File.class, String.class, Object[].class));
assertThat(actualResult.varargFromArgumentNo).isEqualTo(2);

assertThat(machine.getMemoryAt(Variables.get("HERE").getAddress())).isEqualTo(HEREvalue + 2);
}
}
15 changes: 13 additions & 2 deletions bjforth/src/test/java/bjforth/primitives/COMMALANGLETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import bjforth.primitives.COMMALANGLE.MethodDescriptor;
import bjforth.variables.Variables;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
Expand Down Expand Up @@ -56,16 +57,21 @@ void worksOk() {
var COMMALANGLEaddr = getPrimitiveAddress(",<");
var actualState = aMachineState().withInstrcutionPointer(COMMALANGLEaddr).build();
var machine = aMachine().withState(actualState).build();
var HEREvalue = (Integer) machine.getMemoryAt(Variables.get("HERE").getAddress());

// WHEN
machine.step();

// THEN
var actualResult = (MethodDescriptor) machine.popFromParameterStack();
assertThat(machine.getMemoryAt(HEREvalue)).isEqualTo(getPrimitiveAddress("LIT"));

var actualResult = (MethodDescriptor) machine.getMemoryAt(HEREvalue + 1);
assertThat(actualResult.target).isEqualTo(String.class);
assertThat(actualResult.arity).isEqualTo(3);
assertThat(actualResult.parameterTypes).isEqualTo(List.of(String.class, Object[].class));
assertThat(actualResult.name).isEqualTo("format");

assertThat(machine.getMemoryAt(Variables.get("HERE").getAddress())).isEqualTo(HEREvalue + 2);
}

@Test
Expand All @@ -78,16 +84,21 @@ void worksOkUnqualified() {
var COMMALANGLEaddr = getPrimitiveAddress(",<");
var actualState = aMachineState().withInstrcutionPointer(COMMALANGLEaddr).build();
var machine = aMachine().withState(actualState).build();
var HEREvalue = (Integer) machine.getMemoryAt(Variables.get("HERE").getAddress());

// WHEN
machine.step();

// THEN
var actualResult = (MethodDescriptor) machine.popFromParameterStack();
assertThat(machine.getMemoryAt(HEREvalue)).isEqualTo(getPrimitiveAddress("LIT"));

var actualResult = (MethodDescriptor) machine.getMemoryAt(HEREvalue + 1);
assertThat(actualResult.target).isEqualTo(String.class);
assertThat(actualResult.arity).isEqualTo(24);
assertThat(actualResult.parameterTypes)
.isEqualTo(List.of(File.class, String.class, Object[].class));
assertThat(actualResult.name).isEqualTo("format");

assertThat(machine.getMemoryAt(Variables.get("HERE").getAddress())).isEqualTo(HEREvalue + 2);
}
}
15 changes: 13 additions & 2 deletions bjforth/src/test/java/bjforth/primitives/DOTLANGLETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import bjforth.primitives.DOTLANGLE.MethodDescriptor;
import bjforth.variables.Variables;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
Expand Down Expand Up @@ -56,16 +57,21 @@ void worksOkFullyQualified() {
var DOTLANGLEaddr = getPrimitiveAddress(".<");
var actualState = aMachineState().withInstrcutionPointer(DOTLANGLEaddr).build();
var machine = aMachine().withState(actualState).build();
var HEREvalue = (Integer) machine.getMemoryAt(Variables.get("HERE").getAddress());

// WHEN
machine.step();

// THEN
var actualResult = (MethodDescriptor) machine.popFromParameterStack();
assertThat(machine.getMemoryAt(HEREvalue)).isEqualTo(getPrimitiveAddress("LIT"));

var actualResult = (MethodDescriptor) machine.getMemoryAt(HEREvalue + 1);
assertThat(actualResult.name).isEqualTo("format");
assertThat(actualResult.varargFromArgumentNo).isEqualTo(1);
assertThat(actualResult.parameterTypes).isEqualTo(List.of(String.class, Object[].class));
assertThat(actualResult.arity).isEqualTo(2);

assertThat(machine.getMemoryAt(Variables.get("HERE").getAddress())).isEqualTo(HEREvalue + 2);
}

@Test
Expand All @@ -78,16 +84,21 @@ void worksOkUnqualified() {
var DOTLANGLEaddr = getPrimitiveAddress(".<");
var actualState = aMachineState().withInstrcutionPointer(DOTLANGLEaddr).build();
var machine = aMachine().withState(actualState).build();
var HEREvalue = (Integer) machine.getMemoryAt(Variables.get("HERE").getAddress());

// WHEN
machine.step();

// THEN
var actualResult = (MethodDescriptor) machine.popFromParameterStack();
assertThat(machine.getMemoryAt(HEREvalue)).isEqualTo(getPrimitiveAddress("LIT"));

var actualResult = (MethodDescriptor) machine.getMemoryAt(HEREvalue + 1);
assertThat(actualResult.name).isEqualTo("format");
assertThat(actualResult.varargFromArgumentNo).isEqualTo(2);
assertThat(actualResult.parameterTypes)
.isEqualTo(List.of(List.class, File.class, String[].class));
assertThat(actualResult.arity).isEqualTo(12);

assertThat(machine.getMemoryAt(Variables.get("HERE").getAddress())).isEqualTo(HEREvalue + 2);
}
}