File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88 */
99public final class StringValue implements Value {
1010
11+ public static final StringValue EMPTY = new StringValue ("" );
12+
1113 private final String value ;
1214
1315 public StringValue (String value ) {
Original file line number Diff line number Diff line change 1+ package com .annimon .ownlang .lib .modules .functions ;
2+
3+ import com .annimon .ownlang .exceptions .ArgumentsMismatchException ;
4+ import com .annimon .ownlang .lib .*;
5+
6+ public final class robot_exec implements Function {
7+
8+ public static enum Mode { EXEC , EXEC_AND_WAIT };
9+
10+ private final Mode mode ;
11+
12+ public robot_exec (Mode mode ) {
13+ this .mode = mode ;
14+ }
15+
16+ @ Override
17+ public Value execute (Value ... args ) {
18+ if (args .length == 0 ) throw new ArgumentsMismatchException ("At least one argument expected" );
19+
20+ try {
21+ final Process process ;
22+ if (args .length > 1 ) {
23+ process = Runtime .getRuntime ().exec (toStringArray (args ));
24+ } else switch (args [0 ].type ()) {
25+ case Types .ARRAY :
26+ final ArrayValue array = (ArrayValue ) args [0 ];
27+ process = Runtime .getRuntime ().exec (toStringArray (array .getCopyElements ()));
28+ break ;
29+
30+ default :
31+ process = Runtime .getRuntime ().exec (args [0 ].asString ());
32+ }
33+
34+ switch (mode ) {
35+ case EXEC_AND_WAIT :
36+ return new NumberValue (process .waitFor ());
37+ case EXEC :
38+ default :
39+ return NumberValue .ZERO ;
40+ }
41+ } catch (Exception ex ) {
42+ ex .printStackTrace ();
43+ return NumberValue .ZERO ;
44+ }
45+ }
46+
47+ private static String [] toStringArray (Value [] values ) {
48+ final String [] strings = new String [values .length ];
49+ for (int i = 0 ; i < values .length ; i ++) {
50+ strings [i ] = values [i ].asString ();
51+ }
52+ return strings ;
53+ }
54+ }
Original file line number Diff line number Diff line change 1+ package com .annimon .ownlang .lib .modules .functions ;
2+
3+ import com .annimon .ownlang .lib .Function ;
4+ import com .annimon .ownlang .lib .StringValue ;
5+ import com .annimon .ownlang .lib .Value ;
6+ import java .awt .Toolkit ;
7+ import java .awt .datatransfer .DataFlavor ;
8+
9+ public final class robot_fromclipboard implements Function {
10+
11+ @ Override
12+ public Value execute (Value ... args ) {
13+ try {
14+ Object data = Toolkit .getDefaultToolkit ().getSystemClipboard ()
15+ .getData (DataFlavor .stringFlavor );
16+ return new StringValue (data .toString ());
17+ } catch (Exception ex ) {
18+ return StringValue .EMPTY ;
19+ }
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package com .annimon .ownlang .lib .modules .functions ;
2+
3+ import com .annimon .ownlang .exceptions .ArgumentsMismatchException ;
4+ import com .annimon .ownlang .lib .Function ;
5+ import com .annimon .ownlang .lib .NumberValue ;
6+ import com .annimon .ownlang .lib .Value ;
7+ import java .awt .Toolkit ;
8+ import java .awt .datatransfer .StringSelection ;
9+
10+ public final class robot_toclipboard implements Function {
11+
12+ @ Override
13+ public Value execute (Value ... args ) {
14+ if (args .length != 1 ) throw new ArgumentsMismatchException ("One argument expected" );
15+
16+ Toolkit .getDefaultToolkit ().getSystemClipboard ()
17+ .setContents (new StringSelection (args [0 ].asString ()), null );
18+ return NumberValue .ZERO ;
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .annimon .ownlang .lib .modules .functions ;
2+
3+ import com .annimon .ownlang .lib .*;
4+ import java .util .Scanner ;
5+
6+ public final class std_readln implements Function {
7+
8+ @ Override
9+ public Value execute (Value ... args ) {
10+ try (Scanner sc = new Scanner (System .in )) {
11+ return new StringValue (sc .nextLine ());
12+ }
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .annimon .ownlang .lib .modules .functions ;
2+
3+ import com .annimon .ownlang .lib .Function ;
4+ import com .annimon .ownlang .lib .NumberValue ;
5+ import com .annimon .ownlang .lib .Value ;
6+
7+ public final class std_time implements Function {
8+
9+ @ Override
10+ public Value execute (Value ... args ) {
11+ return new NumberValue (System .currentTimeMillis ());
12+ }
13+ }
Original file line number Diff line number Diff line change 22
33import com .annimon .ownlang .exceptions .ArgumentsMismatchException ;
44import com .annimon .ownlang .lib .*;
5+ import com .annimon .ownlang .lib .modules .functions .robot_exec ;
6+ import com .annimon .ownlang .lib .modules .functions .robot_fromclipboard ;
7+ import com .annimon .ownlang .lib .modules .functions .robot_toclipboard ;
58import java .awt .AWTException ;
69import java .awt .Robot ;
710import java .awt .event .InputEvent ;
@@ -53,6 +56,10 @@ public void init() {
5356 } catch (IllegalArgumentException iae ) { }
5457 return NumberValue .ZERO ;
5558 });
59+ Functions .set ("toClipboard" , new robot_toclipboard ());
60+ Functions .set ("fromClipboard" , new robot_fromclipboard ());
61+ Functions .set ("execProcess" , new robot_exec (robot_exec .Mode .EXEC ));
62+ Functions .set ("execProcessAndWait" , new robot_exec (robot_exec .Mode .EXEC_AND_WAIT ));
5663
5764 Variables .set ("VK_DOWN" , new NumberValue (KeyEvent .VK_DOWN ));
5865 Variables .set ("VK_LEFT" , new NumberValue (KeyEvent .VK_LEFT ));
Original file line number Diff line number Diff line change @@ -12,10 +12,12 @@ public final class std implements Module {
1212 @ Override
1313 public void init () {
1414 Functions .set ("echo" , new std_echo ());
15+ Functions .set ("readln" , new std_readln ());
1516 Functions .set ("newarray" , new std_newarray ());
1617 Functions .set ("sort" , new std_sort ());
1718 Functions .set ("length" , new std_length ());
1819 Functions .set ("rand" , new std_rand ());
20+ Functions .set ("time" , new std_time ());
1921 Functions .set ("sleep" , new std_sleep ());
2022 Functions .set ("thread" , new std_thread ());
2123
Original file line number Diff line number Diff line change @@ -22,7 +22,9 @@ public void execute() {
2222 final String moduleName = expression .eval ().asString ();
2323 final Module module = (Module ) Class .forName (PACKAGE + moduleName ).newInstance ();
2424 module .init ();
25- } catch (Exception ex ) { }
25+ } catch (Exception ex ) {
26+ throw new RuntimeException (ex );
27+ }
2628 }
2729
2830 @ Override
You can’t perform that action at this time.
0 commit comments