improved package names, split demos

This commit is contained in:
Parker TenBroeck 2025-05-05 12:30:33 -04:00
parent ecb18b417e
commit a577a825f8
27 changed files with 350 additions and 272 deletions

View file

@ -16,6 +16,21 @@ java {
}
}
application {
mainClass = "demo.Main"
tasks.withType<JavaCompile> {
options.compilerArgs.add("--enable-preview")
}
tasks.register<JavaExec>("lexer"){
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
jvmArgs("--enable-preview")
group = "Demos"
mainClass = "lexer.Main"
classpath = sourceSets["main"].runtimeClasspath
}
tasks.register<JavaExec>("sockets"){
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
group = "Demos"
mainClass = "sockets.Main"
classpath = sourceSets["main"].runtimeClasspath
}

View file

@ -1,67 +0,0 @@
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();
}
}
}
}

View file

@ -1,69 +0,0 @@
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);
}
}
}
}

View file

@ -1,66 +0,0 @@
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);
// }
// }
}

View file

@ -0,0 +1,69 @@
package lexer;
import com.parkertenbroeck.generator.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 char c when Character.isWhitespace(c) -> {}
case char 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 char 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);
}
}
}
}

View file

@ -0,0 +1,20 @@
package lexer;
import com.parkertenbroeck.generator.Gen;
import com.parkertenbroeck.bcms.RT;
import com.parkertenbroeck.bcms.loadtime.StateMachineClassLoader;
public class Main {
public static void main(String[] args) {
RT.runWithStateMachines(StateMachineClassLoader.Config.builtin(), (Object) args);
lexer();
}
static 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);
}
}
}

View file

@ -0,0 +1,21 @@
package sockets;
import com.parkertenbroeck.async_runtime.Jokio;
import com.parkertenbroeck.bcms.RT;
import com.parkertenbroeck.bcms.loadtime.StateMachineClassLoader;
public class Main {
public static void main(String[] args) {
RT.runWithStateMachines(StateMachineClassLoader.Config.builtin(), (Object) args);
await();
}
static void await(){
try{
new Jokio().blocking(Sockets.run());
}catch (Exception e){
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,87 @@
package sockets;
import com.parkertenbroeck.async_runtime.Delay;
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.bcms.loadtime.future.Cancellation;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class Sockets {
private static long bytes_sent = 0;
private static long bytes_received = 0;
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*2; c ++)
builder.append((char)((Math.random()*('z'-'a')+'a')));
Jokio.runtime().await().spawn(verify_echo(builder.toString()));
}
stats().await();
return Future.ret();
}
public static Future<Void, RuntimeException> stats() {
long start = System.currentTimeMillis();
long start_s = bytes_sent;
long start_r = bytes_received;
while(true){
Delay.delay(1000).await();
long end = System.currentTimeMillis();
var duration = (end-start)/1000.0;
start = end;
System.out.format("sent: % 10d B\treceived: % 10d B\tduration: % 2.3fs\n", bytes_sent, bytes_received, duration);
System.out.format("sent: % 2.5f Gib/s\treceived: % 2.5f Gib/s\n", (bytes_sent-start_s)/duration*7.4505805969238E-9, (bytes_received-start_r)/duration*7.4505805969238E-9);
start_s = bytes_sent;
start_r = bytes_received;
}
}
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*2);
while(true){
bytes_received = socket.read(buffer).await() + bytes_received;
buffer.flip();
bytes_sent = socket.write_all(buffer).await() + bytes_sent;
buffer.clear().limit(buffer.capacity());
}
}
}
public static Future<Void, IOException> verify_echo(String message) throws IOException {
byte[] msg_bytes = message.getBytes(StandardCharsets.UTF_8);
ByteBuffer msg_cmp_buffer = ByteBuffer.wrap(msg_bytes);
var buffer = ByteBuffer.allocate(message.length());
buffer.put(msg_bytes).limit(msg_bytes.length).position(0);
try(@Cancellation("close") var socket = Socket.connect(new InetSocketAddress("localhost", 42069)).await()){
while(true){
bytes_sent = socket.write_all(buffer).await() + bytes_sent;
buffer.flip();
bytes_received = socket.read_all(buffer).await() + bytes_received;
buffer.flip();
if(!buffer.equals(msg_cmp_buffer))
throw new RuntimeException();
}
}
}
}