• R/O
  • SSH

kink: 提交

Kink runtime


Commit MetaInfo

修订版74ebf0b6f1a41aa4dcf567a3d2d2d48f03e4656f (tree)
时间2020-03-22 13:02:04
作者miyakawataku
Commitermiyakawataku

Log Message

add package-info for STOPWATCH mod

更改概述

差异

diff -r fe342b20705f -r 74ebf0b6f1a4 src/main/java/org/kink_lang/kink/Vm.java
--- a/src/main/java/org/kink_lang/kink/Vm.java Sun Mar 22 12:38:59 2020 +0900
+++ b/src/main/java/org/kink_lang/kink/Vm.java Sun Mar 22 13:02:04 2020 +0900
@@ -16,7 +16,7 @@
1616 import org.kink_lang.kink.internal.mod.java.JavaProxyMod;
1717 import org.kink_lang.kink.internal.mod.java.ThrowerMod;
1818 import org.kink_lang.kink.internal.mod.prng.RandomMod;
19-import org.kink_lang.kink.internal.mod.StopwatchMod;
19+import org.kink_lang.kink.internal.mod.stopwatch.StopwatchMod;
2020
2121 /**
2222 * A Kink vm.
diff -r fe342b20705f -r 74ebf0b6f1a4 src/main/java/org/kink_lang/kink/internal/mod/StopwatchMod.java
--- a/src/main/java/org/kink_lang/kink/internal/mod/StopwatchMod.java Sun Mar 22 12:38:59 2020 +0900
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
1-package org.kink_lang.kink.internal.mod;
2-
3-import java.math.BigDecimal;
4-import java.util.Locale;
5-import java.util.Map;
6-import java.util.HashMap;
7-
8-import org.kink_lang.kink.*;
9-import org.kink_lang.kink.hostfun.HostContext;
10-import org.kink_lang.kink.hostfun.HostResult;
11-
12-/**
13- * Helper for kink/STOPWATCH mod.
14- */
15-public class StopwatchMod {
16-
17- /** The vm. */
18- private final Vm vm;
19-
20- /** The mod name. */
21- public static final String MOD_NAME = "kink/STOPWATCH";
22-
23- /**
24- * Constructs the helper.
25- */
26- private StopwatchMod(Vm vm) {
27- this.vm = vm;
28- }
29-
30- /**
31- * Makes a mod helper.
32- *
33- * @param vm the vm.
34- * @return a mod helper.
35- */
36- public static Val makeMod(Vm vm) {
37- return new StopwatchMod(vm).makeMod();
38- }
39-
40- /**
41- * Makes the mod.
42- */
43- private Val makeMod() {
44- Map<Integer, Val> map = new HashMap<>();
45- map.put(vm.sym.getHandle("start"),
46- vm.fun.make("STOPWATCH.start").take(0).action(this::stopwatchStartFun));
47- map.put(vm.sym.getHandle("repr"),
48- vm.fun.constant(vm.str.of(String.format(Locale.ROOT, "Mod(%s)", MOD_NAME))));
49- return vm.newVal(vm.varMapping.of(map));
50- }
51-
52- /**
53- * Implementation of STOPWATCH.start.
54- */
55- private HostResult stopwatchStartFun(HostContext c) {
56- return watchFun(System.nanoTime());
57- }
58-
59- /** The scale of nano sconds. */
60- private static final BigDecimal NANO = new BigDecimal("0.000000001");
61-
62- /**
63- * Makes a stopwatch fun from {@code startNano}.
64- */
65- private FunVal watchFun(long startNano) {
66- return vm.fun.make("STOPWATCH.start#watchfun").take(0).action(c -> {
67- long now = System.nanoTime();
68- long elapsed = Math.max(0, now - startNano);
69- return vm.num.of(BigDecimal.valueOf(elapsed).multiply(NANO));
70- });
71- }
72-
73-}
74-
75-// vim: et sw=4 sts=4 fdm=marker
diff -r fe342b20705f -r 74ebf0b6f1a4 src/main/java/org/kink_lang/kink/internal/mod/stopwatch/StopwatchMod.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/kink_lang/kink/internal/mod/stopwatch/StopwatchMod.java Sun Mar 22 13:02:04 2020 +0900
@@ -0,0 +1,75 @@
1+package org.kink_lang.kink.internal.mod.stopwatch;
2+
3+import java.math.BigDecimal;
4+import java.util.Locale;
5+import java.util.Map;
6+import java.util.HashMap;
7+
8+import org.kink_lang.kink.*;
9+import org.kink_lang.kink.hostfun.HostContext;
10+import org.kink_lang.kink.hostfun.HostResult;
11+
12+/**
13+ * Helper for kink/STOPWATCH mod.
14+ */
15+public class StopwatchMod {
16+
17+ /** The vm. */
18+ private final Vm vm;
19+
20+ /** The mod name. */
21+ public static final String MOD_NAME = "kink/STOPWATCH";
22+
23+ /**
24+ * Constructs the helper.
25+ */
26+ private StopwatchMod(Vm vm) {
27+ this.vm = vm;
28+ }
29+
30+ /**
31+ * Makes a mod helper.
32+ *
33+ * @param vm the vm.
34+ * @return a mod helper.
35+ */
36+ public static Val makeMod(Vm vm) {
37+ return new StopwatchMod(vm).makeMod();
38+ }
39+
40+ /**
41+ * Makes the mod.
42+ */
43+ private Val makeMod() {
44+ Map<Integer, Val> map = new HashMap<>();
45+ map.put(vm.sym.getHandle("start"),
46+ vm.fun.make("STOPWATCH.start").take(0).action(this::stopwatchStartFun));
47+ map.put(vm.sym.getHandle("repr"),
48+ vm.fun.constant(vm.str.of(String.format(Locale.ROOT, "Mod(%s)", MOD_NAME))));
49+ return vm.newVal(vm.varMapping.of(map));
50+ }
51+
52+ /**
53+ * Implementation of STOPWATCH.start.
54+ */
55+ private HostResult stopwatchStartFun(HostContext c) {
56+ return watchFun(System.nanoTime());
57+ }
58+
59+ /** The scale of nano sconds. */
60+ private static final BigDecimal NANO = new BigDecimal("0.000000001");
61+
62+ /**
63+ * Makes a stopwatch fun from {@code startNano}.
64+ */
65+ private FunVal watchFun(long startNano) {
66+ return vm.fun.make("STOPWATCH.start#watchfun").take(0).action(c -> {
67+ long now = System.nanoTime();
68+ long elapsed = Math.max(0, now - startNano);
69+ return vm.num.of(BigDecimal.valueOf(elapsed).multiply(NANO));
70+ });
71+ }
72+
73+}
74+
75+// vim: et sw=4 sts=4 fdm=marker
diff -r fe342b20705f -r 74ebf0b6f1a4 src/main/java/org/kink_lang/kink/internal/mod/stopwatch/package-info.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/kink_lang/kink/internal/mod/stopwatch/package-info.java Sun Mar 22 13:02:04 2020 +0900
@@ -0,0 +1,6 @@
1+/**
2+ * Provides kink/STOPWATCH mod.
3+ */
4+package org.kink_lang.kink.internal.mod.stopwatch;
5+
6+// vim: et sw=4 sts=4 fdm=marker
Show on old repository browser