2021-10-14 00:56:01 -07:00
|
|
|
use schala_repl::{Repl, ProgrammingLanguageInterface, ComputationRequest};
|
2017-10-12 02:13:55 -07:00
|
|
|
|
2021-10-14 00:56:01 -07:00
|
|
|
use std::{fs::File, io::Read, path::PathBuf, process::exit, collections::HashSet};
|
2021-10-31 03:30:45 -07:00
|
|
|
use schala_lang::{Schala, SchalaConfig};
|
2017-10-26 02:03:47 -07:00
|
|
|
|
2021-10-14 00:56:01 -07:00
|
|
|
//TODO specify multiple langs, and have a way to switch between them
|
2015-07-16 01:40:37 -07:00
|
|
|
fn main() {
|
2021-10-07 01:41:54 -07:00
|
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
let matches = command_line_options()
|
|
|
|
.parse(&args[1..])
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
eprintln!("Error parsing options: {}", e);
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
if matches.opt_present("help") {
|
|
|
|
println!("{}", command_line_options().usage("Schala metainterpreter"));
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.free.is_empty() {
|
2021-10-14 00:56:01 -07:00
|
|
|
let state = Schala::new();
|
|
|
|
let mut repl = Repl::new(state);
|
2021-10-31 03:30:45 -07:00
|
|
|
let config = SchalaConfig { repl: true };
|
|
|
|
repl.run_repl(config);
|
2021-10-07 01:41:54 -07:00
|
|
|
} else {
|
2021-10-14 00:56:01 -07:00
|
|
|
let paths: Vec<PathBuf> = matches.free.iter().map(PathBuf::from).collect();
|
|
|
|
//TODO handle more than one file
|
|
|
|
let filename = &paths[0];
|
|
|
|
let extension = filename.extension().and_then(|e| e.to_str())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
eprintln!("Source file `{}` has no extension.", filename.display());
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
//TODO this proably should be a macro for every supported language
|
|
|
|
if extension == Schala::source_file_suffix() {
|
2021-10-31 03:30:45 -07:00
|
|
|
let config = SchalaConfig {
|
|
|
|
repl: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
run_noninteractive(paths, Schala::new(), config);
|
2021-10-14 00:56:01 -07:00
|
|
|
} else {
|
|
|
|
eprintln!("Extension .{} not recognized", extension);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-10-07 01:41:54 -07:00
|
|
|
}
|
2017-10-23 20:51:08 -07:00
|
|
|
}
|
|
|
|
|
2021-10-31 03:30:45 -07:00
|
|
|
pub fn run_noninteractive<L: ProgrammingLanguageInterface>(filenames: Vec<PathBuf>, mut language: L, config: L::Config) {
|
2021-10-14 00:56:01 -07:00
|
|
|
// for now, ony do something with the first filename
|
|
|
|
|
|
|
|
let filename = &filenames[0];
|
|
|
|
let mut source_file = File::open(filename).unwrap();
|
|
|
|
let mut buffer = String::new();
|
|
|
|
source_file.read_to_string(&mut buffer).unwrap();
|
|
|
|
|
|
|
|
let request = ComputationRequest {
|
|
|
|
source: &buffer,
|
2021-10-31 03:30:45 -07:00
|
|
|
config,
|
2021-10-14 00:56:01 -07:00
|
|
|
debug_requests: HashSet::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let response = language.run_computation(request);
|
|
|
|
match response.main_output {
|
|
|
|
Ok(s) => println!("{}", s),
|
|
|
|
Err(s) => eprintln!("{}", s),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-07 01:41:54 -07:00
|
|
|
fn command_line_options() -> getopts::Options {
|
|
|
|
let mut options = getopts::Options::new();
|
|
|
|
options.optflag("h", "help", "Show help text");
|
2021-10-13 01:01:55 -07:00
|
|
|
//options.optflag("w", "webapp", "Start up web interpreter");
|
2021-10-07 01:41:54 -07:00
|
|
|
options
|
|
|
|
}
|