Java and Scripting languages - java

I am making some kind of sandbox-engine, the end-users are able to make scripts to make their world dynamic etc, currently I am only looking at LuaJava because I have quitte some experience with Lua and find it a very readable/easy language. But I also understand that it might be a bad idea to choose only based on personal preference, after all Lua is meant to be embedded into C so performance wont be the best I imagine.
But after a look at some of alternatives (Groovy, Clojure) I find the syntax just unreadable/too abstract, Lua was my first programming experience and even that was quite hard to 'get' at first, I'm afraid these languages would just have scared the crap out of me and I would never have looked at scripting again.
Are there scripting languages that can be embedded in Java that compete with Lua on simplicity?
Edit
My problem with JavaScript, JPython is all the braces etc, as a starting user symbols tend to look 'hard'. Also for python there is the concept of Object's that the user needs to comprehend and isn't that useful in this case.
func = function(arg)
print(arg)
end
Is so simple...

I think JavaScript is very simple, and it can be embedded in Java quite easily via Rhino. Scripts can be both pre-compiled, or compiled on-the-fly via the javax.script classes, which are used to connect to script engines for the Java platform (of which Rhino is one).
If you like Lua, though, there's a Lua for Java project called — *cough* — Kahlua. They list "Fast runtime of the most common operations" as one of their goals.
Edit: Re your edit, I'm not immediately seeing why this:
func = function(arg)
print(arg)
end
is substantially easier to understand than this:
func = function(arg) {
print(arg);
};
...which is the literal translation from Lua to JavaScript, pre-supposing a function called print exists on your platform. I would normally write that like this instead:
function func(arg) {
print(arg);
}
...but the other way is fine for most purposes.
But you should use what you're comfortable with.

You should try Jython and JRuby

Related

Java game engine, writing own scripting language?

