Skip to content

Commit c0e2e9f

Browse files
committed
More bug fixes
1 parent 6a37214 commit c0e2e9f

5 files changed

Lines changed: 46 additions & 24 deletions

File tree

src/dssl/interpret/BuiltIn.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ protected void init() {
10001000
KEYWORDS.add("dup");
10011001

10021002
KEYWORDS.add("stacksize");
1003+
KEYWORDS.add("stackindex");
10031004

10041005
KEYWORDS.add("read");
10051006
KEYWORDS.add("print");

src/dssl/interpret/element/DictElement.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void putAll(TokenExecutor exec, @NonNull Element elem) {
130130

131131
@Override
132132
public void removeEntry(TokenExecutor exec, @NonNull Element elem0, @NonNull Element elem1) {
133-
value.remove(elem0, elem1);
133+
value.remove(elem0.toKey(exec), elem1);
134134
}
135135

136136
@Override
@@ -140,13 +140,18 @@ public boolean containsKey(TokenExecutor exec, @NonNull Element elem) {
140140

141141
@Override
142142
public boolean containsValue(TokenExecutor exec, @NonNull Element elem) {
143-
return value.containsValue(elem);
143+
for (@NonNull Element e : value.values()) {
144+
if (elem.dynEqualTo(exec, e)) {
145+
return true;
146+
}
147+
}
148+
return false;
144149
}
145150

146151
@Override
147152
public boolean containsEntry(TokenExecutor exec, @NonNull Element elem0, @NonNull Element elem1) {
148153
@SuppressWarnings("null") Element get = value.get(elem0.toKey(exec));
149-
return get == null ? false : get.equals(elem1);
154+
return get == null ? false : get.dynEqualTo(exec, elem1);
150155
}
151156

152157
@Override
@@ -177,7 +182,7 @@ public boolean containsEntry(TokenExecutor exec, @NonNull Element elem0, @NonNul
177182
@SuppressWarnings("null")
178183
@Override
179184
public int hash(TokenExecutor exec) {
180-
int hash = BuiltIn.LIST.hashCode();
185+
int hash = BuiltIn.DICT.hashCode();
181186
for (Entry<@NonNull ElementKey, @NonNull Element> entry : value.entrySet()) {
182187
hash = 31 * hash + (entry.getKey().hashCode() ^ entry.getValue().dynHash(exec));
183188
}

src/dssl/interpret/element/RangeElement.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,22 @@ else if (elemCount == 2) {
5151
step = args[2];
5252
}
5353

54-
if (step.equals(BigInteger.ZERO)) {
54+
int stepSignum = step.signum();
55+
if (stepSignum == 0) {
5556
throw new IllegalArgumentException(String.format("Range element constructed with zero step size!"));
5657
}
5758

5859
BigInteger diff = stop.subtract(start);
59-
int diffComp = diff.compareTo(BigInteger.ZERO), stepComp = step.compareTo(BigInteger.ZERO);
60-
if ((diffComp > 0 && stepComp < 0) || (diffComp < 0 && stepComp > 0)) {
60+
int diffSignum = diff.signum();
61+
if ((stepSignum < 0 && diffSignum > 0) || (stepSignum > 0 && diffSignum < 0)) {
6162
throw new IllegalArgumentException(String.format("Range element constructed with invalid arguments start = %s, stop = %s, step = %s!", start, stop, step));
6263
}
6364

6465
this.start = start;
6566
this.stop = stop;
6667
this.step = step;
6768

68-
size = diff.abs().subtract(BigInteger.ONE).divide(step.abs()).add(BigInteger.ONE).longValueExact();
69+
size = diffSignum == 0 ? 0L : diff.abs().subtract(BigInteger.ONE).divide(step.abs()).add(BigInteger.ONE).longValueExact();
6970
}
7071

7172
public RangeElement(Interpreter interpreter, @NonNull BigInteger start, @NonNull BigInteger stop, @NonNull BigInteger step, long size) {
@@ -154,10 +155,17 @@ public boolean contains(TokenExecutor exec, @NonNull Element elem) {
154155
}
155156

156157
@NonNull BigInteger intValue = intElem.value.raw;
157-
if (intValue.compareTo(start) < 0 || intValue.compareTo(stop) >= 0) {
158-
return false;
158+
if (step.signum() > 0) {
159+
if (intValue.compareTo(start) < 0 || intValue.compareTo(stop) >= 0) {
160+
return false;
161+
}
159162
}
160-
return intValue.subtract(start).mod(step).equals(BigInteger.ZERO);
163+
else {
164+
if (intValue.compareTo(start) > 0 || intValue.compareTo(stop) <= 0) {
165+
return false;
166+
}
167+
}
168+
return intValue.subtract(start).remainder(step).equals(BigInteger.ZERO);
161169
}
162170

163171
@Override
@@ -219,8 +227,15 @@ public boolean containsAll(TokenExecutor exec, @NonNull Element elem) {
219227
}
220228

221229
@NonNull BigInteger intValue = intElem.value.raw;
222-
if (intValue.compareTo(start) < 0 || intValue.compareTo(stop) >= 0) {
223-
return interpreter.builtIn.nullElement;
230+
if (step.signum() > 0) {
231+
if (intValue.compareTo(start) < 0 || intValue.compareTo(stop) >= 0) {
232+
return interpreter.builtIn.nullElement;
233+
}
234+
}
235+
else {
236+
if (intValue.compareTo(start) > 0 || intValue.compareTo(stop) <= 0) {
237+
return interpreter.builtIn.nullElement;
238+
}
224239
}
225240

226241
BigInteger[] divRem = intValue.subtract(start).divideAndRemainder(step);
@@ -232,7 +247,7 @@ protected BigInteger at(long index) {
232247
}
233248

234249
protected void methodIndexBoundsCheck(long index, String name) {
235-
if (index >= size) {
250+
if (index < 0 || index >= size) {
236251
throw new IndexOutOfBoundsException(String.format("Built-in method \"%s\" (index: %s, size: %s)", name, index, size));
237252
}
238253
}
@@ -255,7 +270,7 @@ public int hashCode() {
255270
@Override
256271
public boolean equals(Object obj) {
257272
if (obj instanceof RangeElement other) {
258-
return start == other.start && stop == other.stop && step == other.step;
273+
return start.equals(other.start) && stop.equals(other.stop) && step.equals(other.step);
259274
}
260275
return false;
261276
}

src/dssl/interpret/element/iter/ChunksIterElement.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class ChunksIterElement extends IterElement {
1111
protected final long size;
1212

1313
protected @Nullable ListElement next = null;
14-
protected boolean end = false;
1514

1615
public ChunksIterElement(Interpreter interpreter, IterElement internal, long size) {
1716
super(interpreter);
@@ -20,24 +19,26 @@ public ChunksIterElement(Interpreter interpreter, IterElement internal, long siz
2019
}
2120

2221
protected void prepare(TokenExecutor exec) {
23-
if (next == null && !end) {
22+
if (next == null) {
2423
long count = 0;
2524
while (internal.hasNext(exec)) {
26-
if (count++ >= size) {
25+
if (next == null) {
2726
next = new ListElement(interpreter);
28-
return;
2927
}
30-
next.value.add(internal.next(exec));
28+
if (count++ < size) {
29+
next.value.add(internal.next(exec));
30+
}
31+
else {
32+
break;
33+
}
3134
}
32-
33-
end = true;
3435
}
3536
}
3637

3738
@Override
3839
public boolean hasNext(TokenExecutor exec) {
3940
prepare(exec);
40-
return !end;
41+
return next != null;
4142
}
4243

4344
@SuppressWarnings("null")

src/dssl/interpret/element/primitive/StringElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public StringElement(Interpreter interpreter, @NonNull String rawValue) {
3535

3636
@Override
3737
public @NonNull TokenResult onConcat(TokenExecutor exec, @NonNull Element other) {
38-
exec.push(new StringElement(interpreter, toString(exec) + other.stringCast(exec)));
38+
exec.push(new StringElement(interpreter, toString(exec) + other.stringCast(exec).toString(exec)));
3939
return TokenResult.PASS;
4040
}
4141

0 commit comments

Comments
 (0)