riffraff / shakespeare-parrot
A Parrot based interpreter for the Shakespeare programming language
Clone this repository (size: 640.1 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/riffraff/shakespeare-parrot/
| commit 92: | e4f0025e7622 |
| parent 91: | dfcc448b7385 |
| branch: | default |
| tags: | tip |
properly release as Beer Ware code
| r92:e4f0025e7622 | 183 loc | 3.3 KB | embed / history / annotate / raw / |
|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | class shakespeare::Grammar::Actions;
method TOP($/) {
my $past := PAST::Block.new();
for $<chardef> {
$past.push( mkcall('declare', mkstr($_<character>)));
}
for $<act> {
$past.push( $( $_ ) );
}
make $past;
}
sub mkstr($arg) {
return PAST::Val.new(:value(""~$arg));
}
sub mkcall($name, $arg) {
return PAST::Op.new($arg, :name($name), :pasttype('call'));
}
sub mkcall0($name) {
return PAST::Op.new(:name($name), :pasttype('call'));
}
method assignment($/) {
make mkcall('assign', $($<value>));
}
method events($/, $k) {
make $( $/{$k} );
}
method act($/, $k) {
my $past := PAST::Stmts.new();
if $k eq 'open' {
#fugly
our $?ACT := ""~$<roman>;
$past.push(PAST::Op.new( :inline("act_"~$?ACT~":")));
}
else {
for $<scene> {
$past.push( $( $_ ) );
}
}
make $past;
}
method scene($/) {
our $?ACT;
my $past := PAST::Stmts.new(:name($<roman>));
my $lbl := PAST::Op.new(
:inline("scene_"~$?ACT~"_"~$<roman>~":"));
$past.push($lbl);
for $<events> {
$past.push( $( $_ ) );
}
make $past;
}
method branch($/) {
our $?ACT;
make PAST::Op.new(:inline("goto "~"scene_"~$?ACT~"_"~$<roman>));
}
method io($/,$k) {
make mkcall0($k);
}
method line($/) {
my $past := PAST::Stmts.new();
$past.push(
PAST::Op.new(
PAST::Var.new(
:name('the_speaker'),
:scope('package')
),
mkstr($<character>),
:pasttype('bind')
)
);
for $<sentence> {
$past.push( $( $_ ) );
}
make $past;
}
method sentence($/, $key) {
make $( $/{$key} );
}
method value($/, $key) {
make $( $/{$key} );
}
method question($/) {
my $res := $($<comparison>);
for $<value> {
$res.push( $( $_ ));
}
make $res;
}
method comparison($/,$k) {
make mkcall0( $k );
}
method reference($/, $k) {
if $k eq 'named_ref' {
make mkcall('valueof', mkstr($<character>));
}
elsif $k eq 'you_ref' {
make mkcall('valueof', mkcall0('find_other_name'));
}
else {
make mkcall('valueof', mkcall0('get_speaker_name'));
}
}
method move($/, $k) {
my $past := PAST::Stmts.new();
if $<character> {
for $<character> {
my $res := mkcall($k, mkstr($_));
$past.push($res);
}
}
else {
$past.push(mkcall0('exeunt_omnes'));
}
make $past;
}
method immediate($/) {
my $value := 1;
if $<noun><negative_noun> {
$value := -1;
}
elsif $<noun><nothing> {
$value := 0;
}
for $<adjective> {
unless $_<first_person_possessive> || $_<second_person_possessive> || $_<third_person_possessive> {
$value:= $value*2;
}
}
make PAST::Val.new( :value( $value ) );
}
method tap($/) {
make mkcall('tap', $($<value>));
}
method memorize($/) {
make mkcall('push', $($<value>));
}
method recall($/) {
make mkcall0( 'pop' );
}
method plan($/) {
make mkcall('plan', $($<value>));
}
method test($/, $k) {
my $test := PAST::Var.new(:name('the_condition'),:scope('package'));
make PAST::Op.new( $test, $($<sentence>), :pasttype($k));
}
method computation($/, $key) {
make $( $/{$key} );
}
method unary($/, $k) {
make mkcall($k, $($<value>));
}
method binary($/, $key) {
my $res := PAST::Op.new(:pirop( $key ));
for $<value> { $res.push( $( $_ )) }
make $res;
}
|
