Skip to content

Commit bb08a81

Browse files
committed
Bug fixes, sets and dicts respect overloaded equals and hashes, new methods
1 parent d04102b commit bb08a81

31 files changed

+731
-271
lines changed

run/primes.dssl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ $math include
77
# Check divisors up to sqrt(n)
88
/isPrime {
99
/n exch def
10-
n 2 < { false } { { n exch % 0 == } ( 2 n isqrt 1 + ) Iter new .filter .count 0 == } ifelse
10+
n 2 < { false } { { n exch % 0 == } ( 2 n isqrt 1 + ) .iter .filter .count 0 == } ifelse
1111
} macro
1212

1313
/primes {
1414
/n exch def
15-
{ isPrime } ( 1 1 n + ) Iter new .filter
15+
{ isPrime } ( 1 1 n + ) .iter .filter
1616
} macro
1717

1818
# Print primes up to 100

src/dssl/NativeImpl.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static TokenResult tryField(TokenExecutor exec, Class<?> clazz, String member, b
8585
if (isInst) {
8686
exec.pop();
8787
}
88-
exec.push(convert(exec.interpreter, result));
88+
exec.push(convert(exec, result));
8989
return TokenResult.PASS;
9090
}
9191
catch (Exception e) {
@@ -130,7 +130,7 @@ static TokenResult tryExecutable(TokenExecutor exec, Class<?> clazz, List<Execut
130130
}
131131
exec.pop(count);
132132
if (result != null) {
133-
exec.push(convert(exec.interpreter, result));
133+
exec.push(convert(exec, result));
134134
}
135135
return TokenResult.PASS;
136136
}
@@ -291,19 +291,19 @@ static <T> T[] newArray(Type componentType) {
291291
return (T[]) Array.newInstance(rawType(componentType), 0);
292292
}
293293

