mirror of
https://github.com/ParkerTenBroeck/coroutines.git
synced 2026-06-06 21:00:35 -04:00
migrated to gradle
This commit is contained in:
parent
0dd6fb237d
commit
ecb18b417e
43 changed files with 619 additions and 177 deletions
21
app/build.gradle.kts
Normal file
21
app/build.gradle.kts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
plugins {
|
||||
application
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":lib"))
|
||||
}
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(24)
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
mainClass = "demo.Main"
|
||||
}
|
||||
67
app/src/main/java/demo/AsyncExamples.java
Normal file
67
app/src/main/java/demo/AsyncExamples.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package demo;
|
||||
|
||||
import com.parkertenbroeck.async_runtime.Jokio;
|
||||
import com.parkertenbroeck.async_runtime.io.net.ServerSocket;
|
||||
import com.parkertenbroeck.async_runtime.io.net.Socket;
|
||||
import com.parkertenbroeck.future.Future;
|
||||
import com.parkertenbroeck.generators.loadtime.future.Cancellation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class AsyncExamples {
|
||||
|
||||
public static Future<Void, RuntimeException> run() throws IOException {
|
||||
Jokio.runtime().await().spawn(server());
|
||||
for(int i = 0; i < 50; i ++){
|
||||
var builder = new StringBuilder();
|
||||
for(int c = 0; c < 4096; c ++)
|
||||
builder.append((char)((Math.random()*('z'-'a')+'a')));
|
||||
Jokio.runtime().await().spawn(verify_echo(builder.toString()));
|
||||
}
|
||||
Runnable meow = () -> {
|
||||
System.out.println();
|
||||
};
|
||||
meow.run();
|
||||
|
||||
return Future.ret();
|
||||
}
|
||||
|
||||
public static Future<Void, IOException> server() throws IOException {
|
||||
try(@Cancellation("close") var ss = ServerSocket.bind(new InetSocketAddress("0.0.0.0", 42069))){
|
||||
while (true){
|
||||
Jokio.runtime().await().spawn(echo(ss.accept().await()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Future<Void, IOException> echo(@Cancellation("close") Socket socket) throws IOException{
|
||||
try(socket){
|
||||
var buffer = ByteBuffer.allocate(4096);
|
||||
while(true){
|
||||
socket.read(buffer).await();
|
||||
buffer.flip();
|
||||
socket.write_all(buffer).await();
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Future<Void, IOException> verify_echo(String message) throws IOException {
|
||||
byte[] msg_bytes = message.getBytes(StandardCharsets.UTF_8);
|
||||
try(@Cancellation("close") var socket = Socket.connect(new InetSocketAddress("localhost", 42069)).await()){
|
||||
var buffer = ByteBuffer.allocate(message.length());
|
||||
while(true){
|
||||
buffer.limit(message.length()).put(msg_bytes).position(0);
|
||||
var wrote = socket.write_all(buffer).await();
|
||||
buffer.clear().limit(wrote);
|
||||
socket.read_all(buffer).await();
|
||||
if(!buffer.position(0).equals(ByteBuffer.wrap(msg_bytes)))
|
||||
throw new RuntimeException();
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
69
app/src/main/java/demo/Lexer.java
Normal file
69
app/src/main/java/demo/Lexer.java
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package demo;
|
||||
|
||||
import com.parkertenbroeck.gen.Gen;
|
||||
|
||||
public class Lexer {
|
||||
public sealed interface Token{}
|
||||
public enum Punc implements Token {
|
||||
LPar,
|
||||
RPar,
|
||||
Add,
|
||||
Sub,
|
||||
Div,
|
||||
Mul,
|
||||
Equals,
|
||||
Carrot,
|
||||
Comma
|
||||
}
|
||||
|
||||
public record Ident(String ident) implements Token {}
|
||||
public record Numeric(double val) implements Token {}
|
||||
|
||||
public static Gen<Token, Void> parse(String input) {
|
||||
int start;
|
||||
int pos = 0;
|
||||
while(true){
|
||||
if (input.length() <= pos)
|
||||
return Gen.ret();
|
||||
start = pos;
|
||||
char curr = input.charAt(pos++);
|
||||
|
||||
switch (curr){
|
||||
case '(' -> Gen.yield(Punc.LPar);
|
||||
case ')' -> Gen.yield(Punc.RPar);
|
||||
case '+' -> Gen.yield(Punc.Add);
|
||||
case '-' -> Gen.yield(Punc.Sub);
|
||||
case '/' -> Gen.yield(Punc.Div);
|
||||
case '*' -> Gen.yield(Punc.Mul);
|
||||
case ',' -> Gen.yield(Punc.Comma);
|
||||
case '^' -> Gen.yield(Punc.Carrot);
|
||||
case '=' -> Gen.yield(Punc.Equals);
|
||||
// case Character c when Character.isWhitespace(c) -> {}
|
||||
// case Character c when Character.isAlphabetic(c) -> {
|
||||
// while((Character.isLetter(curr) || Character.isLetterOrDigit(curr))){
|
||||
// if(pos>=input.length())break;
|
||||
// curr = input.charAt(pos++);
|
||||
// }
|
||||
// Gen.yield(new Ident(input.substring(start, pos-1)));
|
||||
// }
|
||||
// case Character c when '0' <= c && c <= '9' -> {
|
||||
// boolean exp = false;
|
||||
// while(('0' <= curr && curr <= '9') || curr == '_' || curr == '.'){
|
||||
// if(pos>=input.length())break;
|
||||
// curr = input.charAt(pos++);
|
||||
// if(curr=='e'||curr=='E'){
|
||||
// if(exp) throw new RuntimeException("Exponent Already included");
|
||||
// exp = true;
|
||||
// if(pos+2>=input.length())
|
||||
// throw new RuntimeException("Expected another character after exponent");
|
||||
// curr = input.charAt(++pos); pos++;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Gen.yield(new Numeric(Double.parseDouble(input.substring(start, pos-1).replace("_", ""))));
|
||||
// }
|
||||
default -> throw new RuntimeException("Invalid char " + curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
66
app/src/main/java/demo/Main.java
Normal file
66
app/src/main/java/demo/Main.java
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package demo;
|
||||
|
||||
import com.parkertenbroeck.async_runtime.Jokio;
|
||||
import com.parkertenbroeck.async_runtime.io.fs.File;
|
||||
import com.parkertenbroeck.generators.RT;
|
||||
import com.parkertenbroeck.future.Future;
|
||||
import com.parkertenbroeck.gen.Gen;
|
||||
import com.parkertenbroeck.generators.loadtime.future.Cancellation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class Main implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
RT.runWithGeneratorSupport(Main.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// lexer();
|
||||
await();
|
||||
// try {
|
||||
// System.out.println(new Jokio().blocking(files()));
|
||||
// } catch (IOException ignore) {}
|
||||
}
|
||||
//
|
||||
// static Future<String, IOException> files() throws IOException{
|
||||
// try(@Cancellation("close") var file = File.open(Path.of("./src/Main.java"))){
|
||||
// var buf = ByteBuffer.allocate((int) file.size());
|
||||
// var read = file.read_all(buf).await();
|
||||
// return Future.ret(new String(buf.array(), 0, read));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// <T, E extends Throwable> T async_lambda(Supplier<Future<T, E>> lambda) throws E{
|
||||
// return new Jokio().blocking(lambda.get());
|
||||
// }
|
||||
//
|
||||
//
|
||||
private static int nya = 0;
|
||||
class Meow{
|
||||
void test(){
|
||||
nya = 2;
|
||||
}
|
||||
}
|
||||
void await(){
|
||||
|
||||
new Meow().test();
|
||||
try{
|
||||
new Jokio().blocking(AsyncExamples.run());
|
||||
}catch (Exception e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
//
|
||||
//
|
||||
// void lexer(){
|
||||
// var gen = Lexer.parse("f7(x,y,z,w, u,v, othersIg) = v-(x*y+y+ln(z)^2*sin(z*pi/2))/(w*u)+sqrt(othersIg*120e-1)");
|
||||
// while(gen.next() instanceof Gen.Yield(var tok)) {
|
||||
// System.out.println(tok);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue