Changeset 196:51ca3867051f

mearie.org/current

File projects/esotope/ws.en.txt

196:51ca3867051f 198:111012b2f4dd
Author:
Kang Seonghoon <public+hg@mearie.org>
Committed on
Permission:
-rw-r--r--

pages for esotope-ws have been added.

      1 ## vim: syn=mako
      2 <%! title = u'esotope-ws' %>
      3 
      4 **esotope-ws** is a complete implementation of [Whitespace programming language](http://compsoc.dur.ac.uk/whitespace/) version 0.3, which commands are encoded into a sequence of white spaces only. It contains an interpreter, assembler and disassembler, and nicknamed *Tenma*(天満) because of its characteristic shape.
      5 
      6 
      7 Usage
      8 ======
      9 
     10 esotope-ws requires [Python](http://python.org/) 2.3 or later, but not Python 3.x. In the interpreter mode, it receives one argument which is a file name. (`hworld.ws` referred below is [available](http://compsoc.dur.ac.uk/whitespace/hworld.ws) at the official website.)
     11 
     12 	$ esotope-ws hworld.ws
     13 	Hello, world of spaces!
     14 
     15 Similarly the assembler mode (`-c` option) and disassembler mode (`-d` option) receives two arguments.
     16 
     17 	$ esotope-ws -d hworld.ws hworld.wsa
     18 	decompile hworld.ws to hworld.wsa... done.
     19 	$ tail -n 15 hworld.wsa 
     20 	  add
     21 	  jmp read
     22 	read_end:
     23 	  pop
     24 	  push 1
     25 	  add
     26 	  push 0
     27 	  store
     28 	  ret
     29 	newline:
     30 	  push 10
     31 	  push 13
     32 	  putchar
     33 	  putchar
     34 	  ret
     35 	$ esotope-ws -c hworld.wsa hworld2.ws
     36 	compile hworld.wsa to hworld2.ws... done.
     37 	$ esotope-ws hworld2.ws 
     38 	Hello, world of spaces!
     39 
     40 The Whitespace assembly syntax consists of command lines (e.g. <`push 1`>) and label lines (e.g. <`newline:`>). The following command mnemonics are available:
     41 
     42 	push N    dup       copy N    swap      pop       slide     add       sub
     43 	mul       div       mod       store     retrieve  call      jmp L     jz L
     44 	jn L      ret L     halt      putchar   putint    getchar   getint
     45 
     46 ...where <`L`> stands for any label and <`N`> stands for any integer constant. The disassembler converts the label name into the readable string if possible, and falls back with a generated name `label#_####`.
     47 
     48 
     49 Known problems
     50 --------------
     51 
     52 This code is quite anarchic (dated December 2004) and has several known problems:
     53 
     54 - `retrieve` command to uninitialized location generates an error in the official interpreter, but esotope-ws simply returns zero.
     55 - A jump to unknown label generates an exception, not a proper error message.
     56 - There are some edge cases that generate same generated name for different non-string label name.
     57 
     58 As the fix to them may require the change of the code shape, I don't plan to fix them.
     59 
     60 
     61 The code
     62 ========
     63 
     64 <div class="extrasmall">
     65 <%text filter="indent">
     66 #!/usr/bin/env python
     67 '                                            20041208\
     68                                       ';from string import*;\
     69                                  d=replace;R=(d(d(d(d(d(d(d(d(d(d(
     70                              d(d(d(d(d(d(d(d(d("def N#:?p=[0]?whV p["
     71                          "H]<2:p+=[r#]?^turn((p[1]H)%3H)*^duce(F x,y:x*"
     72                       "2+y,p[2:H],0)\ndef L#:?p=(0,)?whV p[H]<2:p=p+(r#,)?"
     73                     "^turn p[1:H]\ndef M(l):?v=^duce(F a,b:a*2+b,l,0L)?if 7&"
     74                   "Jl)C v==0:^turn'label%d_%s'%(Jl)&3,hex(v)[2:HQ?else:^turn "
     75                 "j([`chr(v>>k&255)`[1:H]fC k $ range(Jl)-8,H,-8)Q\nfrom sys imp"
     76               "Ct*;from Str$gIO impCt*;X='push,dup,copy,swap,pop,slide,add,sub,m"
     77             "ul,div,mod,stCe,^trieve,c""all,jmp"",jz,jn,^t,halt,putchar,putP,getch"
     78            "ar,getP'.splR(',~;Y='E,02"  "0,010," "021,022,012,1E0,1E1,1E2,1010,1011"
     79           ",110,111,201,202,210,211,"    "212,2"  "22,12E,1201,1210,1211'.splR(',~;Z"
     80          "='101EEEEE2222EEE';v=argv"     "[1:];"   "n,=Jv)B[v[0][0]=='-'B{'-c':1,'-d'"
     81         ":2,'--':0}.get(v.pop""(0),"      "3)C "    "0]C[3];j=''.jo$;x=exR\nif n>2 C[1"
     82         ",2,2][n]!=Jv):prP'To""kig"        "unS"     "tudio WhRespace 0.3 Implementati"
     83        "onGby Kang Seonghoon" " <"          "tok"      "igun@gmail.com>GGUsage: python "
     84        "%s <fV>G   C: pytho"  "n "           "%s"         " [-c|-d] <$fV> <outfV>GGno ar"
     85       "g\\texecute whRespac"  "e"              ""           " c""ode.G-c\\tcompV whRespa"
     86       "ce assembly code.G-d"  ""                      "\\tdecomp" "V whRespace assembly c"
     87       "ode.G'%(argv[0],arg"                        "v[0Q"";x(0)\n" "try:s=fV(v[0],'r~.^ad#;f=n B fV(v[1"
     88       "],'w~;v=tupA(v)\nex" "cept:"               "pr" "P'c"   "an"   "not o"  "pen fV.';x"         "(1)\nif"
     89       " n""==1:?prP'co""m""pV"" ' '"                  "%s "      "t"   "o %s.."  ".'%v,;N="               "F n:n"
     90       " " "B('\\t '[n>" + "" "0]"                     "+N(a"   "" ""    "bs("  "" "n)/2)[1:"                 "H]+' '"
     91        ""  "'\\t'[n%2]"  "" "+'"                      "G~C'G';L=" ""     "F "  "" "l:j([j(["                     "' \\"
     92        ""  "t'[Cd" "(v" ""  ")>>"  ""                 "k&1]fC k " ""     "$ "  "" "range(7,H"                       ",H)"
     93         "QfC ""v " "$ " ""  "lQ+'G'?"                 "fC l $ s."        ""       "splRl$es#"                          ":"
     94       "? l" "" "=" +"(" ""   "';'$ l"                 " B l[:l.f"                "$d(';~]C l)"                           "."
     95      "sp"       "" +  "l"    "R#;Jl)"                  "B':'==l"            "[" "0][H]B f.wrRe"                            ""
     96     "("            +   ""     "'G  "                                     "'+" "L(l.pop(0)[:HQ)"
     97    "?"             +   ""                ""                            " "  "if Jl):p=X.$dex(l["
     98   ""                +  ""               ""                            "" "0].lower#);p+1 B Jl)>("
     99  ""                  + ""                ""                          r"Z[p]>'0~B f.wrRe(j([' \tG'"
    100 ""                   "["                                            "P(k)]fC k $ Y[p]Q+(Z[p]>'0'B"
    101 ""                   "(0"                  ",F a:N(P"              "(a)),L)[P(Z[pQ](l[1QC'~)?f.clo"
    102                      "se#"               ";prP'done"             ".';x(0)\nelif n:prP'decompV %s to"
    103                     " %s..."                "'\45"             +"v,\ns=Str$gIO(j([{32:'0',9:'1',10:'"
    104                     "2'}.get("                               "C" "d(c),'~fC c $ sQ);r=F:P(s.^ad(1));W"
    105                     "=F h:h==s.^"                          "a"   "d(Jh))C s.seek(i,0);c=[];p={}\nwhV[N"
    106                    "one]!=c[H:]:?i=s"                   ".t"     "ell#?if W('2E~:l=L#;p[l]=Jc);n B f.wr"
    107                    "Re(M(l)+':G~?else:v="            "^d"        "uce(F a,b:a C W(Y[bQB(b,(P,N,L)[P(Z[bQ"
    108                    "]#),range(23),0);c+=[v];n B v B f"            ".wrRe('  '+X[v[0]]+(Z[v[0]]>'0'B(' '+("
    109                   "0,str,M)[P(Z[v[0]Q](v[1Q)C'"                   "~+'G~\nif n:f.close#;prP'done.';x(0)\ns"
    110                   "=[];h={};q=[];u=s.append;U="                   "F:Js)B s.pop#;i=k=0;c.pop#\nwhV i<Jc)B k"
    111                  "H8:?k,v=c[i];i+=1?if k==0:u("                    'v)"1:n=D;u(n);u(n)?elif(k==2)&(v>0):u(s['
    112                  '-vQ"3:n=D;m=D;u(n);u(m)"4:D"'                      '5:s[-vH:H]=[]"6:u(D+D)"7:u(-D+D)"8:u(D*'
    113                 'D)"9:n=D;u(D//n)"10:n=D;u(D%'                          'n)"11:n=D;h[D]=n"12:u(h.get(D,0))"13:'
    114                 'q+=[i];i=p[v]"14:i=p[v]"1'                                '5 B D==0:i=p[v]"16 B D<0:i=p[v]"17 '
    115                'B Jq):i=q.pop#"19:stdo'                                      "u"  't.wrRe(chr(D))"20:stdout.wrRe'
    116                '(str(D))"21:h[D'     ""                                   "]="          'Cd(std$.^ad(1))"22: ? v='
    117               "''? whV('"            ""                                 "0"                   "'+v[v[0:1]=='-':Q.i"
    118               "sdigR"                 ""                              "#"                         ":v+=std$.^ad(1)?"
    119               " h"                    ""                     +"[D]=P(v"                              "[:H]+'0~/10",""
    120              "~",                      ""            "')"),"^",   "r"                                   "e"),"V","iA")
    121              ,""                        ""                      "R"                                      ,"it"),"Q","]"
    122              ""                          ""                    ""                                        ")"),"P","$t"),
    123             ""                            ""        +"J","An("),                          ""              "H","-1" ),"G",
    124            ""                              "\\n"),"F"''     ""                          ,""               "lambda"),"E",""
    125            ""               ""              ""            "0"                          ""       ""        "0"),"D","U#"),""
    126           ""                ""               ""         "C"                           ""       ,""         "or"),"B","and")+
    127           ""                ""                ""      ,""                            ""        ""          "A","le"),"?","\n"
    128           ""                ""                 ""    ""                              ""       ""           " "),"$","in"),"#",
    129           ""                ""                  "" ""                               ""       ""            "()"),'"',"\12 eli"
    130          ""                  ""  ""              "f"                        ""      ""      ""             " k=="));exec("from"
    131          ""                  "" ""                                           ""    ""      ""               " sys import*\nif['"
    132          ""                  "" ""                                           ""    ""    +""                "--source']==argv[1"
    133         ""                   ":2"                                            ""   ""    "]"                  ":print'# TokigunSt"
    134         ""                    "u"                                             ""  ""   ""                    "dio Whitespace Imp"
    135         ""                    ""                                               "" ""  ""                      "lementation\\n\\n'"
    136         ""                    ""                                               "" "" ""                       "+R\nelse:exec R\n")
    137 </%text>
    138 </div>
    139 
    140 It is shaped as like Tsukamoto Tenma(塚本天満), a character of *[School Rumble](http://en.wikipedia.org/wiki/School_Rumble)*. This code is also available as [Mercurial repository](http://hg.mearie.org/esotope/ws/).
    141 

Powered by Mercurial