X11 sample rewritten; now it works again
[k8lst.git] / modules / srcimage.st
blobeda19b72e90a09f672dd148b4defb4c6cc6d0f5e
2  Little Smalltalk, Version 5
4  Copyright (C) 1987-2005 by Timothy A. Budd
5  Copyright (C) 2007 by Charles R. Childers
6  Copyright (C) 2005-2007 by Danny Reinhold
7  Copyright (C) 2010 by Ketmar // Vampire Avalon
9  ============================================================================
10  This license applies to the virtual machine and to the initial image of
11  the Little Smalltalk system and to all files in the Little Smalltalk
12  packages except the files explicitly licensed with another license(s).
13  ============================================================================
14  Permission is hereby granted, free of charge, to any person obtaining a copy
15  of this software and associated documentation files (the 'Software'), to deal
16  in the Software without restriction, including without limitation the rights
17  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  copies of the Software, and to permit persons to whom the Software is
19  furnished to do so, subject to the following conditions:
21  The above copyright notice and this permission notice shall be included in
22  all copies or substantial portions of the Software.
24  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  DEALINGS IN THE SOFTWARE.
32 Package [
33   Image
36 Object subclass: Image [
37 | imagePathName sourcePathName visited |
39 ^new [
40   | obj |
41   obj <- super new.
42   obj imagePathName: 'workimage.image'.
43   obj sourcePathName: 'workimage'.
44   ^obj
47 ^writeSource [
48   ^self new writeSource
51 imagePathName [
52   ^imagePathName
55 imagePathName: path [
56   imagePathName := path
59 sourcePathName [
60   ^sourcePathName
63 sourcePathName: path [
64   sourcePathName := path
67 writeSource [
68   | path |
69   sourcePathName
70     ifNil: [ path := 'workimage' ]
71     ifNotNil: [ path := sourcePathName asString ].
72   ^self writeSource: path
75 writeSource: path [
76   | fname file |
77   path
78     ifNil: [ fname := 'workimage' ]
79     ifNotNil: [ fname := path asString ].
80   file <- File open: fname asString mode: 'wb'.
81   self writeClassesTo: file.
82   file newline.
83   self writeMethodsTo: file.
84   file newline.
85   file write: 'BEGIN nil INITIALIZEEVERYTHING REPL'.
86   file newline.
87   file write: 'END'.
88   file newline.
89   file close.
90   'image source writing seems to be experimental!' printNl.
93 writeClassesTo: file [
94   visited <- Dictionary new.
95   globals do: [:cls |
96     (cls isKindOf: Class)
97       ifTrue: [
98         self writeClass: cls class to: file.
99         self writeClass: cls to: file
100       ]
101   ]
104 writeMethodsTo: file [
105   visited <- Dictionary new.
106   globals do: [:cls |
107     (cls isKindOf: Class)
108       ifTrue: [
109         self writeMethodsOf: cls class to: file.
110         self writeMethodsOf: cls to: file
111       ]
112   ]
115 writeClass: cls to: file [
116   | stmt vars parent |
117   (visited includes: cls asString asSymbol) ifTrue: [ ^self ].
119   parent <- cls parent.
120   parent ifNotNil: [ (visited includes: parent asString asSymbol) ifFalse: [ self writeClass: parent to: file ] ].
121   visited at: cls asString asSymbol put: 1.
123   cls = Object ifTrue: [ ^self ].
124   stmt <- 'RAWCLASS ' + cls asString + ' ' + cls class asString + ' ' + parent asString.
125   vars <- cls variables.
126   vars ifNotNil: [ vars do: [:v | stmt <- stmt + ' ' + v asString ] ].
127   file write: stmt.
128   file newline
131 writeMethodsOf: cls to: file [
132   | methods parent |
134   (visited includes: cls asString asSymbol) ifTrue: [ ^self ].
136   parent <- cls parent.
137   parent ifNotNil: [ (visited includes: parent asString asSymbol) ifFalse: [ self writeMethodsOf: parent to: file ] ].
138   visited at: cls asString asSymbol put: 1.
140   file newline.
141   file write: ( 'COMMENT ----------' + cls asString + '---------' ).
142   file newline.
143   file newline.
144   methods <- cls methods.
145   methods ifNil: [ ^nil ].
146   methods do: [:m | self writeMethod: m of: cls to: file ]
149 writeMethod: meth of: cls to: file [
150   | stmt txt c |
152   txt <- meth text asString.
154   stmt <- 'METHOD ' + cls asString.
155   file write: stmt.
156   file newline.
157   file write: txt.
159   "Verify if txt ends with a newline"
160   c := txt lastChar.
161   (c isNil or: [ c isEOL not ]) ifTrue: [ file newline. ].
163   file write: '!'.
164   file newline.
165   file newline.