Compare commits
No commits in common. "5dcfce46cce4f08735407a4274575ee395dc3137" and "d01d280452d3ee62c86651a187fc2cf00f3950d9" have entirely different histories.
5dcfce46cc
...
d01d280452
@ -152,10 +152,11 @@ impl Expression {
|
|||||||
Expression { id, kind, type_anno: None }
|
Expression { id, kind, type_anno: None }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
/* - commented out because unused
|
||||||
pub fn with_anno(id: ItemId, kind: ExpressionKind, type_anno: TypeIdentifier) -> Expression {
|
pub fn with_anno(id: ItemId, kind: ExpressionKind, type_anno: TypeIdentifier) -> Expression {
|
||||||
Expression { id, kind, type_anno: Some(type_anno) }
|
Expression { id, kind, type_anno: Some(type_anno) }
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
10
schala-lang/language/src/debugging.rs
Normal file
10
schala-lang/language/src/debugging.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use crate::ast::*;
|
||||||
|
|
||||||
|
impl AST {
|
||||||
|
pub fn compact_debug(&self) -> String {
|
||||||
|
format!("{:?}", self)
|
||||||
|
}
|
||||||
|
pub fn expanded_debug(&self) -> String {
|
||||||
|
format!("{:#?}", self)
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,10 @@ impl<'a> State<'a> {
|
|||||||
State { values }
|
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> {
|
fn new_frame(&'a self, items: &'a Vec<Node>, bound_vars: &BoundVars) -> State<'a> {
|
||||||
let mut inner_state = State {
|
let mut inner_state = State {
|
||||||
values: self.values.new_scope(None),
|
values: self.values.new_scope(None),
|
||||||
|
@ -24,6 +24,7 @@ macro_rules! bx {
|
|||||||
mod util;
|
mod util;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod typechecking;
|
mod typechecking;
|
||||||
|
mod debugging;
|
||||||
|
|
||||||
mod tokenizing;
|
mod tokenizing;
|
||||||
mod ast;
|
mod ast;
|
||||||
|
@ -267,7 +267,6 @@ impl Parser {
|
|||||||
self.program()
|
self.program()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn format_parse_trace(&self) -> String {
|
pub fn format_parse_trace(&self) -> String {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
buf.push_str("Parse productions:\n");
|
buf.push_str("Parse productions:\n");
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
use stopwatch::Stopwatch;
|
use stopwatch::Stopwatch;
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use itertools::Itertools;
|
||||||
use schala_repl::{ProgrammingLanguageInterface,
|
use schala_repl::{ProgrammingLanguageInterface,
|
||||||
ComputationRequest, ComputationResponse,
|
ComputationRequest, ComputationResponse,
|
||||||
LangMetaRequest, LangMetaResponse, GlobalOutputStats};
|
LangMetaRequest, LangMetaResponse, GlobalOutputStats,
|
||||||
use crate::{reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map};
|
DebugResponse, DebugAsk};
|
||||||
|
use crate::{ast, reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map};
|
||||||
|
|
||||||
pub type SymbolTableHandle = Rc<RefCell<symbol_table::SymbolTable>>;
|
pub type SymbolTableHandle = Rc<RefCell<symbol_table::SymbolTable>>;
|
||||||
pub type SourceMapHandle = Rc<RefCell<source_map::SourceMap>>;
|
pub type SourceMapHandle = Rc<RefCell<source_map::SourceMap>>;
|
||||||
@ -27,14 +30,11 @@ pub struct Schala {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Schala {
|
impl Schala {
|
||||||
//TODO implement documentation for language items
|
|
||||||
/*
|
|
||||||
fn handle_docs(&self, source: String) -> LangMetaResponse {
|
fn handle_docs(&self, source: String) -> LangMetaResponse {
|
||||||
LangMetaResponse::Docs {
|
LangMetaResponse::Docs {
|
||||||
doc_string: format!("Schala item `{}` : <<Schala-lang documentation not yet implemented>>", source)
|
doc_string: format!("Schala item `{}` : <<Schala-lang documentation not yet implemented>>", source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Schala {
|
impl Schala {
|
||||||
@ -60,7 +60,6 @@ impl Schala {
|
|||||||
let prelude = include_str!("prelude.schala");
|
let prelude = include_str!("prelude.schala");
|
||||||
let mut s = Schala::new_blank_env();
|
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 request = ComputationRequest { source: prelude, debug_requests: HashSet::default() };
|
||||||
let response = s.run_computation(request);
|
let response = s.run_computation(request);
|
||||||
if let Err(msg) = response.main_output {
|
if let Err(msg) = response.main_output {
|
||||||
@ -69,53 +68,67 @@ impl Schala {
|
|||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is where the actual action of interpreting/compilation happens.
|
fn handle_debug_immediate(&self, request: DebugAsk) -> DebugResponse {
|
||||||
/// Note: this should eventually use a query-based system for parallelization, cf.
|
use DebugAsk::*;
|
||||||
/// https://rustc-dev-guide.rust-lang.org/overview.html
|
match request {
|
||||||
fn run_pipeline(&mut self, source: &str) -> Result<String, String> {
|
Timing => DebugResponse { ask: Timing, value: format!("Invalid") },
|
||||||
//TODO `run_pipeline` should return a formatted error struct
|
ByStage { stage_name, token } => match &stage_name[..] {
|
||||||
|
"symbol-table" => {
|
||||||
//TODO every stage should have a common error-format that gets turned into a repl-appropriate
|
let value = self.symbol_table.borrow().debug_symbol_table();
|
||||||
//string in `run_computation`
|
DebugResponse {
|
||||||
// 1st stage - tokenization
|
ask: ByStage { stage_name: format!("symbol-table"), token },
|
||||||
// TODO tokenize should return its own error type
|
value
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
//2nd stage - parsing
|
s => {
|
||||||
self.active_parser.add_new_tokens(tokens);
|
DebugResponse {
|
||||||
let mut ast = self.active_parser.parse()
|
ask: ByStage { stage_name: s.to_string(), token: None },
|
||||||
.map_err(|err| format_parse_error(err, &self.source_reference))?;
|
value: format!("Not-implemented")
|
||||||
|
|
||||||
// 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 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);
|
||||||
|
});
|
||||||
|
|
||||||
|
let errors: Vec<String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||||
|
if errors.len() == 0 {
|
||||||
|
Ok(tokens)
|
||||||
|
} else {
|
||||||
|
Err(format!("{:?}", errors))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
fn format_parse_error(error: parsing::ParseError, source_reference: &SourceReference) -> String {
|
||||||
@ -141,6 +154,52 @@ 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
|
/// Represents lines of source code
|
||||||
struct SourceReference {
|
struct SourceReference {
|
||||||
lines: Option<Vec<String>>
|
lines: Option<Vec<String>>
|
||||||
@ -160,6 +219,24 @@ 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> {
|
fn stage_names() -> Vec<&'static str> {
|
||||||
vec![
|
vec![
|
||||||
"tokenizing",
|
"tokenizing",
|
||||||
@ -174,38 +251,94 @@ fn stage_names() -> Vec<&'static str> {
|
|||||||
|
|
||||||
|
|
||||||
impl ProgrammingLanguageInterface for Schala {
|
impl ProgrammingLanguageInterface for Schala {
|
||||||
type Options = ();
|
fn language_name(&self) -> String {
|
||||||
fn language_name() -> String {
|
|
||||||
"Schala".to_owned()
|
"Schala".to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn source_file_suffix() -> String {
|
fn source_file_suffix(&self) -> String {
|
||||||
"schala".to_owned()
|
"schala".to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse {
|
fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse {
|
||||||
let ComputationRequest { source, debug_requests: _ } = request;
|
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;
|
||||||
self.source_reference.load_new_source(source);
|
self.source_reference.load_new_source(source);
|
||||||
let sw = Stopwatch::start_new();
|
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 = self.run_pipeline(source);
|
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 total_duration = sw.elapsed();
|
||||||
let global_output_stats = GlobalOutputStats {
|
let global_output_stats = GlobalOutputStats {
|
||||||
total_duration: sw.elapsed(),
|
total_duration, stage_durations
|
||||||
stage_durations: vec![]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ComputationResponse {
|
ComputationResponse {
|
||||||
main_output,
|
main_output,
|
||||||
global_output_stats,
|
global_output_stats,
|
||||||
debug_responses: vec![]
|
debug_responses,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request_meta(&mut self, request: LangMetaRequest) -> LangMetaResponse {
|
fn request_meta(&mut self, request: LangMetaRequest) -> LangMetaResponse {
|
||||||
match request {
|
match request {
|
||||||
LangMetaRequest::StageNames => LangMetaResponse::StageNames(stage_names().iter().map(|s| s.to_string()).collect()),
|
LangMetaRequest::StageNames => LangMetaResponse::StageNames(stage_names().iter().map(|s| s.to_string()).collect()),
|
||||||
_ => LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") }
|
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!("") }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -242,7 +242,6 @@ impl SymbolTable {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn debug_symbol_table(&self) -> String {
|
pub fn debug_symbol_table(&self) -> String {
|
||||||
let mut output = format!("Symbol table\n");
|
let mut output = format!("Symbol table\n");
|
||||||
let mut sorted_symbols: Vec<(&FullyQualifiedSymbolName, &Symbol)> = self.symbol_path_to_symbol.iter().collect();
|
let mut sorted_symbols: Vec<(&FullyQualifiedSymbolName, &Symbol)> = self.symbol_path_to_symbol.iter().collect();
|
||||||
|
@ -77,7 +77,6 @@ pub enum TypeConst {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TypeConst {
|
impl TypeConst {
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn to_string(&self) -> String {
|
pub fn to_string(&self) -> String {
|
||||||
use self::TypeConst::*;
|
use self::TypeConst::*;
|
||||||
match self {
|
match self {
|
||||||
@ -108,7 +107,6 @@ macro_rules! ty {
|
|||||||
|
|
||||||
//TODO find a better way to capture the to/from string logic
|
//TODO find a better way to capture the to/from string logic
|
||||||
impl Type {
|
impl Type {
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn to_string(&self) -> String {
|
pub fn to_string(&self) -> String {
|
||||||
use self::Type::*;
|
use self::Type::*;
|
||||||
match self {
|
match self {
|
||||||
|
@ -2,9 +2,8 @@ use std::collections::HashSet;
|
|||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
pub trait ProgrammingLanguageInterface {
|
pub trait ProgrammingLanguageInterface {
|
||||||
type Options;
|
fn language_name(&self) -> String;
|
||||||
fn language_name() -> String;
|
fn source_file_suffix(&self) -> String;
|
||||||
fn source_file_suffix() -> String;
|
|
||||||
|
|
||||||
fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse;
|
fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse;
|
||||||
|
|
||||||
@ -16,16 +15,8 @@ pub trait ProgrammingLanguageInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO this is what I want
|
|
||||||
/*
|
|
||||||
struct Options<T> {
|
|
||||||
lang_options: T
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
pub struct ComputationRequest<'a> {
|
pub struct ComputationRequest<'a> {
|
||||||
pub source: &'a str,
|
pub source: &'a str,
|
||||||
//pub options: Options<()>,
|
|
||||||
pub debug_requests: HashSet<DebugAsk>,
|
pub debug_requests: HashSet<DebugAsk>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,21 +7,14 @@ extern crate includedir;
|
|||||||
extern crate phf;
|
extern crate phf;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
mod command_tree;
|
|
||||||
mod language;
|
|
||||||
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::collections::HashSet;
|
||||||
use std::sync::Arc;
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::process::exit;
|
||||||
|
|
||||||
|
mod language;
|
||||||
|
mod repl;
|
||||||
|
|
||||||
pub use language::{
|
pub use language::{
|
||||||
ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats,
|
ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats,
|
||||||
@ -31,254 +24,46 @@ pub use language::{
|
|||||||
include!(concat!(env!("OUT_DIR"), "/static.rs"));
|
include!(concat!(env!("OUT_DIR"), "/static.rs"));
|
||||||
const VERSION_STRING: &'static str = "0.1.0";
|
const VERSION_STRING: &'static str = "0.1.0";
|
||||||
|
|
||||||
const HISTORY_SAVE_FILE: &'static str = ".schala_history";
|
pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) {
|
||||||
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
|
let mut repl = repl::Repl::new(langs);
|
||||||
|
repl.run_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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
pub fn run_noninteractive(filenames: Vec<PathBuf>, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) {
|
||||||
enum PromptStyle {
|
// for now, ony do something with the first filename
|
||||||
Normal,
|
|
||||||
Multiline,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<L: ProgrammingLanguageInterface> Repl<L> {
|
let filename = &filenames[0];
|
||||||
pub fn new(initial_state: L) -> Self {
|
let ext = filename
|
||||||
use linefeed::Interface;
|
.extension()
|
||||||
let line_reader = Interface::new("schala-repl").unwrap();
|
.and_then(|e| e.to_str())
|
||||||
let sigil = ':';
|
.unwrap_or_else(|| {
|
||||||
|
println!("Source file `{}` has no extension.", filename.display());
|
||||||
|
exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
Repl {
|
let mut language = Box::new(
|
||||||
sigil,
|
languages
|
||||||
line_reader,
|
.into_iter()
|
||||||
language_state: initial_state,
|
.find(|lang| lang.source_file_suffix() == ext)
|
||||||
options: ReplOptions::new(),
|
.unwrap_or_else(|| {
|
||||||
}
|
println!("Extension .{} not recognized", ext);
|
||||||
}
|
exit(1);
|
||||||
|
}),
|
||||||
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...");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_options(&mut self) {
|
let mut source_file = File::open(filename).unwrap();
|
||||||
self.line_reader
|
let mut buffer = String::new();
|
||||||
.load_history(HISTORY_SAVE_FILE)
|
source_file.read_to_string(&mut buffer).unwrap();
|
||||||
.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 {
|
let request = ComputationRequest {
|
||||||
source: input,
|
source: &buffer,
|
||||||
debug_requests,
|
debug_requests: HashSet::new(),
|
||||||
};
|
|
||||||
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)
|
let response = language.run_computation(request);
|
||||||
}
|
match response.main_output {
|
||||||
}
|
Ok(s) => println!("{}", s),
|
||||||
|
Err(s) => println!("{}", s),
|
||||||
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,6 +1,5 @@
|
|||||||
use crate::directive_actions::DirectiveAction;
|
use super::{InterpreterDirectiveOutput, Repl};
|
||||||
use crate::language::ProgrammingLanguageInterface;
|
use crate::repl::directive_actions::DirectiveAction;
|
||||||
use crate::{InterpreterDirectiveOutput, Repl};
|
|
||||||
use colored::*;
|
use colored::*;
|
||||||
|
|
||||||
/// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
|
/// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
|
||||||
@ -86,11 +85,7 @@ impl CommandTree {
|
|||||||
self.get_children().iter().map(|x| x.get_cmd()).collect()
|
self.get_children().iter().map(|x| x.get_cmd()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn perform<L: ProgrammingLanguageInterface>(
|
pub fn perform(&self, repl: &mut Repl, arguments: &Vec<&str>) -> InterpreterDirectiveOutput {
|
||||||
&self,
|
|
||||||
repl: &mut Repl<L>,
|
|
||||||
arguments: &Vec<&str>,
|
|
||||||
) -> InterpreterDirectiveOutput {
|
|
||||||
let mut dir_pointer: &CommandTree = self;
|
let mut dir_pointer: &CommandTree = self;
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
|
|
@ -1,8 +1,6 @@
|
|||||||
use crate::help::help;
|
use super::{InterpreterDirectiveOutput, Repl};
|
||||||
use crate::language::{
|
use crate::language::{DebugAsk, DebugResponse, LangMetaRequest, LangMetaResponse};
|
||||||
DebugAsk, DebugResponse, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
|
use crate::repl::help::help;
|
||||||
};
|
|
||||||
use crate::{InterpreterDirectiveOutput, Repl};
|
|
||||||
use std::fmt::Write as FmtWrite;
|
use std::fmt::Write as FmtWrite;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -22,11 +20,7 @@ pub enum DirectiveAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DirectiveAction {
|
impl DirectiveAction {
|
||||||
pub fn perform<L: ProgrammingLanguageInterface>(
|
pub fn perform(&self, repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
|
||||||
&self,
|
|
||||||
repl: &mut Repl<L>,
|
|
||||||
arguments: &[&str],
|
|
||||||
) -> InterpreterDirectiveOutput {
|
|
||||||
use DirectiveAction::*;
|
use DirectiveAction::*;
|
||||||
match self {
|
match self {
|
||||||
Null => None,
|
Null => None,
|
||||||
@ -36,10 +30,8 @@ impl DirectiveAction {
|
|||||||
::std::process::exit(0)
|
::std::process::exit(0)
|
||||||
}
|
}
|
||||||
ListPasses => {
|
ListPasses => {
|
||||||
let pass_names = match repl
|
let language_state = repl.get_cur_language_state();
|
||||||
.language_state
|
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
|
||||||
.request_meta(LangMetaRequest::StageNames)
|
|
||||||
{
|
|
||||||
LangMetaResponse::StageNames(names) => names,
|
LangMetaResponse::StageNames(names) => names,
|
||||||
_ => vec![],
|
_ => vec![],
|
||||||
};
|
};
|
||||||
@ -54,6 +46,7 @@ impl DirectiveAction {
|
|||||||
Some(buf)
|
Some(buf)
|
||||||
}
|
}
|
||||||
ShowImmediate => {
|
ShowImmediate => {
|
||||||
|
let cur_state = repl.get_cur_language_state();
|
||||||
let stage_name = match arguments.get(0) {
|
let stage_name = match arguments.get(0) {
|
||||||
Some(s) => s.to_string(),
|
Some(s) => s.to_string(),
|
||||||
None => return Some(format!("Must specify a thing to debug")),
|
None => return Some(format!("Must specify a thing to debug")),
|
||||||
@ -62,7 +55,7 @@ impl DirectiveAction {
|
|||||||
stage_name: stage_name.clone(),
|
stage_name: stage_name.clone(),
|
||||||
token: None,
|
token: None,
|
||||||
});
|
});
|
||||||
let meta_response = repl.language_state.request_meta(meta);
|
let meta_response = cur_state.request_meta(meta);
|
||||||
|
|
||||||
let response = match meta_response {
|
let response = match meta_response {
|
||||||
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => match ask {
|
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => match ask {
|
||||||
@ -107,37 +100,43 @@ impl DirectiveAction {
|
|||||||
});
|
});
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
TotalTimeOff => {
|
TotalTimeOff => total_time_off(repl, arguments),
|
||||||
repl.options.show_total_time = false;
|
TotalTimeOn => total_time_on(repl, arguments),
|
||||||
None
|
StageTimeOff => stage_time_off(repl, arguments),
|
||||||
}
|
StageTimeOn => stage_time_on(repl, arguments),
|
||||||
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),
|
Doc => doc(repl, arguments),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn doc<L: ProgrammingLanguageInterface>(
|
fn total_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
||||||
repl: &mut Repl<L>,
|
repl.options.show_total_time = true;
|
||||||
arguments: &[&str],
|
None
|
||||||
) -> InterpreterDirectiveOutput {
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
arguments
|
arguments
|
||||||
.get(0)
|
.get(0)
|
||||||
.map(|cmd| {
|
.map(|cmd| {
|
||||||
let source = cmd.to_string();
|
let source = cmd.to_string();
|
||||||
let meta = LangMetaRequest::Docs { source };
|
let meta = LangMetaRequest::Docs { source };
|
||||||
match repl.language_state.request_meta(meta) {
|
let cur_state = repl.get_cur_language_state();
|
||||||
|
match cur_state.request_meta(meta) {
|
||||||
LangMetaResponse::Docs { doc_string } => Some(doc_string),
|
LangMetaResponse::Docs { doc_string } => Some(doc_string),
|
||||||
_ => Some(format!("Invalid doc response")),
|
_ => Some(format!("Invalid doc response")),
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
use crate::command_tree::CommandTree;
|
use crate::repl::command_tree::CommandTree;
|
||||||
use crate::directive_actions::DirectiveAction;
|
use crate::repl::directive_actions::DirectiveAction;
|
||||||
|
|
||||||
pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
|
pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
|
||||||
let passes_directives: Vec<CommandTree> = pass_names
|
let passes_directives: Vec<CommandTree> = pass_names
|
||||||
@ -28,7 +28,6 @@ fn get_list(passes_directives: &Vec<CommandTree>, include_help: bool) -> Vec<Com
|
|||||||
|
|
||||||
vec![
|
vec![
|
||||||
CommandTree::terminal("exit", Some("exit the REPL"), vec![], QuitProgram),
|
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("quit", Some("exit the REPL"), vec![], QuitProgram),
|
||||||
CommandTree::terminal(
|
CommandTree::terminal(
|
||||||
"help",
|
"help",
|
||||||
@ -86,6 +85,15 @@ 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(
|
CommandTree::terminal(
|
||||||
"doc",
|
"doc",
|
||||||
Some("Get language-specific help for an item"),
|
Some("Get language-specific help for an item"),
|
@ -1,14 +1,10 @@
|
|||||||
use std::fmt::Write as FmtWrite;
|
use std::fmt::Write as FmtWrite;
|
||||||
|
|
||||||
use crate::command_tree::CommandTree;
|
use super::command_tree::CommandTree;
|
||||||
use crate::language::ProgrammingLanguageInterface;
|
use super::{InterpreterDirectiveOutput, Repl};
|
||||||
use crate::{InterpreterDirectiveOutput, Repl};
|
|
||||||
use colored::*;
|
use colored::*;
|
||||||
|
|
||||||
pub fn help<L: ProgrammingLanguageInterface>(
|
pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
|
||||||
repl: &mut Repl<L>,
|
|
||||||
arguments: &[&str],
|
|
||||||
) -> InterpreterDirectiveOutput {
|
|
||||||
match arguments {
|
match arguments {
|
||||||
[] => return global_help(repl),
|
[] => return global_help(repl),
|
||||||
commands => {
|
commands => {
|
||||||
@ -50,7 +46,7 @@ fn get_directive_from_commands<'a>(
|
|||||||
matched_directive
|
matched_directive
|
||||||
}
|
}
|
||||||
|
|
||||||
fn global_help<L: ProgrammingLanguageInterface>(repl: &mut Repl<L>) -> InterpreterDirectiveOutput {
|
fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
||||||
writeln!(
|
writeln!(
|
||||||
@ -73,11 +69,12 @@ fn global_help<L: ProgrammingLanguageInterface>(repl: &mut Repl<L>) -> Interpret
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let ref lang = repl.get_cur_language_state();
|
||||||
writeln!(buf, "").unwrap();
|
writeln!(buf, "").unwrap();
|
||||||
writeln!(
|
writeln!(
|
||||||
buf,
|
buf,
|
||||||
"Language-specific help for {}",
|
"Language-specific help for {}",
|
||||||
<L as ProgrammingLanguageInterface>::language_name()
|
lang.language_name()
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
writeln!(buf, "-----------------------").unwrap();
|
writeln!(buf, "-----------------------").unwrap();
|
274
schala-repl/src/repl/mod.rs
Normal file
274
schala-repl/src/repl/mod.rs
Normal file
@ -0,0 +1,274 @@
|
|||||||
|
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;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
use super::ReplOptions;
|
||||||
use crate::language::{ComputationResponse, DebugAsk};
|
use crate::language::{ComputationResponse, DebugAsk};
|
||||||
use crate::ReplOptions;
|
|
||||||
|
|
||||||
pub struct ReplResponse {
|
pub struct ReplResponse {
|
||||||
label: Option<String>,
|
label: Option<String>,
|
52
src/main.rs
52
src/main.rs
@ -1,9 +1,8 @@
|
|||||||
use schala_repl::{Repl, ProgrammingLanguageInterface, ComputationRequest};
|
use schala_repl::{run_noninteractive, start_repl, ProgrammingLanguageInterface};
|
||||||
|
|
||||||
use std::{fs::File, io::Read, path::PathBuf, process::exit, collections::HashSet};
|
use std::path::PathBuf;
|
||||||
use schala_lang::Schala;
|
use std::process::exit;
|
||||||
|
|
||||||
//TODO specify multiple langs, and have a way to switch between them
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
let matches = command_line_options()
|
let matches = command_line_options()
|
||||||
@ -18,48 +17,15 @@ fn main() {
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let langs: Vec<Box<dyn ProgrammingLanguageInterface>> =
|
||||||
|
vec![Box::new(schala_lang::Schala::new())];
|
||||||
|
|
||||||
if matches.free.is_empty() {
|
if matches.free.is_empty() {
|
||||||
let state = Schala::new();
|
start_repl(langs);
|
||||||
let mut repl = Repl::new(state);
|
|
||||||
repl.run_repl();
|
|
||||||
} else {
|
} else {
|
||||||
let paths: Vec<PathBuf> = matches.free.iter().map(PathBuf::from).collect();
|
let paths = matches.free.iter().map(PathBuf::from).collect();
|
||||||
//TODO handle more than one file
|
run_noninteractive(paths, langs);
|
||||||
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 {
|
fn command_line_options() -> getopts::Options {
|
||||||
|
Loading…
Reference in New Issue
Block a user