Skip to content

Commit

Permalink
Fixed #58 - array slice operator
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborsch committed Jun 23, 2024
1 parent 93feacb commit 4126ca2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
18 changes: 18 additions & 0 deletions programs/tests/fixtures/Rocky_ext/array_ext/array_slicing.rock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cut "ABCDEFGHIJ" into letters

sliced = letters till 3
join sliced
say sliced

sliced = letters since 3
join sliced
say sliced

sliced = letters since 3 till 6
join sliced
say sliced

sliced = letters till 8 since 6
join sliced
say sliced

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ABC
DEFGHIJ
DEF
GH
Binary file modified rocky.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions src/rockstar/expression/SliceExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Type getType() {

@Override
public String getFormat() {
return "(%s" + (type != Type.SLICE_TO ? " from %s" : "") + (type != Type.SLICE_FROM ? " till %s" : "") + ")";
return "(%s" + (type != Type.SLICE_TO ? " since %s" : "") + (type != Type.SLICE_FROM ? " till %s" : "") + ")";
}

@Override
Expand Down Expand Up @@ -79,10 +79,10 @@ public Value evaluate(BlockContext ctx) {
Value retValue = Value.MYSTERIOUS;
if (baseVal.isArray()) {
List<Value> baseList = baseVal.asListArray();
List<Value> newList = baseList.subList(fromVal == null ? 0 : fromVal, toVal == null ? baseList.size() : toVal + 1);
List<Value> newList = baseList.subList(fromVal == null ? 0 : fromVal, toVal == null ? baseList.size() : toVal);
retValue = Value.getValue(newList);
} else {
throw new RockstarRuntimeException("Invalid argument for from/till operation: " + baseVal.getType());
throw new RockstarRuntimeException("Invalid argument for since/till operation: " + baseVal.getType());
}

return ctx.afterExpression(this, retValue);
Expand Down
2 changes: 1 addition & 1 deletion src/rockstar/parser/ExpressionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public CompoundExpression getOperator(boolean isAfterOperator) {
next();
return (CompoundExpression) new RollExpression().withTokens(list, startIdx, idx);
}
if (checkCurrent(Keyword.FROM)) {
if (checkCurrent(Keyword.SINCE)) {
next();
return (CompoundExpression) new SliceExpression(SliceExpression.Type.SLICE_FROM).withTokens(list, startIdx,
idx);
Expand Down
2 changes: 1 addition & 1 deletion src/rockstar/parser/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum Keyword {
ROCK(),
ROLL("roll", "pop"),
INTO("into", "in"),
FROM("+from"),
SINCE("+since"),
TILL("+till"),
TAKING("taking"),
SORTED("+sorted"),
Expand Down

0 comments on commit 4126ca2

Please sign in to comment.