• R/O
  • SSH

kink: 提交

Kink runtime


Commit MetaInfo

修订版fe342b20705fa65d7eacd0275c548ecb9203a468 (tree)
时间2020-03-22 12:38:59
作者miyakawataku
Commitermiyakawataku

Log Message

rename Java classes of STOPWATCH mod

更改概述

差异

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