Added language name functionality
This commit is contained in:
parent
455fe2abe2
commit
8fd5fb5a0b
@ -24,6 +24,7 @@ pub trait ProgrammingLanguage {
|
||||
type AST: Debug;
|
||||
type Evaluator: EvaluationMachine;
|
||||
|
||||
fn name() -> String;
|
||||
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError>;
|
||||
fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError>;
|
||||
fn evaluate(ast: Self::AST, evaluator: &mut Self::Evaluator) -> Vec<String>;
|
||||
@ -44,9 +45,13 @@ pub struct LanguageInterfaceOptions {
|
||||
|
||||
pub trait LanguageInterface {
|
||||
fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String;
|
||||
fn get_language_name(&self) -> String;
|
||||
}
|
||||
|
||||
impl<PL, T, A, E> LanguageInterface for (PL, PL::Evaluator) where PL: ProgrammingLanguage<Token=T, AST=A, Evaluator=E>, T: Debug, A: Debug, E: EvaluationMachine {
|
||||
fn get_language_name(&self) -> String {
|
||||
PL::name()
|
||||
}
|
||||
fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String {
|
||||
let mut output = String::new();
|
||||
|
||||
|
@ -19,6 +19,10 @@ impl ProgrammingLanguage for Maaru {
|
||||
type AST = AST;
|
||||
type Evaluator = MaaruEvaluator;
|
||||
|
||||
fn name() -> String {
|
||||
"Maaru".to_string()
|
||||
}
|
||||
|
||||
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError> {
|
||||
Ok(vec![Token { }])
|
||||
}
|
||||
|
20
src/main.rs
20
src/main.rs
@ -21,6 +21,9 @@ use language::{ProgrammingLanguage, LanguageInterface, LLVMCodeString, Evaluatio
|
||||
mod llvm_wrap;
|
||||
|
||||
fn main() {
|
||||
|
||||
let languages: Vec<Box<LanguageInterface>> = vec![Box::new((Schala::new(), SchalaEvaluator::new(None)))];
|
||||
|
||||
let option_matches =
|
||||
match program_options().parse(std::env::args()) {
|
||||
Ok(o) => o,
|
||||
@ -29,11 +32,17 @@ fn main() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
if option_matches.opt_present("list-languages") {
|
||||
for lang in languages {
|
||||
println!("{}", lang.get_language_name());
|
||||
}
|
||||
std::process::exit(1);
|
||||
}
|
||||
let trace = option_matches.opt_present("t");
|
||||
let show_llvm = option_matches.opt_present("l");
|
||||
match option_matches.free[..] {
|
||||
[] | [_] => {
|
||||
let mut repl = Repl::new(trace, show_llvm);
|
||||
let mut repl = Repl::new(languages);
|
||||
repl.run();
|
||||
}
|
||||
[_, ref filename, _..] => {
|
||||
@ -54,6 +63,9 @@ fn program_options() -> getopts::Options {
|
||||
options.optflag("l",
|
||||
"llvm-in-repl",
|
||||
"Show LLVM IR in REPL");
|
||||
options.optflag("",
|
||||
"list-languages",
|
||||
"Show a list of all supported languages");
|
||||
options
|
||||
}
|
||||
|
||||
@ -104,15 +116,15 @@ struct Repl {
|
||||
}
|
||||
|
||||
impl Repl {
|
||||
fn new(trace_evaluation: bool, show_llvm: bool) -> Repl {
|
||||
fn new(languages: Vec<Box<LanguageInterface>>) -> Repl {
|
||||
let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Schala").unwrap();
|
||||
reader.set_prompt(">> ");
|
||||
|
||||
Repl {
|
||||
show_tokens: false,
|
||||
show_parse: false,
|
||||
show_llvm_ir: show_llvm,
|
||||
languages: vec![Box::new((Schala::new(), SchalaEvaluator::new(None)))],
|
||||
show_llvm_ir: false,
|
||||
languages: languages,
|
||||
interpreter_directive_sigil: '.',
|
||||
reader: reader,
|
||||
}
|
||||
|
@ -20,6 +20,10 @@ impl<'a> ProgrammingLanguage for Schala<'a> {
|
||||
type AST = parser::AST;
|
||||
type Evaluator = SchalaEvaluator<'a>;
|
||||
|
||||
fn name() -> String {
|
||||
"Schala".to_string()
|
||||
}
|
||||
|
||||
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError> {
|
||||
tokenizer::tokenize(input)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user