Skip to content

Commit 7039c7c

Browse files
committed
rt: Fixed the scope issues and added 2 new modules
1 parent 68076ba commit 7039c7c

File tree

4 files changed

+112
-33
lines changed

4 files changed

+112
-33
lines changed

src/main/java/org/piccode/rt/Context.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import java.util.HashMap;
44
import java.util.List;
55
import java.util.Stack;
6+
import java.util.UUID;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.Future;
610
import org.piccode.ast.Ast;
711

812
/**
@@ -16,16 +20,46 @@ public class Context {
1620
public static Context top = new Context();
1721
public static HashMap<String, PiccodeModule> modules = new HashMap<>();
1822
private HashMap<String, List<Ast>> import_cache = new HashMap<>();
23+
private ExecutorService threadPool;
24+
private HashMap<String, Future<PiccodeValue>> futureMap;
1925

2026
public Context() {
2127
call_frames = new Stack<>();
2228
import_cache = new HashMap<>();
29+
threadPool = Executors.newVirtualThreadPerTaskExecutor();
30+
futureMap = new HashMap<>();
2331
}
2432

2533
public Stack<StackFrame> getCallStack() {
2634
return call_frames;
2735
}
2836

37+
public int getFramesCount() {
38+
return call_frames.size();
39+
}
40+
41+
public String makeThread(PiccodeClosure node) {
42+
var future = threadPool.submit(() -> {
43+
var call = (PiccodeClosure) node.call(new PiccodeUnit());
44+
return call.evaluateIfReady();
45+
});
46+
var id = UUID.randomUUID().toString();
47+
futureMap.put(id, future);
48+
return id;
49+
}
50+
51+
public Future<PiccodeValue> getFuture(String id) {
52+
return futureMap.get(id);
53+
}
54+
55+
public void removeFuture(String uuid) {
56+
futureMap.remove(uuid);
57+
}
58+
59+
public StackFrame getTopFrame() {
60+
return call_frames.peek();
61+
}
62+
2963
public static void addGlobal(String name, PiccodeValue value) {
3064
global_scope.put(name, value);
3165
}
@@ -52,7 +86,13 @@ public void dropScope() {
5286
}
5387

5488
public void pushStackFrame(Ast top) {
55-
call_frames.push(new StackFrame(top));
89+
if (call_frames.isEmpty()) {
90+
call_frames.push(new StackFrame(top));
91+
return;
92+
}
93+
94+
var prev = call_frames.peek();
95+
call_frames.push(new StackFrame(top, prev));
5696
}
5797

5898
public void dropStackFrame() {
@@ -109,4 +149,5 @@ public String getSimilarName(String id) {
109149
}
110150
return result;
111151
}
152+
112153
}

src/main/java/org/piccode/rt/StackFrame.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public StackFrame(Ast caller) {
2020
this.scope_stack = new Stack<>();
2121
}
2222

23+
public StackFrame(Ast caller, StackFrame from) {
24+
this.caller = caller;
25+
this.scope_stack = new Stack<>();
26+
scope_stack.addAll(from.scope_stack);
27+
}
28+
29+
2330
public void addScope() {
2431
if (!scope_stack.isEmpty()) {
2532
var _top = scope_stack.peek();

src/main/java/org/piccode/rt/modules/PiccodeAsyncModule.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.piccode.rt.modules;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import org.piccode.rt.Context;
8+
import org.piccode.rt.PiccodeArray;
9+
import org.piccode.rt.PiccodeClosure;
10+
import org.piccode.rt.PiccodeException;
11+
import org.piccode.rt.PiccodeNumber;
12+
import org.piccode.rt.PiccodeObject;
13+
import org.piccode.rt.PiccodeString;
14+
import org.piccode.rt.PiccodeTuple;
15+
import org.piccode.rt.PiccodeUnit;
16+
import org.piccode.rt.PiccodeValue;
17+
18+
19+
/**
20+
*
21+
* @author hexaredecimal
22+
*/
23+
public class PiccodeVirtualModule {
24+
public static void addFunctions() {
25+
26+
NativeFunctionFactory.create("task", List.of("fx"), (args, namedArgs) -> {
27+
var scope = Context.top.getTopFrame();
28+
var fx = namedArgs.get("fx");
29+
30+
if (!(fx instanceof PiccodeClosure)) {
31+
var node = scope.caller;
32+
throw new PiccodeException(node.file, node.line, node.column, "Invalid value passed. Expected a closure but found " + fx);
33+
}
34+
var id = Context.top.makeThread((PiccodeClosure) fx);
35+
var future = Context.top.getFuture(id);
36+
var obj = new HashMap<String, PiccodeValue>();
37+
obj.put("uuid", new PiccodeString(id));
38+
obj.put("future", new PiccodeNumber(future.hashCode()));
39+
return new PiccodeObject(obj);
40+
});
41+
42+
NativeFunctionFactory.create("sleep", List.of("ms"), (args, namedArgs) -> {
43+
var scope = Context.top.getTopFrame();
44+
var ms = namedArgs.get("ms");
45+
46+
if (!(ms instanceof PiccodeNumber)) {
47+
var node = scope.caller;
48+
throw new PiccodeException(node.file, node.line, node.column, "Invalid value passed. Expected a number but found " + ms.type());
49+
}
50+
51+
try {
52+
var millisec = (long) (double) ms.raw();
53+
Thread.sleep(millisec);
54+
} catch (InterruptedException ex) {
55+
var node = scope.caller;
56+
throw new PiccodeException(node.file, node.line, node.column, "Internal Error: " + ex.getMessage());
57+
}
58+
59+
return new PiccodeUnit();
60+
});
61+
62+
}
63+
}

0 commit comments

Comments
 (0)