Acme-Aheui
view release on metacpan or search on metacpan
Revision history for perl module Acme::Aheui
0.05 2015-01-29 01:38:37 Asia/Seoul
- Detects terminal encoding
- If program read from keystrokes, ends with an empty line
- If program read through a pipe, continues until EOF
0.04 2015-01-28 13:27:43 Asia/Seoul
- (Fix) To properly read unicode characters from stdin
0.03 2015-01-28 12:39:51 Asia/Seoul
- If program read from stdin, continues until EOF, not an empty line
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use Encode qw/decode/;
use Term::Encoding;
use Acme::Aheui;
my $encoding = Term::Encoding::get_encoding();
binmode STDIN, ":encoding($encoding)";
my ($filename) = @ARGV;
my $help = << "__HELP__";
Acme::Aheui $Acme::Aheui::VERSION
Usage:
aheui filename : program read from source file
aheui : program read from stdin
__HELP__
die $help if ($filename && $filename =~ /^--?h(elp)?$/);
my $source;
if ($filename) {
open(FH, $filename) or die "cannot open $filename\n";
$source = join '', <FH>;
close(FH);
$source = decode('utf-8', $source); # expect utf8 by aheui spec
}
elsif (-t STDIN and -z STDIN) {
print "Acme::Aheui $Acme::Aheui::VERSION ($encoding)\n";
print "Type aheui commands or empty line for EOF.\n";
my @lines = ();
while (1) {
print '>>> ';
my $line = <>;
last if !$line or $line =~ /^\r?\n$/;
push @lines, $line;
}
$source = join '', @lines;
}
lib/Acme/Aheui.pm view on Meta::CPAN
package Acme::Aheui;
use utf8;
use strict;
use warnings;
use Term::ReadKey;
use Term::Encoding;
use Encode qw/encode/;
=encoding utf8
=head1 NAME
Acme::Aheui - an aheui interpreter
=head1 VERSION
Version 0.05
=cut
lib/Acme/Aheui.pm view on Meta::CPAN
my $interpreter = Acme::Aheui->new( source => 'ìí¬' );
This method will create and return C<Acme::Aheui> object.
=cut
sub new {
my $class = shift;
my %args = @_;
my $source = $args{source} || '';
my $encoding = $args{output_encoding} || Term::Encoding::get_encoding();
my $self = {
_codespace => build_codespace($source),
_stacks => [],
_stack_index => 0,
_x => 0,
_y => 0,
_dx => 0,
_dy => 1,
_encoding => $encoding,
};
bless $self, $class;
return $self;
}
sub build_codespace {
my ($source) = @_;
my @lines = split /\r?\n/, $source;
lib/Acme/Aheui.pm view on Meta::CPAN
sub _output_number {
my ($self, $number) = @_;
print $number;
}
sub _output_code_as_character {
my ($self, $code) = @_;
my $unichar = pack 'U', $code;
print encode($self->{_encoding}, $unichar);
}
sub _get_input_character_as_code {
my ($self) = @_;
my $char = ReadKey(0);
return unpack 'U', $char;
}
sub _get_input_number {
t/01_aheui.t view on Meta::CPAN
is( $stderr, '' );
}
{ # input characters
my ($stdout, $stderr, @result) = capture {
my $stdin;
open($stdin,'<&STDIN');
*STDIN = *DATA;
my $interpreter = Acme::Aheui->new(
source => '밯밯맣맣í',
output_encoding => 'utf-8',
);
$interpreter->execute();
*STDIN = $stdin;
};
is( decode('utf-8', $stdout), '몽ì¦' );
is( $stderr, '' );
}
done_testing();
( run in 0.270 second using v1.01-cache-2.11-cpan-4d50c553e7e )