Building Structs in FORTH20XX

2015-12-28 03:24:00 UTC

Newer (> 2012) versions of FORTH allow you to create struct datatypes in a similar fashion to C.


\ Define a structure named point
BEGIN-STRUCTURE point
   FIELD: ->x
   FIELD: ->y
END-STRUCTURE

\ Create a new point named vertex
variable vertex point

\ set the x value of vertex to 128
128 vertex ->x !

\ Push the value of vertex's x value onto the stack
vertex ->x @

\ show the results
." The value of x in variable `vertex` is: " dup . cr

bye

UP NEXT: Defining float, char, byte and double member types. further reading