From 6ec630b6ad2e2898201547f3420b8961a53ba1cf Mon Sep 17 00:00:00 2001 From: zeroflag Date: Wed, 25 Aug 2021 12:36:57 +0200 Subject: [PATCH] digit grouping, transcript scroll, tone + torch catalog, udp WIP --- src/main/java/com/vectron/fcl/Fcl.java | 4 ++-- src/main/java/com/vectron/fcl/types/Num.java | 25 ++++++++++++++------ src/main/res/raw/misc.forth | 7 ++++-- src/test/java/com/vectron/fcl/FclTest.java | 4 +++- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/vectron/fcl/Fcl.java b/src/main/java/com/vectron/fcl/Fcl.java index f7afe4e..41260c8 100644 --- a/src/main/java/com/vectron/fcl/Fcl.java +++ b/src/main/java/com/vectron/fcl/Fcl.java @@ -492,8 +492,8 @@ public class Fcl { } } - private void show(Obj pop) { - transcript.show(pop.asStr().value()); + private void show(Obj item) { + transcript.show(item == null ? "null" : item.asStr().value()); transcript.cr(); } diff --git a/src/main/java/com/vectron/fcl/types/Num.java b/src/main/java/com/vectron/fcl/types/Num.java index f393bf7..38dbd5c 100644 --- a/src/main/java/com/vectron/fcl/types/Num.java +++ b/src/main/java/com/vectron/fcl/types/Num.java @@ -3,7 +3,9 @@ package com.vectron.fcl.types; import com.vectron.fcl.exceptions.NotUnderstood; import com.vectron.fcl.exceptions.TypeMismatched; -import java.text.NumberFormat; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.Locale; import java.util.Objects; import static com.vectron.fcl.Fcl.STRICT; @@ -12,12 +14,16 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand { public static final Num ZERO = new Num(0); public static final Num ONE = new Num(1); public static final Num NAN = new Num(Double.NaN); - private static final NumberFormat format = NumberFormat.getNumberInstance(); + private static final DecimalFormat format; private final Number value; static { + DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US); + symbols.setDecimalSeparator('.'); + symbols.setGroupingSeparator(' '); + format = new DecimalFormat("###,###.##", symbols); format.setMaximumFractionDigits(4); - format.setGroupingUsed(false); + format.setGroupingUsed(true); format.setParseIntegerOnly(false); } @@ -46,14 +52,19 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand { @Override public String toString() { - if (value instanceof Long) { + if (value instanceof Long) return Long.toString((Long) value); - } else if (value instanceof Double) { - return format.format(value); - } + else if (value instanceof Double) + return Double.toString((Double)value); return value.toString(); } + public String display() { + return value instanceof Long || value instanceof Double + ? format.format(value) + : value.toString(); + } + @Override public Obj add(Obj other) { if (value instanceof Long && other.value() instanceof Long) diff --git a/src/main/res/raw/misc.forth b/src/main/res/raw/misc.forth index faed26c..0c996f2 100644 --- a/src/main/res/raw/misc.forth +++ b/src/main/res/raw/misc.forth @@ -15,7 +15,7 @@ : ms ( n -- ) :java.lang.Thread/sleep/l jvm-call-static ; : tone ( hz ms -- ) swap :com.vectron.forthcalc.support.Tone/play/di jvm-call-static ; -: torch ( n -- ) :com.vectron.forthcalc.support.Torch/toggle/O jvm-call-static ; +: torch ( n -- n ) :com.vectron.forthcalc.support.Torch/toggle/O jvm-call-static ; : match: immediate ` lastword set-predicate ; @@ -53,4 +53,7 @@ var: juggler.steps 5 juggler.steps ! [ ] val: juggler.exclude : juggler.solve ( steps exclude-list output-list input-list -- list/nil ) :com.vectron.fcl.Juggler/solve/TTTi jvm-call-static ; -: wzd* ( stack1 stack2 -- list/nil ) list* exchange list* aux> juggler.steps @ juggler.exclude 2swap juggler.solve ; \ No newline at end of file +: wzd* ( stack1 stack2 -- list/nil ) list* exchange list* aux> juggler.steps @ juggler.exclude 2swap juggler.solve ; + +: udp-send-byte ( host port byte -- n ) :com.vectron.forthcalc.support.Udp/sendByte/Nis jvm-call-static ; +: udp-send-str ( host port byte -- n ) :com.vectron.forthcalc.support.Udp/sendStr/sis jvm-call-static ; \ No newline at end of file diff --git a/src/test/java/com/vectron/fcl/FclTest.java b/src/test/java/com/vectron/fcl/FclTest.java index 7992631..e5cde19 100644 --- a/src/test/java/com/vectron/fcl/FclTest.java +++ b/src/test/java/com/vectron/fcl/FclTest.java @@ -83,6 +83,8 @@ public class FclTest { assertEquals(4, evalPop("6 2 -").longValue()); assertEquals(-3, evalPop("7 10 -").longValue()); assertEquals(2.0, evalPop("100 50 /").doubleValue(), 0.01); + assertEquals("Infinity", evalPop("100 00 /").toString()); + assertEquals("Infinity", evalPop("100.0 00 /").toString()); assertEquals(101, evalPop("100 1+").longValue()); assertEquals(256, evalPop("2 8 pow").doubleValue(), 0.01); assertEquals(65536, evalPop("2 16 pow").doubleValue(), 0.01); @@ -1006,7 +1008,7 @@ public class FclTest { assertEquals("[ 3 6 9 ]", evalPop("3 [ 1 2 3 ] *").toString()); assertEquals("[ 3 6 9 ]", evalPop("[ 1 2 3 ] 3 *").toString()); - assertEquals("[ 1 2 3 ]", evalPop("[ 3 6 9 ] 3 /").toString()); + assertEquals("[ 1.0 2.0 3.0 ]", evalPop("[ 3 6 9 ] 3 /").toString()); assertEquals("[ 4 5 6 ]", evalPop("3 [ 1 2 3 ] +").toString()); assertEquals("[ 4 5 6 ]", evalPop("[ 1 2 3 ] 3 +").toString()); assertEquals("[ 1 2 3 ]", evalPop("[ 4 5 6 ] 3 -").toString());