Forth Interpreter in Java

Here I found a simple Forth interpreter implemented in Java.
However, I do not understand its meaning if I want to use it?

What could be the advantage of the Forth interpreter:

  • If the final compiled code is to be executed by the JVM, the code is still "byte", what would we do The interpreter should do?
  • Did he help writing effective / hard programs?
  • Will I write my code in Fort and the interpreter will convert it to Java?

Your thoughts...

+6
java interpreter forth
source share
5 answers

Will this help writing effective / hard programs?

This is debatable.

I am sure FORTH people 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 correspond to the speed of an equivalent program implemented directly in Java. For starters, the JIT compiler will not be able to do a good job of optimizing the FORTH interpreter, as it could be for a simple version of Java.

If by "tight" you mean "use less memory", I think the difference will be negligible. Remember that in the โ€œFORTH in Javaโ€ and โ€œplain Javaโ€ cases, you have all the overhead for Java JVM memory. This is likely to lead to failure of any comparison of FORTH code density and equivalent compiled Java code density.

+3
source share

The author on the page describes the implementation of a subset of FORTH and is suitable for inclusion in other applications; supposedly intended to provide scripting capabilities for the application. It is rather unlikely that the system will work by spitting out java or JVM byte codes; it almost certainly uses an interpreter written in Java.

Traditionally, the FORTH interpreter can be implemented in a very small amount of memory. I know someone who implemented it on COSMAC, and the main interpreter is 30 bytes. The stack-oriented bytecode was also very compact, since it did not need to specify the location of the operands - it was simply read from the stack and wrote the result at the top of the stack. This made it popular in embedded circles, where the small overhead of the interpreter was more than offset by a compact representation of the program logic.

This is less important these days, because cars tend to be much larger, although digitalross makes a good point in other situations where FORTH is still in use.

+5
source share

Not a bytecode translator

Answers to your questions: "see below, type and no."

It is just a program that takes some input and produces some output. The input is a Forth script. With the exception of some very large systems, it is rarely possible to create bytecode. jRuby, Clojure, Scala .. large systems like these produce bytecode.

However, your Forth interpreter is probably simple: a script interpreter that appears to be written in java. The input that it accepts is a kind of program, so you get a good double indirect execution. Forth runs through a bytecode interpreter running through jvm running on the CPU.

Now, if you ran it on a CPU emulator or wrote an interpreter in Fort, you can make it triple-indirect. (And in a way, that's already the case, because your Intel processor translates most of the x86 in the micro-ops code before executing them. :-)

In any case, the fact is that a program written in a fairly static language, for example java, may want to take some complicated user input and execute it, or, perhaps, the author of the program has things that are easier to execute in the fourth, and this allows him to write both in java and forward.

You have to think about all this until you understand it.

+2
source share

This allows you to write efficient / hard programs. Partly because the ability to define defining words (words executed at compile time) can have the effect of effectively defining Domain Specific Language (DSL). Forth also encourages refactoring (otherwise the stack material just becomes obscure ...), and therefore the code will be hard.

+2
source share

There are several Forth systems that implement the Forth interpreter in Java. There are two that I know that actually compile the fourth source into the JVM class and allow you to execute Forth code directly without using an interpreter.

0
source share

All Articles