Skip to content

Commit

Permalink
Small fixes (formatting, etc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
breitnw committed Jul 18, 2024
1 parent 37e4737 commit f2c2e08
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 275 deletions.
6 changes: 0 additions & 6 deletions .idea/codeStyles/Project.xml

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

8 changes: 8 additions & 0 deletions .idea/misc.xml

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

4 changes: 1 addition & 3 deletions .settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=true
org.eclipse.jdt.apt.genSrcDir=target/generated-sources/annotations
org.eclipse.jdt.apt.genTestSrcDir=target/generated-test-sources/test-annotations
org.eclipse.jdt.apt.aptEnabled=false
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import javax.annotation.ParametersAreNonnullByDefault;

In IntelliJ you can simply copy `package-info.json` from any package into the new one and it will create this.

The project requires at least Java 21.
The project requires at least Java 22.

## Commands

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/prlprg/bc/Bc.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public Builder() {

public Builder(List<SEXP> consts) {
code = new BcCode.Builder();
this.consts = new ConstPool.Builder(consts.size());
this.consts.addAll(consts);
this.consts = new ConstPool.Builder(consts);
}

public void setTrackSrcRefs(boolean track) {
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/org/prlprg/bc/ConstPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import com.google.common.collect.ForwardingList;
import com.google.common.collect.ImmutableList;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import javax.annotation.concurrent.Immutable;
import org.prlprg.parseprint.ParseMethod;
Expand Down Expand Up @@ -128,9 +124,12 @@ public Builder() {
this.values = new ArrayList<>();
}

public Builder(int expectedSize) {
this.index = new HashMap<>(expectedSize);
this.values = new ArrayList<>(expectedSize);
public Builder(Collection<? extends SEXP> consts) {
this.index = new HashMap<>(consts.size());
this.values = new ArrayList<>(consts.size());
for (var e : consts) {
add(e);
}
}

public <S extends SEXP> Idx<S> add(S c) {
Expand All @@ -146,13 +145,6 @@ public <S extends SEXP> Idx<S> add(S c) {
return Idx.create(i, c);
}

/** Adds all the constants from {@code consts} to the builder. */
public void addAll(List<SEXP> consts) {
for (var e : consts) {
add(e);
}
}

/**
* Finish building the pool.
*
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/prlprg/rds/GNURByteCodeDecoderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class GNURByteCodeDecoderFactory {
GNURByteCodeDecoderFactory(ImmutableIntArray byteCode, List<SEXP> consts) {
this.byteCode = byteCode;

cpb = new ConstPool.Builder(consts.size());
cpb.addAll(consts);
cpb = new ConstPool.Builder(consts);
cbb = new BcCode.Builder();
labelMapping = LabelMapping.fromGNUR(byteCode);

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/prlprg/rds/GNURByteCodeEncoderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public class GNURByteCodeEncoderFactory {
this.bc = bc.code();
this.builder = ImmutableIntArray.builder();
this.labelMapping = LabelMapping.toGNUR(this.bc);
this.cpb = new ConstPool.Builder(bc.consts().size());
this.cpb.addAll(bc.consts());
this.cpb = new ConstPool.Builder(bc.consts());
}

public static class GNURByteCode {
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/prlprg/rds/RDSInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,25 @@ public void close() throws IOException {
* @throws IOException if an I/O error occurs.
*/
public int readRaw() throws IOException {
var input = in.read();
return input;
return in.read();
}

public byte readByte() throws IOException {
var input = in.readByte();
return input;
return in.readByte();
}

public int readInt() throws IOException {
var input = in.readInt();
return input;
return in.readInt();
}

public double readDouble() throws IOException {
var input = in.readDouble();
return input;
return in.readDouble();
}

public String readString(int natEncSize, Charset charset) throws IOException {
var buf = new byte[natEncSize];
in.readFully(buf, 0, natEncSize);
var input = new String(buf, charset);
return input;
return new String(buf, charset);
}

public int[] readInts(int length) throws IOException {
Expand Down
80 changes: 32 additions & 48 deletions src/main/java/org/prlprg/rds/RDSReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ private SEXP readItem() throws IOException {
case BCODE -> readByteCode();
case EXPR -> readExpr(flags);
case PROM -> readPromise(flags);
case BUILTIN -> readBuiltinOrSpecial();
case SPECIAL -> readBuiltinOrSpecial();
case BUILTIN, SPECIAL -> readBuiltinOrSpecial();
case CPLX -> readComplex(flags);
default -> throw new RDSException("Unsupported SEXP type: " + s.sexp());
};
Expand Down Expand Up @@ -246,8 +245,7 @@ private BCodeSXP readByteCode() throws IOException {
var length = in.readInt();
var reps = new SEXP[length];

var bc = readByteCode1(reps);
return bc;
return readByteCode1(reps);
}

private BCodeSXP readByteCode1(SEXP[] reps) throws IOException {
Expand Down Expand Up @@ -304,40 +302,36 @@ private SEXP readByteCodeLang(int type, SEXP[] reps) throws IOException {
// If the type is 0, we encountered a padding bit, meaning we jump back to "regular" SEXP
// processing.
if (type == 0) {
var item = readItem();
return item;
return readItem();
}

// Otherwise, we continue with bytecode processing
var rdsType = RDSItemType.valueOf(type);

SEXP res =
switch (rdsType) {
case RDSItemType.Sexp s -> {
if (s.sexp() == SEXPType.LANG || s.sexp() == SEXPType.LIST) {
yield readByteCodeLang1(rdsType, reps);
} else {
throw new UnsupportedOperationException(
"RDS reader error when reading SEXP: expected a padding bit, lang or list SXP, got: "
+ rdsType);
return switch (rdsType) {
case RDSItemType.Sexp s -> {
if (s.sexp() == SEXPType.LANG || s.sexp() == SEXPType.LIST) {
yield readByteCodeLang1(rdsType, reps);
} else {
throw new UnsupportedOperationException(
"RDS reader error when reading SEXP: expected a padding bit, lang or list SXP, got: "
+ rdsType);
}
}
case RDSItemType.Special s ->
switch (s) {
case BCREPREF -> {
int pos = in.readInt();
yield reps[pos];
}
}
case RDSItemType.Special s ->
switch (s) {
case BCREPREF -> {
int pos = in.readInt();
yield reps[pos];
}
case BCREPDEF, ATTRLISTSXP, ATTRLANGSXP -> readByteCodeLang1(rdsType, reps);
default ->
throw new UnsupportedOperationException(
"RDS reader error when reading special: expected a padding bit, BCREPDEF, "
+ "BCREPREF, ATTRLISTSXP, or ATTRLANGSXP, got: "
+ rdsType);
};
};

return res;
case BCREPDEF, ATTRLISTSXP, ATTRLANGSXP -> readByteCodeLang1(rdsType, reps);
default ->
throw new UnsupportedOperationException(
"RDS reader error when reading special: expected a padding bit, BCREPDEF, "
+ "BCREPREF, ATTRLISTSXP, or ATTRLANGSXP, got: "
+ rdsType);
};
};
}

private SEXP readByteCodeLang1(RDSItemType type, SEXP[] reps) throws IOException {
Expand Down Expand Up @@ -425,9 +419,7 @@ private SEXP readRef(Flags flags) throws IOException {
}

// since index is 1-based
var ref = refTable.get(index - 1);

return ref;
return refTable.get(index - 1);
}

private LangSXP readLang(Flags flags) throws IOException {
Expand Down Expand Up @@ -609,30 +601,23 @@ private CloSXP readClosure(Flags flags) throws IOException {
}

private @Nullable String readTag(Flags flags) throws IOException {
String tagVal;
if (flags.hasTag()) {
if (readItem() instanceof RegSymSXP s) {
tagVal = s.name();
return s.name();
} else {
throw new RDSException("Expected tag to be a symbol");
}
} else {
tagVal = null;
return null;
}

return tagVal;
}

private Attributes readAttributes(Flags flags) throws IOException {

Attributes attributes;
if (flags.hasAttributes()) {
attributes = readAttributes();
return readAttributes();
} else {
attributes = Attributes.NONE;
return Attributes.NONE;
}

return attributes;
}

private Attributes readAttributes() throws IOException {
Expand Down Expand Up @@ -667,9 +652,8 @@ private String readChars(Flags flags) throws IOException {
var len = in.readInt();
// charset should never be null for strings
var charset = requireNonNull(flags.getLevels().encoding());
var data = in.readString(len, charset);

return data;
return in.readString(len, charset);
}

private Flags readFlags() throws IOException {
Expand Down
Loading

0 comments on commit f2c2e08

Please sign in to comment.