Published

Thomas’ Functional Interpreter

I just finished a small functional interpreter for an university course. To be precise, the interpreter itself is not functional, but it understands a functional, LISP-like language/syntax. The whole project is written in Java 7 and provides an interface to all Java classes on the class-path, too.

I also tried to create a small IDE in Swing. I built an editor with syntax-highlighting and code-completion (thanks to the RSyntaxTextArea project), an environment inspector and a small debugger to keep track of the stack. Take a look at it.

I called it ThoFu for Thomas’ Functional Interpreter. I think it teats good… You can get some brief impressions from the slideshow below.

The whole project is open, you can find it as an Eclipse project on my Bitbucket account. It contains the actual code, all libraries, screen-shots, unit tests and (of course) some documentation like UML diagrams. Here are the links to the GIT repository:

And this is how the Java API works. The code will create a JFrame with a JButton and a JLabel. When you press the button, the listener will change the text of the label from “click me” to “hello“. The syntax is inspired by the Java Dot Notation by Peter Norvig. I used the Apache Commons Lang library for the reflection-tricks to load class and so on.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

; Imports

(import"java.awt.event.*")

(import"javax.swing.*")

(import"java.lang.*")

(import"java.awt.*")

; Components

(definewin(JFrame."JavaExample1"))

(definebutton(JButton."click me"))

(definelabel(JLabel."nothing"))

(definelayout(FlowLayout.))

; Variables

(definevisible(Boolean.true))

(definesize(Integer.300))

; Listeners

(define(listenername)

(define(actionPerformede)(.setTextlabel"hello"))

(if(eq?name'actionPerformed)actionPerformederror))

(defineint(interfaceActionListener.classlistener))

; Construction

(.setLayoutwinlayout)

(.setSizewinsizesize)

(.addwinbutton)

(.addwinlabel)

(.addActionListenerbuttonint)

(.setVisiblewinvisible))

You can just copy and paste the code snippets into the editor or save them as common text files and open them. They are also in the repository. The JUnit tests contain many more examples as well.

For those who are interested in the topic, you can find my resources (libraries, tool and literature) in the Read-Me.