added some samples
[k8lst.git] / samples / browser.st
blob0e616f2b91e7e76d5ea0c78cd9f232536b7d69c8
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 " browser.st
33   Utility to interactively examine data structures"
35 Object subclass: Browser [
36 | stack |
38 ^on: obj [
39   "Instantiate a browser on the named object"
40   self new on: obj
43 showPos [
44   "Display stack of objects we're inspecting"
45   | cnt |
46   ' == stack -> ' print.
47   cnt <- 0.
48   stack do: [:obj|
49     (cnt > 0) ifTrue: [ ' / ' print ].
50     obj print.
51     cnt <- cnt + 1
52   ].
53   '' printNl
56 ivars: obj [
57   "Dump instance variables of named object"
58   | iv |
59   iv <- obj class instanceVariables.
60   1 to: iv size do: [:idx|
61     idx print. Char tab print.
62     (iv at: idx) print. ': ' print.
63     (Object in: obj at: idx) printNl
64   ]
67 run: args on: obj [
68   "Compile and run Smalltalk expression on object"
69   | t cl |
70   "Assemble arguments into single String"
71   t := nil.
72   args do: [:arg |
73     t ifNil: [ t <- arg ]
74       ifNotNil: [ t <- t + ' ' + arg ]
75   ].
76   "Compile into xxBrowse Method for object's class"
77   cl <- obj class.
78   t <- 'xxBrowse' + String newline + String tab + '^ ' + t.
79   (cl addMethod: t) ifNotNil: [
80     "Compile OK, run and print result"
81     obj xxBrowse printNl.
82     "Now clean up (remove) browse method"
83     cl removeMethod: #xxBrowse
84   ]
87 on: top [
88   "Main UI loop for browser"
89   | cmd done last obj args idx |
90   stack <- Array with: top.
91   [true] whileTrue: [
92     "Show where we are and get next command"
93     self showPos.
94     "Get next command, explode into cmd/args"
95     'Browse> ' print.
96     cmd <- String input.
97     cmd ifNil: [ ^ nil ].
98     args <- cmd break: ' '.
99     cmd <- args at: 1.
100     args <- args from: 2.
101     "Get top of stack in its own variable"
102     obj <- stack at: (stack size).
103     done <- false.
104     "Index with at:"
105     ((cmd = 'i') or: [cmd = 'index']) ifTrue: [
106       last <- stack at: (stack size).
107       idx <- (args at: 1) asNumber.
108       last <- last at: idx.
109       last printNl.
110       done <- true
111     ].
112     "Nest to last displayed object"
113     ((cmd = 'n') or: [cmd = 'nest']) ifTrue: [
114       stack <- stack with: last.
115       done <- true
116     ].
117     "Show class"
118     ((cmd = 'c') or: [cmd = 'class']) ifTrue: [
119       obj class printNl.
120       done <- true
121     ].
122     "Pop up a stack level"
123     ((cmd = 'u') or: [cmd = 'up']) ifTrue: [
124       (stack size < 2) ifTrue: [
125         'Stack is empty' printNl
126       ] ifFalse: [
127         stack <- stack from: 1 to: (stack size - 1)
128       ].
129       done <- true
130     ].
131     "Raw index"
132     (cmd = 'ri') ifTrue: [
133       idx <- (args at: 1) asNumber.
134       last <- Object in: obj at: idx.
135       last printNl.
136       done <- true
137     ].
138     "List instance variables"
139     ((cmd = 'iv') or: [cmd = 'ivars']) ifTrue: [
140       self ivars: obj.
141       done <- true
142     ].
143     "Show size"
144     ((cmd = 'bs') or: [cmd = 'bsize']) ifTrue: [
145       obj basicSize printNl.
146       done <- true
147     ].
148     ((cmd = 'sz') or: [cmd = 'size']) ifTrue: [
149       obj size printNl.
150       done <- true
151     ].
152     "Print arbitrary expression"
153     ((cmd = 'p') or: [cmd = 'print']) ifTrue: [
154       self run: args on: obj.
155       done <- true
156     ].
157     ((cmd = 'h') or: [cmd = 'help']) ifTrue: [
158       'browser commands' printNl.
159       '----------------' printNl.
160       'i  index   index with at:' printNl.
161       'n  nest    nest to last displayed object' printNl.
162       'c  class   show class' printNl.
163       'u  up      pop up a stack level' printNl.
164       'ri         raw index' printNl.
165       'iv ivars   list instance variables' printNl.
166       'bs bsize   show size' printNl.
167       'sz size    show size' printNl.
168       'p  print   print expression' printNl.
169       done := true.
170     ].
171     "All done"
172     ((cmd = 'q') or: [cmd = 'quit']) ifTrue: [
173       ^ nil
174     ].
175     "Unknown command?"
176     done ifFalse: [ ('Unknown command: ' + cmd) printNl ]
177   ]
182 { REPL new REPL }