Queue/Usage: Difference between revisions

Line 810:
Forth is a low level language the runs on a virtual machine with 2 stacks. One stack for Parameters and the second is the call/return stack. Coding begins at an almost assembler like level but the work results in a higher level language.
<br>In this demonstration code we show a feature of Forth that is one of the earliest examples of simple object creation using the word CREATE. With this mechanism we create a queue constructor that can build queue data structures of different sizes. Then we create two operators that enqueue a byte and dequeue a byte. The queue's address is passed to these operators on the data stack.
 
<BR>Implementations in other languages or libraries might use a linked list that could potentially consume all memory. Creating a static circular queue is more typical for Forth where it is commonly used in embedded high reliability systems. The code here makes use of the fact that if the queue size is a power of 2, the circular wrap around can be implemented without an IF statement, and uses logical AND with binary mask to wrap around.
<BR> NOTE: We also used a more Forth like naming convention QC@ (queue char fetch) and QC! (queue char store) rather than PUSH and POP which as stack users we felt were more appropriate for a Stack than a Queue.
Anonymous user