Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

I'm bringing goto back

Name: Anonymous 2014-02-09 11:15

Yeah, "goto considered harmful" considered harmful.
It's so easy to parse when it's one command per line...

Here's what I did so far


open FILE, "filename.txt" or die $!;

my @lines = <FILE>;
my %labels;

my $ind = 0;

while ($ind < @lines.length){
    my $cmd = @lines[$ind];
    if ($cmd =~ /^\s*(\w+):$/){
        if (!exists($labels{$1})){
            $labels{$1} = $ind;
        }
    }
    $ind++;
}

$ind = 0;

while ($ind < @lines.length){
    my $cmd = @lines[$ind];
    if ($cmd =~ /^\s*goto (\w+);$/){
        if (exists($labels{$1})){
            $ind = $labels{$1};
        }
    } elsif ($cmd =~ /^\s*print \"(.+)\";$/){
        print $1;
    }
    $ind++;
}


example program:

asd:
print "asd";
goto doge;
print "never printed";
doge:
print "aw! aw!";
goto asd;

Name: Anonymous 2014-02-09 11:45

imperative programming considered harmful

Name: Anonymous 2014-02-09 12:52

Hey, I was thinking, would be cool something like fork label; that spawns a new process starting at the given label

Name: Anonymous 2014-02-09 12:53

Lets see how we can replace an ugly goto with an elegant structured programming constructs.

Given an unstructured loop

line1:
  printf("Dijkstra sucks cocks in hell.\n");
  goto line1;


Without sacrificing any expressiveness, we can refactor it using goto into:

line = 0;
while (line++ > 0)
{
  switch(line)
  {
  case 1: printf("Dijkstra sucks cocks in hell.\n") break;
  case 2: line = 1; break;
  }
}


Not only did we killed goto, but also gained a way to address lines of code programmatically. I.e. we can compute the next value of "line" variable.

Name: Anonymous 2014-02-09 13:00

>>4
line = 0;
while (line++ > 0)

>while (0 > 0)

also, I know that is possible, etc, but I just wanted a simple language to make to play with, and getting that low-level feel is cool

Name: Anonymous 2014-02-09 13:10

>>5
++line
self fix

Name: Anonymous 2014-02-09 13:57

>>3
All I found was this http://cis.poly.edu/muller/CS623/FORKJOIN.htm but it doesn't say what language it is using

Name: Anonymous 2014-02-09 14:44

>>4
I know you're trolling, but why not while (true) printf("Dijkstra sucks cocks in hell.\n")? That simulated labels thing leads to just as much spaghetti as goto.

Name: Anonymous 2014-02-09 18:15

>>8
that's not what his code does

Name: Anonymous 2014-02-10 3:08

USE FORTH YOU FAT FUCKER

Name: Anonymous 2014-02-10 4:23

>>10
No one except Mentishit uses Forth. It's a dead language that belongs in the ancient Egypt exhibit in a history museum, deal with it.

Name: Anonymous 2014-02-10 4:55

>>11
I use forth.

Name: Anonymous 2014-02-10 7:20

>>12

therefore you are metifex

Don't change these.
Name: Email:
Entire Thread Thread List