schala/schala-repl/src/lib.rs

71 lines
1.9 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)]
2021-10-07 01:19:35 -07:00
extern crate colored;
2017-10-30 20:06:20 -07:00
extern crate itertools;
2021-10-07 01:19:35 -07:00
extern crate linefeed;
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 01:19:35 -07:00
use std::path::Path;
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
}
//TODO should really expect a list of paths
pub fn run_noninteractive(filename: &str, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) {
2021-10-07 01:19:35 -07:00
let path = Path::new(filename);
let ext = path
.extension()
.and_then(|e| e.to_str())
.unwrap_or_else(|| {
println!("Source file lacks extension");
exit(1);
});
let mut language = Box::new(
languages
.into_iter()
.find(|lang| lang.get_source_file_suffix() == ext)
.unwrap_or_else(|| {
println!("Extension .{} not recognized", ext);
exit(1);
}),
);
2017-10-30 20:06:20 -07:00
2021-10-07 01:19:35 -07:00
let mut source_file = File::open(path).unwrap();
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
}