Author Topic: Implementation of a Stack or LIFO in TBASIC  (Read 230 times)

garysdickinson

  • Expert Member
  • Posts: 460
  • Old PLC Coder
    • View Profile
Implementation of a Stack or LIFO in TBASIC
« on: January 06, 2020, 11: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
« Last Edit: January 07, 2020, 12:37:20 AM by garysdickinson »

garysdickinson

  • Expert Member
  • Posts: 460
  • Old PLC Coder
    • View Profile
Re: Implementation mentation of a Stack or LIFO in TBASIC
« Reply #1 on: January 06, 2020, 11:32:09 PM »
Documentation for Stack.PC6
« Last Edit: January 07, 2020, 12:36:45 AM by garysdickinson »

garysdickinson

  • Expert Member
  • Posts: 460
  • Old PLC Coder
    • View Profile
Demo Program that uses the Stack
« Reply #2 on: January 08, 2020, 12:57:08 AM »
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
« Last Edit: January 08, 2020, 01:01:54 AM by garysdickinson »