Skip to content

Commit

Permalink
fix: Shared definition is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Jan 29, 2024
1 parent c9d639a commit 5a3d272
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 39 deletions.
1 change: 1 addition & 0 deletions cli/examples/compute.ly
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Benchmark.fib(35);
17 changes: 11 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
fs::File,
io::{BufReader, Read},
fs::File, io::{BufReader, Read}, time::Duration
};

use clap::Parser;
Expand Down Expand Up @@ -77,11 +76,17 @@ async fn run_file(filename: &str, print_ast: bool) {
// Initialize the connection state. This allocates read/write buffers to
// perform redis protocol frame parsing.
let mut session = ClientSession::new(socket);
loop {
session
.send(Message::Request(Request::Run(content.to_string())))
.await
.unwrap();

let response = session.handle().await;
println!("{:?}", response);
tokio::time::sleep(Duration::from_secs(2)).await;
}

session
.send(Message::Request(Request::Run(content)))
.await
.unwrap();
}

#[tokio::main]
Expand Down
1 change: 1 addition & 0 deletions liblykia/src/protocol/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl Connection {
loop {
// TODO(vck): Replace .to_vec call with something cheaper
if let Ok(parsed) = bson::from_slice::<Message>(&self.read_buffer.to_vec()) {
self.read_buffer.clear();
return Ok(Some(parsed));
}

Expand Down
12 changes: 6 additions & 6 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ struct Server {

pub struct ServerSession {
conn: Connection,
// runtime: Runtime
runtime: Runtime
}

impl ServerSession {
pub fn new(stream: TcpStream) -> Self {
ServerSession {
conn: Connection::new(stream),
// runtime: Runtime::new(RuntimeMode::File)
runtime: Runtime::new(RuntimeMode::File)
}
}

pub async fn handle(&mut self) {
if let Some(message) = self.conn.read().await.unwrap() {
while let Some(message) = self.conn.read().await.unwrap() {
println!("{:?}", message);
match message {
Message::Request(req) => match req {
Request::Run(command) => {
/*let execution = self.runtime.interpret(&command);
let execution = self.runtime.interpret(&command);
if execution.is_ok() {
let bson_response = bson::to_bson(&execution.ok().or_else(|| Some(RV::Undefined)));
self.conn.write(Message::Response(Response::Value(bson_response.unwrap()))).await;
}*/
self.conn.write(Message::Response(Response::Value(bson_response.unwrap()))).await.unwrap();
}
self.conn
.write(Message::Response(Response::Value(
bson::to_bson(&1515).unwrap(),
Expand Down
5 changes: 2 additions & 3 deletions server/src/runtime/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ mod test {
eval::{coerce2number, eval_binary, is_value_truthy},
types::{Function, RV},
},
util::alloc_shared,
};

#[test]
Expand All @@ -163,9 +162,9 @@ mod test {
assert_eq!(is_value_truthy(RV::Num(-1.0)), true);
assert_eq!(is_value_truthy(RV::Str(Arc::new("".to_owned()))), false);
assert_eq!(is_value_truthy(RV::Str(Arc::new("foo".to_owned()))), true);
assert_eq!(is_value_truthy(RV::Array(alloc_shared(vec![]))), true);
assert_eq!(is_value_truthy(RV::Array(Arc::new(vec![]))), true);
assert_eq!(
is_value_truthy(RV::Object(alloc_shared(FxHashMap::default()))),
is_value_truthy(RV::Object(Arc::new(FxHashMap::default()))),
true
);
assert_eq!(
Expand Down
23 changes: 12 additions & 11 deletions server/src/runtime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::lang::tokens::token::Span;
use crate::lang::tokens::token::Spanned;
use crate::runtime::types::RV::Callable;
use crate::runtime::types::{Function, RV};
use crate::util::alloc_shared;

use std::borrow::{Borrow, BorrowMut};
use std::sync::Arc;
use std::vec;

Expand Down Expand Up @@ -250,11 +250,11 @@ impl Interpreter {
for (k, v) in map.iter() {
new_map.insert(k.clone(), self.visit_expr(*v).unwrap());
}
RV::Object(alloc_shared(new_map))
RV::Object(Arc::new(new_map))
}
Literal::Array(arr) => {
let collected = arr.iter().map(|x| self.visit_expr(*x).unwrap()).collect();
RV::Array(alloc_shared(collected))
RV::Array(Arc::new(collected))
}
}
}
Expand Down Expand Up @@ -386,8 +386,8 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
Expr::Get { object, name, span } => {
let object_eval = self.visit_expr(*object)?;
if let RV::Object(map) = object_eval {
let borrowed = map.borrow();
let v = borrowed.get(&name.name.clone());
let cloned = map.clone();
let v = cloned.get(&name.name.clone());
if v.is_some() {
return Ok(v.unwrap().clone());
}
Expand All @@ -413,8 +413,9 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
let object_eval = self.visit_expr(*object)?;
if let RV::Object(map) = object_eval {
let evaluated = self.visit_expr(*value)?;
map.borrow_mut()
.insert(name.name.to_string(), evaluated.clone());
// TODO(vck): Set should really set the value
/*let borrowed: &mut FxHashMap<String, RV> = map.borrow_mut();
borrowed.insert(name.name.to_string(), evaluated.clone());*/
Ok(evaluated)
} else {
Err(HaltReason::Error(InterpretError::Other {
Expand Down Expand Up @@ -566,7 +567,7 @@ pub mod test_helpers {
pub fn exec_assert(code: &str, output: Vec<RV>) -> () {
let (out, mut runtime) = get_runtime();
runtime.interpret(code);
out.borrow_mut().expect(output);
out.write().unwrap().expect(output);
}
}

Expand All @@ -587,7 +588,7 @@ mod test {
";
let (out, mut runtime) = get_runtime();
runtime.interpret(&code);
out.borrow_mut().expect(vec![
out.write().unwrap().expect(vec![
RV::Num(-2.0),
RV::Num(2.0),
RV::Bool(false),
Expand All @@ -606,7 +607,7 @@ mod test {
";
let (out, mut runtime) = get_runtime();
runtime.interpret(&code);
out.borrow_mut().expect(vec![
out.write().unwrap().expect(vec![
RV::Num(7.0),
RV::Num(28.0),
RV::Num(13.0),
Expand All @@ -628,7 +629,7 @@ mod test {
";
let (out, mut runtime) = get_runtime();
runtime.interpret(&code);
out.borrow_mut().expect(vec![
out.write().unwrap().expect(vec![
RV::Bool(true),
RV::Bool(true),
RV::Bool(false),
Expand Down
10 changes: 5 additions & 5 deletions server/src/runtime/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use rustc_hash::FxHashMap;

use crate::util::{alloc_shared, Shared};
use crate::util::Shared;

use self::{
fib::nt_fib,
Expand Down Expand Up @@ -68,16 +68,16 @@ pub fn stdlib(out: Option<Shared<Output>>) -> FxHashMap<String, RV> {

std.insert(
"TestUtils".to_owned(),
RV::Object(alloc_shared(test_namespace)),
RV::Object(Arc::new(test_namespace)),
);
}

std.insert(
"Benchmark".to_owned(),
RV::Object(alloc_shared(benchmark_namespace)),
RV::Object(Arc::new(benchmark_namespace)),
);
std.insert("JSON".to_owned(), RV::Object(alloc_shared(json_namespace)));
std.insert("Time".to_owned(), RV::Object(alloc_shared(time_namespace)));
std.insert("JSON".to_owned(), RV::Object(Arc::new(json_namespace)));
std.insert("Time".to_owned(), RV::Object(Arc::new(time_namespace)));
std.insert(
"print".to_owned(),
RV::Callable(None, Arc::new(Function::Lambda { function: nt_print })),
Expand Down
8 changes: 4 additions & 4 deletions server/src/runtime/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum Function {
Lambda {
function: fn(&mut Interpreter, &[RV]) -> Result<RV, HaltReason>,
},
Stateful(Shared<dyn Stateful>),
Stateful(Shared<dyn Stateful + Send + Sync>),
UserDefined {
name: String,
parameters: Vec<String>,
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Display for Function {
impl Function {
pub fn call(&self, interpreter: &mut Interpreter, arguments: &[RV]) -> Result<RV, HaltReason> {
match self {
Function::Stateful(stateful) => stateful.borrow_mut().call(interpreter, arguments),
Function::Stateful(stateful) => stateful.write().unwrap().call(interpreter, arguments),
Function::Lambda { function } => function(interpreter, arguments),
Function::UserDefined {
name: _,
Expand All @@ -95,8 +95,8 @@ pub enum RV {
Str(Arc<String>),
Num(f64),
Bool(bool),
Object(Shared<FxHashMap<String, RV>>),
Array(Shared<Vec<RV>>),
Object(Arc<FxHashMap<String, RV>>),
Array(Arc<Vec<RV>>),
Callable(Option<usize>, Arc<Function>),
Undefined,
NaN,
Expand Down
8 changes: 4 additions & 4 deletions server/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, sync::Arc};
use std::sync::{Arc, RwLock};

pub type Shared<T> = Arc<RefCell<T>>;
pub type Shared<T> = Arc<RwLock<T>>;

pub fn alloc_shared<T>(obj: T) -> Shared<T> {
Arc::new(RefCell::new(obj))
}
Arc::new(RwLock::new(obj))
}

0 comments on commit 5a3d272

Please sign in to comment.