Skip to content

Commit b6d3c30

Browse files
committed
Добавлена функция foreach
1 parent 4004703 commit b6d3c30

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

program.own

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@ for i = 0, i < 4, i = i + 1 {
8787
map = {"+" : add, "-" : sub. "*" : mul, "/" : div}
8888
//print map["+"]
8989
print "\n"
90-
print function(map["+"], 4, 5)
90+
print function(map["+"], 4, 5)
91+
print "\n"
92+
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))

src/com/annimon/ownlang/lib/ArrayValue.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.annimon.ownlang.lib;
22

33
import java.util.Arrays;
4+
import java.util.Iterator;
45

56
/**
67
*
78
* @author aNNiMON
89
*/
9-
public final class ArrayValue implements Value {
10+
public final class ArrayValue implements Value, Iterable<Value> {
1011

1112
private final Value[] elements;
1213

@@ -41,6 +42,11 @@ public String asString() {
4142
return Arrays.toString(elements);
4243
}
4344

45+
@Override
46+
public Iterator<Value> iterator() {
47+
return Arrays.asList(elements).iterator();
48+
}
49+
4450
@Override
4551
public int hashCode() {
4652
int hash = 5;
@@ -57,8 +63,6 @@ public boolean equals(Object obj) {
5763
final ArrayValue other = (ArrayValue) obj;
5864
return Arrays.deepEquals(this.elements, other.elements);
5965
}
60-
61-
6266

6367
@Override
6468
public String toString() {

src/com/annimon/ownlang/lib/MapValue.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
package com.annimon.ownlang.lib;
22

33
import java.util.HashMap;
4+
import java.util.Iterator;
45
import java.util.Map;
56
import java.util.Objects;
67

78
/**
89
*
910
* @author aNNiMON
1011
*/
11-
public final class MapValue implements Value {
12+
public class MapValue implements Value, Iterable<Map.Entry<Value, Value>> {
1213

1314
private final Map<Value, Value> map;
1415

1516
public MapValue(int size) {
1617
this.map = new HashMap<>(size);
1718
}
18-
19+
20+
public MapValue(Map<Value, Value> map) {
21+
this.map = map;
22+
}
23+
1924
public Value get(Value key) {
2025
return map.get(key);
2126
}
2227

2328
public void set(Value key, Value value) {
2429
map.put(key, value);
2530
}
26-
31+
2732
@Override
2833
public double asNumber() {
2934
throw new RuntimeException("Cannot cast map to number");
@@ -34,6 +39,11 @@ public String asString() {
3439
return map.toString();
3540
}
3641

42+
@Override
43+
public Iterator<Map.Entry<Value, Value>> iterator() {
44+
return map.entrySet().iterator();
45+
}
46+
3747
@Override
3848
public int hashCode() {
3949
int hash = 5;
@@ -51,8 +61,6 @@ public boolean equals(Object obj) {
5161
return Objects.equals(this.map, other.map);
5262
}
5363

54-
55-
5664
@Override
5765
public String toString() {
5866
return asString();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.*;
4+
5+
import java.util.Map;
6+
7+
public final class std_foreach implements Function {
8+
9+
@Override
10+
public Value execute(Value... args) {
11+
if (args.length != 2) return NumberValue.ZERO;
12+
13+
if (!(args[1] instanceof FunctionValue)) return NumberValue.ZERO;
14+
final Function function = ((FunctionValue) args[1]).getValue();
15+
final Value container = args[0];
16+
if (container instanceof ArrayValue) {
17+
final ArrayValue array = (ArrayValue) container;
18+
for (Value element : array) {
19+
function.execute(element);
20+
}
21+
return NumberValue.ZERO;
22+
}
23+
if (container instanceof MapValue) {
24+
final MapValue map = (MapValue) container;
25+
for (Map.Entry<Value, Value> element : map) {
26+
function.execute(element.getKey(), element.getValue());
27+
}
28+
return NumberValue.ZERO;
29+
}
30+
return NumberValue.ZERO;
31+
}
32+
}

src/com/annimon/ownlang/lib/modules/std.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package com.annimon.ownlang.lib.modules;
22

33
import com.annimon.ownlang.lib.*;
4-
import com.annimon.ownlang.lib.modules.functions.std_echo;
5-
import com.annimon.ownlang.lib.modules.functions.std_foreach;
6-
import com.annimon.ownlang.lib.modules.functions.std_newarray;
7-
import com.annimon.ownlang.lib.modules.functions.std_rand;
8-
import com.annimon.ownlang.lib.modules.functions.std_sleep;
9-
import com.annimon.ownlang.lib.modules.functions.std_thread;
10-
import java.util.Random;
4+
import com.annimon.ownlang.lib.modules.functions.*;
115

126
/**
137
*
@@ -18,6 +12,7 @@ public final class std implements Module {
1812
@Override
1913
public void init() {
2014
Functions.set("echo", new std_echo());
15+
Functions.set("foreach", new std_foreach());
2116
Functions.set("newarray", new std_newarray());
2217
Functions.set("rand", new std_rand());
2318
Functions.set("sleep", new std_sleep());

0 commit comments

Comments
 (0)