mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 20:34:44 +00:00
Merge branch 'main' of https://github.com/erg-lang/erg
This commit is contained in:
commit
e1be2d2b51
8 changed files with 33 additions and 41 deletions
|
@ -7,7 +7,6 @@ use std::io::{stdin, BufRead, BufReader, Read};
|
|||
use std::process;
|
||||
|
||||
use crate::stdin::GLOBAL_STDIN;
|
||||
use crate::Str;
|
||||
use crate::{power_assert, read_file};
|
||||
|
||||
pub const SEMVER: &str = env!("CARGO_PKG_VERSION");
|
||||
|
@ -19,12 +18,12 @@ pub const BUILD_DATE: &str = env!("BUILD_DATE");
|
|||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Input {
|
||||
/// filename
|
||||
File(Str),
|
||||
File(String),
|
||||
REPL,
|
||||
/// same content as cfg.command
|
||||
Pipe(Str),
|
||||
Pipe(String),
|
||||
/// from command option | eval
|
||||
Str(Str),
|
||||
Str(String),
|
||||
Dummy,
|
||||
}
|
||||
|
||||
|
@ -52,7 +51,7 @@ impl Input {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn read(&self) -> Str {
|
||||
pub fn read(&self) -> String {
|
||||
match self {
|
||||
Self::File(filename) => {
|
||||
let file = match File::open(&filename[..]) {
|
||||
|
@ -63,15 +62,14 @@ impl Input {
|
|||
process::exit(code);
|
||||
}
|
||||
};
|
||||
let src = match read_file(file) {
|
||||
match read_file(file) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
let code = e.raw_os_error().unwrap_or(1);
|
||||
println!("cannot read '{filename}': [Errno {code}] {e}");
|
||||
process::exit(code);
|
||||
}
|
||||
};
|
||||
Str::from(src)
|
||||
}
|
||||
}
|
||||
Self::Pipe(s) | Self::Str(s) => s.clone(),
|
||||
Self::REPL => GLOBAL_STDIN.read(),
|
||||
|
@ -79,7 +77,7 @@ impl Input {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn reread_lines(&self, ln_begin: usize, ln_end: usize) -> Vec<Str> {
|
||||
pub fn reread_lines(&self, ln_begin: usize, ln_end: usize) -> Vec<String> {
|
||||
power_assert!(ln_begin, >=, 1);
|
||||
match self {
|
||||
Self::File(filename) => match File::open(&filename[..]) {
|
||||
|
@ -87,7 +85,7 @@ impl Input {
|
|||
let mut codes = vec![];
|
||||
let mut lines = BufReader::new(file).lines().skip(ln_begin - 1);
|
||||
for _ in ln_begin..=ln_end {
|
||||
codes.push(Str::from(lines.next().unwrap().unwrap()));
|
||||
codes.push(lines.next().unwrap().unwrap());
|
||||
}
|
||||
codes
|
||||
}
|
||||
|
@ -96,18 +94,18 @@ impl Input {
|
|||
Self::Pipe(s) | Self::Str(s) => s.split('\n').collect::<Vec<_>>()
|
||||
[ln_begin - 1..=ln_end - 1]
|
||||
.iter()
|
||||
.map(|s| Str::rc(*s))
|
||||
.map(|s| s.to_string())
|
||||
.collect(),
|
||||
Self::REPL => GLOBAL_STDIN.reread_lines(ln_begin, ln_end),
|
||||
Self::Dummy => panic!("cannot read lines from a dummy file"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reread(&self) -> Str {
|
||||
pub fn reread(&self) -> String {
|
||||
match self {
|
||||
Self::File(_filename) => todo!(),
|
||||
Self::Pipe(s) | Self::Str(s) => s.clone(),
|
||||
Self::REPL => Str::from(GLOBAL_STDIN.reread().trim_end().to_owned()),
|
||||
Self::REPL => GLOBAL_STDIN.reread().trim_end().to_owned(),
|
||||
Self::Dummy => panic!("cannot read from a dummy file"),
|
||||
}
|
||||
}
|
||||
|
@ -142,7 +140,7 @@ impl Default for ErgConfig {
|
|||
let input = if is_stdin_piped {
|
||||
let mut buffer = String::new();
|
||||
stdin().read_to_string(&mut buffer).unwrap();
|
||||
Input::Pipe(Str::from(buffer))
|
||||
Input::Pipe(buffer)
|
||||
} else {
|
||||
Input::REPL
|
||||
};
|
||||
|
@ -174,7 +172,7 @@ impl ErgConfig {
|
|||
while let Some(arg) = args.next() {
|
||||
match &arg[..] {
|
||||
"-c" => {
|
||||
cfg.input = Input::Str(Str::from(args.next().unwrap()));
|
||||
cfg.input = Input::Str(args.next().unwrap());
|
||||
}
|
||||
"--dump-as-pyc" => {
|
||||
cfg.dump_as_pyc = true;
|
||||
|
@ -223,7 +221,7 @@ impl ErgConfig {
|
|||
panic!("invalid option: {other}");
|
||||
}
|
||||
_ => {
|
||||
cfg.input = Input::File(Str::from(arg));
|
||||
cfg.input = Input::File(arg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,29 +2,27 @@ use std::cell::RefCell;
|
|||
use std::io::{stdin, BufRead, BufReader};
|
||||
use std::thread::LocalKey;
|
||||
|
||||
use crate::Str;
|
||||
|
||||
pub struct StdinReader {
|
||||
pub lineno: usize,
|
||||
buf: Vec<Str>,
|
||||
buf: Vec<String>,
|
||||
}
|
||||
|
||||
impl StdinReader {
|
||||
pub fn read(&mut self) -> Str {
|
||||
pub fn read(&mut self) -> String {
|
||||
let mut buf = "".to_string();
|
||||
let stdin = stdin();
|
||||
let mut reader = BufReader::new(stdin.lock());
|
||||
reader.read_line(&mut buf).unwrap();
|
||||
self.lineno += 1;
|
||||
self.buf.push(buf.into());
|
||||
self.buf.push(buf);
|
||||
self.buf.last().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn reread(&self) -> Str {
|
||||
pub fn reread(&self) -> String {
|
||||
self.buf.last().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn reread_lines(&self, ln_begin: usize, ln_end: usize) -> Vec<Str> {
|
||||
pub fn reread_lines(&self, ln_begin: usize, ln_end: usize) -> Vec<String> {
|
||||
self.buf[ln_begin - 1..=ln_end - 1].to_vec()
|
||||
}
|
||||
}
|
||||
|
@ -39,15 +37,15 @@ pub struct GlobalStdin(LocalKey<RefCell<StdinReader>>);
|
|||
pub static GLOBAL_STDIN: GlobalStdin = GlobalStdin(READER);
|
||||
|
||||
impl GlobalStdin {
|
||||
pub fn read(&'static self) -> Str {
|
||||
pub fn read(&'static self) -> String {
|
||||
self.0.with(|s| s.borrow_mut().read())
|
||||
}
|
||||
|
||||
pub fn reread(&'static self) -> Str {
|
||||
pub fn reread(&'static self) -> String {
|
||||
self.0.with(|s| s.borrow().reread())
|
||||
}
|
||||
|
||||
pub fn reread_lines(&'static self, ln_begin: usize, ln_end: usize) -> Vec<Str> {
|
||||
pub fn reread_lines(&'static self, ln_begin: usize, ln_end: usize) -> Vec<String> {
|
||||
self.0
|
||||
.with(|s| s.borrow_mut().reread_lines(ln_begin, ln_end))
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ use std::vec::IntoIter;
|
|||
|
||||
use crate::config::{ErgConfig, Input, BUILD_DATE, GIT_HASH_SHORT, SEMVER};
|
||||
use crate::error::{ErrorDisplay, ErrorKind, Location, MultiErrorDisplay};
|
||||
use crate::Str;
|
||||
use crate::{addr_eq, chomp, log, switch_unreachable};
|
||||
|
||||
pub trait Stream<T>: Sized {
|
||||
|
@ -334,7 +333,7 @@ pub trait Runnable: Sized {
|
|||
}
|
||||
fn finish(&mut self); // called when the :exit command is received.
|
||||
fn clear(&mut self);
|
||||
fn eval(&mut self, src: Str) -> Result<String, Self::Errs>;
|
||||
fn eval(&mut self, src: String) -> Result<String, Self::Errs>;
|
||||
fn exec(&mut self) -> Result<(), Self::Errs>;
|
||||
|
||||
fn ps1(&self) -> String {
|
||||
|
@ -395,7 +394,7 @@ pub trait Runnable: Sized {
|
|||
output.flush().unwrap();
|
||||
continue;
|
||||
}
|
||||
match instance.eval(mem::take(&mut lines).into()) {
|
||||
match instance.eval(mem::take(&mut lines)) {
|
||||
Ok(out) => {
|
||||
output.write_all((out + "\n").as_bytes()).unwrap();
|
||||
output.flush().unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue