Kill old trait

This commit is contained in:
greg 2017-08-31 18:05:10 -07:00
parent fb009497a4
commit 55e1600b97
3 changed files with 10 additions and 97 deletions

View File

@ -18,86 +18,6 @@ pub struct ParseError {
pub struct LLVMCodeString(pub String);
pub trait ProgrammingLanguage {
type Token: Debug;
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>;
fn compile(ast: Self::AST) -> LLVMCodeString;
}
pub trait EvaluationMachine {
fn set_option(&mut self, option: &str, value: bool) -> bool;
fn new() -> Self;
}
#[derive(Default)]
pub struct LanguageInterfaceOptions {
pub show_parse: bool,
pub show_tokens: bool,
pub show_llvm_ir: bool,
}
pub trait LanguageInterface {
fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String;
fn get_language_name(&self) -> String;
fn set_option(&mut self, option: &str, value: bool) -> bool;
}
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 set_option(&mut self, option: &str, value: bool) -> bool {
self.1.set_option(option, value)
}
fn get_language_name(&self) -> String {
PL::name()
}
fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String {
let mut output = String::new();
let tokens = match PL::tokenize(input) {
Ok(tokens) => tokens,
Err(err) => {
output.push_str(&format!("Tokenization error: {}\n", err.msg));
return output;
}
};
if options.show_tokens {
output.push_str(&format!("Tokens: {:?}\n", tokens));
}
let ast = match PL::parse(tokens) {
Ok(ast) => ast,
Err(err) => {
output.push_str(&format!("Parse error: {:?}\n", err.msg));
return output;
}
};
if options.show_parse {
output.push_str(&format!("AST: {:?}\n", ast));
}
if options.show_llvm_ir {
let LLVMCodeString(s) = PL::compile(ast);
output.push_str(&s);
} else {
// for now only handle last output
let ref mut evaluator = self.1;
let mut full_output: Vec<String> = PL::evaluate(ast, evaluator);
output.push_str(&full_output.pop().unwrap_or("".to_string()));
}
output
}
}
/* below here is new versions of everything */
#[derive(Debug, Default)]
pub struct EvalOptions {
pub debug_tokens: bool,
@ -109,9 +29,6 @@ pub struct EvalOptions {
pub trait ProgrammingLanguageInterface {
fn evaluate_in_repl(&mut self, input: &str, eval_options: EvalOptions) -> Vec<String>;
fn get_language_name(&self) -> String;
fn set_option(&mut self, option: &str, value: bool) -> bool {
false
}
}
pub trait CompileableLanguage : ProgrammingLanguageInterface {

View File

@ -29,15 +29,11 @@ use virtual_machine::{run_vm, run_assembler};
fn main() {
let languages: Vec<Box<ProgrammingLanguageInterface>> =
vec![
Box::new(schala_lang::Schala::new()),
Box::new(maaru_lang::Maaru::new()),
Box::new(robo_lang::Robo::new()),
/*
Box::new((schala_lang::Schala::new(), schala_lang::SchalaEvaluator::new())),
Box::new((maaru_lang::Maaru::new(), maaru_lang::MaaruEvaluator::new(None))),
*/
];
vec![
Box::new(schala_lang::Schala::new()),
Box::new(maaru_lang::Maaru::new()),
Box::new(robo_lang::Robo::new()),
];
let option_matches =
match program_options().parse(std::env::args()) {
@ -126,7 +122,7 @@ fn program_options() -> getopts::Options {
options
}
fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language: &mut T, options: EvalOptions, compile: bool) {
fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language: &mut T, _options: EvalOptions, compile: bool) {
let mut source_file = File::open(&Path::new(filename)).unwrap();
let mut buffer = String::new();
source_file.read_to_string(&mut buffer).unwrap();
@ -306,8 +302,8 @@ impl Repl {
Some(&"tokens") => self.show_tokens = show,
Some(&"parse") => self.show_parse = show,
Some(&"eval") => {
let ref mut language = self.languages[self.current_language_index];
language.set_option("trace_evaluation", show);
//let ref mut language = self.languages[self.current_language_index];
//language.set_option("trace_evaluation", show);
},
Some(&"llvm") => self.show_llvm_ir = show,
Some(e) => {

View File

@ -1,7 +1,7 @@
extern crate itertools;
use self::itertools::Itertools;
use language::{ProgrammingLanguageInterface, EvalOptions, ProgrammingLanguage, ParseError, TokenError, LLVMCodeString};
use language::{ProgrammingLanguageInterface, EvalOptions, ParseError, TokenError, LLVMCodeString};
pub struct Robo {
}
@ -137,7 +137,7 @@ impl ProgrammingLanguageInterface for Robo {
"Robo".to_string()
}
fn evaluate_in_repl(&mut self, input: &str, eval_options: EvalOptions) -> Vec<String> {
fn evaluate_in_repl(&mut self, input: &str, _eval_options: EvalOptions) -> Vec<String> {
let mut output = vec!();
let tokens = match tokenize(input) {
Ok(tokens) => tokens,