Language::Kemuri - Kemuri Interpreter.


Language-Kemuri documentation Contained in the Language-Kemuri distribution.

Index


Code Index:

NAME

Top

Language::Kemuri - Kemuri Interpreter.

SYNOPSIS

Top

    use Language::Kemuri;
    kemuri('`|'); # => Hello, world!

DESCRIPTION

Top

An interpreter for Kemuri language.

METHODS

Top

kemuri

run the kemuri, and return the output value.

BUGS AND LIMITATIONS

Top

No bugs have been reported.

AUTHOR

Top

Tokuhiro Matsuno <tokuhiro __at__ mobilefactory.jp>

LICENSE AND COPYRIGHT

Top


Language-Kemuri documentation Contained in the Language-Kemuri distribution.

package Language::Kemuri;
use strict;
use warnings;
use Carp;
our $VERSION = '0.06';
use Exporter;
use Switch;
our @EXPORT_OK = qw/kemuri/;

sub kemuri {
    my $code = shift;

    my @stack;
    my $buf = "";
    
    for my $c ( split //, $code ) {
        switch ($c) {
            case '`' {
                push @stack, unpack("C*", reverse "Hello, world!");
            }
            case '"' {
                my $x = pop @stack;
                push @stack, $x;
                push @stack, $x;
            }
            case "'" {
                my $x = pop @stack;
                my $y = pop @stack;
                my $z = pop @stack;
                push @stack, $x;
                push @stack, $z;
                push @stack, $y;
            }
            case '^' {
                my $x = pop @stack;
                my $y = pop @stack;
                push @stack, $x ^ $y;
            }
            case '~' {
                my $x = pop @stack;
                push @stack, -($x+1);
            }
            case '|' {
                $buf .= reverse map { chr($_ % 256) } @stack;
            }
            else {
                croak "unknown kemuri token $c in $code";
            }
        }
    }

    return $buf;
}

1;
__END__