diff --git a/src/main/java/com/vectron/fcl/types/Num.java b/src/main/java/com/vectron/fcl/types/Num.java index 959100f..930bf6e 100644 --- a/src/main/java/com/vectron/fcl/types/Num.java +++ b/src/main/java/com/vectron/fcl/types/Num.java @@ -30,6 +30,8 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand { public static Num parse(String str) { try { + if (str.startsWith("0x")) + return new Num(Long.parseLong(str.substring(2), 16)); return new Num(Long.parseLong(str)); } catch (NumberFormatException e1) { try { diff --git a/src/main/res/raw/collections.forth b/src/main/res/raw/collections.forth index 2c2f040..d781bde 100644 --- a/src/main/res/raw/collections.forth +++ b/src/main/res/raw/collections.forth @@ -99,4 +99,14 @@ : index-of ( s sub -- n ) swap :indexOf/O 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 ; -: >str ( o -- s ) :asStr jvm-call-method ; \ No newline at end of file +: >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 @ ; \ No newline at end of file diff --git a/src/main/res/raw/misc.forth b/src/main/res/raw/misc.forth index 36459b8..042502b 100644 --- a/src/main/res/raw/misc.forth +++ b/src/main/res/raw/misc.forth @@ -16,49 +16,4 @@ : 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 ; - -( 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 +: torch ( n -- ) :com.vectron.forthcalc.support.Torch/toggle/O jvm-call-static ; \ No newline at end of file diff --git a/src/main/res/raw/plot.forth b/src/main/res/raw/plot.forth new file mode 100644 index 0000000..f4eb20c --- /dev/null +++ b/src/main/res/raw/plot.forth @@ -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 \ 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 ab6cf87..4642459 100644 --- a/src/test/java/com/vectron/fcl/FclTest.java +++ b/src/test/java/com/vectron/fcl/FclTest.java @@ -804,6 +804,14 @@ public class FclTest { 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 public void testRanges() { assertEquals("[ 1 2 3 4 5 ]", evalPop(": tst 1 5 .. { } map ; tst").toString());