Created
November 17, 2009 15:14
-
-
Save fujiwara/236967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Term::ReadLine; | |
use Data::Dumper; | |
my $term = new Term::ReadLine "Goprog"; | |
my $prompt = ""; | |
my $OUT = $term->OUT || \*STDOUT; | |
my @prog = ("package main"); | |
while ( defined ( my $line = $term->readline($prompt)) ) { | |
if ($line =~ /^\\(\w+)/) { | |
my $cmd = $1; | |
$cmd =~ /^r(un)?/ ? run(\@prog) | |
: $cmd =~ /^q(uit)?/ ? exit() | |
: $cmd =~ /^l(ist)?/ ? list(\@prog) | |
: $cmd =~ /^c(lear)?/ ? clear(\@prog) | |
: print $OUT "command not found: $cmd\n"; | |
} | |
else { | |
push @prog, $line; | |
$term->addhistory($line) if $line =~ /\S/; | |
} | |
} | |
sub clear { | |
my $prog = shift; | |
@$prog = ("package main"); | |
print $OUT "\n"; | |
} | |
sub list { | |
my $prog = shift; | |
print $OUT "----\n"; | |
print $OUT $_, "\n" for @$prog; | |
print $OUT "----\n"; | |
} | |
sub run { | |
my $prog = shift; | |
open my $source, ">", ".goprog.go" or die $!; | |
print $source $_, "\n" for @$prog; | |
close $source; | |
build_and_exec(".goprog"); | |
unlink ".goprog.go"; | |
@$prog = ("package main"); | |
} | |
sub build_and_exec { | |
my $filename = shift; | |
my $go = $ENV{GOARCH} eq "386" ? "8" | |
: $ENV{GOARCH} eq "amd64" ? "6" | |
: $ENV{GOARCH} eq "arm" ? "5" | |
: die "can't detect GOARCH\n"; | |
my $go_compiler = "${go}g"; | |
my $go_linker = "${go}l"; | |
my $go_elf = "${go}.out"; | |
my $res; | |
system($go_compiler, "${filename}.go") == 0 | |
and system($go_linker, "${filename}.${go}") == 0 | |
and system("./${go_elf}"); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment