From 15c0569404112905f0898656f5f7d4e794fbd056 Mon Sep 17 00:00:00 2001 From: crc Date: Wed, 6 May 2020 11:21:34 +0000 Subject: [PATCH] add a numeric ranges example FossilOrigin-Name: c3d30eebc6f43188cd59c30aa4e339e4186bf457d1f7de0d46a165618d73df21 --- example/ByteAddressing.retro | 37 ++++++++++++++++++------------------ example/numeric-ranges.retro | 31 ++++++++++++++++++++++++++++++ vm/nga-c-native-x86/image.c | 2 +- 3 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 example/numeric-ranges.retro diff --git a/example/ByteAddressing.retro b/example/ByteAddressing.retro index c2bda3d..49506c0 100644 --- a/example/ByteAddressing.retro +++ b/example/ByteAddressing.retro @@ -17,25 +17,24 @@ updated for RETRO12 by Charles Childers. ## Functions -+-------------------+--------+----------------------------+ -| Function | Stack | Used For | -+===================+========+============================+ -| b:to-byte-address | a-a | Return the byte address of | -| | | the first byte in a cell | -+-------------------+--------+----------------------------+ -| b:unpack | c-bbbb | Given a byte-packed cell | -| | | on the stack, return the | -| | | bytes it contains | -+-------------------+--------+----------------------------+ -| b:pack | bbbb-c | Pack four byes into a cell,| -| | | return the cell on the | -| | | stack | -+-------------------+--------+----------------------------+ -| b:fetch | a-b | Fetch a byte | -+-------------------+--------+----------------------------+ -| b:store | ba- | Store a byte into memory | -+-------------------+--------+----------------------------+ - + +-------------------+--------+----------------------------+ + | Function | Stack | Used For | + +===================+========+============================+ + | b:to-byte-address | a-a | Return the byte address of | + | | | the first byte in a cell | + +-------------------+--------+----------------------------+ + | b:unpack | c-bbbb | Given a byte-packed cell | + | | | on the stack, return the | + | | | bytes it contains | + +-------------------+--------+----------------------------+ + | b:pack | bbbb-c | Pack four byes into a cell,| + | | | return the cell on the | + | | | stack | + +-------------------+--------+----------------------------+ + | b:fetch | a-b | Fetch a byte | + +-------------------+--------+----------------------------+ + | b:store | ba- | Store a byte into memory | + +-------------------+--------+----------------------------+ ## Code & Commentary diff --git a/example/numeric-ranges.retro b/example/numeric-ranges.retro new file mode 100644 index 0000000..e9c4fe0 --- /dev/null +++ b/example/numeric-ranges.retro @@ -0,0 +1,31 @@ +This implements some words to return a range of numbers on the stack. +It's probably best to capture these values in an array. + +First is a word to return values from a lower to upper limit, incrementing +upwards. The returned values are inclusive of the limits. + +~~~ +:range (lh-a) + over - n:inc [ I over + swap ] times drop ; +~~~ + +Next is a word to return values from an upper to lower limit, decrementing +upwards. The returned values are inclusive of the limits. + +~~~ +:range (hl-a) + over &- dip swap n:inc [ I over swap - swap ] times drop ; +~~~ + +The last word takes the limits and calls the appropriate word. + +~~~ +:range (nn-a) + dup-pair gt? [ range ] [ range ] choose ; +~~~ + +As a simple test case: + +``` +{ #1 #5 range } [ n:put sp ] a:for-each +``` diff --git a/vm/nga-c-native-x86/image.c b/vm/nga-c-native-x86/image.c index 01c2e7c..7b7de28 100644 --- a/vm/nga-c-native-x86/image.c +++ b/vm/nga-c-native-x86/image.c @@ -3,7 +3,7 @@ #define CELL int32_t #endif CELL ngaImageCells = 13827; -CELL ngaImage[] = { 1793,13738,13728,13826,202004,0,10,1,10,2,10,3,10,4,10,5,10,6,10, +CELL ngaImage[] = { 1793,13738,13728,13826,202007,0,10,1,10,2,10,3,10,4,10,5,10,6,10, 7,10,8,10,9,10,10,11,10,12,10,13,10,14,10,15,10,16,10,17, 10,18,10,19,10,20,10,21,10,22,10,23,10,24,10,25,10,68223234,1,2575, 85000450,1,656912,3215,3224,268505089,63,62,135205121,63,10,101384453,0,9,10,2049,56,25,459011,74,