Stack Engine

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Posts: 87
Joined: Feb 11, 2013 12:23

Stack Engine

Postby mrminecrafttnt » Jul 18, 2019 2:12

This one ist Programmed to make an Stack possible in Freebasic, actually only for Double Values!!
For more Information:

Code: Select all

type virtual_stack
    stack_data as any ptr
    stack_pointer as any ptr
    stack_ptr_start as any ptr
    stack_ptr_stop as any ptr
    stack_size as uinteger
    stack_errorcode as byte
    declare sub create (s as uinteger)
    declare sub destroy
    declare sub push (value as double)
    declare function pop as double
    declare function get_errorcode as ubyte
end type

sub virtual_stack.create(s as uinteger)
    if stack_size = 0 then
        stack_data = allocate(s*len(double))
        stack_ptr_start = stack_data
        stack_ptr_stop = stack_data + (s*len(double))
        if stack_data > 0 then
            stack_size = s*len(double)
            stack_pointer = stack_ptr_stop
            print "SET"
            stack_errorcode = -1
        end if
    end if
end sub

sub virtual_stack.push (value as double)
    dim as double ptr x = stack_pointer
    *x= value
end sub

function virtual_stack.pop as double
    dim d as double ptr = stack_pointer   
    return *d
end function

sub virtual_stack.destroy
    if (stack_errorcode = 0) and (stack_size > 0) and (stack_data > 0) then
        stack_pointer = stack_ptr_stop-len(double)
        if stack_errorcode = 0 then push 0 else print "STACK ERRROR DST:";stack_errorcode
        stack_errorcode = -3
    end if
    if stack_errorcode = 0 then deallocate(stack_data)
end sub

'### DEMO ###

dim as virtual_stack mystack
const stacksize = 1024
locate ,,0
    for i as integer = 0 to stacksize-1
        locate 1,
        print "PUSH"
        print i;"  ",HEX(mystack.stack_pointer)
        mystack.push i
    for i as integer = 0 to stacksize-1
        locate 1,
        print "POP "
        print mystack.pop;"  ",HEX(mystack.stack_pointer)
if mystack.stack_errorcode = 0 then print "NO ERROR" ELSE PRINT "STACK ERROR : ";mystack.stack_errorcode
Posts: 8180
Joined: May 28, 2005 3:28

Re: Stack Engine

Postby D.J.Peters » Jul 18, 2019 22:36

Yes stack's are useful some times you know Generic Macro for Dynamic User Stack Type also ?


Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest