Skip to content

Commit

Permalink
fix: Rework how hanging lambda/parens are handled
Browse files Browse the repository at this point in the history
Less hacks which leads to a much more consistent formatting in the face
of newlines and comments
  • Loading branch information
Marwes committed Apr 13, 2020
1 parent 5122fe3 commit 732f09f
Show file tree
Hide file tree
Showing 48 changed files with 1,228 additions and 851 deletions.
23 changes: 6 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,6 @@ debug = 1

[profile.release]
debug = 1

[patch.crates-io]
pretty = { path = "../pretty.rs", version = "0.9.0" }
43 changes: 26 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@ let { (*>), (<*), wrap } = import! std.applicative
let { for } = import! std.traversable
type Op = | Add | Sub | Div | Mul
type Expr = | Int Int | Binop Expr Op Expr
type Op =
| Add
| Sub
| Div
| Mul
type Expr =
| Int Int
| Binop Expr Op Expr
let parse : String -> Result String Expr =
// Gluon has a small parser combinator library which makes it easy to define an expression parser
Expand All @@ -110,7 +116,9 @@ let parse : String -> Result String Expr =
lazy_parser,
chainl1,
(<?>),
? } = import! std.parser
?
} =
import! std.parser
let { (<|>) } = import! std.alternative
let lex x = x <* spaces
Expand All @@ -123,13 +131,14 @@ let parse : String -> Result String Expr =
| Err _ -> parser.fail "Unable to parse integer"
let operator =
satisfy_map (\c ->
match c with
| '*' -> Some Mul
| '+' -> Some Add
| '-' -> Some Sub
| '/' -> Some Div
| _ -> None)
satisfy_map
(\c ->
match c with
| '*' -> Some Mul
| '+' -> Some Add
| '-' -> Some Sub
| '/' -> Some Div
| _ -> None)
<?> "operator"
rec
Expand All @@ -145,9 +154,6 @@ let parse : String -> Result String Expr =
let expr _ = binop ()
in
// Gluon makes it possible to partially apply functions which we use here to scope all parser functions
// inside the `let parse` binding above.
let parse : String -> Result String Expr = parser.parse (expr () <* spaces)
parse
Expand Down Expand Up @@ -175,15 +181,19 @@ let eval expr : Expr -> Int =
| Mul -> (*)
f (eval l) (eval r)
do digits =
do
digits
=
let gen_digit = random.thread_rng.gen_int_range 1 10
do a = gen_digit
do b = gen_digit
do c = gen_digit
do d = gen_digit
wrap [a, b, c, d]
let print_digits = for digits (\d ->
let print_digits = for
digits
(\d ->
seq io.print " "
io.print (show d))
seq io.print "Four digits:" *> print_digits *> io.println ""
Expand All @@ -199,8 +209,7 @@ let guess_loop _ =
| Ok expr ->
if validate digits expr then
let result = eval expr
if result == 24
then io.println "Correct!"
if result == 24 then io.println "Correct!"
else io.println ("Incorrect, " <> int.show.show result <> " != 24") *> guess_loop ()
else
io.println
Expand Down
9 changes: 4 additions & 5 deletions base/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,18 @@ impl<E: fmt::Display> InFile<E> {
self.error
}

pub fn emit_string(&self, code_map: &::codespan::CodeMap) -> io::Result<String>
pub fn emit_string(&self) -> io::Result<String>
where
E: AsDiagnostic,
{
let mut output = Vec::new();
self.emit(
&mut ::codespan_reporting::termcolor::NoColor::new(&mut output),
code_map,
)?;
Ok(String::from_utf8(output).unwrap())
}

pub fn emit<W>(&self, writer: &mut W, code_map: &::codespan::CodeMap) -> io::Result<()>
pub fn emit<W>(&self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + ::codespan_reporting::termcolor::WriteColor,
E: AsDiagnostic,
Expand All @@ -242,7 +241,7 @@ impl<E: fmt::Display> InFile<E> {
if i != 0 {
writeln!(writer)?;
}
::codespan_reporting::emit(&mut *writer, &code_map, &diagnostic)?;
::codespan_reporting::emit(&mut *writer, &self.source, &diagnostic)?;
}
Ok(())
}
Expand All @@ -254,7 +253,7 @@ impl<E: fmt::Display + AsDiagnostic> fmt::Display for InFile<E> {
{
let mut writer = ::codespan_reporting::termcolor::NoColor::new(&mut buffer);

self.emit(&mut writer, &self.source)
self.emit(&mut writer)
.map_err(|_| fmt::Error)?;
}

Expand Down
3 changes: 2 additions & 1 deletion base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ macro_rules! type_cache {
}
}

#[macro_export]
macro_rules! chain {
($alloc: expr; $first: expr, $($rest: expr),+) => {{
($alloc: expr; $first: expr, $($rest: expr),+ $(,)?) => {{
let mut doc = ::pretty::DocBuilder($alloc, $first.into());
$(
doc = doc.append($rest);
Expand Down
2 changes: 1 addition & 1 deletion base/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<'a> DoubleEndedIterator for CommentIter<'a> {
.src
.trim_end_matches(|c: char| c.is_whitespace() && c != '\n');
if self.src.ends_with('\n') {
let comment_line = self.src[..self.src.len() - 1].lines().next_back().unwrap();
let comment_line = self.src[..self.src.len() - 1].lines().next_back()?;
let trimmed = comment_line.trim_start();

let newline_len = if self.src.ends_with("\r\n") { 2 } else { 1 };
Expand Down
Loading

0 comments on commit 732f09f

Please sign in to comment.