Add ANS structure support (#128)
authorPhil Burk <philburk@mobileer.com>
Mon, 5 Dec 2022 15:58:51 +0000 (07:58 -0800)
committerGitHub <noreply@github.com>
Mon, 5 Dec 2022 15:58:51 +0000 (07:58 -0800)
BEGIN-STRUCTURE and END-STRUCTURE
+FIELD FIELD: and CFIELD:
FFIELD: for float members

fth/floats.fth
fth/loadp4th.fth
fth/structure.fth [new file with mode: 0644]
fth/t_corex.fth
fth/t_floats.fth

index 8a73f7c06b6d7743295f19f0847cd5ba81b8ec86..d80738e00d2124c1cd8039e6831c51f5e83d0fdb 100644 (file)
@@ -64,7 +64,12 @@ fp-create-size swap - constant CREATE_SIZE
     fdepth 0 max  0 ?DO fdrop LOOP
 ;
 
-\ Convert between single precision and floating point
+\ Floating point structure member.
+: FFIELD:   ( n1 "name" -- n2 ; addr1 -- addr2 )
+    FALIGNED 1 FLOATS +FIELD
+;
+
+\ Convert between single precision integer and floating point
 : S>F ( s -- ) ( F: -- r )
     s>d d>f
 ;
index 8afa2d99a66c698c0b25b7da0bda5145e207ef97..79550b4437beee36a13a757a1f0bf541585c380e 100644 (file)
@@ -20,6 +20,7 @@ include? forget  forget.fth
 include? >number numberio.fth
 include? task-misc1.fth   misc1.fth
 include? case    case.fth
+include? +field  structure.fth
 include? $=      strings.fth
 include? privatize   private.fth
 include? (local) ansilocs.fth
diff --git a/fth/structure.fth b/fth/structure.fth
new file mode 100644 (file)
index 0000000..0acce21
--- /dev/null
@@ -0,0 +1,40 @@
+\ Structures and fields.
+\
+\ The code is based on the implementation from the ANS standard.
+\
+\ Permission to use, copy, modify, and/or distribute this
+\ software for any purpose with or without fee is hereby granted.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+\ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+\ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+\ THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+\ CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+\ FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+\ CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+\ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+anew task-structure.fth
+
+: BEGIN-STRUCTURE ( "<spaces>name" -- struct-sys 0 , start the definition of a structure )
+    CREATE
+        HERE 0 0 ,      \ mark stack, lay dummy
+    DOES> @             \ -- structure-size
+;
+
+: END-STRUCTURE ( addr n -- , terminate a structure definition )
+    SWAP !
+;
+
+: +FIELD  ( n <"name"> -- ; Exec: addr -- 'addr )
+    CREATE OVER , +
+    DOES> @ +
+;
+
+: FIELD:    ( n1 "name" -- n2 ; addr1 -- addr2 )
+    ALIGNED 1 CELLS +FIELD
+;
+
+: CFIELD:   ( n1 "name" -- n2 ; addr1 -- addr2 )
+    1 CHARS   +FIELD
+;
index c37252ac4b5673a5b6a613791202c52750855f1c..a45114fdd4c746fb6a4051ab6f269bf537485290 100644 (file)
@@ -344,5 +344,29 @@ T{ [undefined] if }T{ false }T  \ high level
 T{ [undefined] dup }T{ false }T \ in kernel
 T{ [undefined] k23jh42 }T{ true }T
 
+\  ----------------------------------------------------- Structures
+
+BEGIN-STRUCTURE XYZS
+    cfield: xyz.c1
+    field:  xyz.w1
+    cfield: xyz.c2
+END-STRUCTURE
+
+T{ xyzs }T{ 2 cells 1+ }T
+T{ 0 xyz.c1 }T{ 0 }T
+T{ 0 xyz.w1 }T{ cell }T
+T{ 0 xyz.c2 }T{ 2 cells }T
+
+CREATE MY-XYZS XYZS ALLOT
+\ test forward order
+77 my-xyzs xyz.c1 c!
+1234567 my-xyzs xyz.w1 !
+99 my-xyzs xyz.c2 c!
+
+T{  my-xyzs xyz.c1 c@ }T{ 77 }T
+T{  my-xyzs xyz.w1 @ }T{ 1234567 }T
+T{  my-xyzs xyz.c2 c@ }T{ 99 }T
+
+
 }TEST
 
index 05612beee7b00ddbeab608f44cbb68faddfe42cd..b0fe98b8ef6c2e2bdbc936a65714cb18c6858e5f 100644 (file)
@@ -129,6 +129,33 @@ T{  PI 2.0 F*  FSIN   0.0   0.00001 F~    }T{  true  }T
 T{  PI 0.5 F*  FSIN   1.0   0.00001 F~    }T{  true  }T
 T{  PI 6.0 F/  FSIN   0.5   0.00001 F~    }T{  true  }T
 
+\  ----------------------------------------------------- FROUND
+T{  0.1 FROUND   0.0  0.0 F~ }T{ true }T
+T{  6.6 FROUND   7.0  0.0 F~ }T{ true }T
+T{ -3.2 FROUND  -3.0  0.0 F~ }T{ true }T
+T{ -8.8 FROUND  -9.0  0.0 F~ }T{ true }T
+
+\  ----------------------------------------------------- FFIELD:
+BEGIN-STRUCTURE ABCS
+    field:  abc.w1
+    ffield: abc.f1
+    field:  abc.w2
+END-STRUCTURE
+
+T{ 0 abc.w1 }T{ 0 }T
+T{ 0 abc.f1 }T{ 1 floats }T  \ aligns to next float boundary
+T{ 0 abc.w2 }T{ 2 cells }T
+T{ abcs }T{ cell  1 floats +  cell + }T
+
+CREATE MY-ABCS ABCS ALLOT
+6543 my-abcs abc.w1 !
+23.45 my-abcs abc.f1 f!
+98765 my-abcs abc.w2 !
+
+T{  my-abcs abc.w1  @ }T{ 6543 }T
+T{  my-abcs abc.f1 f@ 23.45 0.0 F~ }T{ true }T
+T{  my-abcs abc.w2  @ }T{ 98765 }T
+
 \  ----------------------------------------------------- \
 }TEST