tonyday has asked for the wisdom of the Perl Monks concerning the following question:

There is a programming game that uses a mini assembler like language to code the actions of bots fighting each other. I have been thinking about writing a compiler that takes a higher-level language and spits out the assembler code. I had a good look at Bison and how people usually do this sort of thing and got depressed. As a project it started to look too much like hard work, c centric, completely structured etc etc. There was just one way to do it.

But what a project to help discover what is possible with perl. At the very core of compilers/persers/lexical analysers is a problem in text manipulation (isn't it?). It would take some perl-like text (probably using a very restricted subset of perl functionality) and output some assembler-like text.

Can anyone give me some help getting started? Any module that may give me inspiration? - I couldn't find much. Should I maybe look at perl internals? How about starting with a "wrapped" Bison and going from there? I imagine I could get lost very quickly.

Also let me know if you think it a project not suited to perl. I'll still give it a go though - at least to understand why.

Thanks for your help.

tonyday

Replies are listed 'Best First'.
Re: perl as a compiler
by merlyn (Sage) on Mar 14, 2001 at 11:56 UTC
Re: perl as a compiler
by Caillte (Friar) on Mar 14, 2001 at 15:21 UTC

    It is possible to turn a perl script into c code using B::CC or into Jasmin assembler with B::JVM::Jasmin. However I doubt these are what you are looking for. You will probably end up in a situation where you will use a module like Parse::RecDescent to analyse each line in turn and output preformatted assembler. This is likely to be quite a task.

    Another idea to follow would be to turn the whole thing on it's head and do a perl program that does the same as the game does. Accepts 'bots' (blocks of perl code) and evals them. I have worked on something a little like this while at university (which, unfortunately ate my code) while looking at ways of making MUD monsters AI more I and less A. It actually worked quite well.

    $japh->{'Caillte'} = $me;

      Thanks for the idea. I was thinking of automating the process along similar lines - take perl code, eval it by spitting out the assembler routine and running the game. But I sense that you're taking the idea a few steps further.

      now if only I had listened in Compiler 101 class I could begin to comprehend what Parse:RecDescent is on about.

      tonyday