294-
static <T extends Collection<Object>> T collection_nativize(Collection<@NonNull Element> collection, Type type, Tracker tracker, Collector<Object, ?, T> collector) {
295-
return collection.stream().map(x -> trackedNativize(x, type, tracker)).collect(collector);
294+
static <T extends Collection<Object>> T collection_nativize(Stream<@NonNull Element> stream, Type type, Tracker tracker, Collector<Object, ?, T> collector) {
295+
return stream.map(x -> trackedNativize(x, type, tracker)).collect(collector);
296296
}
297297

298298
static Object listNativize(@NonNull Element elem, Type type) {
299299
Tracker tracker = new Tracker();
300-
List<?> obj = collection_nativize(((ListElement) elem).value, type, tracker, Collectors.toList());
300+
List<?> obj = collection_nativize(((ListElement) elem).value.stream(), type, tracker, Collectors.toList());
301301
return tracker.flag ? null : obj;
302302
}
303303

304304
static Object arrayNativize(@NonNull Element elem, Type type) {
305305
Tracker tracker = new Tracker();
306-
List<?> obj = collection_nativize(((ListElement) elem).value, type, tracker, Collectors.toList());
306+
List<?> obj = collection_nativize(((ListElement) elem).value.stream(), type, tracker, Collectors.toList());
307307
if (tracker.flag) {
308308
return null;
309309
}
@@ -373,13 +373,13 @@ else if (type.equals(char.class)) {
373373

374374
static Object setNativize(@NonNull Element elem, Type type) {
375375
Tracker tracker = new Tracker();
376-
Set<?> obj = collection_nativize(((SetElement) elem).value, type, tracker, Collectors.toSet());
376+
Set<?> obj = collection_nativize(((SetElement) elem).value.stream().map(x -> x.elem), type, tracker, Collectors.toSet());
377377
return tracker.flag ? null : obj;
378378
}
379379

380380
static Object mapNativize(@NonNull Element elem, Type keyType, Type valueType) {
381381
Tracker tracker = new Tracker();
382-
Map<?, ?> obj = Helpers.map(((DictElement) elem).value, x -> trackedNativize(x, keyType, tracker), x -> trackedNativize(x, valueType, tracker));
382+
Map<?, ?> obj = Helpers.map(((DictElement) elem).value, x -> trackedNativize(x.elem, keyType, tracker), x -> trackedNativize(x, valueType, tracker));
383383
return tracker.flag ? null : obj;
384384
}
385385

@@ -400,55 +400,54 @@ static Object trackedNativize(@NonNull Element elem, Type type, Tracker tracker)
400400
return tracked(elem, y -> nativize(y, type), tracker);
401401
}
402402

403-
static @NonNull Element convert(Interpreter interpreter, Object obj) {
403+
static @NonNull Element convert(TokenExecutor exec, Object obj) {
404404
if (obj == null) {
405-
return interpreter.builtIn.nullElement;
405+
return exec.interpreter.builtIn.nullElement;
406406
}
407407
else if (obj instanceof Byte) {
408-
return new IntElement(interpreter, (Byte) obj);
408+
return new IntElement(exec.interpreter, (Byte) obj);
409409
}
410410
else if (obj instanceof Short) {
411-
return new IntElement(interpreter, (Short) obj);
411+
return new IntElement(exec.interpreter, (Short) obj);
412412
}
413413
else if (obj instanceof Integer) {
414-
return new IntElement(interpreter, (Integer) obj);
414+
return new IntElement(exec.interpreter, (Integer) obj);
415415
}
416416
else if (obj instanceof Long) {
417-
return new IntElement(interpreter, (Long) obj);
417+
return new IntElement(exec.interpreter, (Long) obj);
418418
}
419419
else if (obj instanceof BigInteger) {
420-
return new IntElement(interpreter, (BigInteger) obj);
420+
return new IntElement(exec.interpreter, (BigInteger) obj);
421421
}
422422
else if (obj instanceof Boolean) {
423-
return new BoolElement(interpreter, (Boolean) obj);
423+
return new BoolElement(exec.interpreter, (Boolean) obj);
424424
}
425425
else if (obj instanceof Float) {
426-
return new FloatElement(interpreter, ((Float) obj).doubleValue());
426+
return new FloatElement(exec.interpreter, ((Float) obj).doubleValue());
427427
}
428428
else if (obj instanceof Double) {
429-
return new FloatElement(interpreter, (Double) obj);
429+
return new FloatElement(exec.interpreter, (Double) obj);
430430
}
431431
else if (obj instanceof Character) {
432-
return new CharElement(interpreter, (Character) obj);
432+
return new CharElement(exec.interpreter, (Character) obj);
433433
}
434434
else if (obj instanceof String) {
435-
return new StringElement(interpreter, (String) obj);
435+
return new StringElement(exec.interpreter, (String) obj);
436436
}
437437
else if (obj instanceof List) {
438-
return new ListElement(interpreter, ((List<?>) obj).stream().map(x -> convert(interpreter, x)));
438+
return new ListElement(exec.interpreter, ((List<?>) obj).stream().map(x -> convert(exec, x)));
439439
}
440440
else if (obj.getClass().isArray()) {
441-
return new ListElement(interpreter, IntStream.range(0, Array.getLength(obj)).mapToObj(x -> NativeImpl.convert(interpreter, Array.get(obj, x))));
441+
return new ListElement(exec.interpreter, IntStream.range(0, Array.getLength(obj)).mapToObj(x -> NativeImpl.convert(exec, Array.get(obj, x))));
442442
}
443443
else if (obj instanceof Set) {
444-
return new SetElement(interpreter, Helpers.map((Set<?>) obj, x -> convert(interpreter, x)));
444+
return new SetElement(exec.interpreter, Helpers.map((Set<?>) obj, x -> convert(exec, x).toKey(exec)));
445445
}
446446
else if (obj instanceof Map) {
447-
Function<Object, @NonNull Element> convert = x -> convert(interpreter, x);
448-
return new DictElement(interpreter, Helpers.map((Map<?, ?>) obj, convert, convert), false);
447+
return new DictElement(exec.interpreter, Helpers.map((Map<?, ?>) obj, x -> convert(exec, x).toKey(exec), x -> convert(exec, x)));
449448
}
450449
else {
451-
return new NativeElement(interpreter, obj);
450+
return new NativeElement(exec.interpreter, obj);
452451
}
453452
}
454453
}

src/dssl/interpret/BuiltIn.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ protected void init() {
255255
@Override
256256
public @NonNull TokenResult instantiate(TokenExecutor exec) {
257257
@NonNull Element elem = exec.pop();
258-
TokenResult result = elem.memberAction(exec, "__iter__");
258+
TokenResult result = elem.memberAction(exec, "iter", false);
259259
if (result == null) {
260260
throw new IllegalArgumentException(String.format("Constructor for type \"%s\" requires %s element as argument!", ITER, ITERABLE));
261261
}
@@ -413,8 +413,8 @@ protected void init() {
413413
}
414414

415415
for (Clazz clazz : Arrays.asList(iterableClazz)) {
416-
clazz.setMacro("__iter__", x -> {
417-
x.push(x.pop().__iter__(x));
416+
clazz.setMacro("iter", x -> {
417+
x.push(x.pop().iter(x));
418418
return TokenResult.PASS;
419419
});
420420

@@ -474,6 +474,18 @@ protected void init() {
474474
x.push(x.pop().last(x));
475475
return TokenResult.PASS;
476476
});
477+
478+
clazz.setMacro("indexOf", x -> {
479+
x.push(x.pop().indexOf(x, x.pop()));
480+
return TokenResult.PASS;
481+
});
482+
}
483+
484+
for (Clazz clazz : Arrays.asList(stringClazz, listClazz)) {
485+
clazz.setMacro("lastIndexOf", x -> {
486+
x.push(x.pop().lastIndexOf(x, x.pop()));
487+
return TokenResult.PASS;
488+
});
477489
}
478490

479491
for (Clazz clazz : Arrays.asList(stringClazz)) {

src/dssl/interpret/Clazz.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ protected <K, V> Hierarchy<K, V> getHierarchy(@Nullable HierarchicalScope base,
6868

6969
@Override
7070
public boolean canShadow() {
71-
return type.canExtend();
71+
return type.canModify();
7272
}
7373

7474
@Override
7575
public boolean canDelete() {
76-
return type.canExtend();
76+
return type.canModify();
7777
}
7878

7979
@Override

src/dssl/interpret/ClazzType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public boolean canExtend() {
1010
return equals(STANDARD);
1111
}
1212

13+
public boolean canModify() {
14+
return equals(STANDARD);
15+
}
16+
1317
public boolean canInstantiate() {
1418
return !equals(INTERNAL);
1519
}

src/dssl/interpret/ElementKey.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dssl.interpret;
2+
3+
import org.eclipse.jdt.annotation.NonNull;
4+
5+
import dssl.interpret.element.Element;
6+
7+
public class ElementKey {
8+
9+
public final TokenExecutor exec;
10+
public final @NonNull Element elem;
11+
12+
public ElementKey(TokenExecutor exec, @NonNull Element elem) {
13+
this.exec = exec;
14+
this.elem = elem;
15+
}
16+
17+
@Override
18+
public @NonNull ElementKey clone() {
19+
return elem.dynClone(exec).toKey(exec);
20+
}
21+
22+
@Override
23+
public int hashCode() {
24+
return elem.dynHash(exec);
25+
}
26+
27+
@Override
28+
public boolean equals(Object obj) {
29+
if (obj instanceof Element) {
30+
return elem.dynEqualTo(exec, (Element) obj);
31+
}
32+
else if (obj instanceof ElementKey) {
33+
return elem.dynEqualTo(exec, ((ElementKey) obj).elem);
34+
}
35+
else {
36+
return false;
37+
}
38+
}
39+
40+
@Override
41+
public @NonNull String toString() {
42+
return elem.stringCast(exec).toString(exec);
43+
}
44+
}

src/dssl/interpret/HierarchicalScope.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ public default Def getDef(@NonNull String identifier) {
2020
return getDefHierarchy().get(identifier, false);
2121
}
2222

23-
public default Def setDef(@NonNull String identifier, @NonNull Def def, boolean shadow) {
23+
public default void setDef(@NonNull String identifier, @NonNull Def def, boolean shadow) {
2424
if (shadow) {
2525
checkCollision(identifier);
2626
}
27-
return getDefHierarchy().put(identifier, def, shadow);
27+
getDefHierarchy().put(identifier, def, shadow);
2828
}
2929

3030
@Override
31-
public default Def setDef(@NonNull String identifier, @NonNull Element value, boolean shadow) {
32-
return setDef(identifier, new Def(identifier, value), shadow);
31+
public default void setDef(@NonNull String identifier, @NonNull Element value, boolean shadow) {
32+
setDef(identifier, new Def(identifier, value), shadow);
3333
}
3434

3535
@Override
@@ -47,14 +47,14 @@ public default Macro getMacro(@NonNull String identifier) {
4747
return getMacroHierarchy().get(identifier, false);
4848
}
4949

50-
public default Macro setMacro(@NonNull String identifier, @NonNull Macro macro, boolean shadow) {
50+
public default void setMacro(@NonNull String identifier, @NonNull Macro macro, boolean shadow) {
5151
checkCollision(identifier);
52-
return getMacroHierarchy().put(identifier, macro, shadow);
52+
getMacroHierarchy().put(identifier, macro, shadow);
5353
}
5454

5555
@Override
56-
public default Macro setMacro(@NonNull String identifier, @NonNull Invokable invokable) {
57-
return setMacro(identifier, new Macro(identifier, invokable), true);
56+
public default void setMacro(@NonNull String identifier, @NonNull Invokable invokable) {
57+
setMacro(identifier, new Macro(identifier, invokable), true);
5858
}
5959

6060
@Override
@@ -72,14 +72,14 @@ public default Clazz getClazz(@NonNull String shallowIdentifier) {
7272
return getClazzHierarchy().get(shallowIdentifier, false);
7373
}
7474

75-
public default Clazz setClazz(@NonNull String shallowIdentifier, @NonNull Clazz clazz, boolean shadow) {
75+
public default void setClazz(@NonNull String shallowIdentifier, @NonNull Clazz clazz, boolean shadow) {
7676
checkCollision(shallowIdentifier);
77-
return getClazzHierarchy().put(shallowIdentifier, clazz, shadow);
77+
getClazzHierarchy().put(shallowIdentifier, clazz, shadow);
7878
}
7979

8080
@Override
81-
public default Clazz setClazz(Interpreter interpreter, @NonNull String shallowIdentifier, @NonNull ClazzType type, @Nullable HierarchicalScope base, @NonNull ArrayList<Clazz> supers) {
82-
return setClazz(shallowIdentifier, new Clazz(interpreter, scopeIdentifier(), shallowIdentifier, type, base, supers), true);
81+
public default void setClazz(Interpreter interpreter, @NonNull String shallowIdentifier, @NonNull ClazzType type, @Nullable HierarchicalScope base, @NonNull ArrayList<Clazz> supers) {
82+
setClazz(shallowIdentifier, new Clazz(interpreter, scopeIdentifier(), shallowIdentifier, type, base, supers), true);
8383
}
8484

8585
@Override
@@ -93,15 +93,15 @@ public default Clazz removeClazz(@NonNull String shallowIdentifier) {
9393

9494
public Hierarchy<@NonNull String, Clazz> getClazzHierarchy();
9595

96-
public default <T> void addToScopeMap(Interpreter interpreter, Hierarchy<@NonNull String, T> source, Map<@NonNull Element, @NonNull Element> target) {
97-
source.forEach((k, v) -> target.put(new StringElement(interpreter, k), new LabelElement(interpreter, this, k)), false);
96+
public default <T> void addToScopeMap(TokenExecutor exec, Hierarchy<@NonNull String, T> source, Map<@NonNull ElementKey, @NonNull Element> target) {
97+
source.forEach((k, v) -> target.put(new StringElement(exec.interpreter, k).toKey(exec), new LabelElement(exec.interpreter, this, k)), false);
9898
}
9999

100100
@Override
101-
public default void addToScopeMap(TokenExecutor exec, @NonNull Map<@NonNull Element, @NonNull Element> map) {
102-
addToScopeMap(exec.interpreter, getDefHierarchy(), map);
103-
addToScopeMap(exec.interpreter, getMacroHierarchy(), map);
104-
addToScopeMap(exec.interpreter, getClazzHierarchy(), map);
101+
public default void addToScopeMap(TokenExecutor exec, @NonNull Map<@NonNull ElementKey, @NonNull Element> map) {
102+
addToScopeMap(exec, getDefHierarchy(), map);
103+
addToScopeMap(exec, getMacroHierarchy(), map);
104+
addToScopeMap(exec, getClazzHierarchy(), map);
105105
}
106106

107107
@SuppressWarnings("null")

src/dssl/interpret/Scope.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ else if (hasClazz(identifier, true)) {
3939

4040
public Def getDef(@NonNull String identifier);
4141

42-
public Def setDef(@NonNull String identifier, @NonNull Element value, boolean shadow);
42+
public void setDef(@NonNull String identifier, @NonNull Element value, boolean shadow);
4343

4444
public Def removeDef(@NonNull String identifier);
4545

4646
public boolean hasMacro(@NonNull String identifier, boolean shallow);
4747

4848
public Macro getMacro(@NonNull String identifier);
4949

50-
public Macro setMacro(@NonNull String identifier, @NonNull Invokable invokable);
50+
public void setMacro(@NonNull String identifier, @NonNull Invokable invokable);
5151

5252
public Macro removeMacro(@NonNull String identifier);
5353

5454
public boolean hasClazz(@NonNull String shallowIdentifier, boolean shallow);
5555

5656
public Clazz getClazz(@NonNull String shallowIdentifier);
5757

58-
public Clazz setClazz(Interpreter interpreter, @NonNull String shallowIdentifier, @NonNull ClazzType type, @Nullable HierarchicalScope base, @NonNull ArrayList<Clazz> supers);
58+
public void setClazz(Interpreter interpreter, @NonNull String shallowIdentifier, @NonNull ClazzType type, @Nullable HierarchicalScope base, @NonNull ArrayList<Clazz> supers);
5959

6060
public Clazz removeClazz(@NonNull String shallowIdentifier);
6161

62-
public void addToScopeMap(TokenExecutor exec, @NonNull Map<@NonNull Element, @NonNull Element> map);
62+
public void addToScopeMap(TokenExecutor exec, @NonNull Map<@NonNull ElementKey, @NonNull Element> map);
6363
}

0 commit comments

Comments
 (0)