Board Logo
« Variables »

Welcome Guest. Please Login or Register.
Jan 21st, 2018, 12:47am


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Variables  (Read 2443 times)
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3151
xx Variables
« Thread started on: Dec 17th, 2014, 02:07am »

A little task since interest is renewed. Write a program that will list the names of all variables used in any program you submit to it. The list can be a simple list or an ordered list or for the more adventurous a linked list that shows variable interactions.

No prize just the glory.

Feel free to post questions or seek advice in this thread.
« Last Edit: Aug 22nd, 2015, 12:24pm by Rod » User IP Logged

Stefan Pendl
Administrator
ImageImageImageImageImage


member is offline

Avatar

Let's talk JB ...


Homepage PM

Gender: Male
Posts: 3712
xx Re: Variables
« Reply #1 on: Dec 20th, 2014, 05:13am »

This challenge is postponed till the Christmas challenge is finished.

Thanks for understanding.
« Last Edit: Dec 20th, 2014, 3:43pm by Stefan Pendl » User IP Logged

Stefan - Homepage

Remember to read the forum rules board wink

Just BASIC 1.01, Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3151
xx Re: Variables
« Reply #2 on: Aug 22nd, 2015, 12:25pm »

Since we have some interest the challenge is open. No timescales as I suspect it will run and run.
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Variables
« Reply #3 on: Aug 22nd, 2015, 2:55pm »

Thanks Rod!

As mentioned in another thread, there are real rewards for attempting this challenge. So to get the ball rolling, here is the current state of my... er... ah... masterpiece ;P

Code:
'List Variables.txt = from challenge problem on Just Basic forum MGA/B+ started 2015-08-18
'Caution: variables are case sensitive a$ <> A$ but keywords are not!
A$="five":Rem colons are not honored in REM: so this one should be ignored:B$="ten"
rEm D=25 : e$="eek!"
dim variable$(1000) 'dim might point to variables but is it possible to use an array without setting one element = ?

variableIndex=0
'filepath$="C:\Users\Marty\Desktop\Just Basic\" 'use as needed
SourceFile$="List Variables.txt" 'well sure! this file better work on itself!
'SourceFile$="First Factors.txt" 'I am making a list of challenge files for testing
fln=0:a=1:b$="test this variable after a colon":c$="another varaible after a colon"
open filepath$+SourceFile$ for input as #1

