views:

685

answers:

4

Here I found a Simple Forth Interpreter implemented in Java.
However I don't understand the significance of it if I want to use it?

What could be the advantage of the Forth Interpreter:

  • If the final compiled code to be executed by the JVM is still "Byte code" what would we the Forth Interpreter be doing?
  • Will it help in writing efficient/tight programs?
  • Will I be writing my code in Forth and the interpreter will convert it to Java?

Your thoughts...

+2  A: 

Not a bytecode translator

The answers to your questions are: "see below, sort of, and no".

It's just a program that takes some input and produces some output. The input is a Forth script. Except for some very major systems, it's rare to actually produce bytecode. jRuby, Clojure, Scala .. big systems like that do produce bytecode.

However your Forth interpreter is probably just that: a script interpreter that happens to be written in java. The input it accepts is a program of sorts, so you do end up with a nice double-indirect execution. Forth executing via bytecode interpreter executing via jvm running on the CPU.

Now, if you ran that on a CPU emulator, or wrote an interpreter in Forth, you could make it triple-indirect. (And in a sense it is already, because your Intel CPU is translating most x86 opcodes into micro-ops before executing them. :-)

Anyway, the point is that a program written in a rather static language like java might want to take some complex user input and execute it, or perhaps the author of the program has things that are more easily done in forth, and this allows him to write in both java and forth.

You should think about all of this until you understand it.

DigitalRoss
Is the Input(I mean the code that I would be writing) in Forth?
Kevin Boyd
Yes, as long as either the running forth interpreter reads your input or you read it in java and make an API call to the forth interpreter.
DigitalRoss
Note that one reason you might want this interpreter in your java program is so you can include prewritten Forth, perhaps from a file, or perhaps just in an initialized String or something. There are some complex uses for things like this, like machine-code-neutral device configuration ROMs.
DigitalRoss
+1  A: 

It does allow you to write efficient/tight programs. Partly because the ability to define defining words (words executing at compile time) can have the effect of effectively defining a Domain Specific Language (DSL). Forth also encourage refactoring (otherwise the stack stuff simply becomes incomprehensible ...) and thus the code will be tight.

Peter Mortensen
+3  A: 

The author on the page describes at as implementing a subset of FORTH and being suitable for incorporationg in other applications; presumably it is intended to provide a scripting capability for an application. It's fairly unlikely that the system works by spitting out java or JVM byte codes; it almostly certainly uses an interpreter written in Java.

Traditionally, a FORTH interpreter can be implemented in a very small memory footprint. I know someone that implemented one on a COSMAC and the core interpreter was 30 bytes long. The stack oriented byte code was also very compact as it did not need to specify the location of operands - it just read from the stack and deposited the result on the top of the stack. This made it popular in embedded systems circles where the small overhead of the interpreter was more than offset by the compact representation of the program logic.

These days it's less important as machines tend to be much larger, although digitalross makes a good point about other situations where FORTH is still used.

ConcernedOfTunbridgeWells
+1  A: 

Will it help in writing efficient/tight programs?

That's debatable.

I'm sure that FORTH folks will tell you that it is fast. But I doubt that the execution speed of a FORTH program running on a FORTH interpreter implemented in Java will line up against the speed of an equivalent program implemented directly in Java. For a start, the JIT compiler won't be able to do as a good job of optimizing the FORTH interpreter as it can for the plain Java version.

If by "tight" you mean "using less memory", I think that the difference will be marginal. Remember that in both the "FORTH in Java" and "plain Java" cases you have all of the memory overheads of a Java JVM. This is likely to swamp any comparison of FORTH code density versus equivalent compiled Java code density.

Stephen C
Wonderfully explained! I haven't worked on Forth neither do I understand Interpreter implementations however your answer is certainly clearing up some cloudy thoughts.
Kevin Boyd
What part of "The goal of SFI is to develop a small interpreter to be embedded in any application and provide scripting functionality with low resources." both of you do not understand?
MaD70
I simply don't see the point of saving 100k or 1Mb or so compared with (say) BeanShell when the total app memory footprint is 10+ Mb. And as a Java programmer, it makes more sense to me to do embedded scripting in a Java-like / Java interoperable language.
Stephen C