From be2d76431c28e6dab3c5afadf1a110ec2fa460a7 Mon Sep 17 00:00:00 2001 From: zeroflag Date: Wed, 1 Sep 2021 12:33:24 +0200 Subject: [PATCH] wip --- README.md | 4 ++-- src/main/res/raw/collections.forth | 11 ++++++----- src/main/res/raw/misc.forth | 8 +++++--- src/main/res/raw/plot.forth | 2 +- src/test/java/com/vectron/fcl/FclTest.java | 13 ++++++++----- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ef1c101..6d26fea 100644 --- a/README.md +++ b/README.md @@ -284,7 +284,7 @@ A list is a dynamic, ordered data structed. `[` and `]` are Forth words, so a wh ``` ```forth -1 2 3 4 5 list* \ creates a new list and loads all items from the stack into it +1 2 3 4 5 >list* \ creates a new list and loads all items from the stack into it ``` ```forth @@ -342,7 +342,7 @@ Maps contain key value pairs. ``` ```forth -'key1' 'value1' map* \ creates a new map and loads all items from the stack into it. There must be an even number of items on the stack. +'key1' 'value1' >map* \ creates a new map and loads all items from the stack into it. There must be an even number of items on the stack. ``` ```forth diff --git a/src/main/res/raw/collections.forth b/src/main/res/raw/collections.forth index 40032bc..bf4dd38 100644 --- a/src/main/res/raw/collections.forth +++ b/src/main/res/raw/collections.forth @@ -58,16 +58,15 @@ : peel ( l -- .. ) -> lst lst { } each ; : peel# ( m -- .. ) -> m m { dup 1st swap 2nd } each ; -: list* ( .. -- l ) +: >list* ( .. -- l ) -> lst depth 0 do lst swap add loop lst reverse ; -: map* ( .. -- m ) - depth odd? if 'expected even number of items for a map*' abort then +: >map* ( .. -- m ) + depth odd? if 'expected even number of items for a >map*' abort then -> m depth 2 / 0 do m -rot put loop - m . m ; : #[ ( -- ) depth >r rswap ; @@ -110,4 +109,6 @@ : maxl ( ls -- n ) nil => result { result @ nil != if result @ max then result ! } each - result @ ; \ No newline at end of file + result @ ; + +: map* ( .. q -- .. ) >r >list* r> map peel ; \ No newline at end of file diff --git a/src/main/res/raw/misc.forth b/src/main/res/raw/misc.forth index 0c996f2..b5b9f87 100644 --- a/src/main/res/raw/misc.forth +++ b/src/main/res/raw/misc.forth @@ -19,11 +19,13 @@ : match: immediate ` lastword set-predicate ; +: round* { round } map* ; + : npv ( cashflow rate -- n ) -> rate 0 => year { rate year @ dis year inc } map sum ; -: npv* ( .. rate -- n ) >r list* r> npv ; +: npv* ( .. rate -- n ) >r >list* r> npv ; : npv/npv' ( cashflow rate -- npv/npv' ) 1+ -> rate 0 => n @@ -47,13 +49,13 @@ var: irr-guess 0 irr-guess ! loop nil ; -: irr* ( cashflow -- n ) list* irr ; +: irr* ( cashflow -- n ) >list* irr ; 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 ; +: 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/main/res/raw/plot.forth b/src/main/res/raw/plot.forth index 99563aa..001f8dd 100644 --- a/src/main/res/raw/plot.forth +++ b/src/main/res/raw/plot.forth @@ -90,6 +90,6 @@ var: oy x inc } each ; -: plots ( .. -- ) depth 0 != if list* plotl then ; +: 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 e5cde19..681cbe0 100644 --- a/src/test/java/com/vectron/fcl/FclTest.java +++ b/src/test/java/com/vectron/fcl/FclTest.java @@ -15,6 +15,7 @@ import org.junit.Test; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.LinkedHashMap; @@ -857,6 +858,8 @@ public class FclTest { assertEquals(55, evalPop(": tst 0 1 10 .. { + } each ; tst").intValue()); assertEquals("[ 1 3 5 7 9 ]", evalPop(": tst 1 10 .. { odd? } filter ; tst").toString()); assertEquals("[ 100 102 104 ]", evalPop(": tst 100 105 .. { even? } filter ; tst").toString()); + assertEquals(asList(1l, 4l, 9l), evalGetStack(": tst 1 2 3 { dup * } map* ; tst")); + assertEquals(asList(1l, 2l, 4l), evalGetStack("1.1 2.2 3.8 round*")); } @Test @@ -880,7 +883,7 @@ public class FclTest { assertEquals("[ 'a' 'c' ]", evalPop("[ 'a' 'b' 'c' ] dup 'b' remove").toString()); assertEquals(asList(1l, 2l, 3l, 4l), evalGetStack("[ 1 2 3 4 ] peel")); assertEquals(asList(1l, 2l, 3l, 4l), evalGetStack(": tst [ 1 2 3 4 ] peel ; tst")); - assertEquals("[ 1 2 3 4 ]", evalPop("[ 1 2 3 4 ] peel list*").toString()); + assertEquals("[ 1 2 3 4 ]", evalPop("[ 1 2 3 4 ] peel >list*").toString()); } @Test @@ -906,9 +909,9 @@ public class FclTest { assertEquals(asList("b", 2l, "a", 1l), evalGetStack("#[ 'a' 1 'b' 2 ]# peel#")); assertEquals("#[ 'b' 2 'a' 1 ]#", - evalPop("'a' 1 'b' 2 map*").toString()); + evalPop("'a' 1 'b' 2 >map*").toString()); assertEquals("#[ 'a' 1 'b' 2 ]#", - evalPop("#[ 'a' 1 'b' 2 ]# peel# map*").toString()); + evalPop("#[ 'a' 1 'b' 2 ]# peel# >map*").toString()); assertEquals("[ 'a' 1 ]", evalPop("#[ 'a' 1 'b' 2 ]# peel nip").toString()); assertEquals("[ 'b' 2 ]", @@ -979,10 +982,10 @@ public class FclTest { resetForth(); } try { - eval("1 map*"); + eval("1 >map*"); fail("expected abort"); } catch (Aborted e) { - assertEquals("expected even number of items for a map*", e.getMessage()); + assertEquals("expected even number of items for a >map*", e.getMessage()); resetForth(); } }