I'm planning to create a simple Visual Novel game engine out of Java. I've developed for Java before but I have experience with other languages. What I would like to do, is to create my own scripting language that people can use with the engine to make the complete game out of a script, since Visual Novels are simple enough to do it, I think.
What I don't know is, is Java suitable for making such a scripting language? The way I imagine it is, the engine reads a script file in the game folder, and takes in the input and executes each command linking up to a certain function or something like that. I'm not sure how difficult theoretically that would be to pull off.
As you can probably tell I'm a newbie when it comes to this.
Thanks.
It sounds like you need a domain-specific-language (DSL). Here's an article on DSLs within Java
DSLs are small, expressive programming languages custom designed for
specific tasks. In this four-part series, Venkat Subramaniam
introduces the concept of DSLs and eventually shows you how to build
them using Java
and there's a related question on SO.
You can use Scala to write DSLs and run them on the JVM. However I would warn you that without Scala knowledge it's a (IMO) non-trivial task to do this.
Alternatively you can use a Java implementation of a scripting language together with the Java Scripting API. This will allow you to use an existing language and have it compiled into bytecode. The advantag eis that you're using an existing language. The disadvantage is that it's not specific to your particular needs.
Don't do it. Writing a new language is surprisingly complex. Designing grammar, parsing, error reporting, performance.
It's already there: Creating meta language with Java
Consider groovy, lua or jruby first. People will love you for using familiar syntax.
While it's technically possible to do this, I don't know if Java would be your best bet. While I don't know the scope or purpose of your project, I'd recommend PyGame as a game/scripting language. (You said that you have experience with other languages, so I assume that switching to Python is ok)
I think the above answers give nice directions.
I would like to offer some more ideas:
A. Provide a java "GAME API", which can be exposed via REST/SOAP web services and can have client classes generated from (for example, take a WSDL, and generate client classes from it).
B. Provide the API as a jar file, and have you game implement a File class loader that will load external jars, and use the classes developers will develop using the API.
Here is an example of how to implement such a class loader.
C. Use the ScriptEngine provided by java, to invoke scripts.
See here an example of how to invoke a JavaScript script from within java.
This question is old and i am a bit late for the party (though this question is not answered yet), but it is possible. From what i understand, this sounds like a job for Just Another Framework. Full disclosure: I am the developer of the Framework i am describing. I had a similar problem and wrote this framework to reuse this system, which worked for this kind of issue.
TL;DR
Do not use JAF, if you want your own, fully functional, perfect language.
Use JAF, if you just want to create a simple scripting part, that decouples parts of your code or if you want to evaluate "user input" scripts at Runtime (like in the topic of this question).
What I don't know is, is Java suitable for making such a scripting language?
You can do it with nearly every programming language. You should consider to use Javascript or Lua or [...] and put those into a file instead of creating a custom scripting language. Java has a place for those languages. You should also consider to maybe read .java or .class files from a folder and take those into your "Game-Engine". If you however want to provide your own language and no one can change your mind about it (that was my issue), JAF can help you with that.
JAF is a framework, which allows you to define custom scripting languages within Java, that may be evaluated and executed at Runtime. You give the Parser some Functions, Rules as well as the raw script and the result is a fixed set of instructions (in the form of consumers), that can be executed as many times as you like.
This is by no means another language build on top of java! It is meant to be a foundation for your language. It is only meant for creating small parts though. An example would be some sort of logic that should be interchangeable without stopping the application. This logic should be executed via small scripts and provided via a Database or (like in your case) from files located in a folder.
You can use a Parser, to parse "raw-scripts" that are created by the users. This parser takes Functions and Rules in the form of a Strategy-/Command-Pattern, which basically define your language. After parsing, a "Script"-Object is returned, that can be executed as many times as you want. You can also set values of the Script and therefor connect this Script to your real and running java-code.
The real thing i loved about this however is, that you can provide custom script elements through Rules and Functions, which basically directly connect to java. Just an example would be the printline function. This calls System.out.println();. As i said, just an example, but you can imagine that you can call any existing code from your Functions and Rules.
A (very) simple example of how to use this would look like this:
String rawScript = ... //your users script here
Parser parser = Parser.create(); // used to parse the script
parser.add(IOModule.getPackage()); // add packages of Functions and Rules
Script script;
try {
script = parser.parse(rawScript); // Create the Script
} catch (ParsingFailedException e) {
// Handle an error while parsing the script
// Do not continue, there is no script!
}
script.setValue("yourKey", /* Whatever should be available in your script */); // Inject outside variables for your Rules/Functions
try {
script.run(); // Execute the script
} catch (ExecutionFailedException e) {
// Handle an error at the scripts execution
}
I do not want to write a book about how to use this framework. Take a look at the wiki, it is better explained there.
The advantage of this is, that you can simply define your own language, by providing simple commands that parse specific pieces of the raw script. They are just "applied"; All the magic is hidden within the framework. It's explained a bit within the frameworks-wiki, but i am bad at explaining. So you can create your own little scripting language by providing the building-blocks of your language without requiring deeper knowledge of theoretical computer science.
It has some issues though. It is currently not compatibly with the JSR 223. Also, it is (of course) slower than playn java.

can we access any c program editor through java

