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 | 300 loc | 4.6 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | # $Id$
=head1
bas.pir -- shakespeare runtime
=cut
.namespace []
.sub 'onload' :anon :load :init
$P0 = new 'ResizablePMCArray'
set_global 'the_cast', $P0
$P0 = 0
set_global 'the_condition', $P0
$P0 = new 'String'
set_global 'the_speaker', $P0
.end
.sub 'say'
.param pmc args :slurpy
.local pmc iter
iter = new 'Iterator', args
iter_loop:
unless iter goto iter_end
$P0 = shift iter
print $P0
goto iter_loop
iter_end:
print "\n"
.return ()
.end
.sub 'declare'
.param string char
$P0 = new 'Hash'
$P0['name'] = char
$P1 = new 'ResizableIntegerArray'
$P0['value']= 0
$P0['list'] = $P1
$P0['onstage'] = 0
set_global char, $P0
get_global $P1 , 'the_cast'
$P1.'push'($P0)
.end
.sub 'assign'
.param int val
.local pmc char
char = 'find_other'()
'valset'(char,val)
.end
.sub 'find_other_name'
$P0 = 'find_other'()
$P1 = $P0['name']
.return ($P1)
.end
.sub 'get_speaker_name'
get_global $P0, 'the_speaker'
$S0 = $P0
.return ($S0)
.end
.sub 'dump'
.param pmc char
.local pmc val,ary
.local int i, v, size, status
.local string name
ary= char['list']
name = char['name']
size = ary
i=0
print "\ndump "
print name
print ": v="
v= char['value']
print v
print " list=("
loop:
if i == size goto end_loop
val = ary[0]
i += 1
print val
print ","
end_loop:
print ")\n"
.end
.sub 'find_other'
.local int i, size, status
.local string name, speaker
.local pmc person
speaker = 'get_speaker_name'()
get_global $P0, 'the_cast'
size = $P0
i = 0
loop:
if i == size goto end_loop
person = $P0[i]
i += 1
status = person['onstage']
unless status goto loop
name = person['name']
if name == speaker goto loop
end_loop:
if status goto real_end
die "there is only one person on stage!"
real_end:
.return (person)
.end
.sub 'valueof'
.param string char
get_global $P0, char
$P2 = 'valget'($P0)
.return ($P2)
.end
.sub 'enter'
.param string char
get_global $P0, char
unless null $P0 goto fin
die "no such character in the cast!"
fin:
$P0['onstage'] = 1
.end
.sub 'exit'
.param string char
get_global $P0, char
$P0['onstage'] = 0
.return ()
.end
.sub 'exeunt_omnes'
get_global $P0, 'the_cast'
.local int i,size
size = $P0
i=0
loop:
$P1 = $P0[i]
i = i + 1
if i == size goto end_loop
$I0 = $P1['onstage']
unless $I0 goto loop
$P1['onstage'] = 0
goto loop
end_loop:
.return ()
.end
.namespace []
.sub 'tap'
.param int val
print "ok "
print val
print "\n"
.return ()
.end
.sub 'plan'
.param int val
print "1.."
print val
print "\n"
.return ()
.end
.sub 'dup'
.param int b
.local int c
c = 2*b
.return (c)
.end
.sub 'fact'
.param int b
.local int c
c = fact b
.return (c)
.end
.sub 'square'
.param num b
.local num c
c = b*b
.return (c)
.end
.sub 'cube'
.param num b
.local num c
c = b*b
c = c*b
.return (c)
.end
.sub 'sqrt'
.param num b
.local num c
c = sqrt b
.return (c)
.end
.sub 'better'
.param int a
.param int b
$I0 = a > b
new $P0, 'Integer'
set $P0, $I0
set_global 'the_condition', $P0
.end
.sub 'worse'
.param int a
.param int b
$I0 = a < b
new $P0, 'Integer'
set $P0, $I0
set_global 'the_condition', $P0
.end
.sub 'equal'
.param int a
.param int b
$I0 = a == b
new $P0, 'Integer'
set $P0, $I0
set_global 'the_condition', $P0
.end
.sub 'push'
.param int a
$P0 = 'find_other'()
$P1 = $P0['list']
push $P1, a
.end
.sub 'pop'
$P0 = 'find_other'()
$P1 = $P0['list']
$I1 = pop $P1
$P0['value']=$I1
.end
.sub 'open_heart'
$P0 = 'find_other'()
$I1 = 'valget'($P0)
print $I1
.end
.sub 'valget'
.param pmc char
$I2 = char['value']
.return ($I2)
.end
.sub 'valset'
.param pmc char
.param int val
char['value'] = val
.end
.sub 'listen_heart'
$P0 = 'find_other'()
$P1 = getstdin
readline $S1, $P1
$I1 = $S1
'valset'($P0, $I1)
.end
.sub 'speak_mind'
$P0 = 'find_other'()
$I1 = 'valget'($P0)
$S1 = chr $I1
print $S1
.end
.sub 'open_mind'
$P0 = 'find_other'()
$P1 = getstdin
read $S1, 1
if ''==$S1 goto empty
$I1 = ord $S1
goto end
empty:
$I1 = -1
end:
'valset'($P0,$I1)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
|
