Page 5 of 5 FirstFirst ... 345
Results 41 to 48 of 48

Thread: StandAlone Oxygen

  1. #41
    Calling subroutines via a table:

    sys tgs={@sr1,@sr2,@sr3} 'subroutine table
    '
    subroutine sr1
      print 10
    end subroutine
    subroutine sr2
      print 20
    end subroutine
    subroutine sr3
      print 30
    end subroutine
    '
    gosub tgs[2] '20
    

  2. #42
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    calling the function|subroutine by its ptr, i see.
    Something i saw in oxygen but don't remember where it was and what was the exact syntax but i am certain it was oxygen, it was like

    Function f() as Int
    {
    
    Dim Int result At @f 
    '...
    result = 42
    }
    
    was it really that simple ? is the function-pointer also the pointer where to read or write the result?

    Ans omething that annys me now for a while and i don't know why:
    I am passing parameters BYVAL to a function - i tried already to use CAST and lots more- and usually BYVAL can not be parameter type mismatch
    but it keeps complaining i would pass double - even i dont.
    Same is when i use Extended type as function parameters - it always complains about DOUBLE and parameter type mismatches.


    I just post the script.
    The bugging line is 63, (### marked ###) but as you see in StrRight at the same position where i used ABS it is the same . Actually i would prefer to use the limit- functions without the need to cast for integer types mainly - and to return also integers without the need to cast parameters nor results
    
    $ filename "teststr.exe"
    $ CRLF chr(13) chr(10)
    $ DQ chr(34)
    
    
    
    
    
    
    Function StrLen(byref s as bstring) as dword 
    ===================================
    dim dword dwLen at strPtr s
        
        if @dwLen then 
            dword dwLen at (strptr s) - 4
            Function = dwLen
        end if
    end function 
    
    
    
    
    function InLimits(byval nX as int, byval nMin as int, byval nMax as int) as boolean 
    ' should tell if numberX is within limits nMin and nMax (inclusive)
        if nX < nMin then return false
        if nX > nMax then return false
        function = true 
    end function 
    
    
    function SetLimits(byval nX as int, byval nMin as int, byval nMax as int) as int 
    ' should return number nX limited between nMin and nMax if outside that range 
        if nX < nMin then return nMin
        if nX > nMax then return nMax
        function = nX 
    end function 
    
    
        
    ------------------------------------
    function StrLeft(byref sX as Bstring, 
                     byval numbytes as int) as bstring
    =============================================================
    ' get the left part (in  bytes count )  passed through lbytLen 
    '       e.g. assumed sX ="1234567890"   
    '  when lbytLen is a: 
    '    value > 0 but <= len(sX) it works as Left(sX, lbytLen) 
    '           e.g. assumed lbytLen = 3 returned is "123"
    '    Value < 0 but abs(lbytLen) <= len(s) it works as Left(s, Len(s)-lbytLen): 
    '         e.g. assumed lbytLen = -3, returns  "1234567"
    
    
    
    
    ' StrRight works exactly opposing to StrLeft
    static sResult as bstring 
    dim dword dwLen at strPtr sX
    
    
    if @dwLen then 
        dword dwLen at (strptr sX) - 4
         '# # # # # # #
         if inLimits( sgn(numbytes) * numBytes, 1, dwLen ) then 
          '# # # # # # #
            if numBytes < 0 then 
                numBytes = dwLen + numbytes 
                sResult = String(numBytes, chr(0))
                copy strPtr sResult, strPtr sX, numBytes 
            else 
                sResult = String(numBytes, chr(0))
                copy strptr sResult, strptr sX, numBytes 
            endif 
        endif
    endif 
    
    
    function = sResult
    
    
    end function 
    -------------------------------------------
    function StrRight(byref sX as Bstring, 
                      byval numbytes as int) as bstring
    =============================================================
    ' get the right part  of sX
    '       e.g. assumed sX ="1234567890"   
    '  when 
    '       numbytes > 0 and <= len(s) it works as Right(s, numbytes) 
    '           e.g. assumed numbytes = 3, returns "890"
    '      numbytes < 0 and abs(numbytes) <= len(s) it works as Right(sX, Len(sX)+numbytes)
    '         e.g. assumed numbytes=-3, returns "4567890"
    
    
    
    
    ' StrRight works exactly opposing to StrLeft
    dim sResult as bstring 
    dim dword dwLen at strPtr sX
    
    
    if @dwLen then 
        dword dwLen at (strptr sX) - 4    
        '# # # # # # #
        if inLimits(abs numBytes, 1, dwLen ) then 
        '# # # # # # #
             if numBytes < 0 then 
                sResult = String(dwLen + numBytes, chr(0))
                copy strPtr sResult, (strPtr sX)+abs(numBytes), dwLen + numBytes 
            else 
                sResult = String(numBytes, chr(0))
                copy strptr sResult, (strptr sX) + dwLen - numBytes, numBytes 
            endif 
        endif
    endif 
    
    
    function = sResult
    
    
    end function 
    
    
    
    
    dim x1, x2, x3, xx as bstring 
    
    
    x1 = "A b c d e f g h i j k l m n o p q r s t u v w x y z"
    x2= strLeft x1, 20
    x3= strLeft x1,-10
    
    
    xx = "String " DQ x1 DQ CRLF Str(strLen x1) " bytes of length" crlf 
    xx &= "StrLeft s, 20 = " DQ x2 DQ str(StrLen x2) " bytes" crlf
    xx &= "StrLeft s,-10 = " DQ x3 DQ str(StrLen x3) " bytes" crlf
    
    
    x2= strRight x1, 20
    x3= strRight x1,-10
    
    
    xx &= "StrRight s, 20 = " dq x2 DQ str(strlen x2) " bytes" crlf 
    xx &= "StrRight s, -10 = " dq x3 DQ str(strlen x3) " bytes" crlf 
     
    print xx
    
    btw. are bStrings the default ? can i also peek a dword in front of a "String"-string without to risk a gpf?
    Last edited by ReneMiner; 27-05-2024 at 17:54.
    I think there are missing some Forum-sections as beta-testing and support

  3. #43
    Integer functions return values in the eax register. This code makes no sense:

    Function f() as Int
    {

    Dim Int result At @f
    '...
    result = 42
    }



    The standard string type is 'string' which contains a garbage-collected bstring. But you can use bstrings directly if you do your own garbage collection.


    inlimits macro function accepting all types:

    
    
    
    macro inlimits int(r,  a,min,max)
    =================================
    r=-1
    if a<min then r=0
    if a>max then r=0
    end macro
    


    PS: abs(..) requires brackets



    Last edited by Charles Pegge; 27-05-2024 at 19:48.

  4. #44
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174

    Post

    Quote Originally Posted by Charles Pegge View Post
    Integer functions return values in the eax register. This code makes no sense:

    Function f() as Int
    {

    Dim Int result At @f
    '...
    result = 42
    }
    I know, it was just to visualize the context where it applies. The actual required information to that was:

    Is it really that easy : the function-result can be written to the functions address when the result type is numeric?

    I am not sure about it and what syntax to use since i want to avoid the risk of GPF with pointers - which i experienced very frustrating worst-case-scenario in the past, when it completely made all partitions of a connected hard-disk unreadable and all contained data was irreversible lost forever.
    Even when error-trapping is used to the most-possible-best-most grade of what reality provides: if a GPF occurs - the trap is instantly terminated with the faulty program before it could catch anything and the risk of that is around 50:50, meaning will happen FOR SURE with only a little chance for stupid users that are not aware of the possibility to remain lucky in the current process but for
    100% without any chance for someone who has ever read what the 3 chars GPF will be expanded to.



    And Strings? Is the functions ptr in that case equal to a VarPtr that points a StrPtr?

    Can i swap the functions-result-StrPtr with some other defined (local or global, whatsoever) string X
    to avoid the creation of a copy of the string X's content (it would require time-consuming memory-allocation when string X is pretty large)

    assumed of course- the original/source-strings variable (here BString sDummy) is not required thereafter any more

    Intention is to swap the value that points a strings position - equal to the result of StrPtr(X) - with the value "@X" that i suspect at the position of a function (when function returns a string) and this "@X" is -alike Varptr(X) - when X were a string-variable in powerbasic -
    the value pointing to the first byte of data for a Stringvariable X

    but since O2basic does not seem to have a function working as VarPtr()
    (only a guess):
    "@X" would be the position to ask for and where to place virtually a sys-variable AT
    and it would make that
    - upon @X placed virtual sys variable - hold a value that is equal to the value returned by StrPtr(X) ?

    and a virtual Dword variable, dimensioned AT @X-4 were located exactly on the result of STRPTR(X) - 4
    (this Dword would be equal to the returned value of a Function as StrLen() - see below

    (Macro?) Function StrLen( s As String )  As Dword
    
    ' get length of a string to pass byref that will - as thinBasic LENF() 
    '(a faster working version of the common LEN()-function)  -
    ' not need to create a local copy (= byval function-parameter) 
    ' and obtain the length of the string that is already counted and present 
     
    if StrPtr(s) Then
         'Question: 
        ' will this lead to a problem when the first time this is called the StrPtr(s) is equal to zero
        ' and this If-clause gets skipped so the virtual variable dwLen is actually not defined after the first
        ' time execution of the StrLen-procedure?
    
        Dim Dword dwLen At StrPtr(s)-4
        StrLen = dwLen
    endif 
    
    end (Macro?) Function
    
    About Macro (Functions)? : can these just replace text or as (in powerbasic "Macro Function") calculate using variables local to the macro [function]
    as known from powerbasic, where it requires to introduce such variables (and local jump-labels) using some statement as
    MACROTEMP localVar, localLabel
    

    Now the main concern:

    I will comment it in form of a question what i expected to obtain in the current line,
    please correct if doable at all
    Function sf() As String       '( no result yet assigned )
    
    Dim sys funcPtr At @sf()  ' is *funcPtr now the position where to write a value that is the stringpointer for the result? 
    ' e.g.
    
    bString sDummy = "Hello!?"   ' (only create an example string here)
    
    sys *_varPtr = @sDummy  ' does @sDummy hold the position where to obtain StrPtr of sDummy? 
                                            ' Is '*_varPtr' equal to result of 'StrPtr(sDummy)'
    sys _strPtr = @_varptr       ' will  '*_strPtr' now hold the same value as returned by StrPtr(sDummy)
    
    sys _temp                         ' temporary swap-container 
      ' when writing 
    Temp = *funcPtr  ' will this copy the original pointer  - that points a value that can be treated as the
    '                          Strptr(of by the function sf() returned string) - into Temp?
    
    *funcPtr=*_strPtr                ' will function sf() now return "Hello!?"
    
    *strPtr=Temp                      ' will exiting the function sf() correctly wipe out an (supposedly empty) BString sDummy 
                                              ' from the procedures local scope level that was the former result-string of function sf() 
    
    Dim Dword dwLen At  funcPtr   ' can I read the length (in bytes)  
    
    End Function
    
    I think there are missing some Forum-sections as beta-testing and support

  5. #45
    If you define a function f, its starting code address will be @f.

    Functions with parameters have signatures attached to the name. For instance:

    function f(string s,double d, int *f) as int would be have an address
    @f#string#double#int



    To get the address of any string content, use strptr(s). It will correctly resolve zstrings bstrings and strings.

    But only bstrings and strings (based on olestrings) have a length field located at strptr(s)-4.

    If a string is uninitialized the strptr will return 0. You will get a GPF if you try to reference it! But I think modern PCs are well protected from memory violations.



    MacroTemps in Oxygen are simply listed after the params for instance:
    macro m(a,b,c, tmp1,tmp2)

    Macro functions are specified by adding a return type and a return parameter at the start, for instance:
    macro f float(r, a,b)
    r=a+b
    end macro
    Last edited by Charles Pegge; 01-06-2024 at 21:50.

  6. #46
    'function' is a variable to hold return values

    I hope this example helps:
    function sf() as string
      function="ok"
      sys p=strptr(function)
      sys le at p-4
      print len(function) "  " le "  " p
    end function
    '
    print sf()
    

  7. #47
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    Quote Originally Posted by Charles Pegge View Post
    If you define a function f, its starting code address will be @f.

    Functions with parameters have signatures attached to the name. For instance:

    function f(string s,double d, int *f) as int would be have an address
    @f#string#double#int
    am a bit confused now of this one: i see "int" twice in function headline. The parameters and a result. Its missing the - what INT ?
    Results position would it be treated as
    common Array of int
    starting first element =3rd parameter second element = result?
    in this case because of same type?
    I think there are missing some Forum-sections as beta-testing and support

  8. #48
    Only the function parameter types are used to compose the signature, not the byref levels or return type. Hence the label f#string#double#int. (The main purpose of a function signature is to support function overloading / polymorphism.)

Page 5 of 5 FirstFirst ... 345

Similar Threads

  1. New Site for StandAlone Oxygen
    By Charles Pegge in forum O2h Compiler
    Replies: 6
    Last Post: 05-10-2010, 10:57

Members who have read this thread: 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •