Creating a metalanguage using Java

guys! I need to create some kind of metalanguage that I could embed in XML and then parse using Java. For instance:

<code>
    [if value1>value2 then "Hello, Bob!" else "Hello, Jack"]
</code>

or

 <code>
     [if value1+2>value2 return true]
 </code>

I need to implement conditional statements, arithmetic.

Any suggestions I should start looking for?

0
source share
5 answers

Java has a built-in JavaScript interpreter:

ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("JavaScript");
jsEngine.put("value1", 8);
jsEngine.put("value2", 9);
String script = "if(value1 + 2 > value2) {'Foo'} else {'Bar'}";
final Object result = jsEngine.eval(script);
System.out.println(result);  //yields "Foo" String

Of course, you can download the script for free from anywhere and provide it with any context ( valueand value2in this example) that you want.

See also Scripts for the Java platform .

+7
source

. Java ANTLR.

+4

Java API, . API javax.script.

, , JavaScript code, API .

+1

If you really want to develop your own language, start with an interpreter . If you just want to use some other language in your Java code, look at the built-in languages ​​of the built-in JSP language.

+1
source

It is almost certain that a home-made language will suck , especially in the long run, so do not digest anything yourself.

There are several jsp-like frameworks, perhaps one of them will do the trick:

JSTL / JSP EL (expression language) in a non-JSP context (stand-alone)

+1
source

All Articles