| 2 | | # |
| 3 | | # grass.rb - Grass interpreter |
| 4 | | # http://www.blue.sky.or.jp/grass/ |
| 5 | | # |
| 6 | | # Copyright (C) 2008 NISHIO Hirokazu. All rights reserved. |
| 7 | | # |
| 8 | | # Redistribution and use in source and binary forms, with or without |
| 9 | | # modification, are permitted provided that the following conditions |
| 10 | | # are met: |
| 11 | | # 1. Redistributions of source code must retain the above copyright |
| 12 | | # notice, this list of conditions and the following disclaimer. |
| 13 | | # 2. Redistributions in binary form must reproduce the above copyright |
| 14 | | # notice, this list of conditions and the following disclaimer in |
| 15 | | # the documentation and/or other materials provided with the |
| 16 | | # distribution. |
| 17 | | # |
| 18 | | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' |
| 19 | | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 | | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 | | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS |
| 22 | | # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | | # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | | # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | | # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 28 | | # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | | # |
| 30 | | # History: |
| 31 | | # |
| 32 | | # 2008-06-06 |
| 33 | | # - Tlanslated to python by NISHIO |
| 34 | | # 2007-10-02 |
| 35 | | # - Follow the latest changes of the definition of Grass. |
| 36 | | # - by UENO Katsuhiro |
| 37 | | # 2007-09-20 |
| 38 | | # - First version by UENO Katsuhiro. |
| 39 | | # |
| 40 | | |
| | 2 | """ |
| | 3 | grass.py - Grass interpreter |
| | 4 | http://www.blue.sky.or.jp/grass/ |
| | 5 | |
| | 6 | Copyright (C) 2008 NISHIO Hirokazu. All rights reserved. |
| | 7 | |
| | 8 | Redistribution and use in source and binary forms, with or without |
| | 9 | modification, are permitted provided that the following conditions |
| | 10 | are met: |
| | 11 | 1. Redistributions of source code must retain the above copyright |
| | 12 | notice, this list of conditions and the following disclaimer. |
| | 13 | 2. Redistributions in binary form must reproduce the above copyright |
| | 14 | notice, this list of conditions and the following disclaimer in |
| | 15 | the documentation and/or other materials provided with the |
| | 16 | distribution. |
| | 17 | |
| | 18 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' |
| | 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| | 20 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| | 21 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS |
| | 22 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| | 23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| | 24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| | 25 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| | 26 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| | 27 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| | 28 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| | 29 | |
| | 30 | History: |
| | 31 | |
| | 32 | 2008-06-06 |
| | 33 | - Tlanslated to python by NISHIO |
| | 34 | 2007-10-02 |
| | 35 | - Follow the latest changes of the definition of Grass. |
| | 36 | - by UENO Katsuhiro |
| | 37 | 2007-09-20 |
| | 38 | - First version by UENO Katsuhiro. |
| | 39 | |
| | 40 | |
| | 41 | TESTS |
| | 42 | >>> run("wWWwwww") |
| | 43 | w |
| | 44 | |
| | 45 | >>> run('''wwWWwv \ |
| | 46 | wwwwWWWwwWwwWWWWWWwwwwWwwv \ |
| | 47 | wWWwwwWwwwwWwwwwwwWwwwwwwwww''') |
| | 48 | ww |
| | 49 | |
| | 50 | >>> run("wWWWwwwwWWWw") # x |
| | 51 | x |
| | 52 | |
| | 53 | """ |
| 208 | | def test(): |
| 209 | | """ |
| 210 | | >>> run("wWWwwww") |
| 211 | | w |
| 212 | | |
| 213 | | >>> run(''' |
| 214 | | wwWWwv |
| 215 | | wwwwWWWwwWwwWWWWWWwwwwWwwv |
| 216 | | wWWwwwWwwwwWwwwwwwWwwwwwwwww |
| 217 | | ''') |
| 218 | | ww |
| 219 | | |
| 220 | | >>> run("wWWWwwwwWWWw") # x |
| 221 | | x |
| 222 | | """ |
| 223 | | |
| 224 | | """ |
| 225 | | >>> map(lambda x: 256 + ord(x) - ord("w") , "Hello, world") |
| 226 | | [209, 238, 245, 245, 248, 181, 169, 256, 248, 251, 245, 237] |
| 227 | | >>> set(_) |
| 228 | | set([256, 169, 237, 238, 209, 245, 248, 251, 181]) |
| 229 | | |
| 230 | | wWWwWWWwv # +16 |
| 231 | | wWWwWWWwv # +32 |
| 232 | | wWWwWWWwv # +64 |
| 233 | | wWWwWWWwv # +128 |
| 234 | | wWWwwwwwwwwwww # print w+128 |
| 235 | | """ |
| 369 | | """ |
| 370 | | funcdef("wWWWwWWWWwv", "+2") + |
| 371 | | funcdef("wWWwWWWwv", "+4") + |
| 372 | | funcdef("wWWwWWWwv", "+8") + |
| 373 | | funcdef("wWWwWWWwv", "+16") + |
| 374 | | funcdef("wWWwWWWwv", "+32") + |
| 375 | | funcdef("wWWwWWWwv", "+64") + |
| 376 | | funcdef("wWWwWWWwv", "+128") + |
| 377 | | |
| 378 | | # [In, Char(119), Succ, Out, +2, +4, +8, +16, +32, +64, +128] |
| 379 | | |
| 380 | | >>> foo(256 + ord("H") - ord("w")) |
| 381 | | [1, 0, 0, 0, 16, 0, 64] |
| 382 | | >>> def foo(x): |
| 383 | | result = [] |
| 384 | | for i in range(8): |
| 385 | | result.append(int((2 ** i) & x)) |
| 386 | | return result |
| 387 | | |
| 388 | | >>> foo(256 + ord("H") - ord("w")) |
| 389 | | [1, 0, 0, 0, 16, 0, 64, 128] |
| 390 | | wWWWWwwwwwwwwwwwWWWWWWWww |
| 391 | | |
| 392 | | |
| 393 | | H [1, 0, 0, 0, 16, 0, 64, 128] |
| 394 | | e [0, 2, 4, 8, 0, 32, 64, 128] |
| 395 | | l [1, 0, 4, 0, 16, 32, 64, 128] |
| 396 | | l [1, 0, 4, 0, 16, 32, 64, 128] |
| 397 | | o [0, 0, 0, 8, 16, 32, 64, 128] |
| 398 | | , [1, 0, 4, 0, 16, 32, 0, 128] |
| 399 | | [1, 0, 0, 8, 0, 32, 0, 128] |
| 400 | | w [0, 0, 0, 0, 0, 0, 0, 0] |
| 401 | | o [0, 0, 0, 8, 16, 32, 64, 128] |
| 402 | | r [1, 2, 0, 8, 16, 32, 64, 128] |
| 403 | | l [1, 0, 4, 0, 16, 32, 64, 128] |
| 404 | | d [1, 0, 4, 8, 0, 32, 64, 128] |
| 405 | | ! [0, 2, 0, 8, 0, 32, 0, 128] |
| 406 | | """ # w + 32 |
| 407 | | |
| 408 | | |
| 409 | | |
| 410 | | #run(code) |
| | 349 | def _test(): |
| | 350 | import doctest |
| | 351 | reload(doctest) |
| | 352 | doctest.testmod() |
| | 353 | print "ok." |
| | 354 | |
| | 355 | def run_stdin(): |
| | 356 | src = sys.stdin.read() |
| | 357 | run(src) |
| | 358 | |
| | 359 | if __name__ == "__main__": |
| | 360 | #_test() |
| | 361 | run_stdin() |