tags:

views:

113

answers:

2

Hi guy,

I need to build an interpreter in smalltalk language. It will interpret a simple language with few fixed instruction set, there are only 2 data types in it and it evaluates the expression in a postfix fashion. The language itself is interpreted line by line and interpreter will throw an error on encountering wrong instruction set.

Please help, how to start with this interpreter. What kinda data structures should be used to pull data from the user input and evaluate it according to the instruction set of the language.

Thanks.

+1  A: 

If you are building a postfix notation interpreter you're sort of building a forth interpreter. There are loads of links to forth resources here.

Preet Sangha
+2  A: 

That doesn't sound too hard to implement.

Postfix languages are easily implemented using a stack. In Smalltalk a stack can be an OrderedCollection on which you use the addLast: and removeLast methods.

If the language is interpreted line by line your interpreter main loop may look something like:

instructions := sourceCode subStrings: (Character cr asString).
instructions do: [:eachInstruction | ...]

One way to structure the code would be to create an Interpreter class which has a stack member variable and a method for each language instruction.

These instruction methods may look something like:

addInstruction
| op1 op2 |
op1  := stack removeLast.
op2  := stack removeLast.
stack addLast: (op1 + op2).
Alexandre Jasmin
Hi Alexandre, since I'm new to the Smalltalk please help me more on this. I need to have 2 data types in the simple language which I have to interpret Integer and a linked list. Do I need to implement the linked list if not then how can I use the existing linked list. I have been working on Cincom's Visual Works 7.7. Since I need to make a GUI version of it, which data structure should I use to take the user's input as and then perform syntax checking and various operations on linked list objects. Thanks for giving the pointers above.
max
@max There's an existing `LinkedList` class you may use either to represent your language datatype or as an alternative stack implementation. I suggest you pick a Smalltalk book and read on collections to get an idea of how to use LinkedList, OrderedCollection and other collection classes.
Alexandre Jasmin
@max I'm not sure what kind of GUI you are trying to build but it occurred to me that your *line-by-line interpreter* probably is an interactive interpreter. A simple way to get user input in VisualWork is to call `Dialog request:`. As it's name suggests `Dialog request:` displays a dialog requesting a line of text from the user. An simple interactive interpreter could just call this method in a loop.
Alexandre Jasmin
@max If you have more specific questions I suggest you post them individually on Stackoverflow.
Alexandre Jasmin