Liberty BASIC Community Forum
« Variables »

Welcome Guest. Please Login or Register.
Mar 29th, 2017, 9:42pm


Rules|Home|Help|Search|Recent Posts|Notification


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


member is offline

Avatar

Graphics = goosebumps!


PM

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

Here is a little task for the seasonal break. Write a program using Liberty BASIC 4.50 beta 3 that will list the names of all variables used in any program submitted to it. The list can be a simple list or something more ordered its up to you.

This will help beta test our new version of Liberty. Code should be ready for the first week of the new year.

http://www.libertybasic.com/lb45b3setup.exe
« Last Edit: Dec 17th, 2014, 02:16am by Rod » User IP Logged

MKnarr
Senior Member
ImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 424
xx Re: Variables
« Reply #1 on: Dec 18th, 2014, 08:45am »

I would like to submit a program I wrote several years ago in LB 4.04 and have now saved it with version 4.5 beta 3. It includes string and numeric reserved keywords words that are new in version 4.5.

It not only finds all the string and numeracy variables in any program but arrays, handles, labels, goto, gosub and subs and function names.

It is in a zip file because it contains bitmaps etc.

http://users.sisna.com/jbross/lbreader.zip
« Last Edit: Dec 18th, 2014, 11:42am by MKnarr » User IP Logged

hooshnik
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 97
xx Re: Variables
« Reply #2 on: Dec 18th, 2014, 2:08pm »

That is a pretty good program! I tried it on my project. It took 90 seconds for lbreader to chew through it. Pretty cool that it knows the difference between a function and an array.

The only alternatives I found is to use linux with perl...

Code:
cat myprogram.bas | perl -ne 'print /\s*(\w*)\s/ ? "\n$1" : ""' | sort | uniq | less
 


... or Liberty Basic Workshop which both don't really do what yours does. LBW has lots of dupes and only looks at variables. That linux line eliminates the dupes but again only really works for variables and doesn't eliminate key words.

I found a minor bug. I sadly can't reproduce without my proprietary code. Any numeric variable (or function?) ending in the word "Validator" show hits for both an ending of "Validator" and "Validato" with no 'r' at the end.

Also my hand was getting tired from having no keyboard support ;D
User IP Logged

looking for file "include" support? Check out Aplomb Scribe http://libertybasic.conforums.com/index.cgi?action=display&board=open&num=1434746448
MKnarr
Senior Member
ImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 424
xx Re: Variables
« Reply #3 on: Dec 19th, 2014, 12:44pm »

Quote:
Pretty cool that it knows the difference between a function and an array.


That's because it looks for dim statements first and removes them then eventually looks for the word sub and function. Turns out, the program must look for things in a specific order.

On my system with a 3.2 GHz processor, it takes a minute 50 to go through a 15681 working line program.
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2251
xx Re: Variables
« Reply #4 on: Dec 20th, 2014, 4:07pm »

A quick question - are LB's built in variables(WindowWidth, DefaultDir$, etc) supposed to be included in this?
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
tenochtitlanuk
Board Moderator

member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1138
xx Re: Variables
« Reply #5 on: Dec 20th, 2014, 4:34pm »

Great bit of coding there Jim/MKnarr.
I'd got far enough into the challenge to have started to meet some of the things you mention- and use in the code.

I think I'd heard of your prog. before, but not tried it. Very helpful as an example of a large bit of useful coding.

Thanks for creating and sharing...

PS liked Rod's description of it as a 'little programming task'.....
« Last Edit: Dec 20th, 2014, 4:35pm by tenochtitlanuk » User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5216
xx Re: Variables
« Reply #6 on: Dec 21st, 2014, 03:25am »

I have decided to exclude the system variables. It seemed easier to exclude everything you can identify then parse the rest. But it is really up to you since it is meant to be a fun head scratcher for the extra time folks will have over the break.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5216
xx Re: Variables
« Reply #7 on: Dec 23rd, 2014, 04:11am »

I'm travelling soon and out of time. This has bugs but the task is for fun, it shows you can do a lot for yourself. I hope to see how other folks have tackled it. Save the code as varlist.bas and it will analyse itself.

