This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
[WIP] Add a proper REPL #464
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
89dc56e
Add repl module with basic skeleton code
Stupremee c16d59b
Improve Repl
Stupremee 3b00ec7
Make analyze function public and add type command
Stupremee c2ef7e7
Use global array and implement command hinting
Stupremee 83668a2
Command hinting only show rest of command
Stupremee fd7696c
Move main to bin module
Stupremee cba41e5
Move most of common module in swcc bin...
Stupremee 0ea9c91
Repl works now
Stupremee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub(super) enum ColorChoice { | ||
Always, | ||
Auto, | ||
Never, | ||
} | ||
|
||
impl ColorChoice { | ||
pub(super) fn use_color_for(self, stream: atty::Stream) -> bool { | ||
match self { | ||
ColorChoice::Always => true, | ||
ColorChoice::Never => false, | ||
ColorChoice::Auto => atty::is(stream), | ||
} | ||
} | ||
} | ||
|
||
impl std::str::FromStr for ColorChoice { | ||
type Err = &'static str; | ||
fn from_str(s: &str) -> Result<ColorChoice, &'static str> { | ||
match s { | ||
"always" => Ok(ColorChoice::Always), | ||
"auto" => Ok(ColorChoice::Auto), | ||
"never" => Ok(ColorChoice::Never), | ||
_ => Err("Invalid color choice"), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "color-backtrace")] | ||
pub(super) mod backtrace { | ||
use super::ColorChoice; | ||
use color_backtrace::termcolor::{self, StandardStream}; | ||
use color_backtrace::BacktracePrinter; | ||
|
||
impl Into<termcolor::ColorChoice> for ColorChoice { | ||
fn into(self) -> termcolor::ColorChoice { | ||
match self { | ||
ColorChoice::Always => termcolor::ColorChoice::Always, | ||
ColorChoice::Auto => termcolor::ColorChoice::Auto, | ||
ColorChoice::Never => termcolor::ColorChoice::Never, | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn install(color: ColorChoice) { | ||
BacktracePrinter::new().install(Box::new(StandardStream::stderr(color.into()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use saltwater::{analyze, Parser, PureAnalyzer}; | ||
use std::fmt; | ||
|
||
const HELP_MESSAGE: &'static str = " | ||
:help Shows this message | ||
:quit Quits the repl | ||
:type Shows the type of the given expression | ||
"; | ||
|
||
#[derive(Debug)] | ||
pub(super) enum CommandError { | ||
CompileError(saltwater::CompileError), | ||
} | ||
|
||
impl fmt::Display for CommandError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
CommandError::CompileError(err) => writeln!(f, "{}", err.data), | ||
} | ||
} | ||
} | ||
|
||
pub(super) fn show_type(code: String) -> Result<(), CommandError> { | ||
let ast = analyze(&code, Parser::expr, PureAnalyzer::expr) | ||
.map_err(|e| CommandError::CompileError(e))?; | ||
println!("Type: {}", ast.ctype); | ||
Ok(()) | ||
} | ||
|
||
pub(super) fn print_help(_code: String) -> Result<(), CommandError> { | ||
println!("{}", HELP_MESSAGE); | ||
Ok(()) | ||
} | ||
|
||
pub(super) fn quit_repl(_code: String) -> Result<(), CommandError> { | ||
std::process::exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use super::COMMANDS; | ||
use rustyline::{ | ||
completion::{Completer, Pair}, | ||
error::ReadlineError, | ||
highlight::{Highlighter, MatchingBracketHighlighter}, | ||
hint::Hinter, | ||
validate::{self, MatchingBracketValidator, Validator}, | ||
Context, | ||
}; | ||
use rustyline_derive::Helper; | ||
use std::borrow::Cow; | ||
|
||
#[derive(Helper)] | ||
pub(super) struct ReplHelper { | ||
pub(super) highlighter: MatchingBracketHighlighter, | ||
pub(super) validator: MatchingBracketValidator, | ||
pub(super) hinter: CommandHinter, | ||
} | ||
|
||
impl Completer for ReplHelper { | ||
type Candidate = Pair; | ||
|
||
fn complete( | ||
&self, | ||
_line: &str, | ||
_pos: usize, | ||
_ctx: &Context<'_>, | ||
) -> Result<(usize, Vec<Pair>), ReadlineError> { | ||
Ok((0, vec![])) | ||
} | ||
} | ||
|
||
impl Hinter for ReplHelper { | ||
fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> { | ||
self.hinter.hint(line, pos, ctx) | ||
} | ||
} | ||
|
||
impl Highlighter for ReplHelper { | ||
fn highlight_prompt<'b, 's: 'b, 'p: 'b>( | ||
&'s self, | ||
prompt: &'p str, | ||
_default: bool, | ||
) -> Cow<'b, str> { | ||
Cow::Borrowed(prompt) | ||
} | ||
|
||
fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { | ||
let style = ansi_term::Style::new().dimmed(); | ||
Cow::Owned(style.paint(hint).to_string()) | ||
} | ||
|
||
fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { | ||
self.highlighter.highlight(line, pos) | ||
} | ||
|
||
fn highlight_char(&self, line: &str, pos: usize) -> bool { | ||
self.highlighter.highlight_char(line, pos) | ||
} | ||
} | ||
|
||
impl Validator for ReplHelper { | ||
fn validate( | ||
&self, | ||
ctx: &mut validate::ValidationContext, | ||
) -> rustyline::Result<validate::ValidationResult> { | ||
self.validator.validate(ctx) | ||
} | ||
|
||
fn validate_while_typing(&self) -> bool { | ||
self.validator.validate_while_typing() | ||
} | ||
} | ||
|
||
pub(super) struct CommandHinter; | ||
|
||
impl Hinter for CommandHinter { | ||
fn hint(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Option<String> { | ||
if pos < line.len() { | ||
return None; | ||
} | ||
let start = &line[..pos]; | ||
if !start.starts_with(":") { | ||
return None; | ||
} | ||
let start = &start[1..]; | ||
|
||
COMMANDS | ||
.iter() | ||
.filter(|(k, _v)| k.starts_with(&start[..])) | ||
.map(|(k, _v)| k) | ||
.next() | ||
.map(|s| &s[start.len()..]) | ||
.map(|s| s.to_string()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ups 😅