scarpet corountines idea

This commit is contained in:
gnembon committed 2023-02-22 02:33:03 +01:00
1 parent 23ed35a398
commit 146573803c
11 files changed
+284 -17

No files matched your search

@@ -143,6 +143,9 @@ public class Loops
for (int i = 0; iterator.hasNext(); i++)
{
Value next = iterator.next();
if(next == Value.EOL) {
continue;
}
String variable = next.boundVariable;
next.bindTo("_");
int doYouReally = i;
@@ -197,6 +200,9 @@ public class Loops
for (int i = 0; iterator.hasNext(); i++)
{
Value next = iterator.next();
if(next == Value.EOL) {
continue;
}
String veriable = next.boundVariable;
next.bindTo("_");
int seriously = i;
@@ -254,6 +260,9 @@ public class Loops
for (int i = 0; iterator.hasNext(); i++)
{
Value next = iterator.next();
if(next == Value.EOL) {
continue;
}
String variable = next.boundVariable;
next.bindTo("_");
int seriously = i;
@@ -311,6 +320,9 @@ public class Loops
for (int i = 0; iterator.hasNext(); i++)
{
Value next = iterator.next();
if(next == Value.EOL) {
continue;
}
String variable = next.boundVariable;
next.bindTo("_");
int seriously = i;
@@ -381,6 +393,9 @@ public class Loops
for (int i = 0; iterator.hasNext(); i++)
{
Value next = iterator.next();
if(next == Value.EOL) {
continue;
}
String variable = next.boundVariable;
next.bindTo("_");
int seriously = i;
@@ -452,6 +467,9 @@ public class Loops
for (int i = 0; iterator.hasNext(); i++)
{
Value next = iterator.next();
if(next == Value.EOL) {
continue;
}
String variable = next.boundVariable;
next.bindTo("_");
Value promiseWontChangeYou = acc;
@@ -1,5 +1,6 @@
package carpet.script.language;
import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.argument.FunctionArgument;
import carpet.script.exception.ExitStatement;
@@ -128,5 +129,63 @@ public class Threading
}
return (cc, tt) -> new NumericValue(time); // pass through for variables
});
expression.addLazyFunction("yield", (c, t, lv) ->
{
if (c.getThreadContext() == null)
{
throw new InternalExpressionException("'yield' can only be used in a task");
}
if (lv.isEmpty())
{
throw new InternalExpressionException("'yield' requires at least one argument");
}
boolean lock = lv.size() > 1 && lv.get(1).evalValue(c, Context.BOOLEAN).getBoolean();
Value value = lv.get(0).evalValue(c);
Value ret = c.getThreadContext().ping(value, lock);
return (cc, tt) -> ret;
});
expression.addLazyFunction("task_send", 2, (c, t, lv) ->
{
Value threadValue = lv.get(0).evalValue(c);
if (!(threadValue instanceof ThreadValue thread))
{
throw new InternalExpressionException("'task_next' requires a task value");
}
if (!thread.isCoroutine)
{
throw new InternalExpressionException("'task_next' requires a coroutine task value");
}
Value ret = lv.get(1).evalValue(c);
thread.send(ret);
return (cc, tt) -> Value.NULL;
});
expression.addLazyFunction("task_await", 1, (c, t, lv) ->
{
Value threadValue = lv.get(0).evalValue(c);
if (!(threadValue instanceof ThreadValue thread))
{
throw new InternalExpressionException("'task_await' requires a task value");
}
if (!thread.isCoroutine)
{
throw new InternalExpressionException("'task_await' requires a coroutine task value");
}
Value ret = thread.next();
return ret == Value.EOL ? ((cc, tt) -> Value.NULL) : ((cc, tt) -> ret);
});
expression.addLazyFunction("task_ready", 1, (c, t, lv) ->
{
Value threadValue = lv.get(0).evalValue(c);
if (!(threadValue instanceof ThreadValue thread))
{
throw new InternalExpressionException("'task_ready' requires a task value");
}
boolean ret = thread.isCoroutine && thread.hasNext();
return (cc, tt) -> BooleanValue.of(ret);
});
}
}