Code:
    nomainwin
    'an array to hold our bas file
    dim code$(5000)
    'setup array of reserved words
    gosub [syntaxcolor]

    WindowWidth  = 1200
    WindowHeight = 600
    UpperLeftX   = (DisplayWidth-WindowWidth)/2
    UpperLeftY   = (DisplayHeight-WindowHeight)/2
    graphicbox #1.structure, 0,0,200,600
    graphicbox #1.code, 199,0,700,600
    graphicbox #1.help, 898,0,300,600
    menu #1, "&File", "E&xit", [quit]
    menu #1, "edit"
    menu #1, "&Help", "&About", [about]
    open "Syntax Color and Variable List" for window as #1
    #1 "trapclose [quit]"
    #1.structure "down ; color black ; fill white ; font courier_new 9"
    #1.structure "vertscrollbar on"
    #1.code "down ; color black ; fill white ; font courier_new 9"
    #1.code "vertscrollbar on ; horizscrollbar on"
    #1.help "down ; color black ; fill white ; font courier_new 9"


    'set our character printing sizes for courier_new 9
    chrWidth=7
    chrHeight=15


    'now simply open this program for analysis
    open "varlist.bas" for input as #2

    'get the .bas into the array
    ln=1
    while eof(#2)=0
        line input #2 , code$(ln)
        ln=ln+1
    wend
    close #2
    lastln=ln


    'now cycle through the array line by line
    for ln=1 to lastln
        gosub [printline]
    next
    #1.code "flush"
    #1.structure "flush"

    'we are done
    wait



    [printline]
    'get the line in its raw state
    line$=code$(ln)

    'pad in spaces round operators
    '"'=<>:,+-*/^;()"+chr$(34)
    'this isolates command words and makes them easier to interpret

    newline$=""
    thischr$=""
    for n = 1 to len(line$)
        thischr$=mid$(line$,n,1)
        if instr(opslist$,thischr$)>0 then
            newline$=newline$+" "+thischr$+" "
        else
            newline$=newline$+thischr$
        end if
    next
    newline$=trim$(newline$)

    'now pull out the words in the line, we will eliminate extra spaces as we go

    'keep track of where we are printing in the graphicbox
    chrX=chrWidth
    chrY=ln*chrHeight
    line$=""

    'get the first word then loop
    w=1
    w$=word$(newline$,w," ")

    'loop till we are done with the line
    while w$<>""

        'set the default color to darkgray
        'if we find the word we set the color
        #1.code "color darkgray"
        var=1
        w$=trim$(w$)

        'if it is a rem statement set the color for the rest of the line
        if w$="'" then remon=1

        'if it might be the start of a literal string flag that
        if w$=chr$(34) and quoteon=1 then quoteon=0
        if w$=chr$(34) and quoteon=0 then quoteon=1

        'if it is a rem statment do nothing but set the color
        if remon then
            #1.code "color 255 128 0"
            var=0
        else
            'check what color it might take
            select case

            'operators are black and we note it is not a variable
            case instr(opslist$,w$,1)>0
                #1.code "color black"
                var=0

            'commands are blue and not a variable
            case instr(comlist$," "+lower$(w$)+" ",1)>0

                'special case of on error
                if lower$(w$)="on" then
                    if lower$(w$)="on" and lower$(word$(newline$,w+1," "))="error" then
                        #1.code "color blue"
                    else
                        #1.code "color darkgreen"
                    end if
                 else
                    #1.code "color blue"
                end if
                var=0

            'functions are pink and not variables
            case instr(funlist$," "+w$+" ")>0
                if instr(funlist$," "+lower$(left$(w$,instr(w$,"(",1))),1)>0 then #1.code "color pink" : var=0

            'system variables are darkgreen and not variables
            case instr(varlist$," "+w$+" ",1)>0
                #1.code "color darkgreen"
                var=0

            'operators are darkgreen and not variables
            case instr(opelist$," "+lower$(w$)+" ",1)>0
                #1.code "color darkgreen"
                var=0

            'struct types are brown and not variables
            case instr(typlist$," "+lower$(w$)+" ",1)>0
                #1.code "color brown"
                var=0

            'graphic commands are green and not variables
            case instr(ctrlist$," "+lower$(w$)+" ",1)>0
                #1.code "color green"
                var=0

            end select

            'special case of [branch label]
            if left$(w$,1)="[" and right$(w$,1)="]" then #1.code "color black" : var=0

            'special case of #handles
            if left$(w$,1)="#" then #1.code "color black" : var=0

            'special case of numeric literals
            if w$="0" or val(w$)>0 then #1.code "color red" : var=0

            'so if var is still standing it might be a variable
            if var=1 then #1.code "color brown"

            'if we are at the start of a string and the first word
            'is a reserved word we are in a command string else
            'we are in a literal string"
            if lastw$=chr$(34) and var=0 then quoteon=0
            if quoteon then #1.code "color cyan"

            'last gasp force the quote character black to wrap the string
            if w$=chr$(34) then #1.code "color black"


        end if

        'so now we can print the word from the string in the appropriate color
        if w$<>"" then
            #1.code "place ";chrX;" ";chrY
            #1.code "\";w$;" "

            'increment the print position
            chrX=chrX+chrWidth*(len(w$)+1)

            'if it is a var and we are not in the midst of a literal string
            'then assume it is truly a variable"
            if var and quoteon=0 then #1.structure "\";w$;" "
        end if

        'get the next word in the line and go round again
        w=w+1
        lastw$=w$
        w$=word$(newline$,w," ")
    wend
    remon=0
    quoteon=0
    return



[syntaxcolor]
'operators and so whitespace drivers
opslist$="'=<>:,+-*/^;()"+chr$(34)

'command list
comlist$=" xor while wend wait until unloadbmp trace to titlebar timer then texteditor "
comlist$=comlist$+"textbox sub stylebits struct stopmidi stop step statictext sort select seek "
comlist$=comlist$+"scan run return resume restore rem redim readjoystick read randomize "
comlist$=comlist$+"radiobutton put prompt printerdialog print popupmenu playwave playmidi "
comlist$=comlist$+"password out or open oncomerror notice nomainwin next name mod menu "
comlist$=comlist$+"maphandle mainwin lprint loop loadbmp listbox line let kill input if "
comlist$=comlist$+"groupbox graphicbox goto gosub global gettrim get function for fontdialog "
comlist$=comlist$+"files filedialog field exit error end else dump do dim data cursor confirm "
comlist$=comlist$+"combobox colordialog cls close checkbox case callfn calldll callback call "
comlist$=comlist$+"button bmpsave bmpbutton beep as and "

'function list
funlist$=" word$ winstring val using  upper$ txcount trim$ time$ tan tab str$ sqr "
funlist$=funlist$+"space$ sin rnd rmdir right$ not mkdir min midipos mid$ max lower$ log "
funlist$=funlist$+"lof loc len left$ int instr inputto$ input$ inp hwnd hexdec hbmp exp "
funlist$=funlist$+"eval eval$ eof dechex$ date$ cos chr$ atn asn asc acs abs "

'system variables
varlist$=" WindowWidth WindowHeight Version$ UpperLeftY UpperLeftX TexteditorColor$ "
varlist$=varlist$+"TextboxColor$ PrinterName$ PrinterFont$ PrintCopies PrintCollate Platform$ "
varlist$=varlist$+"ListboxColor$ Joy2z Joy2y Joy2x Joy2button2 Joy2button1 Joy1z Joy1y Joy1x "
varlist$=varlist$+"Joy1button2 Joy1button1 ForegroundColor$ Err$ Err Drives$ DisplayWidth "
varlist$=varlist$+"DisplayHeight DefaultDir$ CommandLine$ ComboboxColor$ BackgroundColor$ "
'type operators
typlist$=" word void ushort ulong short ptr none long dword double boolean "
'command operators
opelist$=" window text random output graphics dll dialog byref binary append "
opelist$=opelist$+"horizscrollbar vertscrollbar on off min max window_nf window_popup graphics_fs graphics_nsb graphics_fs_nsb graphics_nf_nsb "
opelist$=opelist$+"text_fs text_nsb text_nsb_ins dialog_modal dialog_nf dialog_nf_modal dialog_fs dialog_nf_fs dialog_popup "
opelist$=opelist$+"yellow brown red darkred pink darkpink blue darkblue green darkgreen "
opelist$=opelist$+"cyan darkcyan white black lightgray darkgray buttonface "

'graphic commands
ctrlist$=" \ addsprite !autoresize autoresize backcolor background backgroundxy box "
ctrlist$=ctrlist$+"boxfilled centersprite circle circlefilled !cls color !contents !contents? "
ctrlist$=ctrlist$+"!copy !cut cyclesprite delsegment discard down drawbmp drawsprites ellipse "
ctrlist$=ctrlist$+"ellipsefilled fill font getbmp go goto home !insert line !line !lines? "
ctrlist$=ctrlist$+"locate !modified? north !origin !origin? !paste pie piefilled place "
ctrlist$=ctrlist$+"posxy print redraw refresh removesprite resizehandler rule segment "
ctrlist$=ctrlist$+"!select !selectall !selection? set setfocus size spritecollides "
ctrlist$=ctrlist$+"spriteimage spritemovexy spriteoffset spriteorient spriteround flush "
ctrlist$=ctrlist$+"spritescale spritetoback spritetofront spritetravelxy spritevisible "
ctrlist$=ctrlist$+"spritexy spritexy? stringwidth? !trapclose trapclose turn up when \ "

return

[quit]
close #1
end


 
« Last Edit: Dec 23rd, 2014, 04:16am by Rod » User IP Logged

tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1609
xx Re: Variables
« Reply #8 on: Jan 7th, 2015, 05:46am »

I made contest page (my program just grew over 10k :( )
Variables Challenge 2015

Link to my entry

Here are results then it run over itself:
Code:
Processing C:\progs\Liberty BASIC v4.5 beta 3\proj\variables\vars02.bas
Reading parsing line by line...
----------------
nLines=       497
numTokens=2536
1891 ms
second pass along token$ array...
94 ms
third pass along token$ array...
1359 ms
last pass along token$ array...
31 ms
Counting ...
172 ms
sorting ...
0 ms
nDiffWords    79
----------------
Counter       Type          Name
-------------------------------------------
34            dimmedArray   token$
19            dimmedArray   tokenPos
9             dimmedArray   uniqueName$
4             hndl          #1
1             lbl           [nonUsedLabel]
2             lbl           [setLists]
2             lbl           [SkipPrint]
2             lbl           [SkipPrint2]
2             lbl           [skipToken]
5             name          aIndex$
9             name          aLen
105           name          aLine$
3             name          aString$
2             name          c$
2             name          chars2eat$
2             name          cnt
20            name          comlist$
6             name          contLine
4             name          count
2             name          curLine
2             name          curStmnt
5             name          curStmntNum
21            name          curTokenNum
7             name          curTokNum
4             name          digits$
2             name          dummy
1             name          dummy2
1             name          eatUp
2             name          firstNumChars$
2             name          firstVarChars$
4             name          fname$
10            name          funlist$
66            name          i
2             name          iif
14            name          input$
4             name          isContinuation
2             name          IsNumber
14            name          j
3             name          label$
8             name          letters$
4             name          n1
2             name          n2
2             name          nd
3             name          ne
7             name          nLines
3             name          notANumber
4             name          ns
10            name          opelist$
3             name          pos
6             name          qq$
1             name          remStartSpaces$
4             name          t.maxTokens
16            name          t.name
21            name          t.type
12            name          t0
2             name          test
2             name          tkn$
3             name          toFind$
41            name          token$
9             name          tp.line
6             name          tp.num
9             name          tp.stmnt
2             name          type$
2             name          types2Skip$
2             name          typlist$
5             name          un.count
4             name          un.typeName
2             name          valNo
2             name          valYes
6             name          varChars$
22            name          verbose
3             name          w$
2             name          whiteSpaces$
18            sub           storeToken
8             UDF           eatUp
2             UDF           iif
5             UDF           IsNumber
2             UDF           remStartSpaces$
1             unDimmedArray               unDimmed
-over-----------
 
« Last Edit: Jan 7th, 2015, 05:47am by tsh73 » User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
robmcal
Senior Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 384
xx Re: Variables
« Reply #9 on: May 11th, 2015, 9:18pm »

tsh73,

I tried running your program but it seems to be missing a few functions.

User IP Logged

Definition of an Upgrade: Take old bugs out, put new ones in.
tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1609
xx Re: Variables
« Reply #10 on: Aug 25th, 2015, 1:46pm »

Sorry robmcal
somehow I missed your post
Quote:
tsh73,

I tried running your program but it seems to be missing a few functions.

That's exactly because task says
Quote:
Write a program using Liberty BASIC 4.50 beta 3

So my program uses
UPTO$( source$, match$ )
AFTER$( source$, match$ )
functions
User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

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