Skip to content

Commit a97f0cb

Browse files
authored
Merge branch 'master' into 1.8.3
2 parents 098f8d6 + c7e36ec commit a97f0cb

4 files changed

Lines changed: 326 additions & 67 deletions

File tree

Examples/Calculator.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#Simple calculator with input#
2+
#Should also be an example for functions#
3+
4+
calculate = {
5+
num1 = $0; #Better names#
6+
num2 = $1;
7+
operator = $2;
8+
9+
ifnot (($num1 typeof num) or ($num2 typeof num))
10+
{
11+
println "Error";
12+
return;
13+
};
14+
15+
if ($operator == +)
16+
{
17+
return ($num1 + $num2);
18+
};
19+
if ($operator == -)
20+
{
21+
return ($num1 - $num2);
22+
};
23+
println "Error, unknown operator: " $operator;
24+
};
25+
26+
print "Enter first number: ";
27+
num1 = (input);
28+
print "Enter second number: ";
29+
num2 = (input);
30+
print "Enter operator (+, -): ";
31+
operator = (input);
32+
33+
println "Calculating...";
34+
println (call $calculate $num1 $num2 $operator);

Examples/Selection Sort.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Selection sort example by DevKev#
2+
3+
unsorted = [3 4 7 8 9 543 23 7 5 32 1 56 7 65 34 13 5 7 2];
4+
sorted = [];
5+
record = 99999;
6+
index = -1;
7+
8+
loop
9+
{
10+
for i (length $unsorted)
11+
{
12+
if ($unsorted[$i] lt $record)
13+
{
14+
record = $unsorted[$i];
15+
index = $i;
16+
};
17+
};
18+
19+
push $unsorted[$index] $sorted;
20+
pop $unsorted $index;
21+
record = 99999;
22+
23+
if ((length $unsorted) == 0)
24+
{
25+
break;
26+
};
27+
};
28+
29+
println "Done";
30+
println $sorted;

README.md

Lines changed: 197 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,197 @@
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>
Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
package com.devkev.devscript.raw;
2-
3-
import java.util.ArrayList;
4-
5-
import com.devkev.devscript.raw.ApplicationBuilder.Type;
6-
7-
public class Array { //For dataContainers
8-
private final ArrayList<Object> indexes = new ArrayList<Object>(2);
9-
public DataType arrayType;
10-
11-
public Array(Object... indexes) {
12-
for(Object d : indexes) this.indexes.add(d);
13-
updateArraytype();
14-
}
15-
16-
public Array() {}
17-
18-
public void push(Object container) {
19-
indexes.add(container);
20-
updateArraytype();
21-
}
22-
23-
public void push(Object container, int index) {
24-
indexes.add(index, container);
25-
updateArraytype();
26-
}
27-
28-
public void pop(int index) {
29-
indexes.remove(index);
30-
updateArraytype();
31-
}
32-
33-
public void pop(Object container) {
34-
indexes.remove(container);
35-
updateArraytype();
36-
}
37-
38-
public ArrayList<Object> getIndexes() {
39-
return indexes;
40-
}
41-
42-
public void updateArraytype() {
43-
Type type = Type.NULL;
44-
for(int j = 0; j < indexes.size(); j++) {
45-
DataType containerType = ApplicationBuilder.toDataType(indexes.get(j));
46-
if(j > 0) {
47-
DataType indexType = ApplicationBuilder.toDataType(indexes.get(j-1));
48-
if(indexType.type != containerType.type && type != Type.ANY) {
49-
type = Type.ANY;
50-
break;
51-
}
52-
} else if(j == 0) {
53-
type = containerType.type;
54-
}
55-
}
56-
arrayType = new DataType(type, true);
57-
}
58-
59-
public String toString() {
60-
String s = "ARRAY:{";
61-
for(int i = 0; i < indexes.size()-1; i++) s += indexes.get(i) + ",";
62-
if(indexes.size() > 0) s += indexes.get(indexes.size()-1);
63-
return s + "}";
64-
}
65-
}
1+
package com.devkev.devscript.raw;
2+
3+
import java.util.ArrayList;
4+
5+
import com.devkev.devscript.raw.ApplicationBuilder.Type;
6+
7+
public class Array { //For dataContainers
8+
private final ArrayList<Object> indexes = new ArrayList<Object>(2);
9+
public DataType arrayType;
10+
11+
public Array(Object... indexes) {
12+
for(Object d : indexes) this.indexes.add(d);
13+
updateArraytype();
14+
}
15+
16+
public Array() {}
17+
18+
public void push(Object container) {
19+
indexes.add(container);
20+
updateArraytype();
21+
}
22+
23+
public void push(Object container, int index) {
24+
indexes.add(index, container);
25+
updateArraytype();
26+
}
27+
28+
public void pop(int index) {
29+
indexes.remove(index);
30+
updateArraytype();
31+
}
32+
33+
public void pop(Object container) {
34+
indexes.remove(container);
35+
updateArraytype();
36+
}
37+
38+
public ArrayList<Object> getIndexes() {
39+
return indexes;
40+
}
41+
42+
public void updateArraytype() {
43+
Type type = Type.NULL;
44+
for(int j = 0; j < indexes.size(); j++) {
45+
DataType containerType = ApplicationBuilder.toDataType(indexes.get(j));
46+
if(j > 0) {
47+
DataType indexType = ApplicationBuilder.toDataType(indexes.get(j-1));
48+
if(indexType.type != containerType.type && type != Type.ANY) {
49+
type = Type.ANY;
50+
break;
51+
}
52+
} else if(j == 0) {
53+
type = containerType.type;
54+
}
55+
}
56+
arrayType = new DataType(type, true);
57+
}
58+
59+
public String toString() {
60+
String s = "ARRAY:{";
61+
for(int i = 0; i < indexes.size()-1; i++) s += indexes.get(i) + ",";
62+
if(indexes.size() > 0) s += indexes.get(indexes.size()-1);
63+
return s + "}";
64+
}
65+
}

0 commit comments

Comments
 (0)