Skip to content

Commit

Permalink
set and interval iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Sep 4, 2023
1 parent c7ce35b commit 022ad3c
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 24 deletions.
22 changes: 22 additions & 0 deletions src/main/java/leekscript/common/IntervalType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package leekscript.common;

public class IntervalType extends Type {


public IntervalType() {
super("interval", "t", "IntervalLeekValue", "IntervalLeekValue", "new IntervalLeekValue()");
}

@Override
public Type element() {
return Type.INT;
}

public boolean canBeIterable() {
return true;
}

public boolean isIterable() {
return true;
}
}
4 changes: 0 additions & 4 deletions src/main/java/leekscript/common/SetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ public Type element() {
return type;
}

public boolean isSet() {
return true;
}

public boolean canBeIterable() {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/leekscript/common/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Type {
// public static final Type LEGACY_ARRAY = new LegacyArrayType();
public static final Type ARRAY = array(Type.ANY);
public static final Type SET = set(Type.ANY);
public static final Type INTERVAL = new Type("interval", "t", "IntervalLeekValue", "IntervalLeekValue", "new IntervalLeekValue()");
public static final Type INTERVAL = new IntervalType();
public static final Type ARRAY_INT = array(Type.INT);
public static final Type ARRAY_REAL = array(Type.REAL);
public static final Type ARRAY_STRING = array(Type.STRING);
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/leekscript/runner/AI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1500,11 +1500,11 @@ public Object toJSON(Object v, HashSet<Object> visited) throws LeekRunException
}

public boolean isPrimitive(Object value) {
return !(value instanceof ArrayLeekValue || value instanceof MapLeekValue || value instanceof LegacyArrayLeekValue || value instanceof IntervalLeekValue || value instanceof ObjectLeekValue || value instanceof NativeObjectLeekValue);
return !(value instanceof ArrayLeekValue || value instanceof MapLeekValue || value instanceof LegacyArrayLeekValue || value instanceof SetLeekValue || value instanceof IntervalLeekValue || value instanceof ObjectLeekValue || value instanceof NativeObjectLeekValue);
}

public boolean isIterable(Object value) throws LeekRunException {
boolean ok = value instanceof LegacyArrayLeekValue || value instanceof ArrayLeekValue || value instanceof MapLeekValue || value instanceof IntervalLeekValue;
boolean ok = value instanceof LegacyArrayLeekValue || value instanceof ArrayLeekValue || value instanceof MapLeekValue || value instanceof SetLeekValue || value instanceof IntervalLeekValue;
if (!ok && version >= 2) {
addSystemLog(AILog.ERROR, Error.NOT_ITERABLE, new Object[] { value });
}
Expand All @@ -1518,9 +1518,10 @@ public Iterator<Entry<Object, Object>> iterator(Object value) {
return ((ArrayLeekValue) value).genericIterator();
} else if (value instanceof MapLeekValue) {
return ((MapLeekValue) value).entrySet().iterator();
} else if (value instanceof IntervalLeekValue) {
// TODO
return null;
} else if (value instanceof SetLeekValue set) {
return set.genericIterator();
} else if (value instanceof IntervalLeekValue interval) {
return interval.iterator();
}
return null;
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/leekscript/runner/values/IntervalLeekValue.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package leekscript.runner.values;

import java.util.AbstractMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

import leekscript.AILog;
import leekscript.common.Error;
Expand All @@ -9,6 +12,31 @@

public class IntervalLeekValue {

public static class IntervalIterator implements Iterator<Entry<Object, Object>> {

private IntervalLeekValue interval;
private long i = 0;
private long x;

public IntervalIterator(IntervalLeekValue interval) {
this.interval = interval;
this.x = (long) interval.from;
}

@Override
public boolean hasNext() {
return x < interval.to;
}

@Override
public Entry<Object, Object> next() {
var e = new AbstractMap.SimpleEntry<Object, Object>(i, x);
i++;
x++;
return e;
}
}

private final AI ai;
public final int id;

Expand Down Expand Up @@ -191,4 +219,8 @@ public ArrayLeekValue range(AI ai, Object start, Object end, double step) throws

return array;
}

public Iterator<Entry<Object, Object>> iterator() {
return new IntervalIterator(this);
}
}
54 changes: 40 additions & 14 deletions src/main/java/leekscript/runner/values/SetLeekValue.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
package leekscript.runner.values;

import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import leekscript.runner.AI;
import leekscript.runner.LeekRunException;

public class SetLeekValue {
public class SetLeekValue extends HashSet<Object> {

public static class SetIterator implements Iterator<Entry<Object, Object>> {

private Iterator<Object> it;
private long i = 0;

public SetIterator(SetLeekValue set) {
this.it = set.iterator();
}

@Override
public boolean hasNext() {
return it.hasNext();
}

@Override
public Entry<Object, Object> next() {
var e = new AbstractMap.SimpleEntry<Object, Object>(i, it.next());
i++;
return e;
}
}

private final AI ai;
public final int id;

private final HashSet<Object> set;

public SetLeekValue(AI ai, Object[] values) throws LeekRunException {
this.ai = ai;
this.id = ai.getNextObjectID();
this.set = new HashSet<Object>();
for (Object value : values) {
this.set.add(value);
this.add(value);
}
}

Expand Down Expand Up @@ -48,7 +70,7 @@ public String toString(AI ai, Set<Object> visited) throws LeekRunException {

boolean first = true;

for (Object value : set) {
for (var value : this) {
if (first) {
first = false;
} else {
Expand All @@ -69,17 +91,17 @@ public String toString(AI ai, Set<Object> visited) throws LeekRunException {
}

public Object setPut(AI ai, Object value) throws LeekRunException {
set.add(value);
add(value);
return value;
}

public Object setRemove(AI ai, Object value) throws LeekRunException {
set.remove(value);
remove(value);
return value;
}

public SetLeekValue setClear(AI ai) throws LeekRunException {
set.clear();
clear();
return this;
}

Expand All @@ -88,19 +110,23 @@ public boolean setContains(AI ai, Object value) throws LeekRunException {
}

public boolean operatorIn(Object value) throws LeekRunException {
return set.contains(value);
return contains(value);
}

public long setSize(AI ai) throws LeekRunException {
return set.size();
return size();
}

public boolean setIsEmpty(AI ai) throws LeekRunException {
return set.isEmpty();
return isEmpty();
}

public boolean setIsSubsetOf(AI ai, SetLeekValue set) throws LeekRunException {
ai.ops(this.set.size() * 2);
return set.set.containsAll(this.set);
ai.ops(this.size() * 2);
return set.containsAll(this);
}

public Iterator<Entry<Object, Object>> genericIterator() {
return new SetIterator(this);
}
}
3 changes: 3 additions & 0 deletions src/test/java/test/TestInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,8 @@ public void run() throws Exception {
code_v4_("return [1..4][:-1:];").equals("[1.0, 2.0, 3.0]");
code_v4_("return [1..4][2::];").equals("[3.0, 4.0]");
code_v4_("return [1..4][:2:];").equals("[1.0, 2.0]");

section("Interval iteration");
code_v4_("var i = [0..6] var x = 0 for (var y in i) x += y return x").equals("15");
}
}
2 changes: 2 additions & 0 deletions src/test/java/test/TestSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@ public void run() throws Exception {
code_v4_("Set i = <>; Set j = <1, 2>; return setIsSubsetOf(j, i)").equals("false");
code_v4_("Set i = <>; Set j = <>; return setIsSubsetOf(i, j)").equals("true");

section("Set iteration");
code_v4_("var s = <1, 2, 3, 4, 5> var x = 0 for (var y in s) x += y return x").equals("15");
}
}

0 comments on commit 022ad3c

Please sign in to comment.