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 }
|
Expression { id, kind, type_anno: None }
|
||||||
}
|
}
|
||||||
|
|
||||||
/* - commented out because unused
|
#[cfg(test)]
|
||||||
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)]
|
||||||
|
@ -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 }
|
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,7 +24,6 @@ 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,6 +267,7 @@ 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,16 +1,13 @@
|
|||||||
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};
|
||||||
DebugResponse, DebugAsk};
|
use crate::{reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map};
|
||||||
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>>;
|
||||||
@ -30,11 +27,14 @@ 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,6 +60,7 @@ 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 {
|
||||||
@ -68,67 +69,53 @@ impl Schala {
|
|||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_debug_immediate(&self, request: DebugAsk) -> DebugResponse {
|
/// This is where the actual action of interpreting/compilation happens.
|
||||||
use DebugAsk::*;
|
/// Note: this should eventually use a query-based system for parallelization, cf.
|
||||||
match request {
|
/// https://rustc-dev-guide.rust-lang.org/overview.html
|
||||||
Timing => DebugResponse { ask: Timing, value: format!("Invalid") },
|
fn run_pipeline(&mut self, source: &str) -> Result<String, String> {
|
||||||
ByStage { stage_name, token } => match &stage_name[..] {
|
//TODO `run_pipeline` should return a formatted error struct
|
||||||
"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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tokenizing(input: &str, _handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<Vec<tokenizing::Token>, String> {
|
//TODO every stage should have a common error-format that gets turned into a repl-appropriate
|
||||||
let tokens = tokenizing::tokenize(input);
|
//string in `run_computation`
|
||||||
comp.map(|comp| {
|
// 1st stage - tokenization
|
||||||
let token_string = tokens.iter().map(|t| t.to_string_with_metadata()).join(", ");
|
// TODO tokenize should return its own error type
|
||||||
comp.add_artifact(token_string);
|
let tokens = tokenizing::tokenize(source);
|
||||||
});
|
let token_errors: Vec<String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||||
|
if token_errors.len() > 0 {
|
||||||
let errors: Vec<String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
return Err(format!("{:?}", token_errors));
|
||||||
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> {
|
//2nd stage - parsing
|
||||||
use ParsingDebugType::*;
|
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 ref mut parser = handle.active_parser;
|
// Symbol table
|
||||||
parser.add_new_tokens(input);
|
self.symbol_table.borrow_mut().add_top_level_symbols(&ast)?;
|
||||||
let ast = parser.parse();
|
|
||||||
|
|
||||||
comp.map(|comp| {
|
// Scope resolution - requires mutating AST
|
||||||
let debug_format = comp.parsing.as_ref().unwrap_or(&CompactAST);
|
self.resolver.resolve(&mut ast)?;
|
||||||
let debug_info = match debug_format {
|
|
||||||
CompactAST => match ast{
|
// Typechecking
|
||||||
Ok(ref ast) => ast.compact_debug(),
|
// TODO typechecking not working
|
||||||
Err(_) => "Error - see output".to_string(),
|
let _overall_type = self.type_context.typecheck(&ast)
|
||||||
},
|
.map_err(|err| format!("Type error: {}", err.msg));
|
||||||
ExpandedAST => match ast{
|
|
||||||
Ok(ref ast) => ast.expanded_debug(),
|
// Reduce AST - this doesn't produce an error yet, but probably should
|
||||||
Err(_) => "Error - see output".to_string(),
|
let symbol_table = self.symbol_table.borrow();
|
||||||
},
|
let reduced_ast = reduced_ast::reduce(&ast, &symbol_table);
|
||||||
Trace => parser.format_parse_trace(),
|
|
||||||
};
|
// Tree-walking evaluator. TODO fix this
|
||||||
comp.add_artifact(debug_info);
|
let evaluation_outputs = self.state.evaluate(reduced_ast, true);
|
||||||
});
|
let text_output: Result<Vec<String>, String> = evaluation_outputs
|
||||||
ast.map_err(|err| format_parse_error(err, &handle.source_reference))
|
.into_iter()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let eval_output: String = text_output
|
||||||
|
.map(|v| { Iterator::intersperse(v.into_iter(), "\n".to_owned()).collect() })?;
|
||||||
|
|
||||||
|
Ok(eval_output)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_parse_error(error: parsing::ParseError, source_reference: &SourceReference) -> String {
|
fn format_parse_error(error: parsing::ParseError, source_reference: &SourceReference) -> String {
|
||||||
@ -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
|
/// Represents lines of source code
|
||||||
struct SourceReference {
|
struct SourceReference {
|
||||||
lines: Option<Vec<String>>
|
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> {
|
fn stage_names() -> Vec<&'static str> {
|
||||||
vec![
|
vec![
|
||||||
"tokenizing",
|
"tokenizing",
|
||||||
@ -251,94 +174,38 @@ fn stage_names() -> Vec<&'static str> {
|
|||||||
|
|
||||||
|
|
||||||
impl ProgrammingLanguageInterface for Schala {
|
impl ProgrammingLanguageInterface for Schala {
|
||||||
fn language_name(&self) -> String {
|
type Options = ();
|
||||||
|
fn language_name() -> String {
|
||||||
"Schala".to_owned()
|
"Schala".to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn source_file_suffix(&self) -> String {
|
fn source_file_suffix() -> String {
|
||||||
"schala".to_owned()
|
"schala".to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse {
|
fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse {
|
||||||
struct PassToken<'a> {
|
let ComputationRequest { source, debug_requests: _ } = request;
|
||||||
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: Result<String, String> = Ok(source)
|
let main_output = self.run_pipeline(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, stage_durations
|
total_duration: sw.elapsed(),
|
||||||
|
stage_durations: vec![]
|
||||||
};
|
};
|
||||||
|
|
||||||
ComputationResponse {
|
ComputationResponse {
|
||||||
main_output,
|
main_output,
|
||||||
global_output_stats,
|
global_output_stats,
|
||||||
debug_responses,
|
debug_responses: vec![]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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()),
|
||||||
LangMetaRequest::Docs { source } => self.handle_docs(source),
|
_ => LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") }
|
||||||
LangMetaRequest::ImmediateDebug(debug_request) =>
|
|
||||||
LangMetaResponse::ImmediateDebug(self.handle_debug_immediate(debug_request)),
|
|
||||||
LangMetaRequest::Custom { .. } => LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -242,6 +242,7 @@ 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,6 +77,7 @@ 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 {
|
||||||
@ -107,6 +108,7 @@ 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 {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use super::{InterpreterDirectiveOutput, Repl};
|
use crate::directive_actions::DirectiveAction;
|
||||||
use crate::repl::directive_actions::DirectiveAction;
|
use crate::language::ProgrammingLanguageInterface;
|
||||||
|
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
|
||||||
@ -85,7 +86,11 @@ 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(&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 dir_pointer: &CommandTree = self;
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
|
|
@ -1,6 +1,8 @@
|
|||||||
use super::{InterpreterDirectiveOutput, Repl};
|
use crate::help::help;
|
||||||
use crate::language::{DebugAsk, DebugResponse, LangMetaRequest, LangMetaResponse};
|
use crate::language::{
|
||||||
use crate::repl::help::help;
|
DebugAsk, DebugResponse, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
|
||||||
|
};
|
||||||
|
use crate::{InterpreterDirectiveOutput, Repl};
|
||||||
use std::fmt::Write as FmtWrite;
|
use std::fmt::Write as FmtWrite;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -20,7 +22,11 @@ pub enum DirectiveAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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::*;
|
use DirectiveAction::*;
|
||||||
match self {
|
match self {
|
||||||
Null => None,
|
Null => None,
|
||||||
@ -30,8 +36,10 @@ impl DirectiveAction {
|
|||||||
::std::process::exit(0)
|
::std::process::exit(0)
|
||||||
}
|
}
|
||||||
ListPasses => {
|
ListPasses => {
|
||||||
let language_state = repl.get_cur_language_state();
|
let pass_names = match repl
|
||||||
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
|
.language_state
|
||||||
|
.request_meta(LangMetaRequest::StageNames)
|
||||||
|
{
|
||||||
LangMetaResponse::StageNames(names) => names,
|
LangMetaResponse::StageNames(names) => names,
|
||||||
_ => vec![],
|
_ => vec![],
|
||||||
};
|
};
|
||||||
@ -46,7 +54,6 @@ 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")),
|
||||||
@ -55,7 +62,7 @@ impl DirectiveAction {
|
|||||||
stage_name: stage_name.clone(),
|
stage_name: stage_name.clone(),
|
||||||
token: None,
|
token: None,
|
||||||
});
|
});
|
||||||
let meta_response = cur_state.request_meta(meta);
|
let meta_response = repl.language_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 {
|
||||||
@ -100,43 +107,37 @@ impl DirectiveAction {
|
|||||||
});
|
});
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
TotalTimeOff => total_time_off(repl, arguments),
|
TotalTimeOff => {
|
||||||
TotalTimeOn => total_time_on(repl, arguments),
|
repl.options.show_total_time = false;
|
||||||
StageTimeOff => stage_time_off(repl, arguments),
|
None
|
||||||
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 total_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
fn doc<L: ProgrammingLanguageInterface>(
|
||||||
repl.options.show_total_time = true;
|
repl: &mut Repl<L>,
|
||||||
None
|
arguments: &[&str],
|
||||||
}
|
) -> 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 };
|
||||||
let cur_state = repl.get_cur_language_state();
|
match repl.language_state.request_meta(meta) {
|
||||||
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::repl::command_tree::CommandTree;
|
use crate::command_tree::CommandTree;
|
||||||
use crate::repl::directive_actions::DirectiveAction;
|
use crate::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,6 +28,7 @@ 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",
|
||||||
@ -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(
|
CommandTree::terminal(
|
||||||
"doc",
|
"doc",
|
||||||
Some("Get language-specific help for an item"),
|
Some("Get language-specific help for an item"),
|
@ -1,10 +1,14 @@
|
|||||||
use std::fmt::Write as FmtWrite;
|
use std::fmt::Write as FmtWrite;
|
||||||
|
|
||||||
use super::command_tree::CommandTree;
|
use crate::command_tree::CommandTree;
|
||||||
use super::{InterpreterDirectiveOutput, Repl};
|
use crate::language::ProgrammingLanguageInterface;
|
||||||
|
use crate::{InterpreterDirectiveOutput, Repl};
|
||||||
use colored::*;
|
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 {
|
match arguments {
|
||||||
[] => return global_help(repl),
|
[] => return global_help(repl),
|
||||||
commands => {
|
commands => {
|
||||||
@ -46,7 +50,7 @@ fn get_directive_from_commands<'a>(
|
|||||||
matched_directive
|
matched_directive
|
||||||
}
|
}
|
||||||
|
|
||||||
fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
|
fn global_help<L: ProgrammingLanguageInterface>(repl: &mut Repl<L>) -> InterpreterDirectiveOutput {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
||||||
writeln!(
|
writeln!(
|
||||||
@ -69,12 +73,11 @@ fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
|
|||||||
.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 {}",
|
||||||
lang.language_name()
|
<L as ProgrammingLanguageInterface>::language_name()
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
writeln!(buf, "-----------------------").unwrap();
|
writeln!(buf, "-----------------------").unwrap();
|
@ -2,8 +2,9 @@ use std::collections::HashSet;
|
|||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
pub trait ProgrammingLanguageInterface {
|
pub trait ProgrammingLanguageInterface {
|
||||||
fn language_name(&self) -> String;
|
type Options;
|
||||||
fn source_file_suffix(&self) -> String;
|
fn language_name() -> String;
|
||||||
|
fn source_file_suffix() -> String;
|
||||||
|
|
||||||
fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse;
|
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 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,14 +7,21 @@ extern crate includedir;
|
|||||||
extern crate phf;
|
extern crate phf;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
mod command_tree;
|
||||||
use std::fs::File;
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::process::exit;
|
|
||||||
|
|
||||||
mod language;
|
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::{
|
pub use language::{
|
||||||
ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats,
|
ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats,
|
||||||
@ -24,46 +31,254 @@ 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";
|
||||||
|
|
||||||
pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) {
|
const HISTORY_SAVE_FILE: &'static str = ".schala_history";
|
||||||
let mut repl = repl::Repl::new(langs);
|
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
|
||||||
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,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_noninteractive(filenames: Vec<PathBuf>, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) {
|
#[derive(Clone)]
|
||||||
// for now, ony do something with the first filename
|
enum PromptStyle {
|
||||||
|
Normal,
|
||||||
|
Multiline,
|
||||||
|
}
|
||||||
|
|
||||||
let filename = &filenames[0];
|
impl<L: ProgrammingLanguageInterface> Repl<L> {
|
||||||
let ext = filename
|
pub fn new(initial_state: L) -> Self {
|
||||||
.extension()
|
use linefeed::Interface;
|
||||||
.and_then(|e| e.to_str())
|
let line_reader = Interface::new("schala-repl").unwrap();
|
||||||
.unwrap_or_else(|| {
|
let sigil = ':';
|
||||||
println!("Source file `{}` has no extension.", filename.display());
|
|
||||||
exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut language = Box::new(
|
Repl {
|
||||||
languages
|
sigil,
|
||||||
.into_iter()
|
line_reader,
|
||||||
.find(|lang| lang.source_file_suffix() == ext)
|
language_state: initial_state,
|
||||||
.unwrap_or_else(|| {
|
options: ReplOptions::new(),
|
||||||
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...");
|
||||||
|
}
|
||||||
|
|
||||||
let mut source_file = File::open(filename).unwrap();
|
fn load_options(&mut self) {
|
||||||
let mut buffer = String::new();
|
self.line_reader
|
||||||
source_file.read_to_string(&mut buffer).unwrap();
|
.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 {
|
let request = ComputationRequest {
|
||||||
source: &buffer,
|
source: input,
|
||||||
debug_requests: HashSet::new(),
|
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![],
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = language.run_computation(request);
|
directives_from_pass_names(&pass_names)
|
||||||
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,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;
|
||||||
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,8 +1,9 @@
|
|||||||
use schala_repl::{run_noninteractive, start_repl, ProgrammingLanguageInterface};
|
use schala_repl::{Repl, ProgrammingLanguageInterface, ComputationRequest};
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::{fs::File, io::Read, path::PathBuf, process::exit, collections::HashSet};
|
||||||
use std::process::exit;
|
use schala_lang::Schala;
|
||||||
|
|
||||||
|
//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()
|
||||||
@ -17,15 +18,48 @@ 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() {
|
||||||
start_repl(langs);
|
let state = Schala::new();
|
||||||
|
let mut repl = Repl::new(state);
|
||||||
|
repl.run_repl();
|
||||||
} else {
|
} else {
|
||||||
let paths = matches.free.iter().map(PathBuf::from).collect();
|
let paths: Vec<PathBuf> = matches.free.iter().map(PathBuf::from).collect();
|
||||||
run_noninteractive(paths, langs);
|
//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 {
|
fn command_line_options() -> getopts::Options {
|
||||||
|
Loading…
Reference in New Issue
Block a user