improved repl with a quit instruction

This commit is contained in:
Ishan Jain 2024-06-08 09:04:46 +05:30
parent caee5ce154
commit c034099ff0
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
4 changed files with 167 additions and 6 deletions

120
Cargo.lock generated
View File

@ -2,15 +2,135 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]]
name = "ctrlc"
version = "3.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "672465ae37dc1bc6380a6547a8883d5dd397b0f1faaad4f265726cc7042a5345"
dependencies = [
"nix",
"windows-sys",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "loxi"
version = "0.1.0"
dependencies = [
"ctrlc",
"lazy_static",
]
[[package]]
name = "nix"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
[[package]]
name = "windows_i686_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
[[package]]
name = "windows_i686_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"

View File

@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021"
[dependencies]
ctrlc = "3.4.4"
lazy_static = "1.4.0"

View File

@ -3,18 +3,34 @@ mod scanner;
mod loxi;
mod repl;
use std::env;
use std::{env, fs::File, io::Read, process};
use loxi::run;
fn main() {
let args: Vec<String> = env::args().collect();
match args.len() {
1 => repl::init(),
v if v > 1 => {
println!("Usage: loxi [script]")
2 => {
println!("{:?}", args);
run_file(&args[1]);
}
v if v > 2 => {
println!("Usage: loxi [script]");
process::exit(64);
}
_ => {
// TODO: Read the file
repl::init();
}
}
}
fn run_file(f: &str) {
let mut f = File::open(f).expect("error in opening file");
let mut contents = Vec::new();
f.read_to_end(&mut contents).expect("error in reading file");
let contents = String::from_utf8(contents).expect("file is not encoded in utf8");
run(&contents)
}

View File

@ -1,5 +1,9 @@
use crate::loxi::run;
use std::io::{self, BufRead, Write};
use std::{
io::{self, BufRead, Write},
sync::mpsc,
time::Duration,
};
const PROMPT: &[u8] = b">> ";
@ -13,9 +17,29 @@ pub fn init() {
}
fn start<R: BufRead, W: Write>(mut ip: R, mut out: W) {
let (send, recv) = mpsc::channel();
let mut should_quit = false;
ctrlc::set_handler(move || {
send.send(()).expect("error in sending signal to channel");
})
.expect("error in setting Ctrl+C handler");
loop {
out.write_all(PROMPT).unwrap();
out.flush().unwrap();
if recv.recv_timeout(Duration::from_millis(5)).is_ok() {
if should_quit {
std::process::exit(0);
}
should_quit = true;
out.write_all(b"\r \r")
.unwrap();
continue;
} else {
should_quit = false;
}
let mut s = String::new();
ip.read_line(&mut s).unwrap();