Archive for the 'Smalltalk' Category

AjTalk: a Smalltalk-like interpreter

Weeks ago, I was working on my open source project AjTalk, an Smalltalk-like interpreter, written in .NET (using C#), and now, I want to present the status of that work. For years, I was interested in Smalltalk development, altought only in my free time, never as a professional developer. It’s not the first time I wrote such kind of interpreter (my first one was written in the eighties, and it was very simple: no garbage collector, only text commands), but this time I feel it could become a very complete implementation.

Current version is minimal, but it is taken form. The idea is to have dynamic objects, like in Smalltalk, and, at some point, add prototypes a la Self. The objects and the interpreter would access full .NET framework and other libraries, as I did in my AjBasic interpreter (included as part of AjGenesis code generation project).

The very initial version is now published at Google Code:

http://code.google.com/p/ajtalk/

The solution

I’m using Professional VS 2005. There are four projects in the solution.

AjTalk is the main project, a class library containing the core of the system.

AjTalk.Test01 and AjTalk.Test02 are console applications, only for manual tests.

AjTalk.Tests contains the unit tests, written using NUnit framework 2.2.8.

Most of the core system consists of interfaces, defining the base behaviors, and classes, implementing such interfaces.

The object

The base is to have an interface to represent any object:

using System; namespace AjTalk { public interface IObject { IClass Class { get; } object this[int n] { get; set;} object SendMessage(string msgname, object [] args); } }

I could make a message an object (of type Message), but for now, the message is only a name, and an array of arguments.

The index this[int n] access the intance variables. Each value in AjTalk can point to any .NET object, not only the ones that implements IObject. In this way, I can manage int, long, String, DataSet, from other IObject objects.

The class

I’m implementing a simple IClass interface, without distinguing between class, behaviours, and other classes, as in the classic Smalltalk. I’ll separate these classes in a future version. For now, I’m managing only a interface:

using System; using System.Collections; using System.Collections.Generic; namespace AjTalk { public interface IClass : IObject { IClass SuperClass { get; } string Name { get; } void DefineClassMethod(IMethod method); void DefineInstanceMethod(IMethod method); void DefineClassVariable(string varname); void DefineInstanceVariable(string varname); IObject NewObject(); IMethod GetClassMethod(string mthname); IMethod GetInstanceMethod(string mthname); int GetClassVariableOffset(string varname); int GetInstanceVariableOffset(string varname); } }

There is a dictionary of class and instance methods, and lists of class and instance variable names. It doesn’t have support for indexed variables, yet. IClass interface is now implemented in a BaseClass class.

The method

There is an interface implemented in Method class:

using System; namespace AjTalk { public interface IMethod { string Name { get; } IClass Class { get; } object Execute(IObject receiver, object [] args); } }

The concrete Method class, has an Execute method:

public object Execute(IObject receiver, object[] args) { return (new ExecutionBlock(receiver,receiver,this,args)).Execute(); }

Execution blocks have local variables and arguments. The Execute of an execution block takes the instructions (bytecodes) from “compiled” methods, and then it executes them. Here, I took a departure from Smalltalk ideas: the execution block is not an AjTalk object. In this way, I could run this interpreter without implementing a lot of base classes. I have to research the advantages and problems that this decision could have in the overall design and implementation.

The bytecodes

I must think about using a tree of objects (as in Interpreter pattern) instead of bytecodes. But in this version, bytecodes are used. These are the basic instruction that my “virtual machine” understands and executes step by step.

There is a bytecode list (an enumeration)

namespace AjTalk { public enum ByteCode : byte { Nop = 0, GetVariable = 1, SetVariable = 2, GetArgument = 3, SetArgument = 4, GetConstant = 5, GetLocal = 6, SetLocal = 7, GetClassVariable = 8, SetClassVariable = 9, GetSelf = 20, GetClass = 21, GetSuperClass = 22, NewObject = 23, Pop = 24, ReturnSub = 25, ReturnPop = 26, Add = 40, Substract = 41, Multiply = 42, Divide = 43, Send = 50 } }

The bytecodes contained in a method, are interpreted and executed by the execution block. An excerpt of that code:

 

while (ip < method.ByteCodes.Length) { ByteCode bc = (ByteCode) method.ByteCodes[ip]; Byte arg; switch (bc) { case ByteCode.ReturnSub: return null; case ByteCode.ReturnPop: return Top; case ByteCode.GetConstant: ip++; arg = method.ByteCodes[ip]; Push(method.GetConstant(arg)); break; case ByteCode.GetArgument: ip++; arg = method.ByteCodes[ip]; Push(arguments[arg]); break; ....

Test everywhere

The initial code was written in VB.NET. Last year I began to rewrote the original source code to C#. Then, this year I switched to “TDD mind”, so, late but sure, I added NUnit tests:

Bootstraping

I plan to use a text file, with an ad-hoc format, to inject the definitions of the initial classes and objects. Now, in the AjTest.Test02, there is an example of such format in Definitions.txt file:

class Point
variables x y

method
x ^x.

method
y ^y.

class Rectangle
variables point1 point2
class Square Rectangle

Next steps

There is a lot of work to do:

- Complete the hierarchy of base classes (Behaviour, Class, ….)

- More byte codes

- Support of local variables in methods

- Standard file text format

- Access to native .NET objects

- Use from .NET applications

- Define the classes and methods for a minimal implementation

- Serialization/deserialization of memory image

- Support for adding variables to a class with created instances (this is a tough problem)

- Support for become:

- And much more….

But I’m confident on the shape the project is taken. I’m applying “baby steps”, to improve the base code and functionality.

Angel “Java” Lopez
http://www.ajlopez.com/en

The early history of Smalltalk

Today I wanted to comment a text, in my opinion, a “must be read” by all those of us dedicated to software development. It is a writing of Alan Kay, about the early history of Smalltalk:

Smallhistory.pdf

It is an excellent text to read, and that has influences simply beyond Smalltalk. It is a history of how some ideas were arising, within the American community of software development. How the programming with objects was arising, and how the form to interact with the present computers was taking form.

Read, for example, how Kay detects  some germinal ideas (data along with behaviour) in developments of the Air Force, that today we would see very remote of the OOP. Read on his contact with Lisp, and Seymour Papert. Read on the internal problems of Xerox, the competition with DEC, how some ideas were almost generated by chance. Read on Simula, Euler, IPL predecessor of Lisp. Read on the Sketchpad of Sutherland (who I found by first time in some historical revision of the Scientific American). It is a delicious and enlighten reading, at least for me.

Kay has been having an idea for years, that I share: the idea that the machine, and computing in general, must serve to us to expand our human capacities. Excellent idea. It is what of some form also today we are reaching using Internet. Today, branches of the knowledge and human actions, have been leveraged by software, the hardware and everything what it has happened in our profession in the last decades.

Years ago, people guessed that the space trips were going to revolutionize human history. That has still not happened. But of some form subproduct of the cold war and the space race, the development of the computing science (we remember its modern beginnings in the second war, and the appearance of the cybernetics impelled by military subjects) and of Internet, is what it has caused a change, that is reaching to great part of the humanity.

To read the history of Kay is indispensable to be understanding what it has happened. Somebody that has said ” the best form to dominate the future, is inventing it” (approximated phrase, reads the text, to see where it arose exactly).

Angel ” Java” Lopez
http://www.ajlopez.com/en

Installing Squeak

In the last century, sometimes I was studying Squeak. Now, in these days I have returned to visit it, and wanted to write a simple, introductory article, on its easy installation.

Squeak is an open source implementation of Smalltalk, that has been developed for years. The internal implementation of its virtual machine is interesting, to a great extent written in Smalltalk, with tools to produce its own virtual machine.

Like in other implementations, one needs to install the virtual machine of the operating system that will use. In this article, we will install a version (the 3,9) for Windows. Also we will need an image, a file with the state of the active instances. Smalltalk has been characterized from always, by being able to write and to raise an image, a species of “snapshot” of the state of the system.

The page of Squeak is

http://www.squeak.org

For somebody not warned of the existence of virtual machines and images, it is a little confused explanation of its installation:

http://www.squeak.org/Download/

because there are several options. For example, to lower the versions from

http://ftp.squeak.org

But there is multitude of archives. The one that we are going to use is a file .zip that contains the virtual machine for Windows and image 3,9 (at the moment already there is a developing image 3,10), directly from:

http://ftp.squeak.org/3.9/win/Squeak3.9-win32.zip

This file contains:

We see a Squeak.exe, that it is the feasible virtual machine. The SqueakV39.sources file is of text, of more than 4 megas of size. It contains the source code of the classes that compose this version. The file Squeak3.9-end-7067.image is the image, “snapshot” that contains the instances of this system.

We can expand the content of this .zip in a directory, and make double click on the feasible one of the virtual machine Squeak.exe. Automatically it detects the image. Alternatively, we could drag from the Explorer de Windows the file image on the file .exe. It appears our first welcome to the Squeak world:

All within this window, is created by the own Squeak. Following the tradition of the original Smalltalk, the graphical aspect, the controls, windows, and others “widgets” are drawn by the own system. The appearance of this window and its contents, then, is independent of the operating system.

In the right lapel titled tools, we found a series of tools. Raising the classic one “to browser” of Smalltalk, we found:

The four superior lists, show, in sequence:

  • categories of classes
  • classes of the selected category
  • categories of methods (of instance or class)
  • methods
  • In the screen capture it appears the classic Object, of Kernel-Objects.

    If you have interest on the virtual machine, can visit

    http://www.squeakvm.org

    He is interesting to see the instructions to produce a virtual machine, in Win32:

    http://www.squeakvm.org/win32/compiling.html

    Well, for today, enough. I hope that it serves to you, as an start in the use of this implementation of Smalltalk.

    Angel “Java” Lopez
    http://www.ajlopez.com/en

    Smalltalk Archeology

    Aaron Reichow posted a message to Squeak development mailing list. He published a page with old versions of Smalltalk. You can download these oldies, too:

    http://bitquabit.com/rev/old/
    http://bitquabit.com/rev/old/download

     Look and feel of Digitalk Methods 1.1 (old good times… :-):

     

    I remember such windows, popularized via Borland products…. I want my Sidekick!!… :-)

    The original message:

    > Hey Squeakers!
    >
    > I and others have discussed old versions of Smalltalk a few times on  the
    > list in recent months.  Something I mentioned was putting up a  page with
    > screenshots of various old versions of Smalltalk.  I  recently got a few
    > of these old Smalltalks running and tonight I put  together a simple page
    > with a few screenshots and some basic info.
    >
    > Check it out at:
    > http://bitquabit.com/rev/old/
    >
    > Right now I’ve got some info and screenshots up for: Apple  Smalltalk-80
    > for Mac OS Classic, Digitalk Methods 1.1 (text-mode) for  DOS, and
    > Digitalk Smalltalk/V 286 R3 (graphical) for DOS.
    >
    > Thanks to all those who made this thing we love: Smalltalk! And  thanks to
    > those who have helped me out with this lil project.
    >
    > Regards,
    > Aaron
    >