Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BASFILE - Converts small files to BAS code.
#31
foo my gaad!  Big Grin
Reply
#32
Compile that code in M$QuickBASIC you get an error at the second line.
Reply
#33
(09-29-2023, 02:31 AM)SMcNeill Wrote:
(09-28-2023, 07:16 PM)RhoSigma Wrote:
(09-28-2023, 06:51 PM)SMcNeill Wrote: You guys make me cry, reusing A for both string and long in the same function.

I regularly have those duplicate/triple etc. uses in my programs, no problem as all generate different variables on the C/C++ side of things.

Code: (Select All)
DIM a$, a%%, a~%%, a%, a~%, a&, a~&, a&&, a~&&, a!, a#, a##

turns into:

Code: (Select All)
__STRING_A->len=0;
*__BYTE_A=0;
*__UBYTE_A=0;
*__INTEGER_A=0;
*__UINTEGER_A=0;
*__LONG_A=0;
*__ULONG_A=0;
*__INTEGER64_A=0;
*__UINTEGER64_A=0;
*__SINGLE_A=0;
*__DOUBLE_A=0;
*__FLOAT_A=0;

Here's why I'd never do this:



And the code of this abomination:

Code: (Select All)
TYPE foo
    foo AS STRING
END TYPE
DIM foo AS foo

INPUT "Give me an integer number =>"; foo&
foo$ = STR$(foo&)
foo.foo = LEFT$(foo$, 1)
GOSUB foo
PRINT "Foo"; foo.foo

END
foo:
foo% = ASC(foo.foo)
foo# = foo% + RND * 255
foo& = foo# MOD 256
foo$ = STR$(foo&)
foo&& = ASC(foo$) - RND * 256
foo~%% = foo&& MOD 256
foo.foo = CHR$(foo~%%)
RETURN

Now, I offer a cookie to the first person who can decipher what the heck is going on here, and explain WHY foo.foo is printing out its own "Foo" for us, when the last assigned command for it before the PRINT statement is a very simple:

foo.foo = CHR$(foo~%%)

Shouldn't this be a single ASCII character?  What the heck is going on here?  How'd we get this output?  And could anyone trace this and debug it if there was 100 lines of other code involved here, doing other things in between this mess?

Well Steve, it's your way to turn a made statement into something completely different.

We originally were talking about SIMPLE variables, not about TYPEs, its elements or even program labels, those have their own different namespace.

The made statement was about same named SIMPLE VARIABLES, that's it.

But to answer your question, my guess is that the error already happens in the IDE/Compiler when the code is translated into C/C++. I've seen many places in the QB64pe.bas source which handles variable typing and never got a clue what's exactly done in there.

A computer NEVER does what we expect, it simply is doing what's PROGRAMMED.
Reply
#34
The truth to the glitch in the little program I posted is sooo simple, you'd never see it:

PRINT "Foo"; foo.foo

Quote:Now, I offer a cookie to the first person who can decipher what the heck is going on here, and explain WHY foo.foo is printing out its own "Foo" for us, when the last assigned command for it before the PRINT statement is a very simple:

So why's it printing a "Foo" on the screen? 

WE TOLD IT TO!!  Wink

The glitch is simply in the PRINT statement itself, with us having it print a literal "Foo" that wasn't supposed to be in there.  The issue that arises however, is that once you start checking for all those foo + suffixes, you get lost in trying to track which is which and what the heck they're all supposed to represent, and it's dang easy to just skim over a literal value of "Foo" in quotes, and not even notice it.

The labels and types just help exacerbate the issue, but you've got to admit:  Even without those, just tracking each suffix value for that same variable, and keeping it straight as a separate entity of all the others with the same name, takes more work, effort, and concentration on the part of the programmer.

If I'm going to be debugging code, I'd much rather just debug the variable A, rather than having to sort out A from A&, a%, A&&, A#, A~` and A$3.  Heck, I get completely lost when I see folks type something like DIM A$3, A3$ -- which are two different beasts completely, and both perfectly valid!

Now, I'm not saying doing such things isn't possible.  In fact, if that's someone's personal style and they can keep it all straight and never have any issues with it, more power to them!  It's just the type of code that tends to make me cry and pull my hair out whenever someone pops up on the forum or in an email and says, "Hey, can you help me sort this out?"  Wink

And apologies to @Dav for hijacking his post with my own personal coding issues.  /blush
Reply
#35
Quote:And apologies to @Dav for hijacking his post with my own personal coding issues. /blush

The blame is not as much yours as it is mine. I only proposed a "DIM" line in case somebody is impatient enough to try "BASFILE" but has to use `OPTION _EXPLICIT`. There are quite a few around here including myself. Smile
Reply
#36
All code should be written with OPTION EXPLICIT.
Tread on those who tread on you

Reply
#37
(09-29-2023, 10:07 AM)mnrvovrfc Wrote:
Quote:And apologies to @Dav for hijacking his post with my own personal coding issues. /blush

The blame is not as much yours as it is mine. I only proposed a "DIM" line in case somebody is impatient enough to try "BASFILE" but has to use `OPTION _EXPLICIT`. There are quite a few around here including myself. Smile

No worries, it's Dav's own Forum on the "Prolific Programmers" board here, so he has admin rights here an can delete the whole mess we created.

Sorry Dav
Reply
#38
Hey, I'm kinda tickled my little area has some traffic. Big Grin 

@mnrvovrfc: Good suggestion adding the DIM line.  The goal is to output a portable FUNCTION people can add to any program easy, and that will help make it so.

