#![feature(box_patterns, box_syntax, proc_macro_hygiene, decl_macro)] #![feature(plugin)] extern crate colored; extern crate getopts; extern crate itertools; extern crate linefeed; #[macro_use] extern crate serde_derive; extern crate includedir; extern crate phf; extern crate serde_json; use std::collections::HashSet; use std::fs::File; use std::io::Read; use std::path::Path; use std::process::exit; mod language; mod repl; 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>) { let options = command_line_options() .parse(std::env::args()) .unwrap_or_else(|e| { println!("{:?}", e); exit(1); }); if options.opt_present("help") { println!("{}", command_line_options().usage("Schala metainterpreter")); exit(0); } match options.free[..] { [] | [_] => { let mut repl = repl::Repl::new(langs); repl.run_repl(); } [_, ref filename, ..] => { run_noninteractive(filename, langs); } }; } fn run_noninteractive(filename: &str, languages: Vec>) { 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); }), ); let mut source_file = File::open(path).unwrap(); let mut buffer = String::new(); source_file.read_to_string(&mut buffer).unwrap(); let request = ComputationRequest { source: &buffer, debug_requests: HashSet::new(), }; let response = language.run_computation(request); match response.main_output { Ok(s) => println!("{}", s), Err(s) => println!("{}", s), }; } fn command_line_options() -> getopts::Options { let mut options = getopts::Options::new(); options.optflag("h", "help", "Show help text"); options.optflag("w", "webapp", "Start up web interpreter"); options }