Skip to content

Commit

Permalink
Improved RDS parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Jun 2, 2024
1 parent 1c50c33 commit 36300d9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 53 deletions.
71 changes: 52 additions & 19 deletions pmml-rexp/src/main/java/org/jpmml/rexp/RExpParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private RFunctionCall readFunctionCall(int flags) throws IOException {

RExp tag = readTag(flags);
RExp function = readRExp();
RExp arguments = readRExp();
RPair arguments = (RPair)readRExp();

return new RFunctionCall(tag, function, arguments, attributes);
}
Expand Down Expand Up @@ -337,9 +337,9 @@ private RGenericVector readVector(int flags) throws IOException {
private RExp readBytecode(int flags) throws IOException {
int length = readInt();

readBC1();
RExp[] reps = new RExp[length];

return null;
return readBC1(reps);
}

private RExp readExternalPointer(int flags) throws IOException {
Expand All @@ -363,40 +363,46 @@ private RRaw readRaw(int flags) throws IOException {
return new RRaw(value, readAttributes(flags));
}

private void readBC1() throws IOException {
private RExp readBC1(RExp[] reps) throws IOException {
RExp code = readRExp();

readBCConsts();
RExp[] constants = readBCConsts(reps);

return constants[0];
}

private void readBCConsts() throws IOException {
private RExp[] readBCConsts(RExp[] reps) throws IOException {
int n = readInt();

RExp[] pool = new RExp[n];

for(int i = 0; i < n; i++){
int type = readInt();

switch(type){
case SExpTypes.LISTSXP:
case SExpTypes.LANGSXP:
readBCLang(type);
pool[i] = readBCLang(type, reps);
break;
case SExpTypes.BCODESXP:
readBC1();
pool[i] = readBC1(reps);
break;
case SerializationTypes.ATTRLISTSXP:
case SerializationTypes.ATTRLANGSXP:
case SerializationTypes.BCREPREF:
case SerializationTypes.BCREPDEF:
readBCLang(type);
pool[i] = readBCLang(type, reps);
break;
default:
readRExp();
pool[i] = readRExp();
break;
}
}

return pool;
}

private void readBCLang(int type) throws IOException {
private RExp readBCLang(int type, RExp[] reps) throws IOException {

switch(type){
case SExpTypes.LISTSXP:
Expand All @@ -410,26 +416,53 @@ private void readBCLang(int type) throws IOException {
type = readInt();
}

RPair attributes;

switch(type){
case SerializationTypes.ATTRLISTSXP:
case SerializationTypes.ATTRLANGSXP:
readAttributes();
attributes = readAttributes();
break;
default:
attributes = null;
break;
}

RPair pair;

switch(type){
case SExpTypes.LISTSXP:
case SerializationTypes.ATTRLISTSXP:
pair = new RPair(null, null, attributes);
break;
case SExpTypes.LANGSXP:
case SerializationTypes.ATTRLANGSXP:
pair = new RFunctionCall(null, null, null, attributes);
break;
default:
throw new UnsupportedOperationException(String.valueOf(type));
}

if(pos >= 0){
reps[pos] = pair;
}

RExp tag = readRExp();
pair.setTag(tag);

readBCLang(readInt());
readBCLang(readInt());
break;
RExp value = readBCLang(readInt(), reps);
pair.setValue(value);

RPair next = (RPair)readBCLang(readInt(), reps);
if(next != null){
pair.setNext(next);
}

return pair;
case SerializationTypes.BCREPREF:
readInt();
break;
return reps[readInt()];
default:
readRExp();
break;
return readRExp();
}
}

Expand Down
39 changes: 7 additions & 32 deletions pmml-rexp/src/main/java/org/jpmml/rexp/RFunctionCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,19 @@
*/
package org.jpmml.rexp;

public class RFunctionCall extends RExp {
public class RFunctionCall extends RPair {

private RExp tag = null;
public RFunctionCall(RExp tag, RExp function, RPair arguments, RPair attributes){
super(tag, function, attributes);

private RExp function = null;

private RExp arguments = null;


public RFunctionCall(RExp tag, RExp function, RExp arguments, RPair attributes){
super(attributes);

setTag(tag);
setFunction(function);
setArguments(arguments);
}

public RExp getTag(){
return this.tag;
}

private void setTag(RExp tag){
this.tag = tag;
setNext(arguments);
}

public RExp getFunction(){
return this.function;
}

private void setFunction(RExp function){
this.function = function;
}

public RExp getArguments(){
return this.arguments;
return getValue();
}

private void setArguments(RExp arguments){
this.arguments = arguments;
public RPair getArguments(){
return getNext();
}
}
4 changes: 2 additions & 2 deletions pmml-rexp/src/main/java/org/jpmml/rexp/RPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public RExp getTag(){
return this.tag;
}

private void setTag(RExp tag){
void setTag(RExp tag){
this.tag = tag;
}

public RExp getValue(){
return this.value;
}

private void setValue(RExp value){
void setValue(RExp value){
this.value = value;
}

Expand Down

0 comments on commit 36300d9

Please sign in to comment.