Changeset 13335

Show
Ignore:
Timestamp:
06/06/08 21:59:03 (5 years ago)
Author:
nishio
Message:

/lang/python/grass impl. of a programming language 'Grass'.

Location:
lang/python/grass
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • lang/python/grass/grass.py

    r13333 r13335  
    11# -*- coding: cp932 -*- 
    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 
     41TESTS 
     42>>> run("wWWwwww") 
     43w 
     44 
     45>>> run('''wwWWwv \ 
     46    wwwwWWWwwWwwWWWWWWwwwwWwwv \ 
     47    wWWwwwWwwwwWwwwwwwWwwwwwwwww''') 
     48ww 
     49 
     50>>> run("wWWWwwwwWWWw") # x 
     51x 
     52 
     53""" 
    4154from copy import deepcopy 
    4255import sys 
     
    4760DEBUG = 50 
    4861DEBUG2 = 60 
    49 LOGLEVEL = 0 
     62LOGLEVEL = RELEASE 
    5063NUMERICAL_OUTPUT = False 
    5164def log(level, *msg): 
     
    96109class Fn(Value): 
    97110    count = 0 
     111    name = "" 
    98112    def __init__(self, code, env): 
    99113        self.code, self.env = code, env 
     
    109123 
    110124    def __repr__(self): 
     125        if self.name: 
     126            return self.name 
    111127        return "Fn%d" % self.count 
    112128 
    113129ChurchTrue  = Fn([Abs([App(3,2)])], [Fn([],[])]) 
    114 ChurchTrue.__repr__ = "True" 
     130ChurchTrue.name = "CTrue" 
    115131ChurchFalse = Fn([Abs([])], []) 
    116 ChurchFalse.__repr__ = "False" 
     132ChurchFalse.name = "CFalse" 
    117133 
    118134class CharFn(Value): 
     
    175191 
    176192def start(code): 
    177     global m 
    178     m = Machine(code, deepcopy(InitialEnv), deepcopy(InitialDump)) 
    179     return eval(m) 
     193    return eval(Machine(code, deepcopy(InitialEnv), deepcopy(InitialDump))) 
    180194 
    181195def parse(src): 
     
    206220    start(parse(src)) 
    207221 
    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 """ 
    236222 
    237223## coding utilities 
    238224env = ["In", "w", "Succ", "Out"] 
    239 def funcdef(code, name): 
    240     env.append(name) 
    241     return code 
    242225 
    243226def ws(name, env=env): 
     
    259242    for example 
    260243    [("arg+1", "Succ", "arg1"), ("arg+2", "Succ", "arg1")] 
     244 
     245    >>> print make_func("+2", [\ 
     246            ("tmp", "Succ", "arg1"),\ 
     247            ("result", "Succ", "tmp"),\ 
     248            ]) 
     249    wWWWwWWWWwv 
     250    <BLANKLINE> 
     251    >>> print make_func("+4", [\ 
     252            ("tmp", "+2", "arg1"),\ 
     253            ("result", "+2", "tmp"),\ 
     254            ]) 
     255    wWWwWWWwv 
     256    <BLANKLINE> 
    261257    """ 
    262258    myenv = deepcopy(env) 
     
    270266    return result + "v\n" 
    271267 
    272 """ 
    273 >>> make_func("+2", [ 
    274         ("tmp", "Succ", "arg1"), 
    275         ("result", "Succ", "tmp"), 
    276         ]) 
    277 'wWWWwWWWWwv' 
    278 >>> make_func("+4", [ 
    279         ("tmp", "+2", "arg1"), 
    280         ("result", "+2", "tmp"), 
    281         ]) 
    282 'wWWwWWWwv' 
    283 """ 
    284  
    285 def foo(): 
     268def build_helloworld(): 
    286269    code = ( 
    287270        make_func("+2", [ 
     
    357340            ("tmp", "Out", "d"), 
    358341            ("tmp", "Out", "!"), 
    359             ])+ 
    360  
    361         "" 
    362  
     342            ]) 
    363343    ) 
    364344    import textwrap 
     
    367347    run(code) 
    368348 
    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) 
     349def _test(): 
     350    import doctest 
     351    reload(doctest) 
     352    doctest.testmod() 
     353    print "ok." 
     354 
     355def run_stdin(): 
     356    src = sys.stdin.read() 
     357    run(src) 
     358     
     359if __name__ == "__main__": 
     360    #_test() 
     361    run_stdin()