Skip to content

Commit d3f5212

Browse files
committed
Функции из std вынесены в отдельный пакет
1 parent cea99d4 commit d3f5212

File tree

6 files changed

+126
-71
lines changed

6 files changed

+126
-71
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.Function;
4+
import com.annimon.ownlang.lib.NumberValue;
5+
import com.annimon.ownlang.lib.Value;
6+
7+
public final class std_echo implements Function {
8+
9+
@Override
10+
public Value execute(Value... args) {
11+
for (Value arg : args) {
12+
System.out.print(arg.asString());
13+
System.out.print(" ");
14+
}
15+
System.out.println();
16+
return NumberValue.ZERO;
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.ArrayValue;
4+
import com.annimon.ownlang.lib.Function;
5+
import com.annimon.ownlang.lib.NumberValue;
6+
import com.annimon.ownlang.lib.Value;
7+
8+
public final class std_newarray implements Function {
9+
10+
@Override
11+
public Value execute(Value... args) {
12+
return createArray(args, 0);
13+
}
14+
15+
private ArrayValue createArray(Value[] args, int index) {
16+
final int size = (int) args[index].asNumber();
17+
final int last = args.length - 1;
18+
ArrayValue array = new ArrayValue(size);
19+
if (index == last) {
20+
for (int i = 0; i < size; i++) {
21+
array.set(i, NumberValue.ZERO);
22+
}
23+
} else if (index < last) {
24+
for (int i = 0; i < size; i++) {
25+
array.set(i, createArray(args, index + 1));
26+
}
27+
}
28+
return array;
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.Function;
4+
import com.annimon.ownlang.lib.NumberValue;
5+
import com.annimon.ownlang.lib.Value;
6+
7+
import java.util.Random;
8+
9+
public final class std_rand implements Function {
10+
11+
private static final Random RND = new Random();
12+
13+
@Override
14+
public Value execute(Value... args) {
15+
if (args.length == 0) return new NumberValue(RND.nextDouble());
16+
17+
int from = 0;
18+
int to = 100;
19+
if (args.length == 1) {
20+
to = (int) args[0].asNumber();
21+
} else if (args.length == 2) {
22+
from = (int) args[0].asNumber();
23+
to = (int) args[1].asNumber();
24+
}
25+
return new NumberValue(RND.nextInt(to - from) + from);
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.Function;
4+
import com.annimon.ownlang.lib.NumberValue;
5+
import com.annimon.ownlang.lib.Value;
6+
7+
public final class std_sleep implements Function {
8+
9+
@Override
10+
public Value execute(Value... args) {
11+
if (args.length == 1) {
12+
try {
13+
Thread.sleep((long) args[0].asNumber());
14+
} catch (InterruptedException ex) {
15+
Thread.currentThread().interrupt();
16+
}
17+
}
18+
return NumberValue.ZERO;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.Function;
4+
import com.annimon.ownlang.lib.Functions;
5+
import com.annimon.ownlang.lib.NumberValue;
6+
import com.annimon.ownlang.lib.Value;
7+
8+
public final class std_thread implements Function {
9+
10+
@Override
11+
public Value execute(Value... args) {
12+
if (args.length == 1) {
13+
// Создаём новый поток по имени функции
14+
new Thread(() -> {
15+
Functions.get(args[0].asString()).execute();
16+
}).start();
17+
}
18+
return NumberValue.ZERO;
19+
}
20+
}
Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
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;
410
import java.util.Random;
511

612
/**
@@ -11,76 +17,10 @@ public final class std implements Module {
1117

1218
@Override
1319
public void init() {
14-
Functions.set("echo", args -> {
15-
for (Value arg : args) {
16-
System.out.print(arg.asString());
17-
System.out.print(" ");
18-
}
19-
System.out.println();
20-
return NumberValue.ZERO;
21-
});
22-
Functions.set("newarray", new Function() {
23-
24-
@Override
25-
public Value execute(Value... args) {
26-
return createArray(args, 0);
27-
}
28-
29-
private ArrayValue createArray(Value[] args, int index) {
30-
final int size = (int) args[index].asNumber();
31-
final int last = args.length - 1;
32-
ArrayValue array = new ArrayValue(size);
33-
if (index == last) {
34-
for (int i = 0; i < size; i++) {
35-
array.set(i, NumberValue.ZERO);
36-
}
37-
} else if (index < last) {
38-
for (int i = 0; i < size; i++) {
39-
array.set(i, createArray(args, index + 1));
40-
}
41-
}
42-
return array;
43-
}
44-
});
45-
Functions.set("rand", new Rand());
46-
Functions.set("sleep", args -> {
47-
if (args.length == 1) {
48-
try {
49-
Thread.sleep((long) args[0].asNumber());
50-
} catch (InterruptedException ex) {
51-
Thread.currentThread().interrupt();
52-
}
53-
}
54-
return NumberValue.ZERO;
55-
});
56-
Functions.set("thread", args -> {
57-
if (args.length == 1) {
58-
// Создаём новый поток по имени функции
59-
new Thread(() -> {
60-
Functions.get(args[0].asString()).execute();
61-
}).start();
62-
}
63-
return NumberValue.ZERO;
64-
});
65-
}
66-
67-
private static class Rand implements Function {
68-
69-
private static final Random RND = new Random();
70-
71-
@Override
72-
public Value execute(Value... args) {
73-
if (args.length == 0) return new NumberValue(RND.nextDouble());
74-
75-
int from = 0;
76-
int to = 100;
77-
if (args.length == 1) {
78-
to = (int) args[0].asNumber();
79-
} else if (args.length == 2) {
80-
from = (int) args[0].asNumber();
81-
to = (int) args[1].asNumber();
82-
}
83-
return new NumberValue(RND.nextInt(to - from) + from);
84-
}
20+
Functions.set("echo", new std_echo());
21+
Functions.set("newarray", new std_newarray());
22+
Functions.set("rand", new std_rand());
23+
Functions.set("sleep", new std_sleep());
24+
Functions.set("thread", new std_thread());
8525
}
8626
}

0 commit comments

Comments
 (0)