diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_echo.java b/src/com/annimon/ownlang/lib/modules/functions/std_echo.java new file mode 100644 index 0000000..4aa15ba --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_echo.java @@ -0,0 +1,18 @@ +package com.annimon.ownlang.lib.modules.functions; + +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.NumberValue; +import com.annimon.ownlang.lib.Value; + +public final class std_echo implements Function { + + @Override + public Value execute(Value... args) { + for (Value arg : args) { + System.out.print(arg.asString()); + System.out.print(" "); + } + System.out.println(); + return NumberValue.ZERO; + } +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_newarray.java b/src/com/annimon/ownlang/lib/modules/functions/std_newarray.java new file mode 100644 index 0000000..f2a1fc2 --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_newarray.java @@ -0,0 +1,30 @@ +package com.annimon.ownlang.lib.modules.functions; + +import com.annimon.ownlang.lib.ArrayValue; +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.NumberValue; +import com.annimon.ownlang.lib.Value; + +public final class std_newarray implements Function { + + @Override + public Value execute(Value... args) { + return createArray(args, 0); + } + + private ArrayValue createArray(Value[] args, int index) { + final int size = (int) args[index].asNumber(); + final int last = args.length - 1; + ArrayValue array = new ArrayValue(size); + if (index == last) { + for (int i = 0; i < size; i++) { + array.set(i, NumberValue.ZERO); + } + } else if (index < last) { + for (int i = 0; i < size; i++) { + array.set(i, createArray(args, index + 1)); + } + } + return array; + } +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_rand.java b/src/com/annimon/ownlang/lib/modules/functions/std_rand.java new file mode 100644 index 0000000..cc673bd --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_rand.java @@ -0,0 +1,27 @@ +package com.annimon.ownlang.lib.modules.functions; + +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.NumberValue; +import com.annimon.ownlang.lib.Value; + +import java.util.Random; + +public final class std_rand implements Function { + + private static final Random RND = new Random(); + + @Override + public Value execute(Value... args) { + if (args.length == 0) return new NumberValue(RND.nextDouble()); + + int from = 0; + int to = 100; + if (args.length == 1) { + to = (int) args[0].asNumber(); + } else if (args.length == 2) { + from = (int) args[0].asNumber(); + to = (int) args[1].asNumber(); + } + return new NumberValue(RND.nextInt(to - from) + from); + } +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_sleep.java b/src/com/annimon/ownlang/lib/modules/functions/std_sleep.java new file mode 100644 index 0000000..4b25ea3 --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_sleep.java @@ -0,0 +1,20 @@ +package com.annimon.ownlang.lib.modules.functions; + +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.NumberValue; +import com.annimon.ownlang.lib.Value; + +public final class std_sleep implements Function { + + @Override + public Value execute(Value... args) { + if (args.length == 1) { + try { + Thread.sleep((long) args[0].asNumber()); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + return NumberValue.ZERO; + } +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_thread.java b/src/com/annimon/ownlang/lib/modules/functions/std_thread.java new file mode 100644 index 0000000..9eea576 --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_thread.java @@ -0,0 +1,20 @@ +package com.annimon.ownlang.lib.modules.functions; + +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.Functions; +import com.annimon.ownlang.lib.NumberValue; +import com.annimon.ownlang.lib.Value; + +public final class std_thread implements Function { + + @Override + public Value execute(Value... args) { + if (args.length == 1) { + // Создаём новый поток по имени функции + new Thread(() -> { + Functions.get(args[0].asString()).execute(); + }).start(); + } + return NumberValue.ZERO; + } +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/std.java b/src/com/annimon/ownlang/lib/modules/std.java index f274d02..3b8c725 100644 --- a/src/com/annimon/ownlang/lib/modules/std.java +++ b/src/com/annimon/ownlang/lib/modules/std.java @@ -1,6 +1,12 @@ package com.annimon.ownlang.lib.modules; import com.annimon.ownlang.lib.*; +import com.annimon.ownlang.lib.modules.functions.std_echo; +import com.annimon.ownlang.lib.modules.functions.std_foreach; +import com.annimon.ownlang.lib.modules.functions.std_newarray; +import com.annimon.ownlang.lib.modules.functions.std_rand; +import com.annimon.ownlang.lib.modules.functions.std_sleep; +import com.annimon.ownlang.lib.modules.functions.std_thread; import java.util.Random; /** @@ -11,76 +17,10 @@ public final class std implements Module { @Override public void init() { - Functions.set("echo", args -> { - for (Value arg : args) { - System.out.print(arg.asString()); - System.out.print(" "); - } - System.out.println(); - return NumberValue.ZERO; - }); - Functions.set("newarray", new Function() { - - @Override - public Value execute(Value... args) { - return createArray(args, 0); - } - - private ArrayValue createArray(Value[] args, int index) { - final int size = (int) args[index].asNumber(); - final int last = args.length - 1; - ArrayValue array = new ArrayValue(size); - if (index == last) { - for (int i = 0; i < size; i++) { - array.set(i, NumberValue.ZERO); - } - } else if (index < last) { - for (int i = 0; i < size; i++) { - array.set(i, createArray(args, index + 1)); - } - } - return array; - } - }); - Functions.set("rand", new Rand()); - Functions.set("sleep", args -> { - if (args.length == 1) { - try { - Thread.sleep((long) args[0].asNumber()); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - return NumberValue.ZERO; - }); - Functions.set("thread", args -> { - if (args.length == 1) { - // Создаём новый поток по имени функции - new Thread(() -> { - Functions.get(args[0].asString()).execute(); - }).start(); - } - return NumberValue.ZERO; - }); - } - - private static class Rand implements Function { - - private static final Random RND = new Random(); - - @Override - public Value execute(Value... args) { - if (args.length == 0) return new NumberValue(RND.nextDouble()); - - int from = 0; - int to = 100; - if (args.length == 1) { - to = (int) args[0].asNumber(); - } else if (args.length == 2) { - from = (int) args[0].asNumber(); - to = (int) args[1].asNumber(); - } - return new NumberValue(RND.nextInt(to - from) + from); - } + Functions.set("echo", new std_echo()); + Functions.set("newarray", new std_newarray()); + Functions.set("rand", new std_rand()); + Functions.set("sleep", new std_sleep()); + Functions.set("thread", new std_thread()); } }