Page 4 of 5
Re: NameCheck
Posted: Thu Mar 18, 2021 8:45 pm
by dilwyn
OK, look forward to seeing your finished product.
Re: NameCheck
Posted: Fri Mar 19, 2021 4:24 am
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);
Re: NameCheck
Posted: Fri Mar 19, 2021 4:25 am
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
Re: NameCheck
Posted: Fri Mar 19, 2021 7:28 am
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!
Cheers,
Norm.
Re: NameCheck
Posted: Fri Mar 19, 2021 7:41 am
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.
Re: NameCheck
Posted: Fri Mar 19, 2021 10:25 am
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
Re: NameCheck
Posted: Fri Mar 19, 2021 10:36 am
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.
Re: NameCheck
Posted: Fri Mar 19, 2021 11:52 am
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.
Re: NameCheck
Posted: Fri Mar 19, 2021 12:05 pm
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.
Re: NameCheck
Posted: Fri Mar 19, 2021 12:10 pm
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...