Skip to content

Commit e25b87c

Browse files
committed
1 parent 522b23c commit e25b87c

File tree

84 files changed

+1026
-513
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1026
-513
lines changed

language/src/main/antlr4/QDLParser.g4

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ expression
111111
:
112112
function #functions
113113
| expression op=FunctionMarker expression #dyadicFunctionRefernce
114+
//| expression '::' expression #arg_concat
114115
| variable? Hash expression #moduleExpression
115116
| expression StemDot+ expression #dotOp
116117
| FunctionMarker expression #functionReference // REUSED
@@ -145,7 +146,7 @@ expression
145146
| '(' expression ')' #association
146147
//| expression '&'+ expression #typeCheck
147148
// | expression '`'+ expression #index
148-
// | expression '|'+ expression #stile
149+
| expression '`'+ expression #axis
149150
// | prefix=',' expression #unravel
150151
// | expression ((Stile + expression ) | (Stile '*')) #restriction
151152
// Fix https://github.com/ncsa/qdl/issues/97
852 Bytes
Binary file not shown.

language/src/main/java/org/qdl_lang/evaluate/OpEvaluator.java

-13
Original file line numberDiff line numberDiff line change
@@ -649,19 +649,6 @@ protected QDLStem applyToStem(QDLStem lArg,
649649
protected Object doSingleApply(Object lArg, DyadicFunctionReferenceNode fNode, Object defaultValue, State state, Dyad dyad) {
650650

651651
State actualState = fNode.hasModuleState() ? fNode.getModuleState() : state; // determined per fNode
652-
/* return doSingleApply(lArg, fNode.getFunctionRecord(), fNode.getFunctionArgCount(),
653-
defaultValue,
654-
actualState, dyad);*/
655-
// }
656-
657-
658-
659-
/*protected Object doSingleApply(Object lArg,
660-
FunctionReferenceNodeInterface fNode,
661-
662-
Object defaultValue, State actualState, Dyad dyad) {
663-
*/
664-
//State actualState = fRecord.hasModuleState() ? fRecord.getModuleState() : state; // determined per fRecord
665652
FunctionRecordInterface fRecord = fNode.getFunctionRecord();
666653
boolean isBuiltin = fRecord == null;
667654
if (lArg == null) {

language/src/main/java/org/qdl_lang/evaluate/StemEvaluator.java

+110-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.qdl_lang.state.State;
1111
import org.qdl_lang.statements.ExpressionInterface;
1212
import org.qdl_lang.statements.Statement;
13+
import org.qdl_lang.util.aggregate.AxisRestrictionIdentity;
1314
import org.qdl_lang.variables.*;
1415
import edu.uiuc.ncsa.security.core.util.StringUtils;
1516
import net.sf.json.JSON;
@@ -127,6 +128,11 @@ public String getNamespace() {
127128

128129
public static final String EXCISE = "excise";
129130
public static final int EXCISE_TYPE = 116 + STEM_FUNCTION_BASE_VALUE;
131+
132+
/* public static final String MAP = "map";
133+
public static final int MAP_TYPE = 117 + STEM_FUNCTION_BASE_VALUE;*/
134+
135+
130136
public static final String IS_LIST = "is_list";
131137
public static final int IS_LIST_TYPE = 204 + STEM_FUNCTION_BASE_VALUE;
132138

@@ -162,6 +168,7 @@ public String getNamespace() {
162168
public String[] getFunctionNames() {
163169
if (fNames == null) {
164170
fNames = new String[]{
171+
// MAP,
165172
EXCISE,
166173
HAS_KEYS,
167174
DISPLAY,
@@ -202,6 +209,8 @@ public String[] getFunctionNames() {
202209
@Override
203210
public int getType(String name) {
204211
switch (name) {
212+
/* case MAP:
213+
return MAP_TYPE;*/
205214
case EXCISE:
206215
return EXCISE_TYPE;
207216
case HAS_KEYS:
@@ -281,6 +290,9 @@ public int getType(String name) {
281290
@Override
282291
public boolean dispatch(Polyad polyad, State state) {
283292
switch (polyad.getName()) {
293+
/* case MAP:
294+
doMap(polyad, state);
295+
return true;*/
284296
case EXCISE:
285297
doExcise(polyad, state);
286298
return true;
@@ -393,6 +405,7 @@ public boolean dispatch(Polyad polyad, State state) {
393405
return false;
394406
}
395407

408+
396409
/**
397410
* Removes elements from a stem by value. In general stems this is not as useful as in lists.
398411
*
@@ -836,10 +849,18 @@ protected void doIndices(Polyad polyad, State state) {
836849
if (!isStem(arg0)) {
837850
throw new BadArgException(ALL_KEYS + " requires a stem as its first argument", polyad.getArgAt(0));
838851
}
839-
QDLStem stem = (QDLStem) arg0;
840-
boolean returnAll = true;
852+
boolean hasAxisExpression = arg0 instanceof AxisExpression;
853+
QDLStem stem;
841854
long axis = 0L;
842-
if (polyad.getArgCount() == 2) {
855+
if (hasAxisExpression) {
856+
AxisExpression ae = (AxisExpression) arg0;
857+
stem = ae.getStem();
858+
axis = ae.getAxis();
859+
} else {
860+
stem = (QDLStem) arg0;
861+
}
862+
boolean returnAll = true;
863+
if (!hasAxisExpression && polyad.getArgCount() == 2) {
843864
returnAll = false;
844865
Object arg1 = polyad.evalArg(1, state);
845866
checkNull(arg1, polyad.getArgAt(1), state);
@@ -848,7 +869,7 @@ protected void doIndices(Polyad polyad, State state) {
848869
}
849870
axis = (Long) arg1;
850871
}
851-
QDLStem rc = returnAll ? stem.indices() : stem.indices(axis);
872+
QDLStem rc = returnAll ? stem.indicesByRank() : stem.indicesByRank(axis);
852873
polyad.setResult(rc);
853874
polyad.setResultType(STEM_TYPE);
854875
polyad.setEvaluated(Boolean.TRUE);
@@ -910,7 +931,7 @@ protected void doForEach(Polyad polyad, State state) {
910931
Object arg = polyad.evalArg(i, state);
911932
checkNull(arg, polyad.getArgAt(i));
912933
stems[i - 1] = arg;
913-
allScalars = allScalars && (!(arg instanceof QDLStem));
934+
allScalars = allScalars && (!isStem(arg));
914935
}
915936
ExpressionImpl f;
916937
try {
@@ -930,7 +951,7 @@ protected void doForEach(Polyad polyad, State state) {
930951
QDLStem output = new QDLStem();
931952

932953
// Fixes https://github.com/ncsa/qdl/issues/17
933-
forEachRecursion(output, f, state, stems, new IndexList(), new ArrayList(), 0);
954+
forEachRecursion2(output, f, state, stems, new IndexList(), new ArrayList(), 0);
934955
polyad.setResult(output);
935956
polyad.setResultType(STEM_TYPE);
936957
polyad.setEvaluated(true);
@@ -960,7 +981,7 @@ protected void forEachRecursion(QDLStem output,
960981
ArrayList values,
961982
int currentIndex) {
962983

963-
while (!(args[currentIndex] instanceof QDLStem)) {
984+
while (!isStem(args[currentIndex])) {
964985
values.add(args[currentIndex]); // we can add scalars to the end of this, but it will recurse on the next stem
965986
currentIndex++;
966987
if (currentIndex == args.length) {
@@ -970,7 +991,8 @@ protected void forEachRecursion(QDLStem output,
970991
}
971992
}
972993
QDLStem currentStem = (QDLStem) args[currentIndex++];
973-
ArrayList allIndices = currentStem.indices().getQDLList().getArrayList();
994+
// Next, get *every* index
995+
ArrayList allIndices = currentStem.indicesByRank().getQDLList().getArrayList();
974996
for (Object index : allIndices) {
975997
IndexList currentIndexList = new IndexList((QDLStem) index); // index looks like [0,0,1]
976998
IndexList nextIndexList = new IndexList(); // index looks like [0,0,1]
@@ -988,7 +1010,80 @@ protected void forEachRecursion(QDLStem output,
9881010
}
9891011
}
9901012
}
1013+
protected void forEachRecursion2(QDLStem output,
1014+
ExpressionImpl f,
1015+
State state,
1016+
Object[] args,
1017+
IndexList indexList,
1018+
ArrayList values,
1019+
int currentIndex) {
1020+
1021+
while (!isStem(args[currentIndex])) {
1022+
values.add(args[currentIndex]); // we can add scalars to the end of this, but it will recurse on the next stem
1023+
currentIndex++;
1024+
if (currentIndex == args.length) {
1025+
// end of the line for recursion. Evaluate
1026+
output.set(indexList, forEachEval(f, state, values));
1027+
return;
1028+
}
1029+
}
1030+
QDLStem currentStem;
1031+
Long axis = 0L;
1032+
ArrayList allIndices;
1033+
boolean isAxisExpression = args[currentIndex] instanceof AxisExpression;
1034+
1035+
if (isAxisExpression) {
1036+
AxisExpression ae = (AxisExpression) args[currentIndex];
1037+
currentStem = ae.getStem();
1038+
axis = ae.getAxis();
1039+
//allIndices = currentStem.indicesByRank(axis+1).getQDLList().getArrayList();
1040+
allIndices = currentStem.keysByAxis(axis).getQDLList().getArrayList();
1041+
} else {
1042+
currentStem = (QDLStem) args[currentIndex];
1043+
allIndices = currentStem.indicesByRank().getQDLList().getArrayList();
1044+
}
1045+
currentIndex++;
1046+
1047+
1048+
for (Object index : allIndices) {
1049+
IndexList currentIndexList = new IndexList((QDLStem) index); // index looks like [0,0,1]
1050+
IndexList nextIndexList = new IndexList(); // index looks like [0,0,1]
1051+
nextIndexList.addAll(indexList);
1052+
nextIndexList.addAll(currentIndexList);
1053+
ArrayList valuesList1 = new ArrayList();
1054+
valuesList1.addAll(values);
1055+
// valuesList1.add(currentStem.get(currentIndexList, true).get(0));
1056+
valuesList1.add(currentStem.get(index));
1057+
if (currentIndex == args.length || (isAxisExpression && args.length == axis)) {
1058+
// end of the line for recursion. Evaluate
1059+
output.set(nextIndexList, forEachEval(f, state, valuesList1));
1060+
} else {
1061+
forEachRecursion2(output, f, state, args, nextIndexList, valuesList1, currentIndex);
1062+
}
1063+
}
1064+
}
1065+
// Processor that replaces each stem at a given level with the constant "foo".
1066+
public static class ARForEachImpl extends AxisRestrictionIdentity {
1067+
public ARForEachImpl(ExpressionImpl f, State state, int axis) {
1068+
this.f = f;
1069+
this.state = state;
1070+
this.axis = axis;
1071+
}
1072+
ExpressionImpl f;
1073+
State state;
9911074

1075+
@Override
1076+
public Object getDefaultValue(List<Object> index,Object key, Object value) {
1077+
ArgList argList1 = new ArgList();
1078+
argList1.add(new ConstantNode(value));
1079+
f.setArguments(argList1);
1080+
return f.evaluate(state);
1081+
}
1082+
}
1083+
/*
1084+
f(x.)→x.0+x.1;
1085+
@f∀[n(3,4,4,[;3*4*4])|0]
1086+
*/
9921087

9931088
protected Object forEachEval(ExpressionImpl f, State state, List args) {
9941089
if (f instanceof Monad) {
@@ -2380,15 +2475,15 @@ protected void doRemove(Polyad polyad, State state) {
23802475
polyad.setResult(esn2.remove(state));
23812476
break;
23822477
case ExpressionInterface.FUNCTION_REFERENCE_NODE:
2383-
if(!isFunction){
2478+
if (!isFunction) {
23842479
throw new BadArgException(REMOVE + " requires a n argument count", polyad);
23852480
}
23862481
FunctionReferenceNode functionReferenceNode = (FunctionReferenceNode) polyad.getArgAt(0);
2387-
if(argCount == -1){
2388-
for(FunctionRecordInterface fr : functionReferenceNode.getFunctionRecords()){
2389-
state.getFTStack().remove(new FKey(functionReferenceNode.getFunctionName(), fr.getArgCount()));
2390-
}
2391-
}else {
2482+
if (argCount == -1) {
2483+
for (FunctionRecordInterface fr : functionReferenceNode.getFunctionRecords()) {
2484+
state.getFTStack().remove(new FKey(functionReferenceNode.getFunctionName(), fr.getArgCount()));
2485+
}
2486+
} else {
23922487
state.getFTStack().remove(new FKey(functionReferenceNode.getFunctionName(), argCount.intValue()));
23932488
}
23942489
polyad.setResult(Boolean.TRUE);
@@ -3017,7 +3112,7 @@ protected void doTransform(Polyad polyad, State state) {
30173112
}
30183113
QDLStem stem = (QDLStem) arg0;
30193114

3020-
QDLStem oldIndices = stem.indices(-1L);
3115+
QDLStem oldIndices = stem.indicesByRank(-1L);
30213116
// kludge, assume that the rank of all at the last axis is the same.
30223117
int rank = ((QDLStem) oldIndices.get(0L)).size();
30233118

language/src/main/java/org/qdl_lang/evaluate/SystemEvaluator.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,18 @@ private void doReduceOrExpand(Polyad polyad, State state0, boolean doReduce) {
995995
if (!doReduce && !isStem(arg1)) {
996996
throw new BadArgException(EXPAND + " requires a list as its argument", polyad.getArgAt(1));
997997
}
998-
QDLStem stemVariable = (QDLStem) arg1;
998+
int axis = 0; // default
999+
boolean hasAxisExpression = false;
1000+
QDLStem stemVariable;
1001+
if(arg1 instanceof AxisExpression) {
1002+
hasAxisExpression = true;
1003+
AxisExpression ax = (AxisExpression) arg1;
1004+
axis = ax.getAxis().intValue();
1005+
stemVariable = ax.getStem();
1006+
1007+
}else{
1008+
stemVariable = (QDLStem) arg1;
1009+
}
9991010

10001011
if (!doReduce && !stemVariable.isList()) {
10011012
throw new BadArgException(EXPAND + " requires a list as its argument", polyad.getArgAt(1));
@@ -1033,8 +1044,7 @@ private void doReduceOrExpand(Polyad polyad, State state0, boolean doReduce) {
10331044
return;
10341045
}
10351046

1036-
int axis = 0; // default
1037-
if (polyad.getArgCount() == 3) {
1047+
if (!hasAxisExpression && polyad.getArgCount() == 3) {
10381048
Object axisObj = polyad.evalArg(2, state);
10391049
checkNull(axisObj, polyad.getArgAt(2));
10401050
if (!isLong(axisObj)) {

0 commit comments

Comments
 (0)