I need to develop a c parser in order to extract the function names, macros and its definitions. my approach was not to start from scratch, just access any c program editors like geany which already parses the functions and macros ,
may be a simple api to this editor will get all my requirements, I have googled it, but most of the solutions are to use javacc, so some other parser ...
As this job is already done by editors, so it would be easy , not taking pain to start with building grammar.
this approach would be simple, but unable to find any such editors which have any apis to access it through java.
What you are looking for is existing parser-generator
You could see,
ANTLR
Lex
Yacc
JavaCC
I've already used lex, flex, yacc, bison etc. But nothing can beat Perl for doing it. Moreover Perl regular expressions can be used in Java, PHP.
At least use Perl like regular expression to get it done rather than writing in yacc is very difficult to maintain and which can easily be done in a few lines in Perl or PHP.
Another possibility could be to develop a GCC plugin or a MELT extension to customize the GCC compiler for your needs. (MELT is a domain specific language that I developed to easily extend GCC).
The advantage of customizing GCC for your purposes is that you'll work on the exact internal representations of GCC. However, GCC being complex, extending it requires some work (in particular, partly understanding the complex GCC internal representations and passes).
(It is possible for functions, variables and classes, perhaps not for macros today with GCC 4.7, since GCC preprocessor don't have yet any plugin hooks)
And I am not sure you are right in believing that geany has a complete C parser. I believe it has some regexpr based thing, which e.g. ignores any preprocessor tricks. I don't think that geany is aware of e.g. functions or variables created by expanding complex macros (like some GTK implementation macros for instance).
There are several IDE's or programmers-editors with C parsers, written in Java. So getting at them shouldn't be too horrific (famous last words :-)
Eclipse CDT which has several books on how to write and use plugins/extensions
NetBeans
to mention just two. They both have active user communities who might be able to help too.
Their C editors, have a pretty good grasp of C syntax because they can fold functions. Eclipse's C editor keeps track of definitions, and I think NetBeans does too.
Personally, if I needed to parse C to get function bodies, and the code is syntactically correct, it wouldn't be too hard to use parser-development-tools. IIRC ANTLR might have a C grammar already.

Fastest scripting language for Java?

I'm making a falling sand game in Java. I want users to be able to write their own engine for the game and I thought a scripting language might work for that. I've tried out a small script with jython and it's many times slower than that java version.
I need a scripting language that has fast loops and/or fast array access since that's what the game will be doing a lot of. Or if you have any suggestions on another way to let users write their own engine for the game.
I'm also not entirely sure this can work (well). If you don't think it can please let me know why and maybe some possible alternatives.
JavaCC
You could write you're own simple game language with JavaCC. This does give's you all the flexibily you possibly want and native Java speed (well that actualy depends on you're implementation). But you need to keep you're syntax simple or else it will take some efford...
Java
Java? Yes Java! Why not let users extend you're game by Java code? In Java 6 there is a Java compiler API:
javax.tools API.
Beanshell vs Rhino
Some performace comparison between BeanShell and the Rhino (Javascript) interpreters (Rhino is the winner):
thoughtworker.in
Pankaj Kumar's Weblog
You might look at BeanShell for this. In my use it's been pretty much as fast as native Java (as that is what it becomes) and is pretty accessible as it is Java, albeit locked around Java 1.4.
RelProxy is Java compiler on the fly and an automatic hot class reloader, pure Java code can be executed like a scripting language.
Because Java is faaast Java is then maybe the fastest scripting language, optionally you can use .class files like a sort of compiled cache to avoid compiling on load.

Scala - Java = ? (Or Clojure - Java = ?)

Is it possible for a developer to use Scala without knowing Java?
Is it possible for a developer to use Clojure without knowing Java?
Note: For example I am a C# developer and I use .NET without knowing any VB (Of-course WF 4.0 uses VB for statements, so I refuse any projects involving WF 4.0 :) ).
Summary I: Thanks to all for your answers. Now I can tell one can use these languages without knowing Java (The Language). Now I've got another question: How can one start with Java (JVM) ecosystem? What are beginner, intermediate and advanced knowledge-bases (bags!)? To be honest Java ecosystem was always a bit confusing to me, so it would be very helpful to provide some "reference references" and "defacto tutorials" for learning (curve!) JVM.
(OK! I will post it as another question!)
Scala, and Clojure even more so, are totally different languages from Java. The only thing they have in common is that they run on the Java virtual machine. You do not need to know the Java programming language to program in Scala or Clojure. It would be useful, however, to know something about the Java ecosystem (the Java virtual machine and runtime environment).
Sure you can. You'll lack the knowledge of the standard library at first, but that's not a big problem when you're starting with a new languages anyway. Clojure & Scala, however, put a heavy emphasis on the Java interop and do not try to hide the raw Java stuff, so you'd probably have to learn more about Java(not the language - the Java Platform) at some point to make maximum use of the language and the underlying platform.
Speaking from the perspective of knowing Java and Clojure (but not Scala), you can use Clojure just fine without knowing Java. You have access to the core language, which is fully-featured, and libraries (e.g. clojure-contrib).
However, one of the big benefits of using Clojure (versus other lisps) is access to the Java ecosystem - libraries, debuggers, JVM, etc.
So, yes you can use Clojure effectively and I wouldn't let a lack of Java knowledge hinder you, but you'll add to your bag of tricks if you eventually start to learn enough Java to take advantage.
Well, Clojure has been ported to the CLR. Probably easier thant the whole jump to the JVM since you are a .NET developer. :)
http://github.com/richhickey/clojure-clr
yes, thats me!
Has not been much of a problem for me except for when I needed a stupid simple swing gui. thats when I had to go back and review java.
Scala is totally different language than java. So yes, you can of course use scala without knowing java ( but of course you have to learn scala first). Even though, knowing java (or any OO language) + some functional language makes it easier to understand scala.

Is there a Perl implementation in Java?

I am looking for Perl implementation in Java. Something like Jython is for Python.
I found PLJava but it needs both JVM and Perl compiler installed. I need something which does not need a Perl compiler.
I need to run some Perl code in a Java class.
UPDATES:
I figured out that PLJAVA is what I need. Does anybody know some tutorial?
Has anybody played with the Inline::Java module.
I also could not install Inline::Java.
Jython isn't fully compatible with CPython (or whatever you would rather call the original C++ Python interpreter), but wherever either differs from the language spec is a bug. Unfortunately, Perl 5 is much more complex and lacks any formal language specifications at all -- the language effectively being defined as "what does the perl executable do" -- so there exists no other implementation of the Perl 5 language aside from the Perl 5 interpreter. Unfortunate, but that's history. Perl 6 does have a language spec and multiple (incomplete) implementations, but that's not likely to be useful to you.
PLJava was an attempt to do exactly what you want, call Perl from Java. It does so via JNI (stuffing native code into Java) linking to libperl. However, it's not been updated since 2004 and I don't know how well it works.
Edit
I hadn't seen Inline::Java::PerlInterpreter before -- unfortunately it doesn't seem to work with my system Perl.
Update: There is another options that could be viable: Jerl. Jerl translates a micro-Perl interpreter into Java bycode using NestedVM. In this sense Jerl is almost a Java implementation of Perl. I haven't tested it though and it is reasonable to expect a loss in performances . Nonetheless it's a solution worth of investigation.
Jerl is hosted here: https://code.google.com/p/jerl/.
Sadly not, at least not a complete and usable one. Perl is a difficult language to port to other VMs mainly because of its highly dynamic nature and for historical reasons related to how the language has been developed over the years; theoretical issues about perl parsability are, in my humble opinion, of secondary importance. Perl does not have an a formal specification nor an official grammar: Perl implementation is Perl's own formal specification. This means that to write an alternative implementation of Perl one has to know intimately the internals of the current one and this is obviously a big barrier to the development of such a project. Here lies the real difficulty in porting Perl to other VMs. Additionaly, the dynamic nature of Perl poses other technical problems related to an efficient implementation on the Java virtual machine that is engineered to support statically typed languages. There have been some efforts like this one for example: http://www.ebb.org/perljvm/. A newer one is cited here: http://use.perl.org/~Ovid/journal/38837. Both were abandoned at one point or another not because of infeasability but only because the effort required was too big for a research/hobby project. A new interesting alternative that is proceeding steadly is language-P by Mattia Barbon: http://search.cpan.org/dist/Language-P/. It is an implementation of Perl on the NET clr. The implementation is still incomplete but I know that the man behind the project is a very persistent one and that the project has been going forward slowly but steadily. Maybe Perl on the CLR will come first. :D
If you are not going to use a Perl compiler, exactly what are you looking for?
What do you mean by a Perl implementation for Java? If you want to embed Perl in your Java programs, you are going to need a Perl compiler.
It sounds to me like the problem you are having is that you do not have a Perl compiler/interpreter available, yet you need to execute some Perl code. Unfortunately, I don't think that there exists anything like Jython for Perl. The only projects that I know of that can do what you are asking is PLJava and JPL. Unfortunately, it looks like both projects are abandoned.
It would be a cool project though, as I believe there is a need for something like this.
Look at Sleep
is a multi-paradigm scripting language for the Java Platform
easy to learn with Perl and Objective-C inspired syntax
executes scripts fast with a small package size (~250KB)
excels at data manipulation, component integration, and distributed communication
seamlessly uses Java objects and 3rd party libraries
Rakudo is a JVM implementation of Perl 6 (which is quite different from standard Perl). Now there is Jerl (which runs in JVM but is compiled based on microperl). Both have their limitations but are solid contenders for most uses.
You can use par including modules (or even as an executable) if you don't have perl installed on the target platform: http://metacpan.org/pod/PAR

Categories