Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a WS server for a live REPL #61

Merged
merged 26 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9f176b0
wip: add a websocket server
Vandesm14 Jul 31, 2024
e572833
implement serialize on Expressions
Vandesm14 Jul 31, 2024
a4bbf2e
implement running into the ws server
Vandesm14 Jul 31, 2024
493bde9
remove unnecessary code
Vandesm14 Jul 31, 2024
6edc55d
wip: parse incoming JSON
Vandesm14 Jul 31, 2024
0d32a86
implement run_new
Vandesm14 Jul 31, 2024
3ae9009
serialize all things errors
Vandesm14 Jul 31, 2024
38e6c58
update serialization of the API response
Vandesm14 Jul 31, 2024
6c8b406
update incoming and outgoing responses
Vandesm14 Jul 31, 2024
d6110dc
code splitting and refactoring
Vandesm14 Jul 31, 2024
1915e96
refactor outgoing messages
Vandesm14 Jul 31, 2024
d44b66a
remove unused declarations
Vandesm14 Jul 31, 2024
2845b69
wip: implement deserialize for everything
Vandesm14 Jul 31, 2024
744e459
clear warnings
Vandesm14 Jul 31, 2024
61f71be
use Rc's instead of Arc's
Vandesm14 Jul 31, 2024
1dac908
implement sending context over the wire
Vandesm14 Jul 31, 2024
791f70d
implement serialization for context-related types
Vandesm14 Jul 31, 2024
aee9a8d
add test for serialization
Vandesm14 Jul 31, 2024
442f0a4
use derived serialization for expr
Vandesm14 Jul 31, 2024
438f5fb
add serialization test for engine (e2e)
Vandesm14 Aug 1, 2024
e8eb19b
wip: print results to terminal
Vandesm14 Aug 1, 2024
67f2433
fix closure handling
Vandesm14 Aug 1, 2024
0d53998
clear warnings
Vandesm14 Aug 1, 2024
f9d4252
transmit parse error
Vandesm14 Aug 3, 2024
da91521
update justfile
Vandesm14 Aug 3, 2024
5297ba6
add info for server connection
Vandesm14 Aug 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
414 changes: 358 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ members = ["stack-core", "stack-std", "stack-cli", "stack-debugger"]

[workspace.dependencies]
unicode-segmentation = "1"
compact_str = "=0.8.0-beta"
compact_str = { version = "=0.8.0-beta", features = ["serde"] }

test-case = "3"
clap = { version = "4", features = ["derive"] }
clap = { version = "4", features = ["derive"] }
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.121"
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ debug file:

serve:
cd docs; mdbook serve --open

ws-serve:
cargo run -p stack-cli -- serve

ws-connect:
rlwrap websocat ws://localhost:5001
5 changes: 5 additions & 0 deletions stack-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ stack-core = { path = "../stack-core" }
stack-std = { path = "../stack-std", optional = true }
codespan-reporting = "0.11.1"

# server
serde = { workspace = true }
ws = { version = "0.9.2" }
serde_json.workspace = true

[[bin]]
name = "stack"
path = "src/main.rs"
58 changes: 58 additions & 0 deletions stack-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use core::fmt;
use std::io::{self, prelude::Write};

use crossterm::{
cursor::{self, MoveTo},
style::Print,
terminal, QueueableCommand,
};
use stack_core::prelude::*;

pub mod server;

pub fn ok_or_exit<T, E>(result: Result<T, E>) -> T
where
E: fmt::Display,
{
match result {
Ok(x) => x,
Err(e) => {
eprintln!("error: {e}");
std::process::exit(1);
}
}
}

pub fn print_stack(context: &Context) {
print!("stack:");

core::iter::repeat(" ")
.zip(context.stack())
.for_each(|(sep, x)| print!("{sep}{x:#}"));

println!()
}

pub fn eprint_stack(context: &Context) {
eprint!("stack:");

core::iter::repeat(" ")
.zip(context.stack())
.for_each(|(sep, x)| eprint!("{sep}{x:#}"));

eprintln!()
}

pub fn clear_screen() -> io::Result<()> {
let mut stdout = std::io::stdout();

stdout.queue(cursor::Hide)?;
let (_, num_lines) = terminal::size()?;
for _ in 0..2 * num_lines {
stdout.queue(Print("\n"))?;
}
stdout.queue(MoveTo(0, 0))?;
stdout.queue(cursor::Show)?;

stdout.flush()
}
62 changes: 8 additions & 54 deletions stack-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::fmt;
use std::{
io::{self, prelude::Write, Read},
io::Read,
path::{Path, PathBuf},
sync::Arc,
};
Expand All @@ -14,15 +13,13 @@ use codespan_reporting::{
termcolor::{ColorChoice, StandardStream},
},
};
use crossterm::{
cursor::{self, MoveTo},
style::Print,
terminal, QueueableCommand,
};
use notify::{
Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use reedline::{DefaultPrompt, DefaultPromptSegment, Reedline, Signal};
use stack_cli::{
clear_screen, eprint_stack, ok_or_exit, print_stack, server::listen,
};
use stack_core::prelude::*;

fn main() {
Expand Down Expand Up @@ -223,56 +220,10 @@ fn main() {
}
}
}
Subcommand::Serve => listen(),
}
}

fn ok_or_exit<T, E>(result: Result<T, E>) -> T
where
E: fmt::Display,
{
match result {
Ok(x) => x,
Err(e) => {
eprintln!("error: {e}");
std::process::exit(1);
}
}
}

fn print_stack(context: &Context) {
print!("stack:");

core::iter::repeat(" ")
.zip(context.stack())
.for_each(|(sep, x)| print!("{sep}{x:#}"));

println!()
}

fn eprint_stack(context: &Context) {
eprint!("stack:");

core::iter::repeat(" ")
.zip(context.stack())
.for_each(|(sep, x)| eprint!("{sep}{x:#}"));

eprintln!()
}

fn clear_screen() -> io::Result<()> {
let mut stdout = std::io::stdout();

stdout.queue(cursor::Hide)?;
let (_, num_lines) = terminal::size()?;
for _ in 0..2 * num_lines {
stdout.queue(Print("\n"))?;
}
stdout.queue(MoveTo(0, 0))?;
stdout.queue(cursor::Show)?;

stdout.flush()
}

#[derive(Debug, Clone, PartialEq, Eq, Default, clap::Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
Expand Down Expand Up @@ -329,4 +280,7 @@ enum Subcommand {
#[arg(short, long)]
watch: bool,
},

// TODO: add host and port as options
Serve,
}
Loading
Loading