~cloutier/principia

c39f613dc91803e9729949554679ba03be218fca — Vincent Cloutier 5 months ago d20d21b
work on SocketOpen
M cli/src/main.rs => cli/src/main.rs +3 -3
@@ 44,7 44,7 @@ async fn main() {
        ("stdin", Some(_)) => {
            io::stdin().read_to_string(&mut content).unwrap();
            
            kernelIn.send(content).unwrap();
            kernelIn.send((true,content)).unwrap();

            let result = kernelOut.recv().unwrap();
            println!("{}", result);


@@ 66,7 66,7 @@ async fn main() {

                    info!("file contains: \n{}", content);

                    kernelIn.send(content).unwrap();
                    kernelIn.send((true,content)).unwrap();

                    let result = kernelOut.recv().unwrap();
                    println!("{}", result);


@@ 86,7 86,7 @@ async fn main() {
                        match readline {
                            Ok(line) => {
                                rl.add_history_entry(line.as_str());
                                kernelIn.send(line.clone()).unwrap();
                                kernelIn.send((true,line.clone())).unwrap();

                                let result = kernelOut.recv().unwrap();
                                println!("{}", result);

M kernel/src/builtins.rs => kernel/src/builtins.rs +1 -25
@@ 47,7 47,7 @@ pub enum BuiltinResult {
    impure_res(Arc<ast::Node>, HashMap<String, ast::EnvNode>)
}

pub const BUILTINS: [Builtin; 63] = [
pub const BUILTINS: [Builtin; 62] = [
    Builtin {
        should_evaluate: &|ast, _| ast.head() == "CompoundExpression" && ast.children.len() > 0,
        is_pure: true,


@@ 1542,28 1542,4 @@ pub const BUILTINS: [Builtin; 63] = [
            BuiltinResult::pure_res(Arc::new(new_node))
        }
    },
    Builtin {
        should_evaluate: &|ast, _| ast.head() == "SocketOpen"
            && ast.children.len() <= 2,
        is_pure: false,
        reevaluate_output: false,
        func: &|ast, env| {
            use std::{
                io::{prelude::*, BufReader},
                net::{TcpListener, TcpStream},
            };

            let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
            for stream in listener.incoming() {
                let stream = stream.unwrap();

                break;
            }


            let new_node = ast::Node::new(ast::Item::Symbol("True".to_owned()));

            BuiltinResult::impure_res(Arc::new(new_node), env.clone())
        }
    },
];

M kernel/src/eval.rs => kernel/src/eval.rs +3 -0
@@ 49,6 49,9 @@ pub fn init_env<'a>() -> HashMap<String, ast::EnvNode> {
    let exampledata_wolfram = std::str::from_utf8(&Core::get("05-ExampleData.wl").unwrap().data).unwrap().to_string();
    let (_, env) = eval_ast(parse::parse_str(exampledata_wolfram).unwrap(), env);

    let cloud_wolfram = std::str::from_utf8(&Core::get("06-Cloud.wl").unwrap().data).unwrap().to_string();
    let (_, env) = eval_ast(parse::parse_str(cloud_wolfram).unwrap(), env);

    return env;
}


M kernel/src/lib.rs => kernel/src/lib.rs +21 -4
@@ 31,14 31,29 @@ mod ast;
mod wikidata;
mod builtins;

pub async fn start_kernel() -> (Sender<std::string::String>, Receiver<std::string::String>) {
    let (senderIn, receiverIn) = mpsc::channel::<std::string::String>();
pub async fn start_kernel() -> (Sender<(bool,std::string::String)>, Receiver<std::string::String>) {
    let (senderIn, receiverIn) = mpsc::channel::<(bool,std::string::String)>();
    let (senderOut, receiverOut) = mpsc::channel::<std::string::String>();

    let senderIn2 = senderIn.clone();
    thread::spawn(move || {
        let mut env = eval::init_env();

        for received in receiverIn {
        thread::spawn(move || {
            use std::{
                io::{prelude::*, BufReader},
                net::{TcpListener, TcpStream},
            };

            let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
            for stream in listener.incoming() {
                let stream = stream.unwrap();
                senderIn2.send((false,"$tmp = Lookup[$SocketListeners, 7878]; $tmp[]".to_owned())).unwrap();

            }
        });

        for (wantReturn, received) in receiverIn {
            let tree = match parse::parse_str(received) {
                Ok(x) => x,
                Err(e) => { 


@@ 53,7 68,9 @@ pub async fn start_kernel() -> (Sender<std::string::String>, Receiver<std::strin
                resString.push_str(&ast::ast_to_wolfram(result.clone()));
                resString += "\n";
            }
            senderOut.send(resString).unwrap();
            if wantReturn {
                senderOut.send(resString).unwrap();
            }
        }
    });
    (senderIn, receiverOut)

A kernel/src/wolfram/06-Cloud.wl => kernel/src/wolfram/06-Cloud.wl +7 -0
@@ 0,0 1,7 @@
$Sockets = {}
$SocketListeners = <| |>

SocketOpen = Function[port_Integer, AppendTo[$Sockets, port]]

SocketListen = Function[{port_Integer, fun_Function}, $SocketListeners = <| port -> fun |>]