From 9ac41313ca6cc67ab6c95a57d90fc2c92c61a4d2 Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 30 Jun 2015 10:07:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A4=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20=D0=B2=20?= =?UTF-8?q?=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D1=83=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/annimon/ownlang/lib/Functions.java | 41 --------- src/com/annimon/ownlang/lib/modules/math.java | 26 ++++++ src/com/annimon/ownlang/lib/modules/std.java | 84 +++++++++++++++++++ 3 files changed, 110 insertions(+), 41 deletions(-) create mode 100644 src/com/annimon/ownlang/lib/modules/math.java create mode 100644 src/com/annimon/ownlang/lib/modules/std.java diff --git a/src/com/annimon/ownlang/lib/Functions.java b/src/com/annimon/ownlang/lib/Functions.java index 161abea..8aa188a 100644 --- a/src/com/annimon/ownlang/lib/Functions.java +++ b/src/com/annimon/ownlang/lib/Functions.java @@ -13,47 +13,6 @@ public final class Functions { static { functions = new HashMap<>(); - functions.put("sin", new Function() { - - @Override - public Value execute(Value... args) { - if (args.length != 1) throw new RuntimeException("One arg expected"); - return new NumberValue(Math.sin(args[0].asNumber())); - } - }); - functions.put("cos", (Function) (Value... args) -> { - if (args.length != 1) throw new RuntimeException("One arg expected"); - return new NumberValue(Math.cos(args[0].asNumber())); - }); - functions.put("echo", args -> { - for (Value arg : args) { - System.out.println(arg.asString()); - } - return NumberValue.ZERO; - }); - functions.put("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; - } - }); } public static boolean isExists(String key) { diff --git a/src/com/annimon/ownlang/lib/modules/math.java b/src/com/annimon/ownlang/lib/modules/math.java new file mode 100644 index 0000000..a2ca4c8 --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/math.java @@ -0,0 +1,26 @@ +package com.annimon.ownlang.lib.modules; + +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.Functions; +import com.annimon.ownlang.lib.NumberValue; +import com.annimon.ownlang.lib.Value; + +/** + * + * @author aNNiMON + */ +public final class math implements Module { + + @Override + public void init() { + Functions.set("sin", args -> { + if (args.length != 1) throw new RuntimeException("One arg expected"); + return new NumberValue(Math.sin(args[0].asNumber())); + }); + Functions.set("cos", args -> { + if (args.length != 1) throw new RuntimeException("One arg expected"); + return new NumberValue(Math.cos(args[0].asNumber())); + }); + } + +} diff --git a/src/com/annimon/ownlang/lib/modules/std.java b/src/com/annimon/ownlang/lib/modules/std.java new file mode 100644 index 0000000..24befd8 --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/std.java @@ -0,0 +1,84 @@ +package com.annimon.ownlang.lib.modules; + +import com.annimon.ownlang.lib.*; +import java.util.Random; + +/** + * + * @author aNNiMON + */ +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) { + 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); + } + } +}