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

scaffold for server to send and receive messages #6

Merged
merged 12 commits into from
Feb 6, 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
2 changes: 1 addition & 1 deletion .githooks/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

everything_staged=$(git diff --name-only)

if [ -z "$everything_staged" ]; then
if [ -n "$everything_staged" ]; then
# Don't commit because, after we reformat, the formatting will be unstaged, but it's too hard to determine (and maybe
# ambiguous) what are the formatting differences and what was originally unstaged.
mvn spotless:check || {
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.snapshots/**/*.fail*

### Build ###
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
Expand Down
2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified .idea/settings.zip
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/arithmetic.ast.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/arithmetic.bc.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/calls.ast.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/calls.bc.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/conditions.ast.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/conditions.bc.rds
Binary file not shown.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
</dependency>
</dependencies>
<build>
<testResources>
<!-- Resources including R scripts which are treated as data -->
<testResource>
<directory>src/test/resources</directory>
</testResource>
<!-- R scripts which are treated as code -->
<testResource>
<directory>src/test/R</directory>
</testResource>
</testResources>
<plugins>
<!-- git-build-hook, install git hooks when you run any maven command so you don't forget -->
<plugin>
Expand Down Expand Up @@ -257,6 +267,11 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<!-- Run tests in parallel. You can annotate tests with @NotThreadSafe if necessary -->
<configuration>
<parallel>all</parallel>
<threadCount>8</threadCount>
</configuration>
<!-- automatically runs on `test` (and `verify` because `verify` invokes `test`) -->
</plugin>
</plugins>
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/org/prlprg/RVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.prlprg;

import javax.annotation.Nullable;

/**
* Major.Minor.Patch version number with an optional suffix for things like "Beta" and "RC".
*
* <p>This class needs to support whatever format GNU-R versions can have. But it's not a GNU-R
* specific
*/
public record RVersion(int major, int minor, int patch, @Nullable String suffix) {
/** The latest version we handle. */
public static final RVersion LATEST_AWARE = new RVersion(4, 3, 2);

public static RVersion parse(String textual) {
var parts = textual.split("\\.");

int major;
int minor;
int patch;
try {
major = Integer.parseInt(parts[0]);
minor = Integer.parseInt(parts[1]);
patch = Integer.parseInt(parts[2]);
} catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException("Invalid version number: " + textual, e);
}

String suffix;
if (parts.length > 3) {
if (parts[3].startsWith("-")) {
suffix = parts[3].substring(1);
} else {
throw new IllegalArgumentException(
"Invalid version number: " + textual + " (suffix must start with '-')");
}
} else {
suffix = null;
}

return new RVersion(major, minor, patch, suffix);
}

RVersion(int major, int minor, int patch) {
this(major, minor, patch, null);
}

@Override
public String toString() {
return major + "." + minor + "." + patch + (suffix == null ? "" : "-" + suffix);
}
}
47 changes: 44 additions & 3 deletions src/main/java/org/prlprg/bc/BcCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,54 @@ protected List<BcInstr> delegate() {
*/
static BcCode fromRaw(ImmutableIntArray bytecodes, ConstPool.MakeIdx makePoolIdx)
throws BcFromRawException {
if (bytecodes.isEmpty()) {
throw new BcFromRawException("Bytecode is empty, needs at least version number");
}
if (bytecodes.get(0) != Bc.R_BC_VERSION) {
throw new BcFromRawException("Unsupported bytecode version: " + bytecodes.get(0));
}

var labelMap = labelFactoryFromRaw(bytecodes);

var builder = new Builder();
int i = 0;
int i = 1;
int sanityCheckJ = 0;
while (i < bytecodes.length()) {
try {
var instrAndI = BcInstrs.fromRaw(bytecodes, i, makePoolIdx);
builder.add(instrAndI.a());
var instrAndI = BcInstrs.fromRaw(bytecodes, i, labelMap, makePoolIdx);
var instr = instrAndI.a();
i = instrAndI.b();

builder.add(instr);
sanityCheckJ++;

try {
var sanityCheckJFromI = labelMap.make(i).target;
if (sanityCheckJFromI != sanityCheckJ) {
throw new AssertionError(
"expected target offset " + sanityCheckJ + ", got " + sanityCheckJFromI);
}
} catch (IllegalArgumentException | AssertionError e) {
throw new AssertionError(
"BcInstrs.fromRaw and BcInstrs.sizeFromRaw are out of sync, at instruction " + instr,
e);
}
} catch (BcFromRawException e) {
throw new BcFromRawException(
"malformed bytecode at " + i + "\nBytecode up to this point: " + builder.build(), e);
}
}
return builder.build();
}

static BcLabel.Factory labelFactoryFromRaw(ImmutableIntArray bytecodes) {
var builder = new BcLabel.Factory.Builder();
int i = 1;
while (i < bytecodes.length()) {
try {
var size = BcInstrs.sizeFromRaw(bytecodes, i);
builder.step(size, 1);
i += size;
} catch (BcFromRawException e) {
throw new BcFromRawException(
"malformed bytecode at " + i + "\nBytecode up to this point: " + builder.build(), e);
Expand Down
Loading
Loading