Compare commits
6 Commits
d01d280452
...
5dcfce46cc
Author | SHA1 | Date | |
---|---|---|---|
|
5dcfce46cc | ||
|
76c2257c7e | ||
|
3cbe80e933 | ||
|
0f7e568341 | ||
|
75b1f9cce5 | ||
|
c3131a6d5e |
@ -152,11 +152,10 @@ impl Expression {
|
||||
Expression { id, kind, type_anno: None }
|
||||
}
|
||||
|
||||
/* - commented out because unused
|
||||
#[cfg(test)]
|
||||
pub fn with_anno(id: ItemId, kind: ExpressionKind, type_anno: TypeIdentifier) -> Expression {
|
||||
Expression { id, kind, type_anno: Some(type_anno) }
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
|
@ -1,10 +0,0 @@
|
||||
use crate::ast::*;
|
||||
|
||||
impl AST {
|
||||
pub fn compact_debug(&self) -> String {
|
||||
format!("{:?}", self)
|
||||
}
|
||||
pub fn expanded_debug(&self) -> String {
|
||||
format!("{:#?}", self)
|
||||
}
|
||||
}
|
@ -20,10 +20,6 @@ impl<'a> State<'a> {
|
||||
State { values }
|
||||
}
|
||||
|
||||
pub fn debug_print(&self) -> String {
|
||||
format!("Values: {:?}", self.values)
|
||||
}
|
||||
|
||||
fn new_frame(&'a self, items: &'a Vec<Node>, bound_vars: &BoundVars) -> State<'a> {
|
||||
let mut inner_state = State {
|
||||
values: self.values.new_scope(None),
|
||||
|
@ -24,7 +24,6 @@ macro_rules! bx {
|
||||
mod util;
|
||||
#[macro_use]
|
||||
mod typechecking;
|
||||
mod debugging;
|
||||
|
||||
mod tokenizing;
|
||||
mod ast;
|
||||
|
@ -267,6 +267,7 @@ impl Parser {
|
||||
self.program()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn format_parse_trace(&self) -> String {
|
||||
let mut buf = String::new();
|
||||
buf.push_str("Parse productions:\n");
|
||||
|
@ -1,16 +1,13 @@
|
||||
use stopwatch::Stopwatch;
|
||||
|
||||
use std::time::Duration;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use itertools::Itertools;
|
||||
use schala_repl::{ProgrammingLanguageInterface,
|
||||
ComputationRequest, ComputationResponse,
|
||||
LangMetaRequest, LangMetaResponse, GlobalOutputStats,
|
||||
DebugResponse, DebugAsk};
|
||||
use crate::{ast, reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map};
|
||||
LangMetaRequest, LangMetaResponse, GlobalOutputStats};
|
||||
use crate::{reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map};
|
||||
|
||||
pub type SymbolTableHandle = Rc<RefCell<symbol_table::SymbolTable>>;
|
||||
pub type SourceMapHandle = Rc<RefCell<source_map::SourceMap>>;
|
||||
@ -30,11 +27,14 @@ pub struct Schala {
|
||||
}
|
||||
|
||||
impl Schala {
|
||||
//TODO implement documentation for language items
|
||||
/*
|
||||
fn handle_docs(&self, source: String) -> LangMetaResponse {
|
||||
LangMetaResponse::Docs {
|
||||
doc_string: format!("Schala item `{}` : <<Schala-lang documentation not yet implemented>>", source)
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl Schala {
|
||||
@ -60,6 +60,7 @@ impl Schala {
|
||||
let prelude = include_str!("prelude.schala");
|
||||
let mut s = Schala::new_blank_env();
|
||||
|
||||
//TODO this shouldn't depend on schala-repl types
|
||||
let request = ComputationRequest { source: prelude, debug_requests: HashSet::default() };
|
||||
let response = s.run_computation(request);
|
||||
if let Err(msg) = response.main_output {
|
||||
@ -68,69 +69,55 @@ impl Schala {
|
||||
s
|
||||
}
|
||||
|
||||
fn handle_debug_immediate(&self, request: DebugAsk) -> DebugResponse {
|
||||
use DebugAsk::*;
|
||||
match request {
|
||||
Timing => DebugResponse { ask: Timing, value: format!("Invalid") },
|
||||
ByStage { stage_name, token } => match &stage_name[..] {
|
||||
"symbol-table" => {
|
||||
let value = self.symbol_table.borrow().debug_symbol_table();
|
||||
DebugResponse {
|
||||
ask: ByStage { stage_name: format!("symbol-table"), token },
|
||||
value
|
||||
}
|
||||
},
|
||||
s => {
|
||||
DebugResponse {
|
||||
ask: ByStage { stage_name: s.to_string(), token: None },
|
||||
value: format!("Not-implemented")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// This is where the actual action of interpreting/compilation happens.
|
||||
/// Note: this should eventually use a query-based system for parallelization, cf.
|
||||
/// https://rustc-dev-guide.rust-lang.org/overview.html
|
||||
fn run_pipeline(&mut self, source: &str) -> Result<String, String> {
|
||||
//TODO `run_pipeline` should return a formatted error struct
|
||||
|
||||
//TODO every stage should have a common error-format that gets turned into a repl-appropriate
|
||||
//string in `run_computation`
|
||||
// 1st stage - tokenization
|
||||
// TODO tokenize should return its own error type
|
||||
let tokens = tokenizing::tokenize(source);
|
||||
let token_errors: Vec<String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||
if token_errors.len() > 0 {
|
||||
return Err(format!("{:?}", token_errors));
|
||||
}
|
||||
|
||||
fn tokenizing(input: &str, _handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<Vec<tokenizing::Token>, String> {
|
||||
let tokens = tokenizing::tokenize(input);
|
||||
comp.map(|comp| {
|
||||
let token_string = tokens.iter().map(|t| t.to_string_with_metadata()).join(", ");
|
||||
comp.add_artifact(token_string);
|
||||
});
|
||||
//2nd stage - parsing
|
||||
self.active_parser.add_new_tokens(tokens);
|
||||
let mut ast = self.active_parser.parse()
|
||||
.map_err(|err| format_parse_error(err, &self.source_reference))?;
|
||||
|
||||
let errors: Vec<String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||
if errors.len() == 0 {
|
||||
Ok(tokens)
|
||||
} else {
|
||||
Err(format!("{:?}", errors))
|
||||
// Symbol table
|
||||
self.symbol_table.borrow_mut().add_top_level_symbols(&ast)?;
|
||||
|
||||
// Scope resolution - requires mutating AST
|
||||
self.resolver.resolve(&mut ast)?;
|
||||
|
||||
// Typechecking
|
||||
// TODO typechecking not working
|
||||
let _overall_type = self.type_context.typecheck(&ast)
|
||||
.map_err(|err| format!("Type error: {}", err.msg));
|
||||
|
||||
// Reduce AST - this doesn't produce an error yet, but probably should
|
||||
let symbol_table = self.symbol_table.borrow();
|
||||
let reduced_ast = reduced_ast::reduce(&ast, &symbol_table);
|
||||
|
||||
// Tree-walking evaluator. TODO fix this
|
||||
let evaluation_outputs = self.state.evaluate(reduced_ast, true);
|
||||
let text_output: Result<Vec<String>, String> = evaluation_outputs
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let eval_output: String = text_output
|
||||
.map(|v| { Iterator::intersperse(v.into_iter(), "\n".to_owned()).collect() })?;
|
||||
|
||||
Ok(eval_output)
|
||||
}
|
||||
}
|
||||
|
||||
fn parsing(input: Vec<tokenizing::Token>, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<ast::AST, String> {
|
||||
use ParsingDebugType::*;
|
||||
|
||||
let ref mut parser = handle.active_parser;
|
||||
parser.add_new_tokens(input);
|
||||
let ast = parser.parse();
|
||||
|
||||
comp.map(|comp| {
|
||||
let debug_format = comp.parsing.as_ref().unwrap_or(&CompactAST);
|
||||
let debug_info = match debug_format {
|
||||
CompactAST => match ast{
|
||||
Ok(ref ast) => ast.compact_debug(),
|
||||
Err(_) => "Error - see output".to_string(),
|
||||
},
|
||||
ExpandedAST => match ast{
|
||||
Ok(ref ast) => ast.expanded_debug(),
|
||||
Err(_) => "Error - see output".to_string(),
|
||||
},
|
||||
Trace => parser.format_parse_trace(),
|
||||
};
|
||||
comp.add_artifact(debug_info);
|
||||
});
|
||||
ast.map_err(|err| format_parse_error(err, &handle.source_reference))
|
||||
}
|
||||
|
||||
fn format_parse_error(error: parsing::ParseError, source_reference: &SourceReference) -> String {
|
||||
let line_num = error.token.location.line_num;
|
||||
let ch = error.token.location.char_num;
|
||||
@ -154,52 +141,6 @@ fn format_parse_error(error: parsing::ParseError, source_reference: &SourceRefer
|
||||
)
|
||||
}
|
||||
|
||||
fn symbol_table(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<ast::AST, String> {
|
||||
let () = handle.symbol_table.borrow_mut().add_top_level_symbols(&input)?;
|
||||
comp.map(|comp| {
|
||||
let debug = handle.symbol_table.borrow().debug_symbol_table();
|
||||
comp.add_artifact(debug);
|
||||
});
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
fn scope_resolution(mut input: ast::AST, handle: &mut Schala, _com: Option<&mut PassDebugArtifact>) -> Result<ast::AST, String> {
|
||||
let () = handle.resolver.resolve(&mut input)?;
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
fn typechecking(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<ast::AST, String> {
|
||||
let result = handle.type_context.typecheck(&input);
|
||||
|
||||
comp.map(|comp| {
|
||||
comp.add_artifact(match result {
|
||||
Ok(ty) => ty.to_string(),
|
||||
Err(err) => format!("Type error: {}", err.msg)
|
||||
});
|
||||
});
|
||||
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
fn ast_reducing(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<reduced_ast::ReducedAST, String> {
|
||||
let ref symbol_table = handle.symbol_table.borrow();
|
||||
let output = reduced_ast::reduce(&input, symbol_table);
|
||||
comp.map(|comp| comp.add_artifact(format!("{:?}", output)));
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn eval(input: reduced_ast::ReducedAST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<String, String> {
|
||||
comp.map(|comp| comp.add_artifact(handle.state.debug_print()));
|
||||
let evaluation_outputs = handle.state.evaluate(input, true);
|
||||
let text_output: Result<Vec<String>, String> = evaluation_outputs
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let eval_output: Result<String, String> = text_output
|
||||
.map(|v| { Iterator::intersperse(v.into_iter(), "\n".to_owned()).collect() });
|
||||
eval_output
|
||||
}
|
||||
|
||||
/// Represents lines of source code
|
||||
struct SourceReference {
|
||||
lines: Option<Vec<String>>
|
||||
@ -219,24 +160,6 @@ impl SourceReference {
|
||||
}
|
||||
}
|
||||
|
||||
enum ParsingDebugType {
|
||||
CompactAST,
|
||||
ExpandedAST,
|
||||
Trace
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct PassDebugArtifact {
|
||||
parsing: Option<ParsingDebugType>,
|
||||
artifacts: Vec<String>
|
||||
|
||||
}
|
||||
impl PassDebugArtifact {
|
||||
fn add_artifact(&mut self, artifact: String) {
|
||||
self.artifacts.push(artifact)
|
||||
}
|
||||
}
|
||||
|
||||
fn stage_names() -> Vec<&'static str> {
|
||||
vec![
|
||||
"tokenizing",
|
||||
@ -251,94 +174,38 @@ fn stage_names() -> Vec<&'static str> {
|
||||
|
||||
|
||||
impl ProgrammingLanguageInterface for Schala {
|
||||
fn language_name(&self) -> String {
|
||||
type Options = ();
|
||||
fn language_name() -> String {
|
||||
"Schala".to_owned()
|
||||
}
|
||||
|
||||
fn source_file_suffix(&self) -> String {
|
||||
fn source_file_suffix() -> String {
|
||||
"schala".to_owned()
|
||||
}
|
||||
|
||||
fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse {
|
||||
struct PassToken<'a> {
|
||||
schala: &'a mut Schala,
|
||||
stage_durations: &'a mut Vec<(String, Duration)>,
|
||||
sw: &'a Stopwatch,
|
||||
debug_requests: &'a HashSet<DebugAsk>,
|
||||
debug_responses: &'a mut Vec<DebugResponse>,
|
||||
}
|
||||
|
||||
fn output_wrapper<Input, Output, F>(n: usize, func: F, input: Input, token: &mut PassToken) -> Result<Output, String>
|
||||
where F: Fn(Input, &mut Schala, Option<&mut PassDebugArtifact>) -> Result<Output, String>
|
||||
{
|
||||
let stage_names = stage_names();
|
||||
let cur_stage_name = stage_names[n];
|
||||
let ask = token.debug_requests.iter().find(|ask| ask.is_for_stage(cur_stage_name));
|
||||
|
||||
let parsing = match ask {
|
||||
Some(DebugAsk::ByStage { token, .. }) if cur_stage_name == "parsing" => Some(
|
||||
token.as_ref().map(|token| match &token[..] {
|
||||
"compact" => ParsingDebugType::CompactAST,
|
||||
"expanded" => ParsingDebugType::ExpandedAST,
|
||||
"trace" => ParsingDebugType::Trace,
|
||||
_ => ParsingDebugType::CompactAST,
|
||||
}).unwrap_or(ParsingDebugType::CompactAST)
|
||||
),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let mut debug_artifact = ask.map(|_| PassDebugArtifact {
|
||||
parsing, ..Default::default()
|
||||
});
|
||||
|
||||
let output = func(input, token.schala, debug_artifact.as_mut());
|
||||
|
||||
//TODO I think this is not counting the time since the *previous* stage
|
||||
token.stage_durations.push((cur_stage_name.to_string(), token.sw.elapsed()));
|
||||
if let Some(artifact) = debug_artifact {
|
||||
for value in artifact.artifacts.into_iter() {
|
||||
let resp = DebugResponse { ask: ask.unwrap().clone(), value };
|
||||
token.debug_responses.push(resp);
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
let ComputationRequest { source, debug_requests } = request;
|
||||
let ComputationRequest { source, debug_requests: _ } = request;
|
||||
self.source_reference.load_new_source(source);
|
||||
let sw = Stopwatch::start_new();
|
||||
let mut stage_durations = Vec::new();
|
||||
let mut debug_responses = Vec::new();
|
||||
let mut tok = PassToken { schala: self, stage_durations: &mut stage_durations, sw: &sw, debug_requests: &debug_requests, debug_responses: &mut debug_responses };
|
||||
|
||||
let main_output: Result<String, String> = Ok(source)
|
||||
.and_then(|source| output_wrapper(0, tokenizing, source, &mut tok))
|
||||
.and_then(|tokens| output_wrapper(1, parsing, tokens, &mut tok))
|
||||
.and_then(|ast| output_wrapper(2, symbol_table, ast, &mut tok))
|
||||
.and_then(|ast| output_wrapper(3, scope_resolution, ast, &mut tok))
|
||||
.and_then(|ast| output_wrapper(4, typechecking, ast, &mut tok))
|
||||
.and_then(|ast| output_wrapper(5, ast_reducing, ast, &mut tok))
|
||||
.and_then(|reduced_ast| output_wrapper(6, eval, reduced_ast, &mut tok));
|
||||
let main_output = self.run_pipeline(source);
|
||||
|
||||
let total_duration = sw.elapsed();
|
||||
let global_output_stats = GlobalOutputStats {
|
||||
total_duration, stage_durations
|
||||
total_duration: sw.elapsed(),
|
||||
stage_durations: vec![]
|
||||
};
|
||||
|
||||
ComputationResponse {
|
||||
main_output,
|
||||
global_output_stats,
|
||||
debug_responses,
|
||||
debug_responses: vec![]
|
||||
}
|
||||
}
|
||||
|
||||
fn request_meta(&mut self, request: LangMetaRequest) -> LangMetaResponse {
|
||||
match request {
|
||||
LangMetaRequest::StageNames => LangMetaResponse::StageNames(stage_names().iter().map(|s| s.to_string()).collect()),
|
||||
LangMetaRequest::Docs { source } => self.handle_docs(source),
|
||||
LangMetaRequest::ImmediateDebug(debug_request) =>
|
||||
LangMetaResponse::ImmediateDebug(self.handle_debug_immediate(debug_request)),
|
||||
LangMetaRequest::Custom { .. } => LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") }
|
||||
_ => LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -242,6 +242,7 @@ impl SymbolTable {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn debug_symbol_table(&self) -> String {
|
||||
let mut output = format!("Symbol table\n");
|
||||
let mut sorted_symbols: Vec<(&FullyQualifiedSymbolName, &Symbol)> = self.symbol_path_to_symbol.iter().collect();
|
||||
|
@ -77,6 +77,7 @@ pub enum TypeConst {
|
||||
}
|
||||
|
||||
impl TypeConst {
|
||||
#[allow(dead_code)]
|
||||
pub fn to_string(&self) -> String {
|
||||
use self::TypeConst::*;
|
||||
match self {
|
||||
@ -107,6 +108,7 @@ macro_rules! ty {
|
||||
|
||||
//TODO find a better way to capture the to/from string logic
|
||||
impl Type {
|
||||
#[allow(dead_code)]
|
||||
pub fn to_string(&self) -> String {
|
||||
use self::Type::*;
|
||||
match self {
|
||||
|
@ -1,5 +1,6 @@
|
||||
use super::{InterpreterDirectiveOutput, Repl};
|
||||
use crate::repl::directive_actions::DirectiveAction;
|
||||
use crate::directive_actions::DirectiveAction;
|
||||
use crate::language::ProgrammingLanguageInterface;
|
||||
use crate::{InterpreterDirectiveOutput, Repl};
|
||||
use colored::*;
|
||||
|
||||
/// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
|
||||
@ -85,7 +86,11 @@ impl CommandTree {
|
||||
self.get_children().iter().map(|x| x.get_cmd()).collect()
|
||||
}
|
||||
|
||||
pub fn perform(&self, repl: &mut Repl, arguments: &Vec<&str>) -> InterpreterDirectiveOutput {
|
||||
pub fn perform<L: ProgrammingLanguageInterface>(
|
||||
&self,
|
||||
repl: &mut Repl<L>,
|
||||
arguments: &Vec<&str>,
|
||||
) -> InterpreterDirectiveOutput {
|
||||
let mut dir_pointer: &CommandTree = self;
|
||||
let mut idx = 0;
|
||||
|
@ -1,6 +1,8 @@
|
||||
use super::{InterpreterDirectiveOutput, Repl};
|
||||
use crate::language::{DebugAsk, DebugResponse, LangMetaRequest, LangMetaResponse};
|
||||
use crate::repl::help::help;
|
||||
use crate::help::help;
|
||||
use crate::language::{
|
||||
DebugAsk, DebugResponse, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
|
||||
};
|
||||
use crate::{InterpreterDirectiveOutput, Repl};
|
||||
use std::fmt::Write as FmtWrite;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -20,7 +22,11 @@ pub enum DirectiveAction {
|
||||
}
|
||||
|
||||
impl DirectiveAction {
|
||||
pub fn perform(&self, repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
|
||||
pub fn perform<L: ProgrammingLanguageInterface>(
|
||||
&self,
|
||||
repl: &mut Repl<L>,
|
||||
arguments: &[&str],
|
||||
) -> InterpreterDirectiveOutput {
|
||||
use DirectiveAction::*;
|
||||
match self {
|
||||
Null => None,
|
||||
@ -30,8 +36,10 @@ impl DirectiveAction {
|
||||
::std::process::exit(0)
|
||||
}
|
||||
ListPasses => {
|
||||
let language_state = repl.get_cur_language_state();
|
||||
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
|
||||
let pass_names = match repl
|
||||
.language_state
|
||||
.request_meta(LangMetaRequest::StageNames)
|
||||
{
|
||||
LangMetaResponse::StageNames(names) => names,
|
||||
_ => vec![],
|
||||
};
|
||||
@ -46,7 +54,6 @@ impl DirectiveAction {
|
||||
Some(buf)
|
||||
}
|
||||
ShowImmediate => {
|
||||
let cur_state = repl.get_cur_language_state();
|
||||
let stage_name = match arguments.get(0) {
|
||||
Some(s) => s.to_string(),
|
||||
None => return Some(format!("Must specify a thing to debug")),
|
||||
@ -55,7 +62,7 @@ impl DirectiveAction {
|
||||
stage_name: stage_name.clone(),
|
||||
token: None,
|
||||
});
|
||||
let meta_response = cur_state.request_meta(meta);
|
||||
let meta_response = repl.language_state.request_meta(meta);
|
||||
|
||||
let response = match meta_response {
|
||||
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => match ask {
|
||||
@ -100,43 +107,37 @@ impl DirectiveAction {
|
||||
});
|
||||
None
|
||||
}
|
||||
TotalTimeOff => total_time_off(repl, arguments),
|
||||
TotalTimeOn => total_time_on(repl, arguments),
|
||||
StageTimeOff => stage_time_off(repl, arguments),
|
||||
StageTimeOn => stage_time_on(repl, arguments),
|
||||
TotalTimeOff => {
|
||||
repl.options.show_total_time = false;
|
||||
None
|
||||
}
|
||||
TotalTimeOn => {
|
||||
repl.options.show_total_time = true;
|
||||
None
|
||||
}
|
||||
StageTimeOff => {
|
||||
repl.options.show_stage_times = false;
|
||||
None
|
||||
}
|
||||
StageTimeOn => {
|
||||
repl.options.show_stage_times = true;
|
||||
None
|
||||
}
|
||||
Doc => doc(repl, arguments),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn total_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
||||
repl.options.show_total_time = true;
|
||||
None
|
||||
}
|
||||
|
||||
fn total_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
||||
repl.options.show_total_time = false;
|
||||
None
|
||||
}
|
||||
|
||||
fn stage_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
||||
repl.options.show_stage_times = true;
|
||||
None
|
||||
}
|
||||
|
||||
fn stage_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
||||
repl.options.show_stage_times = false;
|
||||
None
|
||||
}
|
||||
|
||||
fn doc(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
|
||||
fn doc<L: ProgrammingLanguageInterface>(
|
||||
repl: &mut Repl<L>,
|
||||
arguments: &[&str],
|
||||
) -> InterpreterDirectiveOutput {
|
||||
arguments
|
||||
.get(0)
|
||||
.map(|cmd| {
|
||||
let source = cmd.to_string();
|
||||
let meta = LangMetaRequest::Docs { source };
|
||||
let cur_state = repl.get_cur_language_state();
|
||||
match cur_state.request_meta(meta) {
|
||||
match repl.language_state.request_meta(meta) {
|
||||
LangMetaResponse::Docs { doc_string } => Some(doc_string),
|
||||
_ => Some(format!("Invalid doc response")),
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
use crate::repl::command_tree::CommandTree;
|
||||
use crate::repl::directive_actions::DirectiveAction;
|
||||
use crate::command_tree::CommandTree;
|
||||
use crate::directive_actions::DirectiveAction;
|
||||
|
||||
pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
|
||||
let passes_directives: Vec<CommandTree> = pass_names
|
||||
@ -28,6 +28,7 @@ fn get_list(passes_directives: &Vec<CommandTree>, include_help: bool) -> Vec<Com
|
||||
|
||||
vec![
|
||||
CommandTree::terminal("exit", Some("exit the REPL"), vec![], QuitProgram),
|
||||
//TODO there should be an alias for this
|
||||
CommandTree::terminal("quit", Some("exit the REPL"), vec![], QuitProgram),
|
||||
CommandTree::terminal(
|
||||
"help",
|
||||
@ -85,15 +86,6 @@ fn get_list(passes_directives: &Vec<CommandTree>, include_help: bool) -> Vec<Com
|
||||
),
|
||||
],
|
||||
),
|
||||
CommandTree::nonterm(
|
||||
"lang",
|
||||
Some("switch between languages, or go directly to a langauge by name"),
|
||||
vec![
|
||||
CommandTree::nonterm_no_further_tab_completions("next", None),
|
||||
CommandTree::nonterm_no_further_tab_completions("prev", None),
|
||||
CommandTree::nonterm("go", None, vec![]),
|
||||
],
|
||||
),
|
||||
CommandTree::terminal(
|
||||
"doc",
|
||||
Some("Get language-specific help for an item"),
|
@ -1,10 +1,14 @@
|
||||
use std::fmt::Write as FmtWrite;
|
||||
|
||||
use super::command_tree::CommandTree;
|
||||
use super::{InterpreterDirectiveOutput, Repl};
|
||||
use crate::command_tree::CommandTree;
|
||||
use crate::language::ProgrammingLanguageInterface;
|
||||
use crate::{InterpreterDirectiveOutput, Repl};
|
||||
use colored::*;
|
||||
|
||||
pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
|
||||
pub fn help<L: ProgrammingLanguageInterface>(
|
||||
repl: &mut Repl<L>,
|
||||
arguments: &[&str],
|
||||
) -> InterpreterDirectiveOutput {
|
||||
match arguments {
|
||||
[] => return global_help(repl),
|
||||
commands => {
|
||||
@ -46,7 +50,7 @@ fn get_directive_from_commands<'a>(
|
||||
matched_directive
|
||||
}
|
||||
|
||||
fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
|
||||
fn global_help<L: ProgrammingLanguageInterface>(repl: &mut Repl<L>) -> InterpreterDirectiveOutput {
|
||||
let mut buf = String::new();
|
||||
|
||||
writeln!(
|
||||
@ -69,12 +73,11 @@ fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let ref lang = repl.get_cur_language_state();
|
||||
writeln!(buf, "").unwrap();
|
||||
writeln!(
|
||||
buf,
|
||||
"Language-specific help for {}",
|
||||
lang.language_name()
|
||||
<L as ProgrammingLanguageInterface>::language_name()
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(buf, "-----------------------").unwrap();
|
@ -2,8 +2,9 @@ use std::collections::HashSet;
|
||||
use std::time;
|
||||
|
||||
pub trait ProgrammingLanguageInterface {
|
||||
fn language_name(&self) -> String;
|
||||
fn source_file_suffix(&self) -> String;
|
||||
type Options;
|
||||
fn language_name() -> String;
|
||||
fn source_file_suffix() -> String;
|
||||
|
||||
fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse;
|
||||
|
||||
@ -15,8 +16,16 @@ pub trait ProgrammingLanguageInterface {
|
||||
}
|
||||
}
|
||||
|
||||
//TODO this is what I want
|
||||
/*
|
||||
struct Options<T> {
|
||||
lang_options: T
|
||||
}
|
||||
*/
|
||||
|
||||
pub struct ComputationRequest<'a> {
|
||||
pub source: &'a str,
|
||||
//pub options: Options<()>,
|
||||
pub debug_requests: HashSet<DebugAsk>,
|
||||
}
|
||||
|
||||
|
@ -7,14 +7,21 @@ extern crate includedir;
|
||||
extern crate phf;
|
||||
extern crate serde_json;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use std::process::exit;
|
||||
|
||||
mod command_tree;
|
||||
mod language;
|
||||
mod repl;
|
||||
use self::command_tree::CommandTree;
|
||||
mod repl_options;
|
||||
use repl_options::ReplOptions;
|
||||
mod directive_actions;
|
||||
mod directives;
|
||||
use directives::directives_from_pass_names;
|
||||
mod help;
|
||||
mod response;
|
||||
use response::ReplResponse;
|
||||
|
||||
use colored::*;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub use language::{
|
||||
ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats,
|
||||
@ -24,46 +31,254 @@ pub use language::{
|
||||
include!(concat!(env!("OUT_DIR"), "/static.rs"));
|
||||
const VERSION_STRING: &'static str = "0.1.0";
|
||||
|
||||
pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) {
|
||||
let mut repl = repl::Repl::new(langs);
|
||||
repl.run_repl();
|
||||
const HISTORY_SAVE_FILE: &'static str = ".schala_history";
|
||||
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
|
||||
|
||||
type InterpreterDirectiveOutput = Option<String>;
|
||||
|
||||
pub struct Repl<L: ProgrammingLanguageInterface> {
|
||||
/// If this is the first character typed by a user into the repl, the following
|
||||
/// will be interpreted as a directive to the REPL rather than a command in the
|
||||
/// running programming language.
|
||||
sigil: char,
|
||||
line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>,
|
||||
language_state: L,
|
||||
options: ReplOptions,
|
||||
}
|
||||
|
||||
pub fn run_noninteractive(filenames: Vec<PathBuf>, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) {
|
||||
// for now, ony do something with the first filename
|
||||
#[derive(Clone)]
|
||||
enum PromptStyle {
|
||||
Normal,
|
||||
Multiline,
|
||||
}
|
||||
|
||||
let filename = &filenames[0];
|
||||
let ext = filename
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.unwrap_or_else(|| {
|
||||
println!("Source file `{}` has no extension.", filename.display());
|
||||
exit(1);
|
||||
});
|
||||
impl<L: ProgrammingLanguageInterface> Repl<L> {
|
||||
pub fn new(initial_state: L) -> Self {
|
||||
use linefeed::Interface;
|
||||
let line_reader = Interface::new("schala-repl").unwrap();
|
||||
let sigil = ':';
|
||||
|
||||
let mut language = Box::new(
|
||||
languages
|
||||
.into_iter()
|
||||
.find(|lang| lang.source_file_suffix() == ext)
|
||||
.unwrap_or_else(|| {
|
||||
println!("Extension .{} not recognized", ext);
|
||||
exit(1);
|
||||
}),
|
||||
Repl {
|
||||
sigil,
|
||||
line_reader,
|
||||
language_state: initial_state,
|
||||
options: ReplOptions::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_repl(&mut self) {
|
||||
println!("Schala meta-interpeter version {}", VERSION_STRING);
|
||||
println!(
|
||||
"Type {} for help with the REPL",
|
||||
format!("{}help", self.sigil).bright_green().bold()
|
||||
);
|
||||
self.load_options();
|
||||
self.handle_repl_loop();
|
||||
self.save_before_exit();
|
||||
println!("Exiting...");
|
||||
}
|
||||
|
||||
let mut source_file = File::open(filename).unwrap();
|
||||
let mut buffer = String::new();
|
||||
source_file.read_to_string(&mut buffer).unwrap();
|
||||
fn load_options(&mut self) {
|
||||
self.line_reader
|
||||
.load_history(HISTORY_SAVE_FILE)
|
||||
.unwrap_or(());
|
||||
match ReplOptions::load_from_file(OPTIONS_SAVE_FILE) {
|
||||
Ok(options) => {
|
||||
self.options = options;
|
||||
}
|
||||
Err(e) => eprintln!("{}", e),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_repl_loop(&mut self) {
|
||||
use linefeed::ReadResult::*;
|
||||
|
||||
'main: loop {
|
||||
macro_rules! match_or_break {
|
||||
($line:expr) => {
|
||||
match $line {
|
||||
Err(e) => {
|
||||
println!("readline IO Error: {}", e);
|
||||
break 'main;
|
||||
}
|
||||
Ok(Eof) | Ok(Signal(_)) => break 'main,
|
||||
Ok(Input(ref input)) => input,
|
||||
}
|
||||
};
|
||||
}
|
||||
self.update_line_reader();
|
||||
let line = self.line_reader.read_line();
|
||||
let input: &str = match_or_break!(line);
|
||||
|
||||
self.line_reader.add_history_unique(input.to_string());
|
||||
let mut chars = input.chars().peekable();
|
||||
let repl_responses = match chars.nth(0) {
|
||||
Some(ch) if ch == self.sigil => {
|
||||
if chars.peek() == Some(&'{') {
|
||||
let mut buf = String::new();
|
||||
buf.push_str(input.get(2..).unwrap());
|
||||
'multiline: loop {
|
||||
self.set_prompt(PromptStyle::Multiline);
|
||||
let new_line = self.line_reader.read_line();
|
||||
let new_input = match_or_break!(new_line);
|
||||
if new_input.starts_with(":}") {
|
||||
break 'multiline;
|
||||
} else {
|
||||
buf.push_str(new_input);
|
||||
buf.push_str("\n");
|
||||
}
|
||||
}
|
||||
self.handle_input(&buf)
|
||||
} else {
|
||||
match self.handle_interpreter_directive(input.get(1..).unwrap()) {
|
||||
Some(directive_output) => println!("{}", directive_output),
|
||||
None => (),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_ => self.handle_input(input),
|
||||
};
|
||||
|
||||
for repl_response in repl_responses.iter() {
|
||||
println!("{}", repl_response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_line_reader(&mut self) {
|
||||
let tab_complete_handler = TabCompleteHandler::new(self.sigil, self.get_directives());
|
||||
self.line_reader
|
||||
.set_completer(Arc::new(tab_complete_handler)); //TODO fix this here
|
||||
self.set_prompt(PromptStyle::Normal);
|
||||
}
|
||||
|
||||
fn set_prompt(&mut self, prompt_style: PromptStyle) {
|
||||
let prompt_str = match prompt_style {
|
||||
PromptStyle::Normal => ">> ",
|
||||
PromptStyle::Multiline => ">| ",
|
||||
};
|
||||
|
||||
self.line_reader.set_prompt(&prompt_str).unwrap();
|
||||
}
|
||||
|
||||
fn save_before_exit(&self) {
|
||||
self.line_reader
|
||||
.save_history(HISTORY_SAVE_FILE)
|
||||
.unwrap_or(());
|
||||
self.options.save_to_file(OPTIONS_SAVE_FILE);
|
||||
}
|
||||
|
||||
fn handle_interpreter_directive(&mut self, input: &str) -> InterpreterDirectiveOutput {
|
||||
let arguments: Vec<&str> = input.split_whitespace().collect();
|
||||
|
||||
if arguments.len() < 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let directives = self.get_directives();
|
||||
directives.perform(self, &arguments)
|
||||
}
|
||||
|
||||
fn handle_input(&mut self, input: &str) -> Vec<ReplResponse> {
|
||||
let mut debug_requests = HashSet::new();
|
||||
for ask in self.options.debug_asks.iter() {
|
||||
debug_requests.insert(ask.clone());
|
||||
}
|
||||
|
||||
let request = ComputationRequest {
|
||||
source: &buffer,
|
||||
debug_requests: HashSet::new(),
|
||||
};
|
||||
|
||||
let response = language.run_computation(request);
|
||||
match response.main_output {
|
||||
Ok(s) => println!("{}", s),
|
||||
Err(s) => println!("{}", s),
|
||||
source: input,
|
||||
debug_requests,
|
||||
};
|
||||
let response = self.language_state.run_computation(request);
|
||||
response::handle_computation_response(response, &self.options)
|
||||
}
|
||||
|
||||
fn get_directives(&mut self) -> CommandTree {
|
||||
let pass_names = match self
|
||||
.language_state
|
||||
.request_meta(LangMetaRequest::StageNames)
|
||||
{
|
||||
LangMetaResponse::StageNames(names) => names,
|
||||
_ => vec![],
|
||||
};
|
||||
|
||||
directives_from_pass_names(&pass_names)
|
||||
}
|
||||
}
|
||||
|
||||
struct TabCompleteHandler {
|
||||
sigil: char,
|
||||
top_level_commands: CommandTree,
|
||||
}
|
||||
|
||||
use linefeed::complete::{Completer, Completion};
|
||||
use linefeed::terminal::Terminal;
|
||||
|
||||
impl TabCompleteHandler {
|
||||
fn new(sigil: char, top_level_commands: CommandTree) -> TabCompleteHandler {
|
||||
TabCompleteHandler {
|
||||
top_level_commands,
|
||||
sigil,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Terminal> Completer<T> for TabCompleteHandler {
|
||||
fn complete(
|
||||
&self,
|
||||
word: &str,
|
||||
prompter: &::linefeed::prompter::Prompter<T>,
|
||||
start: usize,
|
||||
_end: usize,
|
||||
) -> Option<Vec<Completion>> {
|
||||
let line = prompter.buffer();
|
||||
|
||||
if !line.starts_with(self.sigil) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace();
|
||||
let mut completions = Vec::new();
|
||||
let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands);
|
||||
|
||||
loop {
|
||||
match words.next() {
|
||||
None => {
|
||||
let top = match command_tree {
|
||||
Some(CommandTree::Top(_)) => true,
|
||||
_ => false,
|
||||
};
|
||||
let word = if top { word.get(1..).unwrap() } else { word };
|
||||
for cmd in command_tree
|
||||
.map(|x| x.get_subcommands())
|
||||
.unwrap_or(vec![])
|
||||
.into_iter()
|
||||
{
|
||||
if cmd.starts_with(word) {
|
||||
completions.push(Completion {
|
||||
completion: format!("{}{}", if top { ":" } else { "" }, cmd),
|
||||
display: Some(cmd.to_string()),
|
||||
suffix: ::linefeed::complete::Suffix::Some(' '),
|
||||
})
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
Some(s) => {
|
||||
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
|
||||
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
|
||||
CommandTree::NonTerminal { children, .. } => {
|
||||
children.iter().find(|c| c.get_cmd() == s)
|
||||
}
|
||||
CommandTree::Terminal { children, .. } => {
|
||||
children.iter().find(|c| c.get_cmd() == s)
|
||||
}
|
||||
});
|
||||
command_tree = new_ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(completions)
|
||||
}
|
||||
}
|
||||
|
@ -1,274 +0,0 @@
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use colored::*;
|
||||
|
||||
use crate::language::{
|
||||
ComputationRequest, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
|
||||
};
|
||||
|
||||
mod command_tree;
|
||||
use self::command_tree::CommandTree;
|
||||
mod repl_options;
|
||||
use repl_options::ReplOptions;
|
||||
mod directive_actions;
|
||||
mod directives;
|
||||
use directives::directives_from_pass_names;
|
||||
mod help;
|
||||
mod response;
|
||||
use response::ReplResponse;
|
||||
|
||||
const HISTORY_SAVE_FILE: &'static str = ".schala_history";
|
||||
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
|
||||
|
||||
type InterpreterDirectiveOutput = Option<String>;
|
||||
|
||||
pub struct Repl {
|
||||
/// If this is the first character typed by a user into the repl, the following
|
||||
/// will be interpreted as a directive to the REPL rather than a command in the
|
||||
/// running programming language.
|
||||
sigil: char,
|
||||
line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>,
|
||||
language_states: Vec<Box<dyn ProgrammingLanguageInterface>>,
|
||||
options: ReplOptions,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum PromptStyle {
|
||||
Normal,
|
||||
Multiline,
|
||||
}
|
||||
|
||||
impl Repl {
|
||||
pub fn new(initial_states: Vec<Box<dyn ProgrammingLanguageInterface>>) -> Repl {
|
||||
use linefeed::Interface;
|
||||
let line_reader = Interface::new("schala-repl").unwrap();
|
||||
let sigil = ':';
|
||||
|
||||
Repl {
|
||||
sigil,
|
||||
line_reader,
|
||||
language_states: initial_states,
|
||||
options: ReplOptions::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_repl(&mut self) {
|
||||
println!("Schala meta-interpeter version {}", crate::VERSION_STRING);
|
||||
println!(
|
||||
"Type {} for help with the REPL", format!("{}help", self.sigil).bright_green().bold()
|
||||
);
|
||||
self.load_options();
|
||||
self.handle_repl_loop();
|
||||
self.save_before_exit();
|
||||
println!("Exiting...");
|
||||
}
|
||||
|
||||
fn load_options(&mut self) {
|
||||
self.line_reader
|
||||
.load_history(HISTORY_SAVE_FILE)
|
||||
.unwrap_or(());
|
||||
match ReplOptions::load_from_file(OPTIONS_SAVE_FILE) {
|
||||
Ok(options) => {
|
||||
self.options = options;
|
||||
}
|
||||
Err(e) => eprintln!("{}",e)
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_repl_loop(&mut self) {
|
||||
use linefeed::ReadResult::*;
|
||||
|
||||
'main: loop {
|
||||
macro_rules! match_or_break {
|
||||
($line:expr) => {
|
||||
match $line {
|
||||
Err(e) => {
|
||||
println!("readline IO Error: {}", e);
|
||||
break 'main;
|
||||
}
|
||||
Ok(Eof) | Ok(Signal(_)) => break 'main,
|
||||
Ok(Input(ref input)) => input,
|
||||
}
|
||||
};
|
||||
}
|
||||
self.update_line_reader();
|
||||
let line = self.line_reader.read_line();
|
||||
let input: &str = match_or_break!(line);
|
||||
|
||||
self.line_reader.add_history_unique(input.to_string());
|
||||
let mut chars = input.chars().peekable();
|
||||
let repl_responses = match chars.nth(0) {
|
||||
Some(ch) if ch == self.sigil => {
|
||||
if chars.peek() == Some(&'{') {
|
||||
let mut buf = String::new();
|
||||
buf.push_str(input.get(2..).unwrap());
|
||||
'multiline: loop {
|
||||
self.set_prompt(PromptStyle::Multiline);
|
||||
let new_line = self.line_reader.read_line();
|
||||
let new_input = match_or_break!(new_line);
|
||||
if new_input.starts_with(":}") {
|
||||
break 'multiline;
|
||||
} else {
|
||||
buf.push_str(new_input);
|
||||
buf.push_str("\n");
|
||||
}
|
||||
}
|
||||
self.handle_input(&buf)
|
||||
} else {
|
||||
match self.handle_interpreter_directive(input.get(1..).unwrap()) {
|
||||
Some(directive_output) => println!("{}", directive_output),
|
||||
None => (),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_ => self.handle_input(input),
|
||||
};
|
||||
|
||||
for repl_response in repl_responses.iter() {
|
||||
println!("{}", repl_response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_line_reader(&mut self) {
|
||||
let tab_complete_handler =
|
||||
TabCompleteHandler::new(self.sigil, self.get_directives());
|
||||
self.line_reader
|
||||
.set_completer(Arc::new(tab_complete_handler)); //TODO fix this here
|
||||
self.set_prompt(PromptStyle::Normal);
|
||||
}
|
||||
|
||||
fn set_prompt(&mut self, prompt_style: PromptStyle) {
|
||||
let prompt_str = match prompt_style {
|
||||
PromptStyle::Normal => ">> ",
|
||||
PromptStyle::Multiline => ">| ",
|
||||
};
|
||||
|
||||
self.line_reader.set_prompt(&prompt_str).unwrap();
|
||||
}
|
||||
|
||||
fn save_before_exit(&self) {
|
||||
self.line_reader
|
||||
.save_history(HISTORY_SAVE_FILE)
|
||||
.unwrap_or(());
|
||||
self.options.save_to_file(OPTIONS_SAVE_FILE);
|
||||
}
|
||||
|
||||
fn handle_interpreter_directive(&mut self, input: &str) -> InterpreterDirectiveOutput {
|
||||
let arguments: Vec<&str> = input.split_whitespace().collect();
|
||||
|
||||
if arguments.len() < 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let directives = self.get_directives();
|
||||
directives.perform(self, &arguments)
|
||||
}
|
||||
|
||||
fn get_cur_language_state(&mut self) -> &mut Box<dyn ProgrammingLanguageInterface> {
|
||||
//TODO this is obviously not complete
|
||||
&mut self.language_states[0]
|
||||
}
|
||||
|
||||
fn handle_input(&mut self, input: &str) -> Vec<ReplResponse> {
|
||||
let mut debug_requests = HashSet::new();
|
||||
for ask in self.options.debug_asks.iter() {
|
||||
debug_requests.insert(ask.clone());
|
||||
}
|
||||
|
||||
let request = ComputationRequest {
|
||||
source: input,
|
||||
debug_requests,
|
||||
};
|
||||
let ref mut language_state = self.get_cur_language_state();
|
||||
let response = language_state.run_computation(request);
|
||||
response::handle_computation_response(response, &self.options)
|
||||
}
|
||||
|
||||
fn get_directives(&mut self) -> CommandTree {
|
||||
let language_state = self.get_cur_language_state();
|
||||
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
|
||||
LangMetaResponse::StageNames(names) => names,
|
||||
_ => vec![],
|
||||
};
|
||||
|
||||
directives_from_pass_names(&pass_names)
|
||||
}
|
||||
}
|
||||
|
||||
struct TabCompleteHandler {
|
||||
sigil: char,
|
||||
top_level_commands: CommandTree,
|
||||
}
|
||||
|
||||
use linefeed::complete::{Completer, Completion};
|
||||
use linefeed::terminal::Terminal;
|
||||
|
||||
impl TabCompleteHandler {
|
||||
fn new(sigil: char, top_level_commands: CommandTree) -> TabCompleteHandler {
|
||||
TabCompleteHandler {
|
||||
top_level_commands,
|
||||
sigil,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Terminal> Completer<T> for TabCompleteHandler {
|
||||
fn complete(
|
||||
&self,
|
||||
word: &str,
|
||||
prompter: &::linefeed::prompter::Prompter<T>,
|
||||
start: usize,
|
||||
_end: usize,
|
||||
) -> Option<Vec<Completion>> {
|
||||
let line = prompter.buffer();
|
||||
|
||||
if !line.starts_with(self.sigil) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace();
|
||||
let mut completions = Vec::new();
|
||||
let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands);
|
||||
|
||||
loop {
|
||||
match words.next() {
|
||||
None => {
|
||||
let top = match command_tree {
|
||||
Some(CommandTree::Top(_)) => true,
|
||||
_ => false,
|
||||
};
|
||||
let word = if top { word.get(1..).unwrap() } else { word };
|
||||
for cmd in command_tree
|
||||
.map(|x| x.get_subcommands())
|
||||
.unwrap_or(vec![])
|
||||
.into_iter()
|
||||
{
|
||||
if cmd.starts_with(word) {
|
||||
completions.push(Completion {
|
||||
completion: format!("{}{}", if top { ":" } else { "" }, cmd),
|
||||
display: Some(cmd.to_string()),
|
||||
suffix: ::linefeed::complete::Suffix::Some(' '),
|
||||
})
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
Some(s) => {
|
||||
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
|
||||
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
|
||||
CommandTree::NonTerminal { children, .. } => {
|
||||
children.iter().find(|c| c.get_cmd() == s)
|
||||
}
|
||||
CommandTree::Terminal { children, .. } => {
|
||||
children.iter().find(|c| c.get_cmd() == s)
|
||||
}
|
||||
});
|
||||
command_tree = new_ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(completions)
|
||||
}
|
||||
}
|
@ -2,8 +2,8 @@ use colored::*;
|
||||
use std::fmt;
|
||||
use std::fmt::Write;
|
||||
|
||||
use super::ReplOptions;
|
||||
use crate::language::{ComputationResponse, DebugAsk};
|
||||
use crate::ReplOptions;
|
||||
|
||||
pub struct ReplResponse {
|
||||
label: Option<String>,
|
52
src/main.rs
52
src/main.rs
@ -1,8 +1,9 @@
|
||||
use schala_repl::{run_noninteractive, start_repl, ProgrammingLanguageInterface};
|
||||
use schala_repl::{Repl, ProgrammingLanguageInterface, ComputationRequest};
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::process::exit;
|
||||
use std::{fs::File, io::Read, path::PathBuf, process::exit, collections::HashSet};
|
||||
use schala_lang::Schala;
|
||||
|
||||
//TODO specify multiple langs, and have a way to switch between them
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let matches = command_line_options()
|
||||
@ -17,16 +18,49 @@ fn main() {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
let langs: Vec<Box<dyn ProgrammingLanguageInterface>> =
|
||||
vec![Box::new(schala_lang::Schala::new())];
|
||||
|
||||
if matches.free.is_empty() {
|
||||
start_repl(langs);
|
||||
let state = Schala::new();
|
||||
let mut repl = Repl::new(state);
|
||||
repl.run_repl();
|
||||
} else {
|
||||
let paths = matches.free.iter().map(PathBuf::from).collect();
|
||||
run_noninteractive(paths, langs);
|
||||
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() {
|
||||
run_noninteractive(paths, Schala::new());
|
||||
} else {
|
||||
eprintln!("Extension .{} not recognized", extension);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_noninteractive<L: ProgrammingLanguageInterface>(filenames: Vec<PathBuf>, mut language: L) {
|
||||
// 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,
|
||||
debug_requests: HashSet::new(),
|
||||
};
|
||||
|
||||
let response = language.run_computation(request);
|
||||
match response.main_output {
|
||||
Ok(s) => println!("{}", s),
|
||||
Err(s) => eprintln!("{}", s),
|
||||
};
|
||||
}
|
||||
|
||||
fn command_line_options() -> getopts::Options {
|
||||
let mut options = getopts::Options::new();
|
||||
|
Loading…
Reference in New Issue
Block a user