Download code

Jump to: navigation, search

Back to Literate_Programming_(Python)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

blit.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Literate_Programming_(Python)?oldid=19169
14 
15 import sys
16 
17 if len(sys.argv) < 2:
18     print "Usage: %s node" % sys.argv[0]
19     sys.exit(1)
20 
21 def findcode(file):
22     cs, cur = ({}, None)
23     for line in file.readlines():
24         if line[0] is '@':    cur = line[1:-1]
25         if line[0] is '>':    cs[cur] = cs.get(cur,"") + line[1:]
26     return cs
27 
28 def expand(node, cs):
29     phase = True
30     for x in cs[node][:-1].split('@'):
31         if phase:    sys.stdout.write(x)
32         else:        expand(x, cs)
33         phase = not phase
34 
35 expand(sys.argv[1], findcode(sys.stdin))


hijacker
hijacker
hijacker
hijacker

test.bl

 1 this is a test of barely-literate programming.
 2 I'm afraid I can't find the developer tools CD
 3 for my PowerBook, so none of the code here has
 4 been tested.
 5 
 6 :: :: ::
 7 
 8 First, we try an old-timer program:
 9 
10 @k & r
11 >main(@formal names@)
12 >@formal declarations@
13 >{
14 >	printf(@greetings@);
15 >}
16 >
17 
18 Not that we use them in this program (it is,
19 after all, a constant function, cf. the 'K'
20 combinator) but for nostalgia value, we have
21 
22 @formal names
23 >argc, argv
24 
25 @formal declarations
26 >int argc;
27 >char *argv[];
28 
29 and, of course, we should specify the value
30 which the function takes at all arguments:
31 
32 @greetings
33 >"Hello, world\n"
34 
35 :: :: ::
36 
37 Now, let's rewrite this in a more modern style:
38 
39 @ansi
40 >@external declarations@
41 >
42 >int main(@inline formal declarations@)
43 >{
44 >	printf(@greetings@);
45 >	@more boilerplate...@
46 >}
47 >
48 
49 You'll notice that we have an 'int' in there.
50 That's because once one has more than a few
51 K of memory to play with during a compile, it
52 turns out that silently assuming all params
53 are machine words isn't the best tradeoff.
54 
55 Of course, now that we've declared that we're
56 returning an int, we'd probably better do so:
57 
58 @more boilerplate...
59 >return 0;
60 
61 We must declare the argument types as well
62 as the return types:
63 @inline formal declarations
64 >int argc, char **argv, char **envp
65 
66 As in Pascal, the types are now specified in
67 with the argument names.  One might expect
68 that we'd need some additional bookkeeping
69 for printf() as well as main(), but luckily
70 (if one is not the preprocessor) this is a
71 matter of including the proper header file.
72 
73 @external declarations
74 >#include <stdio.h>
75 
76 :: :: ::
77 
78 To extract either of these two versions, use
79 the following command lines:
80 
81 % python blit.py "k & r" < test.bl
82 or
83 % python blit.py "ansi" < test.bl
84 
85 (to get fancier, one might try piping output
86 through "tcc -run", but it would probably be
87 better to fill in the code sketched here and
88 improve its robustness and input format)


hijacker
hijacker
hijacker
hijacker