From c034099ff0df4d71c0d3ac0c7455834866d5be3f Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Sat, 8 Jun 2024 09:04:46 +0530 Subject: [PATCH] improved repl with a quit instruction --- Cargo.lock | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 26 +++++++++--- src/repl.rs | 26 +++++++++++- 4 files changed, 167 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d88eb5..be9e531 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 361b7ba..9434ec4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +ctrlc = "3.4.4" lazy_static = "1.4.0" diff --git a/src/main.rs b/src/main.rs index 1f12993..f8f5b1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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) +} diff --git a/src/repl.rs b/src/repl.rs index 0253451..dfaf6bf 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -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(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();