Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Very Simple Stack-based Language

by Aristotle (Chancellor)
on Aug 18, 2002 at 00:13 UTC ( [id://190942]=note: print w/replies, xml ) Need Help??


in reply to Very Simple Stack-based Language

It would be more elegant if, rather than a huge if elsif construct, you used an array with subroutine references. The main loop would shrink to
while($alive) { $turn++; printf "Turn \#% 4d\t", $turn; push @histx, $x; push @histy, $y; push @histh, $heading; $pc = 0 if $pc < 0; $pc = CODESIZE - 1 if $pc >= CODESIZE; my $ci = $code[$pc]; # Current Instruction print $opcode[$ci], "\t"; $pc++; # Just like CPUs do $microcode[$ci]->(@some_params); $pc++; $alive = 0 if $turn > 1000; }
which makes the main loop easier to understand and the individual instructions' code easier to find. Also, I don't see any reason why you do while($alive) { ... ; $alive = 0 if $turn > 1000; } rather than just while($turn <= 1000).

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Very Simple Stack-based Language
by orkysoft (Friar) on Aug 18, 2002 at 17:58 UTC

    Well, the main reason for the lack of optimization is that I created it rather quickly :-).

    Originally, I wrote the loop without knowing what would be at the end, so that is one of the reasons the statements are as they are. Another reason, is that you might not want to use a 1000-turn limit, and indeed want to use $alive to control how long the program runs. In fact, I added the $turn comparison on later after I discovered that endless loops were quite common.

    The suggestion about the subroutine references is interesting, though. I'll see what I can do about that some time. Thanks for your comments!

    Lur: "But if this cape shrinks, consider your species extinct!"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://190942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found