Author Topic: MrBcx vs AI  (Read 808 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2538
    • View Profile
MrBcx vs AI
« on: July 20, 2024, 01:26:03 PM »
I wanted to find -all- the recursive SUBS and FUNCTIONS inside Bc.bas

Being the lazy type, I told ChatGPT what I wanted.  After wasting well over an
hour yesterday, I gave up on that idea and decided to write it myself today.

Here are the recursive SUBS and FUNCTIONS:


JoinStrings
JoinStrTest
BracketHandler
MakeDecProto
GetParameter

Press any key to continue . . .


Here is how I found them:

Code: [Select]

DIM Stk$[256], Ndx, Filename$, Src$, NamedFunc$

Filename$ = "bc.bas"

IF NOT EXIST(Filename$) THEN
    PRINT "File not found ... Aborted."
    END
END IF

OPEN Filename$ FOR INPUT AS 1

WHILE NOT EOF(1)
    LINE INPUT 1, Src$
    Src$ = TRIM$(STRIM$(EXTRACT$(Src$, "'")))
    FastLexer(Src$, SPC$, "=&()[]{}',+-*/<>?;.|:^")
    IF LCASE$(Stk$[1]) = "sub" OR LCASE$(Stk$[1]) = "function" AND TRIM$(Stk$[2]) <> "=" THEN
        NamedFunc$ = Stk$[2]
        ITERATE
    END IF
    IF LCASE$(Stk$[1]) = "end" THEN
        IF LCASE$(Stk$[2]) = "sub" OR LCASE$(Stk$[2]) = "function" THEN
            NamedFunc$ = ""
            ITERATE
        END IF
    END IF
    FOR INT i = 1 TO Ndx
        IF Stk$[i] = NamedFunc$ THEN
            PRINT NamedFunc$
            NamedFunc$ = ""
            EXIT FOR
        END IF
    NEXT
WEND
CLOSE 1
PAUSE


SUB FastLexer (Arg$ AS LPCTSTR, delim1$ AS LPCTSTR, delim2$ AS LPCTSTR, TokQuote = 1)
    '**********************************************************************************
    ' delim1$ = delimiters to remove
    ' delim2$ = delimiters to keep
    '
    ' When TokQuote = 1: Quoted strings are treated as individual tokens.
    ' Each quoted string is processed separately, terminated, and stored
    ' in the token stack as a distinct token.
    '
    ' When TokQuote = 0: Quoted strings are not treated as separate tokens.
    ' They are included as part of larger tokens, potentially concatenated
    ' with other characters outside the quotes.
    '
    ' Stk$[] and Ndx are GLOBAL
    ' As long as Ndx is honored, Stk$[] does not need to be initialized
    '**********************************************************************************
    DIM RAW pd1 AS PCHAR
    DIM RAW pd2 AS PCHAR
    DIM RAW cnt1, cnt2
    cnt1 = cnt2 = 0
    Ndx  = 1

    DO WHILE Arg[cnt1]

        IF Arg[cnt1] = 34 THEN    ' quotes - string literals

            IF cnt2 AND TokQuote THEN
                Stk[Ndx++][cnt2] = 0
                cnt2 = 0
            END IF

            Stk[Ndx][cnt2] = 34

            DO WHILE Arg[++cnt1] <> 34
                Stk[Ndx][++cnt2] = Arg[cnt1]
                IF Arg[cnt1] = 0 THEN EXIT SUB
            LOOP

            Stk[Ndx][++cnt2] = Arg[cnt1]

            IF TokQuote THEN
                Stk[Ndx++][++cnt2] = 0
                cnt2 = 0
                GOTO again
            END IF

        END IF

        pd1 = CAST(PCHAR, delim1)

        DO WHILE *pd1
            IF *(pd1++) = Arg[cnt1] THEN
                IF cnt2 THEN
                    Stk[Ndx++][cnt2] = 0
                    cnt2 = 0
                END IF
                GOTO again
            END IF
        LOOP

        pd2 = CAST(PCHAR, delim2)

        DO WHILE *pd2
            IF *(pd2++) = Arg[cnt1] THEN
                IF cnt2 THEN Stk[Ndx++][cnt2] = 0
                Stk[Ndx][0] = Arg[cnt1]
                Stk[Ndx++][1] = 0
                cnt2 = 0
                GOTO again
            END IF
        LOOP

        Stk[Ndx][cnt2++] = Arg[cnt1]

        again:

        INCR cnt1
    LOOP
    Stk[Ndx][cnt2] = 0
    IF cnt2 = 0 THEN DECR Ndx
END SUB ' FastLexer


Quin

  • Sr. Member
  • ****
  • Posts: 299
    • View Profile
    • Quin's site
Re: MrBcx vs AI
« Reply #1 on: July 20, 2024, 02:17:28 PM »
Nice! 8)
Out of curiosity, how long did it take you relative to the wasted hour trying with ChatGPT?

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2538
    • View Profile
Re: MrBcx vs AI
« Reply #2 on: July 20, 2024, 02:59:02 PM »
Nice! 8)
Out of curiosity, how long did it take you relative to the wasted hour trying with ChatGPT?

Maybe 10 minutes ...

FastLexer was written by Wayne Halsdorf many years ago and
it is used extensively inside BCX, so I am quite familiar with it.


Quin

  • Sr. Member
  • ****
  • Posts: 299
    • View Profile
    • Quin's site
Re: MrBcx vs AI
« Reply #3 on: July 20, 2024, 04:43:08 PM »
Impressive!
Guess it just goes to show that AI isn't (always) the best tool for the job.