digit grouping, transcript scroll, tone + torch catalog, udp WIP

This commit is contained in:
zeroflag 2021-08-25 12:36:57 +02:00
parent e6c8f36760
commit 6ec630b6ad
4 changed files with 28 additions and 12 deletions

View file

@ -492,8 +492,8 @@ public class Fcl {
} }
} }
private void show(Obj pop) { private void show(Obj item) {
transcript.show(pop.asStr().value()); transcript.show(item == null ? "null" : item.asStr().value());
transcript.cr(); transcript.cr();
} }

View file

@ -3,7 +3,9 @@ package com.vectron.fcl.types;
import com.vectron.fcl.exceptions.NotUnderstood; import com.vectron.fcl.exceptions.NotUnderstood;
import com.vectron.fcl.exceptions.TypeMismatched; 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 java.util.Objects;
import static com.vectron.fcl.Fcl.STRICT; 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 ZERO = new Num(0);
public static final Num ONE = new Num(1); public static final Num ONE = new Num(1);
public static final Num NAN = new Num(Double.NaN); 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; private final Number value;
static { static {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(' ');
format = new DecimalFormat("###,###.##", symbols);
format.setMaximumFractionDigits(4); format.setMaximumFractionDigits(4);
format.setGroupingUsed(false); format.setGroupingUsed(true);
format.setParseIntegerOnly(false); format.setParseIntegerOnly(false);
} }
@ -46,14 +52,19 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand {
@Override @Override
public String toString() { public String toString() {
if (value instanceof Long) { if (value instanceof Long)
return Long.toString((Long) value); return Long.toString((Long) value);
} else if (value instanceof Double) { else if (value instanceof Double)
return format.format(value); return Double.toString((Double)value);
}
return value.toString(); return value.toString();
} }
public String display() {
return value instanceof Long || value instanceof Double
? format.format(value)
: value.toString();
}
@Override @Override
public Obj add(Obj other) { public Obj add(Obj other) {
if (value instanceof Long && other.value() instanceof Long) if (value instanceof Long && other.value() instanceof Long)

View file

@ -15,7 +15,7 @@
: ms ( n -- ) :java.lang.Thread/sleep/l jvm-call-static ; : ms ( n -- ) :java.lang.Thread/sleep/l jvm-call-static ;
: tone ( hz ms -- ) swap :com.vectron.forthcalc.support.Tone/play/di 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 ; : match: immediate ` lastword set-predicate ;
@ -53,4 +53,7 @@ var: juggler.steps 5 juggler.steps !
[ ] val: juggler.exclude [ ] val: juggler.exclude
: juggler.solve ( steps exclude-list output-list input-list -- list/nil ) :com.vectron.fcl.Juggler/solve/TTTi jvm-call-static ; : 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 ; : 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 ;

View file

@ -83,6 +83,8 @@ public class FclTest {
assertEquals(4, evalPop("6 2 -").longValue()); assertEquals(4, evalPop("6 2 -").longValue());
assertEquals(-3, evalPop("7 10 -").longValue()); assertEquals(-3, evalPop("7 10 -").longValue());
assertEquals(2.0, evalPop("100 50 /").doubleValue(), 0.01); 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(101, evalPop("100 1+").longValue());
assertEquals(256, evalPop("2 8 pow").doubleValue(), 0.01); assertEquals(256, evalPop("2 8 pow").doubleValue(), 0.01);
assertEquals(65536, evalPop("2 16 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("3 [ 1 2 3 ] *").toString());
assertEquals("[ 3 6 9 ]", evalPop("[ 1 2 3 ] 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("3 [ 1 2 3 ] +").toString());
assertEquals("[ 4 5 6 ]", evalPop("[ 1 2 3 ] 3 +").toString()); assertEquals("[ 4 5 6 ]", evalPop("[ 1 2 3 ] 3 +").toString());
assertEquals("[ 1 2 3 ]", evalPop("[ 4 5 6 ] 3 -").toString()); assertEquals("[ 1 2 3 ]", evalPop("[ 4 5 6 ] 3 -").toString());