sub parse_commands
{
my ($filename) = @_;
my $fh = IO::File->new($filename) || die ...;
while (<$fh>)
{
# All commands being with a letter and must be the first chara
+cter
next if /^[^A-Za-z]/;
# A command is space-delimited and must be on one line.
# We want to deal with the entire command in uppercase.
my ($command, @line) = split /\s+/, uc;
unless (exists $DispatchTable{$command})
{
warn "'$command' isn't a valid command.\n";
next;
}
push @commands, [$command, \@line];
}
return \@commands;
}
All of a sudden, you have a parsed list of commands. Obviously, you'd do something a little more in-depth when dealing with WHILE/ENDWHILE, IF/ELSE/ENDIF, SUB/ENDSUB, and FOR/ENDFOR, but it's not that much more complicated. It can even be a one-pass compiler if you
- do all includes during the parsing phase (this requires reading the initial file first into an array for splicing)
- require all subroutines be defined before usage (if SUB is even allowed)
- require all labels to be on a line by themselves. (Something along the lines of /^([A-Z]\w*):$/)
- don't care if a label is defined until runtime.
If interested, I'll write one. Remember - the handler for each command is the one that cares about parameters and the like. This isn't a fully-featured language (though it is Turing-complete).
------
We are the carpenters and bricklayers of the Information Age.
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|