forked from hpi-swa-lab/SqueakByExample-english
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.st
executable file
·105 lines (92 loc) · 2.91 KB
/
examples.st
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
#! /usr/local/bin/gst -f
"
examples --- extract code examples from Squeak by Example LaTeX source
Assumes you have Gnu Smalltalk with scripting syntax installed (at least v2.95c).
$Id$
"
header := '
===== SQUEAK BY EXAMPLE ==========
Below follow all the (displayed) code examples from the book "Squeak by
Example".
For details about this book, see: http://SqueakByExample.org
The examples are provided, as is, for your convenience, in case you want
to copy and paste fragments to Squeak to try out.
Note that in almost all cases the annotation "--> ..." suggests that you
can select and apply <print it> to the previous expression and you should
obtain as a result the value following the arrow.
Many of these actually serve as test cases for the book. For more details
about testing, see the Wiki link under:
http://www.squeaksource.com/SBEtesting.html
'.
Object subclass: Chapter [
| title code |
initWithName: aName [
<category: 'initialization'>
| name fileName file codeStream line |
name := aName.
title := '<unknown>'. "default value"
codeStream := WriteStream on: String new.
fileName := name, '/', name, '.tex'.
file := FileStream open: fileName mode: FileStream read.
[ file atEnd ] whileFalse: [
line := file nextLine.
line =~ '\\chapter\{([^}]*)\}' ifMatched: [ :result |
self setTitle: (result at: 1)
] ifNotMatched: [
line =~ '^\\begin\{(code|example|script|classdef|methods?|numMethod)\}'
ifMatched: [ :result |
self getCode: file to: codeStream
] ifNotMatched: [ ]
]
].
code := codeStream contents.
file close.
]
setTitle: aString [
<category: 'initialization'>
title := aString.
title := title replacingAllRegex: '\\sq' with: 'Squeak'.
title := title replacingAllRegex: '\\st' with: 'Smalltalk'.
]
getCode: file to: codeStream [
<category: 'private'>
| line |
line := file nextLine.
[ line ~ '^\\end\{' ] whileFalse: [
"comment out --> incantation"
line := line replacingAllRegex: '\s*(-->[^"\r\n]*)' with: ' "%1" '.
"translate listings macros"
line := line replacingAllRegex: '>>>' with: '>>'.
line := line replacingAllRegex: 'BANG' with: '!'.
line := line replacingAllRegex: 'UNDERSCORE' with: '_'.
"compact extra space around comments"
line := line replacingAllRegex: '" +' with: '"'.
line := line replacingAllRegex: '""' with: ''.
line := line replacingAllRegex: ' +"' with: ' "'.
codeStream nextPutAll: line; nl.
line := file nextLine
].
codeStream nextPutAll: '-----'; nl.
]
printOn: stream [
<category: 'printing'>
stream
nextPutAll: '===== CHAPTER: '; nextPutAll: title;
nextPutAll: ' =========='; nl; nl;
nextPutAll: '-----'; nl; nextPutAll: code.
]
]
Chapter class extend [
named: aName [
<category: 'instance creation'>
| ch |
ch := super new.
ch initWithName: aName.
^ ch
]
]
Transcript show: header.
(Smalltalk arguments) do: [ :each | |ch|
ch := Chapter named: each.
ch printNl.
].