experimental plot words + hex literal

This commit is contained in:
zeroflag 2021-07-10 20:33:06 +02:00
parent 04c6ec1d09
commit 214704f1ef
5 changed files with 115 additions and 47 deletions

View file

@ -30,6 +30,8 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand {
public static Num parse(String str) { public static Num parse(String str) {
try { try {
if (str.startsWith("0x"))
return new Num(Long.parseLong(str.substring(2), 16));
return new Num(Long.parseLong(str)); return new Num(Long.parseLong(str));
} catch (NumberFormatException e1) { } catch (NumberFormatException e1) {
try { try {

View file

@ -99,4 +99,14 @@
: index-of ( s sub -- n ) swap :indexOf/O jvm-call-method ; : index-of ( s sub -- n ) swap :indexOf/O jvm-call-method ;
: replace ( s old new -- s ) swap rot :replace/ss jvm-call-method ; : replace ( s old new -- s ) swap rot :replace/ss jvm-call-method ;
: concat ( s1 s2 -- s ) swap :concat/O jvm-call-method ; : concat ( s1 s2 -- s ) swap :concat/O jvm-call-method ;
: >str ( o -- s ) :asStr jvm-call-method ; : >str ( o -- s ) :asStr jvm-call-method ;
: minl ( ls -- n )
nil => result
{ result @ nil != if result @ min then result ! } each
result @ ;
: maxl ( ls -- n )
nil => result
{ result @ nil != if result @ max then result ! } each
result @ ;

View file

@ -16,49 +16,4 @@
: 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 -- ) :com.vectron.forthcalc.support.Torch/toggle/O jvm-call-static ;
( TODO: wip )
: draw-circle ( x y r -- ) :com.vectron.forthcalc.CanvasView/drawCircle/ddd jvm-call-static ;
: draw-rect ( left top right bottom -- ) :com.vectron.forthcalc.CanvasView/drawRect/dddd jvm-call-static ;
: draw-point ( x y -- ) :com.vectron.forthcalc.CanvasView/drawPoint/dd jvm-call-static ;
: draw-line ( x1 y1 x2 y2 -- ) :com.vectron.forthcalc.CanvasView/drawLine/dddd jvm-call-static ;
: width :com.vectron.forthcalc.CanvasView/width jvm-call-static ;
: height :com.vectron.forthcalc.CanvasView/height jvm-call-static ;
: width 320 ;
: height 200 ;
var: xmin -10 xmin !
var: ymin -10 ymin !
var: xmax 10 xmax !
var: ymax 10 ymax !
: xstep ( -- n ) xmax @ xmin @ - 100 / ;
: translate -> y -> x
x width 2 / * xmax @ / width 2 / +
height y height 2 / * ymax @ / height 2 / + - ;
: draw-axis
0 height 2 / width height 2 / draw-line
width 2 / 0 width 2 / height draw-line ;
: plotq ( q -- ) -> q
draw-axis
nil => px
nil => py
xmin @ xmax @ xstep ... { -> x
x q yield -> y
px @ nil != if
px @ py @
x y translate
draw-line
then
x y translate py ! px !
} each ;
\ { dup dup dup * * swap 2 * - } plotq

View file

@ -0,0 +1,93 @@
: draw-circle ( x y r -- ) :com.vectron.forthcalc.CanvasView/drawCircle/ddd jvm-call-static ;
: draw-rect ( left top right bottom -- ) :com.vectron.forthcalc.CanvasView/drawRect/dddd jvm-call-static ;
: draw-point ( x y -- ) :com.vectron.forthcalc.CanvasView/drawPoint/dd jvm-call-static ;
: draw-line ( x1 y1 x2 y2 -- ) :com.vectron.forthcalc.CanvasView/drawLine/dddd jvm-call-static ;
: draw-text ( i x y s -- ) :com.vectron.forthcalc.CanvasView/drawText/sddi jvm-call-static ;
: paint ( width color -- ) :com.vectron.forthcalc.CanvasView/setPaint/id jvm-call-static ;
: clear-canvas ( -- ) :com.vectron.forthcalc.CanvasView/clear jvm-call-static ;
320 val: width
320 val: height
0xFF000000 val: AXIS-COLOR
0xFF641E16 val: LINE-COLOR
0xFF93D078 val: GRID-COLOR
0xFF000000 val: TEXT-COLOR
var: xmin
var: ymin
var: xmax
var: ymax
var: ox
var: oy
: xstep ( -- n ) xmax @ xmin @ - 100 / ;
: reset-zoom ( -- )
-10 xmin ! 10 xmax !
-10 ymin ! 10 ymax !
0 ox ! 0 oy ! ;
: trans -> y -> x
x xmin @ - width 1- * xmax @ xmin @ - abs /
height 1- y ymin @ - height 1- * ymax @ ymin @ - abs / - ;
: draw-axis
3 AXIS-COLOR paint
xmin @ oy @ trans xmax @ oy @ trans draw-line
ox @ ymin @ trans ox @ ymax @ trans draw-line ;
: draw-scale
0 GRID-COLOR paint
xmin @ xmax @ xmax @ xmin @ - abs 10 / ... { -> x
x ymin @ trans
x ymax @ trans
draw-line
} each
ymin @ ymax @ ymax @ ymin @ - abs 10 / ... { -> y
xmin @ y trans
xmax @ y trans
draw-line
} each
1 TEXT-COLOR paint
1 xmax @ oy @ trans xmax @ >str draw-text
2 ox @ ymax @ trans ymax @ >str draw-text ;
: plotq ( q -- ) -> q
clear-canvas
draw-scale
draw-axis
nil => px
nil => py
2 LINE-COLOR paint
xmin @ xmax @ xstep ... { -> x
x q yield -> y
px @ nil != if
px @ py @
x y trans
draw-line
then
x y trans py ! px !
} each ;
: plotl ( ls -- ) -> ls
1 xmin !
ls size xmax !
ls minl ymin !
ls maxl ymax !
xmin @ ox !
ymin @ oy !
clear-canvas
draw-scale
draw-axis
2 LINE-COLOR paint
nil => px nil => py 1 => x
ls { -> y
px @ nil != if
px @ py @ trans x @ y trans draw-line
then
x @ px ! y py !
x inc
} each ;
: plots ( .. -- ) depth 0 != if list* plotl then ;
reset-zoom

View file

@ -804,6 +804,14 @@ public class FclTest {
assertEquals(0, evalPop("m clear m size").intValue()); assertEquals(0, evalPop("m clear m size").intValue());
} }
@Test
public void testListMinMax() {
assertEquals(17, evalPop("[ 1 -2 3.5 -3 17 3 -1 ] maxl").intValue());
assertEquals(-3, evalPop("[ 1 -2 3.5 -3 17 3 -1 ] minl").intValue());
assertEquals(Nil.INSTANCE, evalPop("[ ] minl"));
assertEquals(Nil.INSTANCE, evalPop("[ ] maxl"));
}
@Test @Test
public void testRanges() { public void testRanges() {
assertEquals("[ 1 2 3 4 5 ]", evalPop(": tst 1 5 .. { } map ; tst").toString()); assertEquals("[ 1 2 3 4 5 ]", evalPop(": tst 1 5 .. { } map ; tst").toString());