while eof(#1)=0
    line input #1, fileline$
    fln=fln+1
    print fln,fileline$ 'at first this was just for debugging this code but this is quite useful

    'strip literals (this file especially is catching non variables in literal expressions)
    'strip literals BEFORE comments because I had example of "'" in code
    'id rather flag this " one unmatched double quote
    markPos=instr(fileline$,chr$(34))
    while markPos
        markPosR=instr(fileline$,chr$(34),markPos+1)
        if markPosR then
            fileline$=left$(fileline$,markPos-1)+mid$(fileline$,markPosR+1)
        else
            Print "Uneven number of double quotes found on line = ";fln
            exit while
        end if
        markPos=instr(fileline$,chr$(34))
    wend

    'strip comments
    markPos=instr(fileline$,"'") 'this line was flagged with unmatched double quote because I stripped comments first
    if markPos<>0 then
        if markPos<>1 then
            fileline$=left$(fileline$,markPos-1)
        else
            goto [skipdown]
        end if
    end if
    markPos=instr(lower$(fileline$),"rem ")
    if markPos<>0 then
        if markPos<>1 then
            fileline$=left$(fileline$,markPos-1)
        else
            goto [skipdown]
        end if
    end if

    text$=fileline$
    lineindex=0
    while text$<>""
        lineindex = lineindex + 1
        text$ = word$(fileline$, lineindex,":")
        markPos=instr(lower$(text$)," then")
        if markPos THen text$=mid$(text$,markPos+6)
        eqPos=instr(text$,"=") '<=== bummer have to remove stuff inside quotes
        if eqPos then
            if instr(lower$(text$),"while ") or instr(lower$(text$),"until ") then goto [skipdown]
            downer=eqPos-1
            WHILE mid$(text$,downer,1)=" ":downer=downer-1:wend
            while mid$(text$,downer,1)<>" "
                if downer=1 then exit while
                downer=downer-1
            wend
            var$=trim$(mid$(text$,downer,eqPos-downer))
            gosub [processVar]
        end if
        markPos=instr(lower$(text$),"input ")
        if markPos then
            if instr(text$,"#",markPos) then
                markPos=instr(text$,",",instr(text$,"#",markPos))
            else
                markPos=markPos+6
            end if
            if markPos then
                var$=""
                for i=markPos to len(text$)
                    if mid$(text$,i,1)<>";" then
                        if mid$(text$,i,1)<>"," then
                            var$=var$+mid$(text$,i,1)
                        else
                            gosub [processVar]
                            var$=""
                        end if
                    end if
                next i
                if var$<>"" then gosub [processVar]
            end if
        end if
    wend
[skipdown]
wend
close #1
Print:print "=== ";SourceFile$;":":print"=== Variables (ended with + to check trimming) ==="
if variableIndex then
    for i=1 to variableIndex
        print variable$(i)+"+"
    next i
else
    print "No variables found."
end if
'print:print "Just to prove JB sees A$=(five) but did not see B$=(ten) print A$+B$:";A$+B$
end

[processVar]
    var$=trim$(var$)
    if instr("abcdefghijklmnopqrstuvwxyz",left$(lower$(var$),1)) then
        ppos=instr(var$,"(")
        if ppos<>0 then
             var$=left$(var$,ppos -1)
        end if
        print var$;" on line = ";fln
        foundFlag=0
        for i=0 to variableIndex
            if variable$(i)=var$ then foundFlag=1:exit for
        next i
        if foundFlag=0 then
            variableIndex=variableIndex+1
            variable$(variableIndex)=var$
        end if
    end if
return
  


Not ready for prime time yet but (I think) at least it can do a proper listing of itself which is a start (and which explains some of the crazy code it contains for testing).

A different approach to all this might make more use of the very handy WORD$ Command that I have learned of through examples on this forum.

EDIT: BTW is there a way to widen the code window, it seems a waste of space to have at narrow width.
« Last Edit: Aug 22nd, 2015, 2:59pm by bplus » User IP Logged

B+
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Variables
« Reply #4 on: Sep 15th, 2015, 3:25pm »

OK: Globals, Arrays, Subs and Functions and of course the Main Variables all identified BUT...
Some (many?) might say I rigged the deck. :-/

Code:
'List Variables v2.txt = from challenge problem on Just Basic forum MGA/B+
'v1 started 2015-08-18 could find all variables in it's own source code
'2015-09-12 V2 will approach this task from completely new angle !
'Caution: variables are case sensitive a$ <> A$ but keywords are not!
'V2 depends on case sensitive variables:
'V2 make all variables uppercase, use all lower case for everything else
'V2 thusly we have made it easy to detect variables AND PLUS eliminate case sensitive errors in code!!!
'V2 handle Sub, function and Global variables
'V2 next add arrays 2015-09-14 looking good!
Global VARIABLEINDEX,FLN,PREFIX$,GLOBEINDEX,ARRAYINDEX
PREFIX$=""
PUNC$=" <,>?/:;'{[}]|\!@#%^&*()_-+="
A$="five":Rem colons are not honored in REM: so this one should be ignored:B$="ten"
rEm D=25 : e$="eek!"
dim VARIABLE$(1000) 'dim might point to variables but is it possible to use an array without setting one element = ?
dim GLOBE$(100),ARRAY$(100)
VARIABLEINDEX=0:GLOBEINDEX=0:ARRAYINDEX=0
FILEPATH$="C:\Users\Marty\Desktop\Just Basic\workbench\" 'use as needed
SOURCEFILE$="List Variables V2.txt"
'SOURCEFILE$="First Factors.txt"
FLN=0:a=1:b$="test this variable after a colon":c$="another varaible after a colon"
open FILEPATH$+SOURCEFILE$ for input as #1

while eof(#1)=0
    line input #1, FILELINE$
    FLN=FLN+1
print FLN,FILELINE$ 'at first this was just for debugging this code but this is quite useful

    'strip literals (this file especially is catching non variables in literal expressions)
    'strip literals BEFORE comments because I had example of "'" in code
    'id rather flag this " one unmatched double quote
    MARKPOS=instr(FILELINE$,chr$(34))
    while MARKPOS
        MARKPOSR=instr(FILELINE$,chr$(34),MARKPOS+1)
        if MARKPOSR then
            FILELINE$=left$(FILELINE$,MARKPOS-1)+mid$(FILELINE$,MARKPOSR+1)
        else
            Print "Uneven number of double quotes found on line = ";FLN
            exit while
        end if
        MARKPOS=instr(FILELINE$,chr$(34))
    wend

    'strip comments
    MARKPOS=instr(FILELINE$,"'") 'this line was flagged with unmatched double quote because I stripped comments first
    if MARKPOS<>0 then
        if MARKPOS<>1 then
            FILELINE$=left$(FILELINE$,MARKPOS-1)
        else
            goto [skipdown]
        end if
    end if
    MARKPOS=instr(lower$(FILELINE$),"rem ")
    if MARKPOS<>0 then
        if MARKPOS<>1 then
            FILELINE$=left$(FILELINE$,MARKPOS-1)
        else
            goto [skipdown]
        end if
    end if
    if word$(lower$(FILELINE$),1)="global" then
        GB$ = "?"
        INDEX=0
        while GB$ <> ""
            INDEX = INDEX + 1
            GB$ = word$(mid$(FILELINE$,8), INDEX, ",")
            if GB$<>"" then
                FOUNDFLAG=2
                if GLOBEINDEX then
                    for GI=1 to GLOBEINDEX
                        if GLOBE$(GI)=GB$ then FOUNDFLAG=1:exit for
                    next GI
                end if
                if FOUNDFLAG=2 then
                    GLOBEINDEX=GLOBEINDEX+1
                    GLOBE$(GLOBEINDEX)=GB$
                end if
            end if
        wend
        goto [skipdown]
    end if
    if word$(lower$(FILELINE$),1)="dim" then
        AR$ = "?"
        INDEX=0
        while AR$ <> ""
            INDEX = INDEX + 1
            AR$ = word$(mid$(FILELINE$,5), INDEX, ",")
            AR$=left$(AR$,instr(AR$,"(")-1)
            if AR$<>"" then
                FOUNDFLAG=2
                if ARRAYINDEX then
                    for GI=1 to ARRAYINDEX
                        if ARRAY$(GI)=AR$ then FOUNDFLAG=1:exit for
                    next GI
                end if
                if FOUNDFLAG=2 then
                    ARRAYINDEX=ARRAYINDEX+1
                    ARRAY$(ARRAYINDEX)=AR$
                end if
            end if
        wend
        goto [skipdown]
    end if
    if word$(lower$(FILELINE$),1)="function" or word$(lower$(FILELINE$),1)="sub" then
        PREFIX$=word$(FILELINE$,1)+" "+word$(FILELINE$,2)+":"
    else
        if word$(lower$(FILELINE$),1)="end" and word$(lower$(FILELINE$),2)="sub" then PREFIX$=""
        if word$(lower$(FILELINE$),1)="end" and word$(lower$(FILELINE$),2)="function" then PREFIX$=""
    end if
'print FLN,FILELINE$
   LASTP=0:BUILD$=""
   for P=1 to len(FILELINE$)
      if instr(PUNC$,mid$(FILELINE$,P,1)) then
         VAR$=trim$(mid$(FILELINE$,LASTP+1,P-LASTP-1))
'if VAR$<>"" then print VAR$+", ";
         if len(VAR$) then
            call processVar VAR$
         end if
         LASTP=P
      end if
   next
'print
    if LASTP<len(FILELINE$) then
        VAR$=trim$(mid$(FILELINE$,LASTP+1))
        if len(VAR$) then
            call processVar VAR$
        end if
    end if
[skipdown]
wend
close #1
Print:print "=== ";SOURCEFILE$;":":print"=== Variables (ended with + to check trimming) ==="
if GLOBEINDEX then
    S$="Global:"
    for I=1 to GLOBEINDEX
        print S$+GLOBE$(I)+"+"
    next
end if
print
if ARRAYINDEX then
    S$="Array:"
    for I=1 to ARRAYINDEX
        print S$+ARRAY$(I)+"+"
    next
end if
print
if VARIABLEINDEX then
    Print"          Main Variable:"
    for I=1 to VARIABLEINDEX
        print VARIABLE$(I)+"+"
    next
else
    print "No variables found."
end if
end

sub processVar VAR$
'print "sub just got:*";VAR$;"*"
    if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",left$(VAR$,1)) then
'print "sub instr just passed:*";VAR$;"*"
        NOGOOD=0
        if len(VAR$)>1 then
            for VP=2 to len(VAR$)
                if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ.1234567890$",mid$(VAR$,VP,1)) then
                    ZOK=1
                else
                    NOGOOD=1
                    exit for
                end if
            next VP
        end if
        if NOGOOD=0 and GLOBEINDEX>0 then
            for GI=1 to GLOBEINDEX
                if GLOBE$(GI)=VAR$ then NOGOOD=1:exit for
            next GI
        end if
        if NOGOOD=0 and ARRAYINDEX>0 then
            for GI=1 to ARRAYINDEX
                if ARRAY$(GI)=VAR$ then NOGOOD=1:exit for
            next GI
        end if
        if NOGOOD=0 then
'print VAR$;" on line = ";FLN
            VAR$=PREFIX$+VAR$
            FOUNDFLAG=2
            for VI=0 to VARIABLEINDEX
                if VARIABLE$(VI)=VAR$ then FOUNDFLAG=1:exit for
            next VI
            if FOUNDFLAG=2 then
print VAR$;" found new on line = ";FLN
                VARIABLEINDEX=VARIABLEINDEX+1
                VARIABLE$(VARIABLEINDEX)=VAR$
            end if
        end if
    end if
end sub
function ptt$(D$)
    ptt$="This and that. "+D$+str$(GLOBEINDEX)
end function
sub likewise
    S$=ptt$("and that... ")+" So there."
    print S$
end sub
 

User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Variables
« Reply #5 on: Sep 16th, 2015, 02:17am »

So, for now your program silently ignores lowercase variables (a, b$, c$ in line 22) ?
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Variables
« Reply #6 on: Sep 16th, 2015, 09:33am »

on Sep 16th, 2015, 02:17am, tsh73 wrote:
So, for now your program silently ignores lowercase variables (a, b$, c$ in line 22) ?


Correct, "for now"... "silent". Thanks for noticing.

I could PRINT a loud warning message:

THIS PROGRAM ONLY RECOGNIZES ALL UPPER CASED VARIABLES

AND WORSE,

THIS PROGRAM WILL MISTAKE ANY ALL UPPER CASED KEYWORDS AS A VARIABLE! (EXCEPT IN DIM AND GLOBAL STATEMENTS)

(I could PRINT that, but I have been rightly accused of PRINTing too much, once already.)

This of course renders it useless for checking old programs but they are probably working fine already, otherwise why save them.

If the coder accepts these conditions they may find errors from incorrectly CASED variables with the help of this code (or just by playing by that one rule, alone). BUT, the price is high when one is not in the habit of shifting to all upper case for variables creation and use. Reusable code might be adjusted but it is already running fine so probably not needed.

I am still hoping to get some practical use from this effort. VARIABLES do stick out like a sore thumb with this style.
User IP Logged

B+
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Variables
« Reply #7 on: Nov 11th, 2015, 11:56am »

News update:

Obviously all capital Variables is not practical enough to be useful.

Richard rekindled my desire to work on this project again in a discussion at BasicProgramming.org (BP.org)

I left off this project overwhelmed as to how to get all those Commands and keywords into a file I can read and compare words to.

Turns out it was a piece of cake! Just copy and paste with mouse off help into txt file.

I thought I would have to edit "everything", no, unless you want to clean up the txt file a bit for human readers.
(To make things easy you might be advised to keep one item restricted to one line.)

So for your edification here is my work last night:
The text file:
http://forum.basicprogramming.org/index.php/topic,5165.msg33003.html#msg33003
look for "JB Commands and keywords.txt" attachment

and here is some test code for loading the file into an array:
Code:
'Loading Commands.txt for JB 2015-11-10 MGA/B+
filelines=0
Open "JB Commands and keywords.txt" for input as #1
while not(eof(#1))
    line input #1, fline$
    if left$(fline$,1)<>chr$(34) and trim$(fline$)<>"" then filelines=filelines+1
wend
close #1

dim commands$(filelines)
ccount=0:fcount=0
Open "JB Commands and keywords.txt" for input as #1
while not(eof(#1))
    line input #1, fline$
    fline$=trim$(fline$)
    fcount=fcount+1
    if left$(fline$,1)<>chr$(34) and fline$<>"" then
        cmd$=word$(fline$,1)
        cmd$=word$(cmd$,1,"(") 'remove punc
        cmd$=word$(cmd$,1,"-")
        cmd$=word$(cmd$,1,".")
        if i=1 then
            ccount=ccount+1
            commands$(ccount)=cmd$
        else
            if commands$(ccount)<>cmd$ then 'redundant not needed
                ccount=ccount+1
                commands$(ccount)=cmd$
            end if
        end if
    end if
wend
close #1
for i=1 to filelines
    print commands$(i);" ";
    if i mod 5=4 then print
next
print
print "The number of file lines was ";fcount
print "The number of usable commands was ";filelines
print "With redundants removed, ";ccount;" commands remain."

 


From that link you may also find another link to work already expertly completed, LBreader, that Richard had generously supplied.
(In case you may not already know about this.)
And in same thread as link was advice as to an alternate way to create the file off Wiki.

I think this good exercise, to see your code through eyes of compiler. Plus the JB Commands and keywords.txt file makes an excellent summary reference or quick lookup sheet.
User IP Logged

B+
ezprogramming
Guest
xx Re: Variables
« Reply #8 on: Nov 11th, 2015, 4:24pm »

on Nov 11th, 2015, 11:56am, bplus wrote:
From that link you may also find another link to work already expertly completed, LBreader, that Richard had generously supplied.

Just to clarify, LBReader is a program written by Jim Brossman (MKnarr).

Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls