#!/usr/bin/perl -w use strict; use 5.12.0; my %dictionary; # set the directory containing the source files my $dirname = "C:\\MyCode\\PropForth\\V5.5\\SourceFiles"; sub processForthFile ($) { my $fn = shift(@_); # say("forth file: $fn") ; open SOURCE, "$dirname\\$fn" or die; my $sourceLine = 0; while (my $line = ) { $sourceLine += 1; chomp($line); my @tokens = split(/ /,$line); # for a valid definition, $tokens[0] will be ':' and $tokens[1] will be the word # see if we got a valid parse and if the first token is ':' if ((defined($tokens[0])) && ($tokens[0] eq ':')) { my $word = $tokens[1]; # say "Found Definition: $word"; # build the index point - file durrecnt in and line we're currently processing my $idxPoint = " ($fn:$sourceLine) "; # if the word is already in the dictionary, tack this index point onto teh end of the string # if it isn't in the dictionary, then add it and use the index point as the first. if (exists($dictionary{$tokens[1]})){ $dictionary{$tokens[1]} = $dictionary{$tokens[1]} . $idxPoint; } else { $dictionary{$tokens[1]} = $idxPoint; } } } # close out this file - we're done close SOURCE; } sub processSpinFile ($) { my $fn = shift(@_); # say("spin file: $fn = skipping"); } sub printDictionary() { open OUT, '>forth_index.txt' or die; # walk through the dictionary hash and print out each word and the index point(s) foreach my $word (sort keys %dictionary) { print OUT "$word -> $dictionary{$word}\n"; } close OUT; } #MAIN CODE STARTS HERE # # load the filenames into an array for processing opendir DIR, $dirname or die "cannot open dir $dirname: $!"; my @file= readdir DIR; closedir DIR; # walk through the array and process each file # each word definition found will be added to a hash keyed by definition # the value will be a string of one of more index entries 'file:line' foreach my $file (@file) { if ($file eq '.' || $file eq '..') {next}; # print "Processing $file\n"; my (@fparts)= split(/\./,$file); if ($fparts[1] eq 'f') {processForthFile($file)}; if ($fparts[1] eq 'spin') {processSpinFile($file)}; } printDictionary();