Skip to content

Commit

Permalink
Added primitives for selecting/setting field on nodes; settles #1078
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKlint committed May 28, 2017
1 parent febbef6 commit f15878e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6197,6 +6197,31 @@ public Object execute2(final Object arg_2, final Object arg_1, final Frame curre
}
}
},

/**
* Retrieve value of named field of node
*
* [ ..., INode nd, IString fieldName ] => [ ..., IValue value of field fieldName ]
*/
node_field_access {
@Override
public Object execute2(final Object arg_2, final Object arg_1, final Frame currentFrame, final RascalExecutionContext rex) {
INode nd = (INode) arg_2;
IString field = (IString) arg_1;
String fieldName = field.getValue();
// A default field that was set?

IValue v = null;
if(nd.mayHaveKeywordParameters()){
v = nd.asWithKeywordParameters().getParameter(fieldName);
}
if(v != null){
return v;
}

return rex.getFrameObserver().exception(currentFrame, RascalRuntimeException.noSuchField(fieldName, currentFrame));
}
},

/**
* Retrieve value of named field of constructor
Expand Down Expand Up @@ -7069,6 +7094,30 @@ public int executeN(Object[] stack, int sp, int arity, Frame currentFrame, Rasca
return sp - arity + 1;
}
},

/**
* Set named field of node value
*
* [ ..., INode nd, IString fieldName, IValue repl... ] => [ ..., new INode with named field set to repl ]
*/
node_field_update {
@Override
public int executeN(Object[] stack, int sp, int arity, Frame currentFrame, RascalExecutionContext rex) {
assert arity == 3;
INode nd = (INode) stack[sp - 3];
IString field = ((IString) stack[sp - 2]);
String fieldName = field.getValue();
IValue repl = (IValue) stack[sp - 1];
if(nd.mayHaveKeywordParameters()){
stack[sp - 3] = nd.asWithKeywordParameters().setParameter(fieldName, repl);
return sp - 2;
} else {
rex.getFrameObserver().exception(currentFrame,
RascalRuntimeException.noSuchField(fieldName, currentFrame));
}
return sp - 2;
}
},

/**
* Set named field of constructor value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ MuExp translate (e:(Expression) `<Expression expression> [ <Name key> = <Express
} else if(isLocType(tp)){
return muCallPrim3("loc_field_update", [ translate(expression), muCon(unescape("<key>")), translate(replacement) ], e@\loc);
} else if(isNodeType(tp)){
throw "Field update on node: <e>";
return muCallPrim3("node_field_update", [ translate(expression), muCon(unescape("<key>")), translate(replacement) ], e@\loc);
}
if(tupleHasFieldNames(tp)){
fieldNames = getTupleFieldNames(tp);
Expand Down

0 comments on commit f15878e

Please sign in to comment.