I admit I haven't used OPTION EXPLICIT before, and didn't really understand what it was for until I heard Fellippe talk about it in a podcast.  I think it will help me prevent coding errors, so I'm going to get to know it.   Also, I am very bad about naming variables well (I rush coding) so that is something I will try to work on.

I think these Base91 functions are working without errors. I scrapped the ones I came up with because they kept corrupting data, so I went back to these I adapted from sources on the internet in another language.  These seem to work ok.

These haven't been speed up using the MID$ technique yet, will do that next.  Will have to do something different than the Base85 function do.  Since Base91 encoded output size is not 100% predictable like Base64 & Base85 output is (it can fluctuate according to input data), I will have to save the original input size in the B91 encoded data, and then strip it out during decoding.

- Dav

Code: (Select All)
'==========
'BASE91.BAS
'==========
'Base-91 Encoder/Decoder Functions.
'Coded by Dav for QB64-PE 3.8.0, SEP/2023
'(adpated from sources found on the web)

'NOTE: This uses a modified Base-91 character set so code output doesn't
'include symbols that conflict with the QB64-PE forum message posting.

'Unlike Base64 & Base85, Base91 space efficiency changes according to input.
'It is somewhere near 23% percent added to the input source, a wee bit better
'than Base85 which is 25% more, and much better than Base64 which is 33%.

Screen 12

_ControlChr Off

Do
    Cls
    Color 15, 1: Print "Checking bytes loop -- Hit any key to stop checking ..."
    a$ = ""
    Print
    Color 7, 0: Print "Original:"
    bytes = 200 + Int(Rnd * 55)
    For i = 0 To bytes
        a$ = a$ + Chr$(Int(Rnd * 255))
    Next
    Print a$
    Print Len(a$); "bytes"

    Print
    Print "Encoded:"
    a2$ = Base91Encode$(a$): Print a2$
    Print Len(a2$); "bytes"
    Print
    Print "Decoded:"
    a3$ = Base91Decode$(a2$): Print a3$
    Print Len(a3$); "bytes"
    Print

    If Len(a$) <> Len(a3$) Then Color 12, 0: Print "Size don't match!"; t: End
    If a$ <> a3$ Then Color 12, 0: Print "Bytes don't match!"; t: End
    _Display

    _Limit 15

    If _KeyHit Then Exit Do

    loopcount = loopcount + 1
    bytecount = bytecount + bytes

Loop

Print "Checked"; loopcount; "loops of"; bytecount; "bytes."
Print "No errors detected."
End

Function Base91Encode$ (in$)

    'Build 91 characters to use
    For i = 33 To 126
        If i <> 34 And i <> 64 And i <> 96 Then Chars$ = Chars$ + Chr$(i)
    Next

    For i& = 1 To Len(in$)
        bits = (bits Or (Asc(Mid$(in$, i&, 1)) * (2 ^ nbits)))
        nbits = nbits + 8
        If nbits > 13 Then
            value = bits Mod 8192
            If value > 88 Then
                bits = (bits \ (2 ^ 13))
                nbits = nbits - 13
            Else
                value = bits Mod 16384
                bits = (bits \ (2 ^ 14))
                nbits = nbits - 14
            End If
            out$ = out$ + Mid$(Chars$, (value Mod 91) + 1, 1) + Mid$(Chars$, (value \ 91) + 1, 1)
        End If
    Next

    If nbits > 0 Then
        out$ = out$ + Mid$(Chars$, (bits Mod 91) + 1, 1)
        If nbits > 7 Or bits > 90 Then
            out$ = out$ + Mid$(Chars$, (bits \ 91) + 1, 1)
        End If
    End If

    Base91Encode$ = out$

End Function



Function Base91Decode$ (in$)
    'Build 91 characters to use
    For i = 33 To 126
        If i <> 34 And i <> 64 And i <> 96 Then Chars$ = Chars$ + Chr$(i)
    Next
    value = -1
    For i& = 1 To Len(in$)
        ascii = InStr(Chars$, Mid$(in$, i&, 1)) - 1
        If value < 0 Then
            value = ascii
        Else
            value = value + (ascii * 91)
            bits = bits Or _ShL(value, nbits)
            nbits = nbits + 13 + (((value And 8191) <= 88) * -1)
            Do Until (nbits > 7) = 0
                out$ = out$ + Chr$(bits And 255)
                bits = _ShR(bits, 8)
                nbits = nbits - 8
            Loop
            value = -1
        End If
    Next

    If (value + 1) Then
        out$ = out$ + Chr$((bits Or _ShL(value, nbits)) And 255)
    End If

    Base91Decode$ = out$

End Function

Find my programs here in Dav's QB64 Corner
Reply
#39
I might have to make me another GUI frontend for your code, Dav! No promises, though. I rarely code for fun anymore. But I think it would be nice to not only make a GUI frontend but make it have a radio button for Base64, Base85, and Base91 for both BIN2BAS and PIC2MEM.
Tread on those who tread on you

Reply
#40
Another update to BASFILE.

I made it a dialog only app, there's no main window.  This hopefully makes it more user friendly.

I also added the DIM line that was suggested to make it OPTION _EXPLICIT friendly.

Also it now saves the encoded filename in the output function.

Probably this will be the last update for a while unless there's an error that needs corrected.  I have tested it in Windows & Linux.  I would appreciate any feedback from a Mac user if it works OK or not.  Thanks!

@SpriggsySpriggs, sure go ahead. You may use them however you want.

- Dav

Find my programs here in Dav's QB64 Corner
Reply




Users browsing this thread: 4 Guest(s)