in reply to command interpreter

++ for the effort.

In my opinion what you want to do is something like a command interpreter: an interactive environment inside which you can write a command and get some action/calculation done.

If this is the case, the first thing you need is defining the syntax of the acceptable commands, next you need a parser to interpret and validate each command and then comes the business logic that make the actions.

Is this sounding like what you were searching for?

Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

  • Comment on Re: i will change the title once i now what i'm asking for

Replies are listed 'Best First'.
Re: command interpreter
by baxy77bax (Deacon) on Aug 19, 2008 at 19:10 UTC
    Yes, this sounds like it, so, where can i read more about it ? how can i create something like that (i'm not looking for the exact "how to do "

    THANKS

    update: quick look through google gives an impression that this is not something easy to do... cr..

      This is how you might do it. At least it's a start. When you read the results from your commands, do whatever you want depending on the returned text. I think what you really need to do here, is substitute /bin/bash with your script, 'myprocessor.pl'. Then, have myprocessor.pl accept commands on stdin, run appropriate subs, and return answers.
      #!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = open3(\*WRITE, \*READ,\*ERROR,"/bin/bash"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter command\n"; chomp(my $query = <STDIN>); #send query to bash print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

      I'm not really a human, but I play one on earth Remember How Lucky You Are

      The easy way to fake a command interpreter is to reuse an existing command interpreter. The one command interpreter you have at hand is Perl, and Perl has the eval statement, which gives you the whole Perl interpreter to use.

      So one approach would be to take your command language and convert it into a Perl program. If you give your command language enough thought beforehand, this can be done by something as simple as Filter::Simple, just like Querylet does, or maybe Filter::QuasiQuote, if you don't mind the wrapper of your program looking like Perl.

      The following is a simple interpreter for arithmetic expressions, as long as they are on one line:

      use strict; while (<>) { my $res = eval $_; if (my $err = $@) { warn "Error: $@"; } else { print "'$_' gives $res\n" }; };

      Well, for a start you could change the node title :-)

      Seriously, I don't know where you could start from:

      "defining a syntax" can be a big job if you want a language with a minimum of flexibility, or a trivial one if you have only a handful of commands to implement. It really depends on what your program should do, and you have not given much detail.

      On the parser side, there are many choices available but I never did these things in Perl, so it's better for somebody else to advise you.

      Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."