schala/src/schala_lang/mod.rs

96 lines
2.6 KiB
Rust
Raw Normal View History

2017-09-18 19:09:27 -07:00
use itertools::Itertools;
2017-08-31 20:59:43 -07:00
use language::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, ReplOutput};
2017-08-29 04:15:31 -07:00
2017-08-29 04:27:07 -07:00
mod parsing;
2017-10-04 02:07:30 -07:00
mod type_check;
2017-09-30 23:30:02 -07:00
mod eval;
2017-08-29 04:27:07 -07:00
2017-10-07 22:08:48 -07:00
use self::type_check::{TypeContext};
2017-10-01 00:48:08 -07:00
pub struct Schala {
2017-10-04 02:07:30 -07:00
state: eval::ReplState,
type_context: TypeContext
2017-08-29 04:15:31 -07:00
}
impl Schala {
pub fn new() -> Schala {
2017-09-30 23:30:02 -07:00
Schala {
state: eval::ReplState::new(),
2017-10-04 02:07:30 -07:00
type_context: TypeContext::new(),
2017-09-30 23:30:02 -07:00
}
2017-08-29 04:15:31 -07:00
}
}
2017-08-31 14:57:56 -07:00
impl ProgrammingLanguageInterface for Schala {
fn get_language_name(&self) -> String {
2017-08-29 04:15:31 -07:00
"Schala".to_string()
}
2017-10-02 23:07:05 -07:00
fn get_source_file_suffix(&self) -> String {
format!("schala")
}
2017-09-01 02:08:26 -07:00
fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> ReplOutput {
2017-08-31 20:59:43 -07:00
let mut output = ReplOutput::default();
let tokens = parsing::tokenize(input);
if options.debug_tokens {
2017-09-18 19:09:27 -07:00
let token_string = tokens.iter().map(|t| format!("{:?}<{}>", t.token_type, t.offset)).join(", ");
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", token_string)));
}
2017-09-08 16:42:42 -07:00
{
let token_errors: Vec<&String> = tokens.iter().filter_map(|t| t.get_error()).collect();
if token_errors.len() != 0 {
output.add_output(format!("Tokenization error: {:?}\n", token_errors));
return output;
}
}
2017-08-31 16:26:55 -07:00
2017-08-31 20:59:43 -07:00
let ast = match parsing::parse(tokens) {
(Ok(ast), trace) => {
2017-08-31 20:59:43 -07:00
if options.debug_parse {
2017-09-16 14:29:22 -07:00
output.add_artifact(TraceArtifact::new_parse_trace(trace));
2017-08-31 20:59:43 -07:00
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
}
ast
},
(Err(err), trace) => {
2017-09-16 14:29:22 -07:00
output.add_artifact(TraceArtifact::new_parse_trace(trace));
2017-08-31 20:59:43 -07:00
output.add_output(format!("Parse error: {:?}\n", err.msg));
2017-08-31 16:26:55 -07:00
return output;
}
};
self.type_context.add_symbols(&ast);
if options.debug_symbol_table {
let text = self.type_context.debug_symbol_table();
output.add_artifact(TraceArtifact::new("symbol_table", text));
}
2017-10-04 02:07:30 -07:00
match self.type_context.type_check(&ast) {
Ok(ty) => {
output.add_artifact(TraceArtifact::new("type_check", format!("type: {:?}", ty)));
},
2017-10-07 22:08:48 -07:00
Err(msg) => {
output.add_artifact(TraceArtifact::new("type_check", msg));
2017-10-01 00:48:08 -07:00
output.add_output(format!("Type error"));
return output;
}
}
2017-09-30 23:30:02 -07:00
let evaluation_output = self.state.evaluate(ast);
2017-10-01 19:29:05 -07:00
let mut acc = String::new();
let mut iter = evaluation_output.iter().peekable();
while let Some(s) = iter.next() {
acc.push_str(&s);
if let Some(_) = iter.peek() {
acc.push_str("\n");
}
}
output.add_output(acc);
2017-08-31 20:59:43 -07:00
return output;
2017-08-29 04:15:31 -07:00
}
}