Skip to content

Commit 6c05bd7

Browse files
committed
ast: Implement await for a thread handle
1 parent 7039c7c commit 6c05bd7

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/main/java/org/piccode/ast/DotOperationAst.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.piccode.ast;
22

3+
import java.util.concurrent.ExecutionException;
34
import org.piccode.piccodescript.TargetEnvironment;
45
import org.piccode.rt.Context;
56
import org.piccode.rt.PiccodeArray;
@@ -38,11 +39,11 @@ public PiccodeValue execute() {
3839
var mod = Context.modules.get(id.text);
3940

4041
if (!(rhs instanceof CallAst) && !(rhs instanceof IdentifierAst)) {
41-
throw new PiccodeException(file, line, column,"No node " + rhs + " found in module " + id.text);
42+
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + id.text);
4243
}
4344

4445
return process(id, mod);
45-
}
46+
}
4647

4748
var left = lhs.execute();
4849

@@ -52,28 +53,39 @@ public PiccodeValue execute() {
5253

5354
if (left instanceof PiccodeString str && rhs instanceof IdentifierAst id && id.text.equals("len")) {
5455
return new PiccodeNumber("" + str.toString().length());
55-
}
56-
57-
else if (left instanceof PiccodeArray arr){
56+
} else if (left instanceof PiccodeArray arr) {
5857
return processArrayIndexing(arr.array(), rhs.execute());
59-
}
60-
else if (left instanceof PiccodeTuple tupl){
58+
} else if (left instanceof PiccodeTuple tupl) {
6159
return processArrayIndexing(tupl.array(), rhs.execute());
6260
}
63-
61+
6462
if (left instanceof PiccodeModule mod) {
6563
return process(new IdentifierAst(mod.name), mod);
6664
}
6765

6866
if (!(left instanceof PiccodeObject)) {
69-
var err = new PiccodeException(file, line, column,"Invalid expression on the side of `.` : " + lhs + " has value " + left + " which is not an object");
67+
var err = new PiccodeException(file, line, column, "Invalid expression on the side of `.` : " + lhs + " has value " + left + " which is not an object");
7068
err.addNote(new PiccodeSimpleNote("Pehaphs consider adding a check to verify if " + lhs + " is indeed an object."));
7169
throw err;
7270
}
7371

7472
var obj = (PiccodeObject) left;
7573

7674
String key = null;
75+
if (rhs instanceof IdentifierAst id
76+
&& id.text.equals("await")
77+
&& obj.obj.containsKey("uuid")
78+
&& obj.obj.containsKey("future")) {
79+
var uuid = obj.obj.get("uuid").raw().toString();
80+
var future = Context.top.getFuture(uuid);
81+
Context.top.removeFuture(uuid);
82+
try {
83+
var result = future.get();
84+
return result;
85+
} catch (InterruptedException | ExecutionException ex) {
86+
throw new PiccodeException(file, line, column, "Internal error: " + ex.getMessage());
87+
}
88+
}
7789
if (rhs instanceof IdentifierAst id) {
7890
key = id.text;
7991
} else {
@@ -82,7 +94,7 @@ else if (left instanceof PiccodeTuple tupl){
8294

8395
var value = obj.getValue(key);
8496
if (value == null) {
85-
throw new PiccodeException(file, line, column,"Field `" + key + "` is not part of " + obj.raw());
97+
throw new PiccodeException(file, line, column, "Field `" + key + "` is not part of " + obj.raw());
8698
}
8799

88100
return value;
@@ -114,13 +126,13 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod) {
114126
}
115127
}
116128

117-
throw new PiccodeException(file, line, column,"No function or identifier " + _id.text + " found in module " + id.text);
129+
throw new PiccodeException(file, line, column, "No function or identifier " + _id.text + " found in module " + id.text);
118130
}
119131

120132
var call = (CallAst) rhs;
121133

122134
if (!(call.expr instanceof IdentifierAst)) {
123-
throw new PiccodeException(file, line, column,"Invalid function reference in module access module " + id.text + ": " + call.expr);
135+
throw new PiccodeException(file, line, column, "Invalid function reference in module access module " + id.text + ": " + call.expr);
124136
}
125137

126138
var _id = (IdentifierAst) call.expr;
@@ -139,29 +151,30 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod) {
139151
}
140152
}
141153

142-
throw new PiccodeException(file, line, column,"No function or identifier " + _id.text + " found in module " + id.text);
154+
throw new PiccodeException(file, line, column, "No function or identifier " + _id.text + " found in module " + id.text);
143155
}
144156

145157
private PiccodeValue processArrayIndexing(PiccodeValue[] arr, PiccodeValue execute) {
146158
if (!(execute instanceof PiccodeNumber)) {
147-
throw new PiccodeException(file, line, column,"Attempt to index array value with non numeric index: " + rhs + " which evaluates to " + execute + " is used as an index");
159+
throw new PiccodeException(file, line, column, "Attempt to index array value with non numeric index: " + rhs + " which evaluates to " + execute + " is used as an index");
148160
}
149161

150162
int index = (int) ((double) execute.raw());
151163
if (index < 0 || index >= arr.length) {
152-
throw new PiccodeException(file, line, column,"Array index out of bounds: "+ lhs + " evaluates to an array with size" + arr.length + " which is indexed with " + execute);
164+
throw new PiccodeException(file, line, column, "Array index out of bounds: " + lhs + " evaluates to an array with size" + arr.length + " which is indexed with " + execute);
153165
}
154166

155167
return arr[index];
156168
}
169+
157170
private PiccodeValue processTupleIndexing(PiccodeValue[] arr, PiccodeValue execute) {
158171
if (!(execute instanceof PiccodeNumber)) {
159-
throw new PiccodeException(file, line, column,"Attempt to index a tuple value with non numeric index: " + rhs + " which evaluates to " + execute + " is used as an index");
172+
throw new PiccodeException(file, line, column, "Attempt to index a tuple value with non numeric index: " + rhs + " which evaluates to " + execute + " is used as an index");
160173
}
161174

162175
int index = (int) ((double) execute.raw());
163176
if (index < 0 || index >= arr.length) {
164-
throw new PiccodeException(file, line, column,"Array index out of bounds: "+ lhs + " evaluates to a tuple with size" + arr.length + " which is indexed with " + execute);
177+
throw new PiccodeException(file, line, column, "Array index out of bounds: " + lhs + " evaluates to a tuple with size" + arr.length + " which is indexed with " + execute);
165178
}
166179

167180
return arr[index];

0 commit comments

Comments
 (0)