schala/schala-repl/src/lib.rs

70 lines
1.8 KiB
Rust
Raw Normal View History

2021-10-07 00:51:45 -07:00
#![feature(box_patterns, box_syntax, proc_macro_hygiene, decl_macro)]
2017-10-30 20:06:20 -07:00
#![feature(plugin)]
2018-03-20 20:29:07 -07:00
2017-10-30 20:06:20 -07:00
#[macro_use]
extern crate serde_derive;
extern crate includedir;
extern crate phf;
2021-10-07 01:19:35 -07:00
extern crate serde_json;
2017-10-30 20:06:20 -07:00
2019-05-26 04:16:40 -07:00
use std::collections::HashSet;
2017-10-30 20:06:20 -07:00
use std::fs::File;
2018-09-21 19:43:50 -07:00
use std::io::Read;
2021-10-07 02:19:24 -07:00
use std::path::PathBuf;
2017-10-30 20:06:20 -07:00
use std::process::exit;
2018-03-01 02:43:11 -08:00
mod language;
2021-10-07 01:19:35 -07:00
mod repl;
2017-10-30 20:06:20 -07:00
2021-10-07 01:19:35 -07:00
pub use language::{
ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats,
LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
};
include!(concat!(env!("OUT_DIR"), "/static.rs"));
const VERSION_STRING: &'static str = "0.1.0";
pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) {
let mut repl = repl::Repl::new(langs);
repl.run_repl();
2017-10-30 20:06:20 -07:00
}
2021-10-07 02:19:24 -07:00
pub fn run_noninteractive(filenames: Vec<PathBuf>, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) {
// for now, ony do something with the first filename
let filename = &filenames[0];
let ext = filename
2021-10-07 01:19:35 -07:00
.extension()
.and_then(|e| e.to_str())
.unwrap_or_else(|| {
2021-10-07 02:19:24 -07:00
println!("Source file `{}` has no extension.", filename.display());
2021-10-07 01:19:35 -07:00
exit(1);
});
2021-10-07 02:19:24 -07:00
2021-10-07 01:19:35 -07:00
let mut language = Box::new(
languages
.into_iter()
2021-10-13 01:09:24 -07:00
.find(|lang| lang.source_file_suffix() == ext)
2021-10-07 01:19:35 -07:00
.unwrap_or_else(|| {
println!("Extension .{} not recognized", ext);
exit(1);
}),
);
2017-10-30 20:06:20 -07:00
2021-10-07 02:19:24 -07:00
let mut source_file = File::open(filename).unwrap();
2021-10-07 01:19:35 -07:00
let mut buffer = String::new();
source_file.read_to_string(&mut buffer).unwrap();
2017-10-30 20:06:20 -07:00
2021-10-07 01:19:35 -07:00
let request = ComputationRequest {
source: &buffer,
debug_requests: HashSet::new(),
};
2019-05-14 01:57:31 -07:00
2021-10-07 01:19:35 -07:00
let response = language.run_computation(request);
match response.main_output {
Ok(s) => println!("{}", s),
Err(s) => println!("{}", s),
};
2017-10-30 20:06:20 -07:00
}