More conversions to trait version

This commit is contained in:
greg 2017-01-23 01:32:57 -08:00
parent 8326a12c9c
commit 9d6bdf22da
3 changed files with 16 additions and 29 deletions

View File

@ -11,7 +11,7 @@ use parser::{AST, Statement, Function, Prototype, Expression, BinOp};
use llvm_wrap as LLVMWrap; use llvm_wrap as LLVMWrap;
pub fn compilation_sequence(ast: AST, sourcefile: &str) { pub fn compilation_sequence(llvm_code: String, sourcefile: &str) {
use std::process::Command; use std::process::Command;
let ll_filename = "out.ll"; let ll_filename = "out.ll";
@ -22,7 +22,6 @@ pub fn compilation_sequence(ast: AST, sourcefile: &str) {
_ => panic!("Bad filename {}", sourcefile), _ => panic!("Bad filename {}", sourcefile),
}; };
let llvm_code = compile_ast(ast);
println!("Compilation process finished for {}", ll_filename); println!("Compilation process finished for {}", ll_filename);
File::create(ll_filename) File::create(ll_filename)
.and_then(|mut f| f.write_all(llvm_code.as_bytes())) .and_then(|mut f| f.write_all(llvm_code.as_bytes()))

View File

@ -14,6 +14,6 @@ pub trait ProgrammingLanguage<Evaluator> {
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError>; fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError>;
fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError>; fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError>;
fn evaluate(input: Self::AST, evaluator: &mut Evaluator) -> Vec<String>; fn evaluate(ast: Self::AST, evaluator: &mut Evaluator) -> Vec<String>;
fn compile(input: &Self::AST); fn compile(ast: Self::AST) -> String;
} }

View File

@ -7,10 +7,7 @@ use std::fs::File;
use std::io::Read; use std::io::Read;
use std::process; use std::process;
use tokenizer::tokenize;
mod tokenizer; mod tokenizer;
use parser::parse;
mod parser; mod parser;
use eval::Evaluator; use eval::Evaluator;
@ -40,7 +37,8 @@ fn main() {
repl.run(); repl.run();
} }
[_, ref filename, _..] => { [_, ref filename, _..] => {
run_noninteractive(filename, !option_matches.opt_present("i"), trace); let language = Schala { };
run_noninteractive(filename, !option_matches.opt_present("i"), trace, &language);
} }
}; };
} }
@ -59,12 +57,12 @@ fn program_options() -> getopts::Options {
options options
} }
fn run_noninteractive(filename: &str, compile: bool, trace_evaluation: bool) { fn run_noninteractive<'a, T: ProgrammingLanguage<eval::Evaluator<'a>>>(filename: &str, compile: bool, trace_evaluation: bool, language: &T) {
let mut source_file = File::open(&Path::new(filename)).unwrap(); let mut source_file = File::open(&Path::new(filename)).unwrap();
let mut buffer = String::new(); let mut buffer = String::new();
source_file.read_to_string(&mut buffer).unwrap(); source_file.read_to_string(&mut buffer).unwrap();
let tokens = match tokenize(&buffer) { let tokens = match T::tokenize(&buffer) {
Ok(t) => t, Ok(t) => t,
Err(e) => { Err(e) => {
println!("Tokenization error: {}", e.msg); println!("Tokenization error: {}", e.msg);
@ -72,20 +70,20 @@ fn run_noninteractive(filename: &str, compile: bool, trace_evaluation: bool) {
} }
}; };
let ast = match parse(&tokens, &[]) { let ast = match T::parse(tokens) {
Ok(ast) => ast, Ok(ast) => ast,
Err(err) => { Err(err) => {
println!("Parse error: {:?}", err.msg); println!("Parse error: {:?}", err.msg);
println!("Remaining tokens: {:?}", err.remaining_tokens); /*println!("Remaining tokens: {:?}", err.remaining_tokens);*/
std::process::exit(1) std::process::exit(1)
} }
}; };
if compile { if compile {
compilation_sequence(ast, filename); compilation_sequence(T::compile(ast), filename);
} else { } else {
let mut evaluator = Evaluator::new_with_opts(None, trace_evaluation); let mut evaluator = Evaluator::new_with_opts(None, trace_evaluation);
let results = evaluator.run(ast); let results = T::evaluate(ast, &mut evaluator);
for result in results.iter() { for result in results.iter() {
println!("{}", result); println!("{}", result);
} }
@ -140,13 +138,6 @@ impl<'a> Repl<'a> {
println!("Exiting..."); println!("Exiting...");
} }
fn new_input_handler(input: &str) -> String {
let language = Schala {};
unimplemented!()
}
fn input_handler(&mut self, input: &str) -> String { fn input_handler(&mut self, input: &str) -> String {
let schala = Schala { }; let schala = Schala { };
self.input_handler_new(input, schala) self.input_handler_new(input, schala)
@ -179,12 +170,9 @@ impl<'a> Repl<'a> {
output.push_str(&format!("AST: {:?}\n", ast)); output.push_str(&format!("AST: {:?}\n", ast));
} }
/*
if self.show_llvm_ir { if self.show_llvm_ir {
let s = compile_ast(ast); let s = T::compile(ast);
output.push_str(&s); output.push_str(&s);
*/
if false {
} else { } else {
// for now only handle last output // for now only handle last output
let mut full_output: Vec<String> = T::evaluate(ast, &mut self.evaluator); let mut full_output: Vec<String> = T::evaluate(ast, &mut self.evaluator);
@ -258,11 +246,11 @@ impl<'a> ProgrammingLanguage<eval::Evaluator<'a>> for Schala {
fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError> { fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError> {
parser::parse(&input, &[]).map_err(|x| ParseError { msg: x.msg }) parser::parse(&input, &[]).map_err(|x| ParseError { msg: x.msg })
} }
fn evaluate(input: Self::AST, evaluator: &mut Evaluator) -> Vec<String> { fn evaluate(ast: Self::AST, evaluator: &mut Evaluator) -> Vec<String> {
evaluator.run(input) evaluator.run(ast)
} }
fn compile(input: &Self::AST) { fn compile(ast: Self::AST) -> String {
unimplemented!() compile_ast(ast)
} }
} }