Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

とりあえずコミッチ

  • Loading branch information...
commit f2259d47311ce95b0c511aba887e4bb3d421dfd2 0 parents
@kentaro authored
6 .gitignore
@@ -0,0 +1,6 @@
+META.yml
+Makefile
+blib/
+inc/
+pm_to_blib
+covered
3  .shipit
@@ -0,0 +1,3 @@
+steps = FindVersion, ChangeVersion, CheckChangeLog, DistTest, Commit, Tag, MakeDist, UploadCPAN
+svk.tagpattern = release-%v
+CheckChangeLog.files = Changes
4 Changes
@@ -0,0 +1,4 @@
+Revision history for Perl extension Language::Misa
+
+0.01 Tue Jan 12 18:49:14 2010
+ - original version
23 MANIFEST
@@ -0,0 +1,23 @@
+Changes
+inc/Module/Install.pm
+inc/Module/Install/Base.pm
+inc/Module/Install/Can.pm
+inc/Module/Install/Fetch.pm
+inc/Module/Install/Include.pm
+inc/Module/Install/Makefile.pm
+inc/Module/Install/Metadata.pm
+inc/Module/Install/TestBase.pm
+inc/Module/Install/Win32.pm
+inc/Module/Install/WriteAll.pm
+inc/Spiffy.pm
+inc/Test/Base.pm
+inc/Test/Base/Filter.pm
+inc/Test/Builder.pm
+inc/Test/Builder/Module.pm
+inc/Test/More.pm
+lib/Language/Misa.pm
+Makefile.PL
+MANIFEST This list of files
+META.yml
+README
+t/00_compile.t
23 MANIFEST.SKIP
@@ -0,0 +1,23 @@
+\bRCS\b
+\bCVS\b
+\.svn/
+\.git/
+^tags$
+^TAGS$
+^MANIFEST\.
+^Makefile$
+~$
+^#
+\.old$
+^blib/
+^pm_to_blib
+^MakeMaker-\d
+\.gz$
+\.cvsignore
+^t/9\d_.*\.t
+^t/perlcritic
+^tools/
+^[^/]+\.yaml$
+^[^/]+\.pl$
+^\.shipit$
+^\.PerlySenseProject/
13 Makefile.PL
@@ -0,0 +1,13 @@
+use inc::Module::Install;
+
+name 'Language-Misa';
+author 'Kentaro Kuribayashi, <kentarok@cpan.org>';
+license 'MIT';
+all_from 'lib/Language/Misa.pm';
+
+requires 'Class::Accessor::Lvalue';
+
+author_tests 'xt';
+auto_set_repository;
+auto_include_deps;
+WriteAll;
52 README
@@ -0,0 +1,52 @@
+This is Perl module Language::Misa.
+
+INSTALLATION
+
+Language::Misa installation is straightforward. If your CPAN shell is
+set up, you should just be able to do
+
+ % cpan Language::Misa
+
+Download it, unpack it, then build it as per the usual:
+
+ % perl Makefile.PL
+ % make && make test
+
+Then install it:
+
+ % make install
+
+DOCUMENTATION
+
+Language::Misa documentation is available as in POD. So you can do:
+
+ % perldoc Language::Misa
+
+to read the documentation online with your favorite pager.
+
+AUTHOR
+
+Kentaro Kuribayashi <kentaro@cpan.org>
+
+COPYRIGHT AND LICENSE (The MIT License)
+
+Copyright (c) Kentaro Kuribayashi <kentaro@cpan.org>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
131 lib/Language/Misa.pm
@@ -0,0 +1,131 @@
+package Language::Misa;
+use 5.008_001;
+use strict;
+use warnings;
+use base qw(Language::Misa::Thing);
+use Language::Misa::VM;
+use Language::Misa::Compiler;
+
+our $VERSION = '0.01';
+
+sub run {
+ my ($class, $code) = @_;
+ my $instructions = Language::Misa::Compiler->compile($code);
+ Language::Misa::VM->run($instructions);
+}
+
+!!1;
+
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+Language::Misa - A Perl implementation of programming language Misa.
+
+=head1 SYNOPSIS
+
+ use Language::Misa;
+
+ Language::Misa->run(<'EOS');
+ ごっ、ごぉおっ、ご~きげんよおぉおおぉおほっ。ほおぉおぉおっ。
+
+ 「ごきげん☆みゃぁああ”あ”ぁ”ぁああ~っ」
+
+ さわやかな朝の☆ご挨拶! お挨拶がっ。
+ 澄みきった青空にこだましちゃうぉ~ああぉおおおぉん。
+
+ 「は、はひっ、はろおぉっ☆わぁるどおおぉっぉ~っ」
+
+ こ、この文章は☆おサンプル! おおぉおぉおおサンプルプログラム!!
+ どんなおプログラム言語でも基本のご挨拶させていただくのぉぉおッ!
+
+ 「ぽうっ」
+
+ 長々と書くのがこ、ここでの~、ここでのぉおおぉおぉぉおたしなみぃぃいぃ。
+
+ 「長いぃ。長すぎましゅう。ご挨拶にこんなプログラム長すぎまひゅぅうぅ☆
+ んおおぉぉ、ばかになる、おばかになっちゃいましゅ~ッ」
+
+ 長いのがっ、バッファの奥まで入ってきましゅたぁあぁあっ!
+ ばっふぁ☆溢れちゃいまひゅぅ~。あみゃぁあ”あ”ぁ”ぁああ”あ”ぁぁ。
+
+ 「で、出ます☆ んおおぉぉおおっ、エラー出ちゃいまひゅっ」
+
+ ほひぃ☆! え、えらーっ、んお”お”ぉお”お”ぉおぉおおぉっっ。
+
+ 「出た☆ 出た出た出た出たぁぁあっ えらあぴゅるーっって出たあぁっ」
+
+ はしたない☆! ぉおおぉはしたないっ! おはしたない言語ですっっっっっっっ!
+ おほっほおぉっっっほおぉっっっっっっっっっ!
+
+ 「えらあらいしゅきぃぃぃいぃっっ」
+
+ 止まらない すごい エラーみるく
+ こってりしたのがいっぱい出てるよぉぉぉおおぉぉおおぉぉおっっ。
+
+ 「んほぉっ☆ っおぉぉぉおお国が分からなくなっちゃいまひゅう~っ」
+
+ ま、まだ出るぅ☆ 出てるのおぉっ☆ エラーまだまだ出ましゅぅぅ!
+ ばんじゃ~ぁぁあい、ばんじゃいぃぃ、ばんにゃんじゃぁんじゃあぁぁああぁい!
+ EOS
+
+ #=> prints "HelloWorld!"
+
+=head1 DESCRIPTION
+
+Language::Misa is blah, blah, blah...
+
+=head1 METHODS
+
+=head2 run ( I<$code> )
+
+=over 4
+
+ Language::Misa->run($code);
+
+Execute given I<$code> as Misa code.
+
+=back
+
+=head1 SEE ALSO
+
+=over 4
+
+=item * Programming Language Misa
+
+http://homepage2.nifty.com/kujira_niku/okayu/misa.html
+
+=back
+
+=head1 AUTHOR
+
+Kentaro Kuribayashi E<lt>kentaro@cpan.orgE<gt>
+
+=head1 SEE ALSO
+
+=head1 COPYRIGHT AND LICENSE (The MIT License)
+
+Copyright (c) Kentaro Kuribayashi E<lt>kentaro@cpan.orgE<gt>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+=cut
71 lib/Language/Misa/Compiler.pm
@@ -0,0 +1,71 @@
+package Language::Misa::Compiler;
+use strict;
+use warnings;
+use utf8;
+use Carp qw(croak);
+use Encode qw(decode_utf8 is_utf8);
+
+my %instruction_map;
+ $instruction_map{$_} = 'incr' for qw(> > → ~ ー);
+ $instruction_map{$_} = 'decr' for qw(< < ← ★ ☆);
+ $instruction_map{$_} = 'add' for qw(+ + あ ぁ お ぉ);
+ $instruction_map{$_} = 'sub' for qw(- − っ ッ);
+ $instruction_map{$_} = 'put' for qw(. ! !);
+ $instruction_map{$_} = 'set' for ',', qw(? ?);
+ $instruction_map{$_} = 'loop_start' for qw([ 「 『);
+ $instruction_map{$_} = 'loop_end' for qw(] 」 』);
+
+sub tokenize {
+ my ($class, $code) = @_;
+ $code = decode_utf8($code) if !is_utf8($code);
+
+ # strip comment line
+ $code =~ s/^#.+?$//mg;
+
+ # strip nonsense chars
+ my $pattern = '[^' . (join '', map { quotemeta $_ } keys %instruction_map) . ']';
+ $code =~ s/$pattern//g;
+
+ [split //, $code];
+}
+
+sub compile {
+ my ($class, $code) = @_;
+ my @tokens = @{$class->tokenize($code)};
+ my @instructions;
+ my $i = 0;
+
+ for my $token (@tokens) {
+ my $instruction = $instruction_map{$token} or next;
+
+ if ($instruction eq 'loop_start') {
+ for (my $j = $i + 1; $j <= $#tokens; $j++) {
+ if (($instruction_map{$tokens[$j]} || '') eq 'loop_end') {
+ push @instructions, [$instruction, $j];
+ last;
+ }
+ croak "mathing $token not found"
+ if $j == $#tokens;
+ }
+ }
+ elsif ($instruction eq 'loop_end') {
+ for (my $j = $i - 1; $j >= 0; $j--) {
+ if (($instruction_map{$tokens[$j]} || '') eq 'loop_start') {
+ push @instructions, [$instruction, $j - 1];
+ last;
+ }
+ croak "mathing $token not found"
+ if $j == 0;
+ }
+ }
+ else {
+ push @instructions, [$instruction];
+ }
+
+ $i++;
+ }
+
+ \@instructions;
+}
+
+!!1;
6 lib/Language/Misa/Thing.pm
@@ -0,0 +1,6 @@
+package Language::Misa::Thing;
+use strict;
+use warnings;
+use base qw(Class::Accessor::Lvalue::Fast);
+
+!!1;
66 lib/Language/Misa/VM.pm
@@ -0,0 +1,66 @@
+package Language::Misa::VM;
+use strict;
+use warnings;
+use base qw(Language::Misa::Thing);
+
+__PACKAGE__->mk_accessors(qw(
+ stack
+ sp
+ ip
+ instructions
+));
+
+sub new {
+ my $class = shift;
+ my $self = $class->SUPER::new(@_);
+ $self->stack = [];
+ $self->sp = 0;
+ $self->ip = 0;
+ $self;
+}
+
+sub current : lvalue {
+ my $self = shift;
+ $self->stack->[$self->sp];
+}
+
+sub size { scalar @{shift->instructions} }
+
+sub run {
+ my ($class, $instructions) = @_;
+ my $self = $class->new({ instructions => $instructions });
+ while ($self->ip < $self->size) {
+ my $insn = $self->instructions->[$self->ip][0];
+ my $length = scalar @{$self->instructions->[$self->ip]};
+ my @args = @{$self->instructions->[$self->ip]}[1..$length];
+ $self->can($insn)->($self, @args);
+ $self->ip++;
+ }
+}
+
+sub jump_to {
+ my ($self, $to) = @_;
+ $self->ip = $to;
+}
+
+# instructions
+sub incr { shift->sp++ }
+sub decr { shift->sp-- }
+sub add { shift->current++ }
+sub sub { shift->current-- }
+sub put { print chr (shift->current || 0) }
+sub set { shift->current = substr <STDIN>, 0, 1 }
+
+sub loop_start {
+ my ($self, $end) = @_;
+ if (!$self->current) {
+ $self->jump_to($end);
+ }
+}
+
+sub loop_end {
+ my ($self, $start) = @_;
+ $self->jump_to($start);
+}
+
+!!1;
3  t/compile.t
@@ -0,0 +1,3 @@
+use strict;
+use Test::More tests => 1;
+BEGIN { use_ok 'Language::Misa' }

0 comments on commit f2259d4

Please sign in to comment.
Something went wrong with that request. Please try again.