A script inside Java for developing and debugging
Just copy the Code into your project or import the downloadable .jar (under "releases") file to your build path and you are done!
DevScript does not use any unnessecary 3rd party libraries.
Basic syntax to execute a "Hello World" script as a string inside your program: Both input and outputs are the default System.in and System.out, but you can define your own.
Process p = new Process(true);
p.addSystemOutput();
p.setInput(System.in);
p.execute("println \"Hello World\"", false);
p.execute(new File("path_to_file"), true);You can also execute the script from the command line with the raw jar file and the argument --nogui.
This example will execute the command "version" and print the current version.
java -jar devscript_1.9.0.jar --nogui -e "version"
Command line arguments are:
- -e or --execute <script> Executes a script right from the command line
- -f or --file Executes the contents of a text file
- --nogui rejects the program to open the GUI editor
- If only --nogui is given, the jar opens the default editor, stored in Editor.txt
In this big section, I will try to bring you near the usage and capabillities of the DevScript, so you can use them for your own projects
There are 6 main data types: STRING, BOOLEAN, BLOCK, DICTIONARY, ARRAY, OBJECT (OBJECT is any java object, except the primitive data types)
There are also 4 sub- data types: ANY, ARRAY_ANY, INTEGER, FLOAT, CONTINUE
Sub- data types are not considered as 'real' data types. They are only important in specific situations
This would be a typical command, that expects certain argument types:
exampleCommand [BLOCK] [ANY] [STRING] [CONTINUE];
The example command expects a block (Discussed in section Block) as the first argument, then any other type and Strings.
The [CONTINUE] type means, there is no limitation for the last argument in number,
so you could pass an infinite amount of STRINGs (Because the last type before CONTINUE was a string).
The whole syntax is based on commands. These act like functions, that means, they can return values and accept arguments and are separated with ';'.
Example for the command println:
- This command expects strings without limitation in number: println [STRING] [CONTINUE] and returns $null, that means nothing.
println "Hello World" "And another line";
Command names can also be shifted, like the [string] + [string] command to make the code more readable. So, instead of writing + 1 1 ('+' or 'add' being the command here) we can shift the command one section to the right:
1 + 1; <- Better to read and write!
This command returns the sum of its two arguments. But this command alone does not do very much. How do you use the new, returned value? Look at this example:
println (1 + 1);
This is the same as:
println 2;
Commands can be combined with others with parantheses.
println (1 + (4 / 2));
Note that if you want to use $null as a returned value, the program will throw an error, because $null is not interpreted as argument,
so the interpreter thinks this command has not arguments: use [STRING] != use
use (println "null?!")
Error at [println "null?!"] No such command use.
Variables can get declared with the [STRING] = [ANY] command. And you can access them with a $ sign, followed by the variable name:
foo = 10;
println (1 + $foo);
There are also a few inbuild variables: ´$true, $false and $null´
Blocks are code, wrapped inside two curly braces { }. In theDevScript Language, blocks are treated as a data type [BLOCK]. Basically, a block only consists of a String of code and their main goal is to provide functions. To define a function, you create a variable as usual (With the "=" command), since -as already mentioned- blocks are just data types and can get passed as command argument.
function = {
println "I am a function :)";
println "But how do you execute me?"
};
To execute a function, use the "call [BLOCK] [CONTINUE] ..." command:
call $function;
Functions can also accept arguments (This is why the call command has some more optional arguments).
Inside the function, the passed argument values are disguised as $0, $1, $2 and so on, depending on how much arguments you pass.
function = {
println "Argument 0 is:" $0;
println "Argument 1 is:" $1;
};
call $function "Argument0" "Argument1";
Last but not least, functions can also return value with the command return [ANY]. If you use this command, the "call" command has now a new return value.
add = {
#Adds argument0 and argument1 and return new new value#
return ($0 + $1);
}
println (call $add 1 1);
Please take your time to understand this section, as it can get quite complex.
Arrays can get declared as empty, or filled. All arrays are dynamic. They can get altered with ´push [ANY] [ARRAY]´ and ´pop [STRING]´.
emptyArray = [];
filledArray = ["string1" "string2" $true (20 + 12) {println "Arrays can hold blocks. They are data types :)"}];
newObject = "This is a String";
push $newObject $emptyArray;
As you can see, you can set multiple data types to one array. However if an array -for example- only has Strings, the type of the array will be STRING.
- $filledArray would have the type ANY, since it contains multiple, different data types, such as STRING, BOOLEAN, BLOCK
- $emptyArray's type would be STRING: It has only strings in it.
Tip: To check a variable for a type, you can use the [ANY] typeof [STRING] command.
The [STRING] argument would be the type written out. Different types are listed in the Section: Data Types. Minor and Major work.
$filledArray typeof "any" would return true.
Also arrays with multiple dimensions are possible. You access an array index like in any other programming language:
array = [["inner" "inner2"] "string"];
println $array[1];
println $array[0][1];
And if we are discussing Arrays and you already know about blocks, you may want to learn how to use for loops etc.: This would be a typical for- loop:
for i 10 {
println "This text will be printed 10 times!";
println "Iteration: " $i;
};
The first argument of the for [STRING] [STRING] [BLOCK] command is the variable name starting at zero.
array = ["John" "Peter" "Chris"];
for i (length $array) {
println $array[$i];
};
Here is an example of an if- statement:
if (10 == 11) {
println "Condition is true";
} {
println "Condition is false";
};
But keep in mind, that the parantheses after the if are just wrapping another command ([ANY] == [ANY]) and are not there like in Java or other languages.
if $true {
println "true"
};
Now, if you understand the basic syntax and command usage, you are ready to start! It may also be handy to notice, that this script also supports threading. You can use the help command for a list of commands, but I will print them below, so you can have a look.
LIBRARY 'Native' (65 Commands)
println [ARRAY_ANY] Prints any object's toString() method in a new line
print [ARRAY_ANY] Prints any objecs's toString() method
[STRING] = [ARRAY_ANY] Defines a variable. Access it with $variableName
[ARRAY_ANY] === [ANY] [STRING] [object] === [array] [index] [index] ... Like '=' for arrays
[STRING] + [STRING] Adds two numbers and returns the result, if either of the arguments is not a number, two strings are added
[STRING] - [STRING] Subtracts two numbers
[STRING] * [STRING] Multiplies two numbers
[STRING] / [STRING] Divides two numbers
exec [STRING] Executes a shell command
script [STRING] Executes a new script sub-process with its parent in- and outputs
length [ANY] Returns the size of the array
pop [ANY] [STRING] Removes the specified index of the array
createdict Returns an empty dictionary
get [DICTIONARY] [STRING] get [dictionary] [key] Returns the corresponding value of the key inside the dictionary
set [DICTIONARY] [STRING] [ARRAY_ANY] set [dictionary] [key] [object]
remove [DICTIONARY] [STRING] remove [dictionary] [key] Returns false, if there was no associated key with the name.
clear [DICTIONARY] clear [dict] Removes all values from the dictionary
isEmpty [DICTIONARY] Checks, if a dictionary is empty
push [ARRAY_ANY] [ANY] Pushes a new value into the array
[STRING] lt [STRING] Returns true, if argument 1 is less than 2 (If the arguments are not numbers, it checks the length of the string)
[STRING] lteq [STRING] Returns true, if argument 1 is less or equal than 2 (If the arguments are not numbers, it checks the length of the string)
[STRING] gteq [STRING] Returns true, if argument 1 is grater or equal than 2 (If the arguments are not numbers, it checks the length of the string)
[STRING] gt [STRING] Returns true, if argument 1 is grater than 2 (If the arguments are not numbers, it checks the length of the string)
[ANY] == [ANY] Returns true, if argument 1 and 2 are equal
[ANY] != [ANY] Returns true, if argument 1 and 2 are not equal
not [BOOLEAN] Inverts a boolean
[BOOLEAN] and [BOOLEAN] [ANY] Chain boolean conditions: if ($true and $true or $false) {...
random Returns a random number between 0 and 1
[BOOLEAN] or [BOOLEAN] [ANY] Chain boolean conditions: if ($true or $true and $false) {...
int [ANY] Casts the given value into java.lang.Integer
float [ANY] Casts the given object into java.lang.Float
string [ANY] Casts the given value into java.lang.String
call [BLOCK] [ARRAY_ANY] Executes a function (Variable that is a block: x = { function code... }. You can also pass arguments. Access them inside the block with $0 $1 etc...Returns the returned value of the function
if [BOOLEAN] [BLOCK] [BLOCK] If statement
if [BOOLEAN] [BLOCK] If statement
ifnot [BOOLEAN] [BLOCK] Inverted If-statement
for [STRING] [STRING] [BLOCK] For loop: for i 10 {...}
loop [BLOCK] Infinite loop. Use the break command inside it.
break Breaks out of the next found loop in the stack. If no loop was found, this command interrupts the block
kill [STRING] Stops the application and throws an error message
input Reads input from the set input stream
[ARRAY_ANY] typeof [STRING] Argument 2 is a string representation of the type like the command arguments. There are two adittional useful types: int and float
wait [STRING]
thread [STRING] [BLOCK] Runs a separate thread along the process
kill [BLOCK]
pause Pauses the block, the command is executed in
pause [BLOCK] Pauses the specified block, if it is running in a separate thread.
waitfor [BLOCK] Waits for a block to finish
wake [BLOCK] Wakes the specified thread
alive [BLOCK]
return [ARRAY_ANY] Searches the first occurrence of a block that is a function and returns its given value or null (Block execution gets terminated)
return [BLOCK]
charAt [STRING] [STRING] [index] [string]
stringLength [STRING]
toArray [STRING]
substring [STRING] [STRING] [STRING] [string] [begin] [end]
version Prints the version of the script
help Prints all the available commands with a brief explanation and arguments
import [STRING] Imports a library from a compiled .jar file. The class should extend com.mygdx.devkev.devscript.raw.Library and be named CustomLibrary
You can use a * to reference the current path the process is executed in (*/library.jar
getFile [STRING] Returns a java.io.File object
fileExists [OBJECT] Checks if a file exists
deleteFile [OBJECT] Deletes a file. You can use this command, if you want to clear a files content and append lines with the writeFileLine command
writeFileLine [OBJECT] [STRING] [file] [content] Appends a new line to the file
listDirectory [OBJECT] Returns an array containing all files inside this directory
isDirectory [OBJECT]