NameCheck

Anything QL Software or Programming Related.
User avatar
dilwyn
Mr QL
Posts: 2753
Joined: Wed Dec 01, 2010 10:39 pm

Re: NameCheck

Post by dilwyn »

OK, look forward to seeing your finished product.


swensont
Forum Moderator
Posts: 252
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: NameCheck

Post by swensont »

After thinking on this, it looks like perl is the best option over awk or text utilities (the GNU text utils don't have all that I was hoping for). Here is the perl script that will take a SuperBASIC file (with or without line numbers), generate a list of variables (assuming that all variables will be part of an assignment), and then compare that list of variables with a list of SuperBasic keywords.

Run it on the QL like this:

EXEC perl;"namecheck_pl < program_bas"

The program expects the file keywords_txt to be in the DATA_USE location. The file vars_txt will be created in the DATA_USE location. I start the work under LIinux and tested on the QL with SMSQmulator.

Tim
--------------------------------------------------------------------------------------------------

Code: Select all

#!/usr/bin/perl
# namecheck_pl

# PART 1 - get list of variables from SuperBASIC program

open(OUT,">var_txt") || die "failed to open var_txt";

while (<STDIN>) {
   $str = " ".$_;
   $a = index($str,"=");
   if ($a != 0) {
      $str = substr($str,0,$a);
      if (substr($str,length($str)-1,1) eq " ") {
          chop $str;
      }
      $str = reverse($str);
      $a = index($str," ");
      $str = substr($str,0,$a);
      $str = reverse($str);
      print OUT $str,"\n";
   }
}

close(OUT);
# PART 2 - Compare variables with keywords

Code: Select all

@words = "";

open(FILE,"keywords_txt") || die "Failed to open keywords_txt";
while (<FILE>) {
   chop ($_);
   push (@words,$_);
}
close(FILE);

open(IN,"var_txt") || die "failed to open var_txt";

while (<IN>) {
   chop($_);
   $_ =~ tr/a-z/A-Z/;
   
   foreach $word (@words) {
      if ($_ eq $word) {
         print $_,"\n";
      }
   }
}

close(IN);


swensont
Forum Moderator
Posts: 252
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: NameCheck

Post by swensont »

It looks like the indenting was taking out of the program, but it should still work since Perl does not use white space for managing blocks of code.

Tim


User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: NameCheck

Post by NormanDunbar »

Tim,

Fixed it for you Tim. (Admin privs rule!) -- hope you don't mind.

When posting code, use the "Code" button above the editor to create code markers, then type/paste your code between them. That keeps your formatting.

Useful for bigger bits. No good for inlining code as it always starts on a new line.

Code: Select all

[code]
    Your code here.
    With indentation preserved!
[/code]


HTH

Perl: is a "worn" language. Write once, read never! :D

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: NameCheck

Post by NormanDunbar »

I'm useless at Perl.

Does the code above correctly handle source lines where there is/are equal signs within a string?

Code: Select all

1000 Version$ = "Toolkit version = 1.16"
For example.

I'm also wondering, if someone does something stupid like:

Code: Select all

DEFine FuNction whatEver(X, ...)
...
Where X and any of the other parameters, are keywords for a toolkit which is not currently loaded, those would need to be filtered out also, not just those in assignment statements.

This would apply to Dilwyn's version and yours I should imagine?

Just thinking out loud. These things have a tendency to get bigger the more you think about them.

Cheers,
Norm.

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
swensont
Forum Moderator
Posts: 252
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: NameCheck

Post by swensont »

Norman,

Thanks for the fix. The two sections should be all one script. Easy enough to be fixed by who ever downloads it.

In the example with version$, the script will find "version$" and compare that with the keyword list and it will not match if the keyword is "version". The script assumes that all variables must have an assignment, so I looked for "=".

I did not think about function or procedure names. If needed, I could create a version that looks for DEFine statements and strips out the name.

Tim


User avatar
tofro
Font of All Knowledge
Posts: 2688
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: NameCheck

Post by tofro »

Has anyone of you had a look into MasterBasic? A former commercial product that does a lot of what you're discussing here (Static S*Basic code analysis, and a lot more). I would definitely recommend it to anyone doing projects of considerable size in S*Basic.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
Derek_Stewart
Font of All Knowledge
Posts: 3932
Joined: Mon Dec 20, 2010 11:40 am
Location: Sunny Runcorn, Cheshire, UK

Re: NameCheck

Post by Derek_Stewart »

Hi,

I used to use MasterBasic many years ago, it was a great programming upgrade. Now it is freeware, it worth a look at the features Masterbasic can give for programme development.


Regards,

Derek
User avatar
dilwyn
Mr QL
Posts: 2753
Joined: Wed Dec 01, 2010 10:39 pm

Re: NameCheck

Post by dilwyn »

tofro wrote:Has anyone of you had a look into MasterBasic? A former commercial product that does a lot of what you're discussing here (Static S*Basic code analysis, and a lot more). I would definitely recommend it to anyone doing projects of considerable size in S*Basic.
Bear in mind that NameCheck was never intended to compete with QREF, MasterBasic, Reporter etc, it had one sole aim in life, to check names used against a master list of keyword names.

Tim's script will be great and will do the job neatly and simply for those prepared to use Perl or similar.

The NameCheck program itself is so near to being finished I may as well complete it now. At least it did what Steve wanted. Update soon.


martyn_hill
Aurora
Posts: 909
Joined: Sat Oct 25, 2014 9:53 am

Re: NameCheck

Post by martyn_hill »

Hi everyone

[Arguably going off-topic - or rather expanding upon this specific and valid use-case that Dilwyn's solution aims to address...]

I would also like to encourage exploring MasterBasic in this context - fundamentally it works 'within' the SBASIC environment, thus knows the context of each name in the Name Table, rather than trying to deduce usage from a purely external syntactical analysis using awk, sed and their ilk.

Whilst I'm not so thrilled in limiting SBASIC editing capability to what QDOS/SMSQE offer (i.e. ED), MasterBasic to my mind is a truly impressive bit of software engineering with some incredible SBASIC profiling tools. To my mind, if QD or some other SBASIC 'aware' editing tool could be integrated with MasterBasic's tools against a 'resident' SBASIC tokenised program, I think we'd have a fantastic basis for a next-gen SBASIC 'IDE'. Wishful thinking, I know...


Post Reply