Internet PLC Forum

General => Technical support => Topic started by: garysdickinson on January 06, 2020, 03:31:35 PM

Title: Implementation of a Stack or LIFO in TBASIC
Post by: garysdickinson on January 06, 2020, 03:31:35 PM
This post shows how to implement a stack or Last In First Out data structure in TBASIC.

This example builds the stack in DM32[]. 
The size and location of the stack is controlled by entries in the #Define table.
Separate custom functions to initialize, push data and pop data are written to be as small and fast as possible.

The Stack.PC6 file is attached to this post. This is test bed code that will run in the i-TRiLOGI  simulator. This code should be easily adaptable for many uses.

The next post will have post a bit of documentation on how the stack operates.

Best regards,

Gary Dickinson
Title: Re: Implementation mentation of a Stack or LIFO in TBASIC
Post by: garysdickinson on January 06, 2020, 03:32:09 PM
Documentation for Stack.PC6
Title: Demo Program that uses the Stack
Post by: garysdickinson on January 07, 2020, 04:57:08 PM
I attached a simple program that uses the stack mechanism.

The code shows a method to generate a string representation of a 32-bit signed value.  My version allows you to specify a field width and will right justify the value in a column width that you specify.

The demo code is similar in behavior to the TRI Str$(n,d) function.  The TRI version pads the converted value with leading zeros which are not very useful for my purposes.

The demo code is just that.  If I really want to right justify a 32-bit integer value in a fixed-width column I use code like this:

a$ = Str$(n) : a$ = mid$("                  ",1,d-len(a$)) + a$


I apologize for the fact that this code is 100% unreadable, it's just the best you can do in TBASIC.
 
Best regards,

Gary Dickinson