|
1 | | -# devscript |
2 | | -A Java inbuild script for developing and debugging |
| 1 | +# Devscript |
| 2 | +A script inside Java for developing and debugging |
| 3 | + |
| 4 | +# Implementation |
| 5 | + |
| 6 | +Just copy the Code into your favorite IDE or import the downloadable .jar file to your build path and you are done!<br> |
| 7 | +DevScript does not use any unnessecary external libraries. |
| 8 | + |
| 9 | +Basic syntax to execute a "Hello World" script as a string inside your program: |
| 10 | +Both input and outputs are the default System.in and System.out, but you can define your own. |
| 11 | + |
| 12 | +```java |
| 13 | +Process p = new Process(true); |
| 14 | +p.addSystemOutput(); |
| 15 | +p.addInput(System.in); |
| 16 | +p.execute("println \"Hello World\"", false); //If the process should be executed in a separate thread. |
| 17 | +p.execute(new File("path_to_file"), true); |
| 18 | +``` |
| 19 | +You can also execute the script from the command line with the raw jar file. |
| 20 | +> java -jar devscript_1.8.2.jar -e "version" |
| 21 | +
|
| 22 | +Command line arguments are:<br> |
| 23 | +- -e or --execute <script> Executes a script right from the command line |
| 24 | +- -f or --file <pathToFile> Executes the contents of a text file |
| 25 | +- If no argument is passed, the jar opens the default editor, stored in Editor.txt |
| 26 | + |
| 27 | +# Syntax |
| 28 | +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 |
| 29 | + |
| 30 | +## Commands |
| 31 | + |
| 32 | +### Data Types |
| 33 | +*This section is only important, if you want to implement your own library if not, you can skip this section*<br> |
| 34 | +There are 6 main data types: _STRING, BOOLEAN, BLOCK, DICTIONARY, ARRAY, OBJECT_ (OBJECT is any Java object)<br> |
| 35 | +There are also 4 minor data types: _ANY, ARRAY_ANY, INTEGER, FLOAT, CONTINUE_<br> |
| 36 | +They are called, minor types, because they are not considered as 'real' data types. They are only important in specific situations<br> |
| 37 | + |
| 38 | +This would be a typical command, that expects certain argument types: |
| 39 | +> exampleCommand [BLOCK] [ANY] [STRING] [CONTINUE]; |
| 40 | +
|
| 41 | +The example command expects a block (Discussed in section _Block_) as the first argument, then any other type and Strings. |
| 42 | +The [CONTINUE] type means, there is no limitation for the last argument in number,<br> |
| 43 | +so you could pass an infinite amount of STRINGs (Because the last type before CONTINUE was a string). |
| 44 | + |
| 45 | +### Command Syntax |
| 46 | +The whole syntax is based on commands. These act like functions, that means, they can return values and accept arguments and are separated with ';'.<br> |
| 47 | +Example for the command println: |
| 48 | + - This command expects strings without limitation in number: _println [STRING] [CONTINUE]_ and returns $null, that means nothing. |
| 49 | + > println "Hello World" "And another line"; |
| 50 | +
|
| 51 | +Command names can also be shifted, like the _[string] + [string]_ command to make the code more readable: |
| 52 | + > 1 + 1; |
| 53 | + |
| 54 | +But this command alone does not do very much. How do you use the new, returned value? |
| 55 | +Look at this example: |
| 56 | + > println (1 + 1); |
| 57 | +
|
| 58 | +This is the same as: |
| 59 | + > println 2; |
| 60 | + |
| 61 | +Commands can be combined with others with parantheses. |
| 62 | + > println (1 + (4 / 2)); |
| 63 | +
|
| 64 | +## Variables |
| 65 | + |
| 66 | +Variables can get declared with the _[STRING] = [ANY]_ command. |
| 67 | +And you can access them with a $ sign, followed by the variable name: |
| 68 | +> foo = 10;<br>println (1 + $foo); |
| 69 | +
|
| 70 | + There are also a few inbuild variables: |
| 71 | + ´$true, $false and $null´ |
| 72 | + |
| 73 | +## Blocks |
| 74 | +Blocks are code, wrapped inside two curly braces { }. |
| 75 | +In theDevScript Language, blocks are treated as a data type [BLOCK]. Basically, a block only consists of a code String and |
| 76 | +their main goal is to provide functions. |
| 77 | +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. |
| 78 | +> function = {<br>println "I am a function :)";<br>println "But how do you execute me?"<br>}; |
| 79 | +
|
| 80 | +To execute a function, use the _"call [BLOCK] [CONTINUE] ..."_ command: |
| 81 | +> call $function; |
| 82 | +
|
| 83 | +Functions can also accept arguments (This is why the call command has some more optional arguments).<br> |
| 84 | +Inside the function, the passed argument values are disguised as $0, $1, $2 and so on, depending on how much arguments you pass. |
| 85 | +> function = {<br> println "Argument 0 is:" $0;<br> println "Argument 1 is:" $1;<br>};<br>call $function "Argument0" "Argument1"; |
| 86 | +
|
| 87 | +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. |
| 88 | +> add = {<br> #Adds argument0 and argument1 and return new new value#<br> return ($0 + $1);<br>}<br>println (call $add 1 1); |
| 89 | +
|
| 90 | +Please take your time to understand this section, as it can get quite complex. |
| 91 | +## Arrays |
| 92 | + Arrays can get declared as empty, or filled. |
| 93 | + All arrays are dynamic. They can get altered with ´push [ANY] [ARRAY]´ and ´pop [STRING]´. |
| 94 | + |
| 95 | +> emptyArray = [];<br> |
| 96 | +filledArray = ["string1" "string2" $true (20 + 12) {println "Arrays can hold blocks. They are data types :)"}];<br> |
| 97 | +newObject = "This is a String";<br> |
| 98 | +push $newObject $emptyArray; |
| 99 | + |
| 100 | +As you can see, you can set multiple data types to one array. |
| 101 | +However if an array -for example- only has Strings, the type of the array will be STRING. |
| 102 | +- $filledArray would have the type ANY, since it contains multiple, different data types, such as STRING, BOOLEAN, BLOCK |
| 103 | +- $emptyArray's type would be STRING: It has only strings in it.<br> |
| 104 | +_Tip: To check a variable for a type, you can use the [ANY] typeof [STRING] command.<br> |
| 105 | +The [STRING] argument would be the type written out. Different types are listed in the Section: Data Types. Minor and Major work.<br> |
| 106 | +$filledArray typeof "any" would return true._ |
| 107 | + |
| 108 | +Also arrays with multiple dimensions are possible. You access an array index like in any other programming language: |
| 109 | +>array = [["inner" "inner2"] "string"];<br>println $array[1];<br>println $array[0][1]; |
| 110 | +
|
| 111 | +And if we are discussing Arrays and you already know about blocks, you may want to learn how to use for loops etc.: |
| 112 | +This would be a typical for- loop: |
| 113 | +>for i 10 {<br>println "This text will be printed 10 times!";<br>println "Iteration: " $i;<br>}; |
| 114 | +
|
| 115 | +The first argument of the _for [STRING] [STRING] [BLOCK]_ command is the variable name starting at zero. |
| 116 | +>array = ["John" "Peter" "Chris"];<br>for i (length $array) {<br>println $array[$i];<br>}; |
| 117 | +
|
| 118 | +## If |
| 119 | +Here is an example of an if- statement: |
| 120 | +>if (10 == 11) {<br>println "Condition is true";<br>} {<br>println "Condition is false";<br>}; |
| 121 | +
|
| 122 | +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. |
| 123 | +>if $true {<br>println "true"<br>}; |
| 124 | +## Thats it! |
| 125 | +Now, if you understand the basic syntax and command usage, you are ready to start! |
| 126 | +It may also be handy to notice, that this script also supports threading. |
| 127 | +You can use the _help_ command for a list of commands, but I will print them below, so you can have a look. |
| 128 | + |
| 129 | +# All default commands |
| 130 | + |
| 131 | +LIBRARY 'Native' (65 Commands)<br> |
| 132 | +println [ARRAY_ANY] Prints any object's toString() method in a new line<br> |
| 133 | +print [ARRAY_ANY] Prints any objecs's toString() method<br> |
| 134 | +[STRING] = [ARRAY_ANY] Defines a variable. Access it with $variableName<br> |
| 135 | +[ARRAY_ANY] === [ANY] [STRING] [object] === [array] [index] [index] ... Like '=' for arrays<br> |
| 136 | +[STRING] + [STRING] Adds two numbers and returns the result, if either of the arguments is not a number, two strings are added<br> |
| 137 | +[STRING] - [STRING] Subtracts two numbers<br> |
| 138 | +[STRING] * [STRING] Multiplies two numbers<br> |
| 139 | +[STRING] / [STRING] Divides two numbers<br> |
| 140 | +exec [STRING] Executes a shell command<br> |
| 141 | +script [STRING] Executes a new script sub-process with its parent in- and outputs<br> |
| 142 | +length [ANY] Returns the size of the array<br> |
| 143 | +pop [ANY] [STRING] Removes the specified index of the array<br> |
| 144 | +createdict Returns an empty dictionary<br> |
| 145 | +get [DICTIONARY] [STRING] get [dictionary] [key] Returns the corresponding value of the key inside the dictionary<br> |
| 146 | +set [DICTIONARY] [STRING] [ARRAY_ANY] set [dictionary] [key] [object]<br> |
| 147 | +remove [DICTIONARY] [STRING] remove [dictionary] [key] Returns false, if there was no associated key with the name.<br> |
| 148 | +clear [DICTIONARY] clear [dict] Removes all values from the dictionary<br> |
| 149 | +isEmpty [DICTIONARY] Checks, if a dictionary is empty<br> |
| 150 | +push [ARRAY_ANY] [ANY] Pushes a new value into the array<br> |
| 151 | +[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)<br> |
| 152 | +[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)<br> |
| 153 | +[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)<br> |
| 154 | +[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)<br> |
| 155 | +[ANY] == [ANY] Returns true, if argument 1 and 2 are equal<br> |
| 156 | +[ANY] != [ANY] Returns true, if argument 1 and 2 are not equal<br> |
| 157 | +not [BOOLEAN] Inverts a boolean<br> |
| 158 | +[BOOLEAN] and [BOOLEAN] [ANY] Chain boolean conditions: if ($true and $true or $false) {...<br> |
| 159 | +random Returns a random number between 0 and 1<br> |
| 160 | +[BOOLEAN] or [BOOLEAN] [ANY] Chain boolean conditions: if ($true or $true and $false) {...<br> |
| 161 | +int [ANY] Casts the given value into java.lang.Integer<br> |
| 162 | +float [ANY] Casts the given object into java.lang.Float<br> |
| 163 | +string [ANY] Casts the given value into java.lang.String<br> |
| 164 | +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<br> |
| 165 | +if [BOOLEAN] [BLOCK] [BLOCK] If statement<br> |
| 166 | +if [BOOLEAN] [BLOCK] If statement<br> |
| 167 | +ifnot [BOOLEAN] [BLOCK] Inverted If-statement<br> |
| 168 | +for [STRING] [STRING] [BLOCK] For loop: for i 10 {...}<br> |
| 169 | +loop [BLOCK] Infinite loop. Use the break command inside it.<br> |
| 170 | +break Breaks out of the next found loop in the stack. If no loop was found, this command interrupts the block<br> |
| 171 | +kill [STRING] Stops the application and throws an error message<br> |
| 172 | +input Reads input from the set input stream<br> |
| 173 | +[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<br> |
| 174 | +wait [STRING]<br> |
| 175 | +thread [STRING] [BLOCK] Runs a separate thread along the process<br> |
| 176 | +kill [BLOCK]<br> |
| 177 | +pause Pauses the block, the command is executed in<br> |
| 178 | +pause [BLOCK] Pauses the specified block, if it is running in a separate thread.<br> |
| 179 | +waitfor [BLOCK] Waits for a block to finish<br> |
| 180 | +wake [BLOCK] Wakes the specified thread<br> |
| 181 | +alive [BLOCK]<br> |
| 182 | +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)<br> |
| 183 | +return [BLOCK]<br> |
| 184 | +charAt [STRING] [STRING] [index] [string]<br> |
| 185 | +stringLength [STRING]<br> |
| 186 | +toArray [STRING]<br> |
| 187 | +substring [STRING] [STRING] [STRING] [string] [begin] [end]<br> |
| 188 | +version Prints the version of the script<br> |
| 189 | +help Prints all the available commands with a brief explanation and arguments<br> |
| 190 | +import [STRING] Imports a library from a compiled .jar file. The class should extend com.mygdx.devkev.devscript.raw.Library and be named CustomLibrary<br> |
| 191 | +You can use a * to reference the current path the process is executed in (*/library.jar<br> |
| 192 | +getFile [STRING] Returns a java.io.File object<br> |
| 193 | +fileExists [OBJECT] Checks if a file exists<br> |
| 194 | +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<br> |
| 195 | +writeFileLine [OBJECT] [STRING] [file] [content] Appends a new line to the file<br> |
| 196 | +listDirectory [OBJECT] Returns an array containing all files inside this directory<br> |
| 197 | +isDirectory [OBJECT]<br> |
0 commit comments