11package org .piccode .rt .modules ;
22
3+ import java .util .ArrayList ;
34import java .util .HashMap ;
45import java .util .List ;
6+ import java .util .Map ;
7+ import org .piccode .ast .Ast ;
8+ import org .piccode .ast .CallAst ;
9+ import org .piccode .ast .ClosureAst ;
10+ import org .piccode .ast .IdentifierAst ;
11+ import org .piccode .ast .UnitAst ;
512import org .piccode .rt .Context ;
613import org .piccode .rt .PiccodeClosure ;
714import org .piccode .rt .PiccodeException ;
1118import org .piccode .rt .PiccodeValue ;
1219import org .piccode .rt .PiccodeValue .Type ;
1320
14-
1521/**
1622 *
1723 * @author hexaredecimal
1824 */
1925public class PiccodeVirtualModule {
26+
2027 public static void addFunctions () {
21-
28+
29+ Context .addAnnotation ("Virtual" , (frame , node ) -> {
30+ var ctx = frame == null
31+ ? Context .top
32+ : Context .getContextAt (frame );
33+
34+ var top = ctx .getTopFrame ().caller ;
35+ var ignored = Ast .finalizeNode (new UnitAst (), top );
36+
37+ Map <String , PiccodeValue > newArgs = new HashMap <>();
38+ var args = new ArrayList <Ast >();
39+ args .add (ignored );
40+
41+ var result = new PiccodeClosure (args , newArgs , 0 , node );
42+ var id = Context .makeThread (result );
43+
44+ var obj = new HashMap <String , PiccodeValue >();
45+ obj .put ("uuid" , new PiccodeString (id ));
46+ return new PiccodeObject (obj );
47+ });
48+
2249 NativeFunctionFactory .create ("task" , List .of ("fx" ), (args , namedArgs , frame ) -> {
23- var ctx = frame == null ?
24- Context .top
25- : Context .getContextAt (frame );
26-
27- var scope = ctx .getTopFrame ();
28-
29- var fx = namedArgs .get ("fx" );
30- PiccodeValue .verifyType (scope .caller , fx , Type .CLOSURE );
31-
32- var closure = (PiccodeClosure ) fx ;
33- scope = closure .frame == null ?
34- scope
35- : Context .getContextAt (closure .frame ).getTopFrame ();
36-
37- var id = Context .makeThread (closure );
38- var obj = new HashMap <String , PiccodeValue >();
39- obj .put ("uuid" , new PiccodeString (id ));
40- return new PiccodeObject (obj );
50+ var ctx = frame == null
51+ ? Context .top
52+ : Context .getContextAt (frame );
53+
54+ var scope = ctx .getTopFrame ();
55+
56+ var fx = namedArgs .get ("fx" );
57+ PiccodeValue .verifyType (scope .caller , fx , Type .CLOSURE );
58+
59+ var closure = (PiccodeClosure ) fx ;
60+ scope = closure .frame == null
61+ ? scope
62+ : Context .getContextAt (closure .frame ).getTopFrame ();
63+
64+ var id = Context .makeThread (closure );
65+ var obj = new HashMap <String , PiccodeValue >();
66+ obj .put ("uuid" , new PiccodeString (id ));
67+ return new PiccodeObject (obj );
4168 }, null );
42-
69+
4370 NativeFunctionFactory .create ("sleep" , List .of ("ms" ), (args , namedArgs , frame ) -> {
44- var ctx = frame == null ?
45- Context .top
46- : Context .getContextAt (frame );
71+ var ctx = frame == null
72+ ? Context .top
73+ : Context .getContextAt (frame );
4774
48- var scope = ctx .getTopFrame ();
49- var ms = namedArgs .get ("ms" );
50- PiccodeValue .verifyType (scope .caller , ms , Type .NUMBER );
75+ var scope = ctx .getTopFrame ();
76+ var ms = namedArgs .get ("ms" );
77+ PiccodeValue .verifyType (scope .caller , ms , Type .NUMBER );
5178
5279 try {
5380 var millisec = (long ) (double ) ms .raw ();
@@ -56,9 +83,9 @@ public static void addFunctions() {
5683 var node = scope .caller ;
5784 throw new PiccodeException (node .file , node .line , node .column , "Internal Error: " + ex .getMessage ());
5885 }
59-
60- return new PiccodeUnit ();
86+
87+ return new PiccodeUnit ();
6188 }, null );
62-
89+
6390 }
6491}
0 commit comments