How do i list the execution order of objects inside a method?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How do i list the execution order of objects inside a method?

basilmir
Hello,

I have this piece of code (taken from ProfSted). How do i programmatically get the order in which the objects are executed by Squeak. I know the order of execution from smalltalk documentation. But what if i need this information inside an app i'm building.

tutorial: aTutorialClass lesson: aSelector
        | tutorial |
        tutorial := aTutorialClass new.
        self player tutorial: tutorial.
        self tutorial: aTutorialClass lessonAt: (tutorial indexOfLesson: aSelector).

Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: How do i list the execution order of objects inside a method?

dcorking
I have never tried to do this, but it is interesting.

I think the bytecode represents the execution order, so if your method
is already compiled, you could, for example, send #symbolic to the
CompiledMethod, as the browser does when you select bytecodes:

(Tutorial compiledMethodAt: #tutorial:lesson: ) symbolic  '29 <10> pushTemp: 0
30 <CC> send: new
31 <6A> popIntoTemp: 2
32 <70> self
33 <D1> send: player
34 <12> pushTemp: 2
35 <E0> send: tutorial:
36 <87> pop
37 <70> self
38 <10> pushTemp: 0
39 <12> pushTemp: 2
40 <11> pushTemp: 1
41 <E3> send: indexOfLesson:
42 <F2> send: tutorial:lessonAt:
43 <87> pop
44 <78> returnSelf
'

For a more sophisticated data structure, I suggest digging into the
Debugger or the Compiler docs and sources, but those are areas I have
not explored.

Have fun! David
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How do i list the execution order of objects inside a method?

dcorking
A member of the AST-Interpreter[1] team posted a very interesting
answer on Stack Overflow.[2]

1 - http://smalltalkhub.com/#!/~dh83/ast-interpreter
2 - http://stackoverflow.com/questions/14520133/programmatically-get-the-execution-order-of-objects-inside-a-method

Have fun! David
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How do i list the execution order of objects inside a method?

basilmir
I know… i also posted the question there, but thank you for pointing it out!

The keywords contained in the answer have indeed pointed me in a good direction. I've since compiled a list of all the things i am supposed to know before i can attack the problem myself.

Conceptually i think i have the solution, as all the pieces seem to be available, but i think it will take some time before i slowly transition this mental model to a "working" model given my lack of practical expertise.

I have high hopes that what i'm trying to achieve will also help alleviate some of the caveats OO has now, a rather opaqueness of objects, a "hiding" of relevant information behind something else, and the need to "know" the model of all the separate packages involved, which you don't actually need, to build something rather small in between.

I find it rather peculiar that it is in this field also, the best advice of how to fix things comes from people complaining.
I may not share the expertise of the writer but i can say, with confidence, that i am quite familiar with the feeling.

Regards.


Pe 28.01.2013, la 18:09, dcorking [via Smalltalk] <[hidden email]> a scris:

A member of the AST-Interpreter[1] team posted a very interesting
answer on Stack Overflow.[2]

1 - http://smalltalkhub.com/#!/~dh83/ast-interpreter
2 - http://stackoverflow.com/questions/14520133/programmatically-get-the-execution-order-of-objects-inside-a-method

Have fun! David
_______________________________________________
Beginners mailing list
<a href="x-msg://29/user/SendEmail.jtp?type=node&amp;node=4665876&amp;i=0" target="_top" rel="nofollow" link="external">[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/How-do-i-list-the-execution-order-of-objects-inside-a-method-tp4665301p4665876.html
To unsubscribe from How do i list the execution order of objects inside a method?, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: How do i list the execution order of objects inside a method?

basilmir
In reply to this post by dcorking
Also i have more 'intel' on the problem form questions i posted elsewhere.

I will serve as a conduit for this and try to keep every place update to the latest developments. In the end i think it will create a kind of cohesion between ideas, hopefully enough to generate something real.

Here goes:

First advice: You should use the debugger to go to the "edge" of the logic inside your code and "code from there" onwards.
"The easiest way to figure that out is to debug the code and follow the execution step by step. 

Note that you can basically develop in the debugger, unlike most other programming languages! 

There are two ways to launch the debugger: 
1. put a "self halt" in the code, this is the Smalltalk way of adding a breakpoint 
2. Select a piece of code and debug it, right click and select debug "

Second advice: It should make execution clear to the user.

"Essentially showing the execution order of any smalltalk code. I wanted to use the "way" pharo works itself to do this… instead of parsing the text myself and checking.
For example… in the Workspace… i can write a small piece of code that does something. When i select it and click Do It. Pharo goes on to execute the text… it first parses it i think and then figures out what to execute first and then pass the product of that object to the next thing and so on.
It might look like this

something dothis.
anotherobject something dothis.
finalobject [ anotherobject something do this ]."

Third advice: Go in and do this after the code is compiled, this will give exact data of that the code really compiles to and not just a layer that parses text.

There is a configuration and a lot of tests which document how it works. 
There you have complete control over what is executed where. 
Simply override the method that handles sends and you should be able to easily add the things you want."

Forth advice: Showing the execution order as you create code, should also work for illegal code so you can can create without the backfire of an error (works best with dynamically typed languages) and this to be done "debugger" style at the very edge of the logic in your code, this is where you create… and should have a free but "helped and guided" hand.

"1. If you want to show the order of the messages sent *without* actually 
executing the code you need to compile the code and inspect the AST. 
This is not that hard to do. 

2. If you want to visualize the order while executing it - you could 
look at how Debugger works and simply "debug" the code programmatically. 
It basically means spawning a Process and sending "step" to it over and 
over and looking at where it is etc. Funky enough the Debugger is "just 
Smalltalk code". :) 

But what happens if the code does not exist, class names are not valid, method names are just examples.

If i were to analyze something like an example someone has posted somewhere.

In Squeak I can't say, but in Pharo, the Compiler can be "stupid" and compile more or less everything :) 

So to get this "execution order" and no run-time errors... i would need to get behind the complier and before the code actually runs. I don't want it to bump up errors but rather just "mark" the code, in a manner similar to code highlighting but not at the character/word level but at language level. "

The AST-Interpreter part: It seems we can do this with the help of the AST, which has a certain implementation that requires a precise pattern of interaction, and it's called the "visitor pattern". I also received a bump in the direction of "Moose and friends (Roasal, PetitParser, .. )" which might shed a little more light on the subject.

"You would need to write something like this (a visitor) or use the AST interpreter (which
is just a fancy visitor)."

" Execute (Point>>#x) parseTree explore 
in the Workspace to see a little of AST's power of going into classes and check out below for a more detailed information
(Point>>#x) "==> (Point>>#x "a CompiledMethod(880017408)")"

#aSymbol is just a singletone version of the string "aSymbol"; In what to classes concerns it is used as a unique identifier in the method dictionary. The rest is just syntax ">>" is the selector(s) (method(s))  accessor, it will retrieve the keyed selector (in this case #x which again is unique in the class) value, which is a CompiledMethod(atSomeAddress). 

Now you can also get the contents  as a string if you want to parse it yourself (compare to (Point>>#x) parseTree ):

(Point>>#x) definition "==> 'x
"Answer the x coordinate."
^x'"
Which is nothing more than the contents of the selector #x. I suggest, if you want to get a "personalized" version of the parse tree, to try PetitParser, there are many examples and even a Smalltalk80 parser in there."

PS. I will try to build a wiki to host this on a server so we can have a central reference. Right now i will have to keep things in sync by hand.
PPS. I will try to keep a reference of who said what so that we can see ideas from multiple people merging to a coherent solution.





Pe 28.01.2013, la 18:09, dcorking [via Smalltalk] <[hidden email]> a scris:

A member of the AST-Interpreter[1] team posted a very interesting
answer on Stack Overflow.[2]

1 - http://smalltalkhub.com/#!/~dh83/ast-interpreter
2 - http://stackoverflow.com/questions/14520133/programmatically-get-the-execution-order-of-objects-inside-a-method

Have fun! David
_______________________________________________
Beginners mailing list
<a href="x-msg://514/user/SendEmail.jtp?type=node&amp;node=4665876&amp;i=0" target="_top" rel="nofollow" link="external">[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/How-do-i-list-the-execution-order-of-objects-inside-a-method-tp4665301p4665876.html
To unsubscribe from How do i list the execution order of objects inside a method?, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: How do i list the execution order of objects inside a method?

dcorking
mircea wrote:

> 2. Select a piece of code and debug it, right click and select debug "

That is a Pharo thing. I am not sure how to do the same thing in Squeak.

> But what happens if the code does not exist, class names are not valid,
> method names are just examples.
>
> If i were to analyze something like an example someone has posted somewhere.
>
> In Squeak I can't say, but in Pharo, the Compiler can be "stupid" and
> compile more or less everything :)

In Squeak, the Compiler is just as stupid. The objects in the compiled
bytecode are just symbols that are resolved at run time. The warnings
the compiler gives you about missing methods and classes are just
warnings - ignore them and the compiler will compile the code anyway.

> So to get this "execution order" and no run-time errors... i would need to
> get behind the complier and before the code actually runs.

Unlike your other correspondents, I am no computer scientist, but as I
understand, this is exactly what the Abstract Syntax Tree can do for
you.

I think of compilation has 3 steps.
1. A parser makes an abstract syntax tree that represents each message
in your source code as a node in the tree, according to Smalltalk's
precedence rules.
2. The compiler walks that tree in order, turning each message into
bytecode (with some optimizations.)
3. The compiler saves that bytecode into the class as a CompiledMethod.

Then that CompiledMethod can be executed (by the VM) whenever it is needed.

If you evaluate a DoIt, then instead of step 3, the code actually
runs. Since you don't want that, I suggest you might find it easier to
avoid DoIts for now. If it was me, I might consider starting with
simple methods, and later moving on to block closures.

Although the CompiledMethod is always accessible before the code
actually runs, I can see from your explanation that you probably
actually want to get behind the compiler after step 1, and get hold of
an AST before the compiler discards it. This is what the StackOverflow
answer gives you (and Roassal or other projects give you tools to
visualize trees.)

If you think about the Debugger, it has to do the same tasks: parse
your source code into execution order, then step by step animate and
then execute. That is why we suggested looking into Debugger code.

I would love to help you in your endeavour as a spare time project,
but I don't have the Smalltalk expertise needed. I am good at using
the Debugger to crash!

Have fun! David
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How do i list the execution order of objects inside a method?

Ron Teitelbaum
In reply to this post by basilmir
Hi Mircea,

>From: [hidden email] [mailto:[hidden email]] On Behalf Of mircea
>Sent: Wednesday, January 30, 2013 5:27 AM

>Conceptually i think i have the solution, as all the pieces seem to be available, but i think it will take some time before i slowly transition this mental >model to a "working" model given my lack of practical expertise.

See http://news.squeak.org/2006/11/07/ah-ha/#more-91 and the comments for some pointers that might help some.  The order of execution is all about the message send.  Object message.  What's the Object and what's the message.  The message is sent to the Object.  Sometimes what is the message is not easy to answer since it can be overridden by a subclass (polymorphism), or defined on a parent superclass (inheritance).  Sometimes the object is tricky!  aCollection add: anItem returns anItem not aCollection.  So aResult := aCollection add: anItem.  knowing what object aResult can be tricky.  The debugger is your friend.  (you can get aCollection by chaining messages to aCollection and asking it for its self.  aResult := aCollection add: anItem; yourself. aResult is now the collection).

Here is a good one:

instance := (MentalModel new)
                ponder;
                yourself.

Returns a good MentalModel instance.


All the best,

Ron Teitelbaum
Head Of Engineering
3d Immersive Collaboration Consulting
[hidden email]
Follow Me On Twitter: @RonTeitelbaum
www.3dicc.com






_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners