Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite parser macros #34

Merged
merged 13 commits into from
Mar 20, 2019
5 changes: 5 additions & 0 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 @@ -5,6 +5,9 @@ members = [
"abnf_to_pest",
"dhall",
"dhall_parser",
"dhall_core",
"dhall_generator",
"iter_patterns",
]

# # Parser is super slow when not optimized
Expand Down
34 changes: 20 additions & 14 deletions dhall/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use dhall_generator::dhall_expr;
use std::fmt;
use std::rc::Rc;

fn apply_builtin<S, A>(b: Builtin, mut args: Vec<SubExpr<S, A>>) -> SubExpr<S, A>
fn apply_builtin<S, A>(
b: Builtin,
mut args: Vec<SubExpr<S, A>>,
) -> SubExpr<S, A>
where
S: fmt::Debug,
A: fmt::Debug,
Expand Down Expand Up @@ -72,9 +75,7 @@ where
(ListLast, Some(NEListLit(ys)), _) => {
rc(OptionalLit(None, ys.iter().cloned().last()))
}
(ListReverse, Some(EmptyListLit(t)), _) => {
rc(EmptyListLit(t.clone()))
}
(ListReverse, Some(EmptyListLit(t)), _) => rc(EmptyListLit(t.clone())),
(ListReverse, Some(NEListLit(ys)), _) => {
let ys = ys.iter().rev().cloned().collect();
rc(NEListLit(ys))
Expand All @@ -84,10 +85,15 @@ where
dhall_expr!([] : List ({ index : Natural, value : t }))
}
(ListIndexed, Some(NEListLit(xs)), _) => {
let xs = xs.iter().cloned().enumerate().map(|(i, e)| {
let i = rc(NaturalLit(i));
dhall_expr!({ index = i, value = e })
}).collect();
let xs = xs
.iter()
.cloned()
.enumerate()
.map(|(i, e)| {
let i = rc(NaturalLit(i));
dhall_expr!({ index = i, value = e })
})
.collect();
rc(NEListLit(xs))
}
(ListBuild, _, [a0, g, ..]) => {
Expand Down Expand Up @@ -165,15 +171,13 @@ where
}
};
// TODO: use Embed to avoid reevaluating g
break dhall_expr!(g Natural (λ(x : Natural) -> x + 1) 0)
break dhall_expr!(g Natural (λ(x : Natural) -> x + 1) 0);
}
}
(NaturalFold, Some(NaturalLit(0)), [_, _, _, zero]) => {
Rc::clone(zero)
}
(NaturalFold, Some(NaturalLit(0)), [_, _, _, zero]) => Rc::clone(zero),
(NaturalFold, Some(NaturalLit(n)), [_, t, succ, zero]) => {
let fold = rc(Builtin(NaturalFold));
let n = rc(NaturalLit(n-1));
let n = rc(NaturalLit(n - 1));
let t = Rc::clone(t);
let succ = Rc::clone(succ);
let zero = Rc::clone(zero);
Expand Down Expand Up @@ -274,7 +278,9 @@ where
NaturalLit(x * y)
}
(TextAppend, TextLit(x), TextLit(y)) => TextLit(x + y),
(ListAppend, EmptyListLit(t), EmptyListLit(_)) => EmptyListLit(Rc::clone(t)),
(ListAppend, EmptyListLit(t), EmptyListLit(_)) => {
EmptyListLit(Rc::clone(t))
}
(ListAppend, EmptyListLit(_), _) => return y,
(ListAppend, _, EmptyListLit(_)) => return x,
(ListAppend, NEListLit(xs), NEListLit(ys)) => {
Expand Down
17 changes: 1 addition & 16 deletions dhall/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,7 @@ macro_rules! make_spec_test {
#[allow(non_snake_case)]
fn $name() {
use crate::common::*;

if cfg!(feature="nothreads") {
run_test($path, Feature::$type);
} else {
use std::thread;
// The parser stack overflows even on small files
// when compiled without optimizations
thread::Builder::new()
.stack_size(4 * 1024 * 1024)
.spawn(move || {
run_test($path, Feature::$type);
})
.unwrap()
.join()
.unwrap();
}
run_test($path, Feature::$type);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion dhall/tests/normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ norm!(spec_normalization_success_unit_Record, "unit/Record");
norm!(spec_normalization_success_unit_RecordEmpty, "unit/RecordEmpty");
// norm!(spec_normalization_success_unit_RecordProjection, "unit/RecordProjection");
// norm!(spec_normalization_success_unit_RecordProjectionEmpty, "unit/RecordProjectionEmpty");
norm!(spec_normalization_success_unit_RecordProjectionNormalizeArguments, "unit/RecordProjectionNormalizeArguments");
// norm!(spec_normalization_success_unit_RecordProjectionNormalizeArguments, "unit/RecordProjectionNormalizeArguments");
norm!(spec_normalization_success_unit_RecordSelection, "unit/RecordSelection");
norm!(spec_normalization_success_unit_RecordSelectionNormalizeArguments, "unit/RecordSelectionNormalizeArguments");
norm!(spec_normalization_success_unit_RecordType, "unit/RecordType");
Expand Down
1 change: 1 addition & 0 deletions dhall_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ itertools = "0.8.0"
term-painter = "0.2.3"
pest = { git = "https://github.com/pest-parser/pest" }
dhall_parser = { path = "../dhall_parser" }
iter_patterns = { path = "../iter_patterns" }
1 change: 1 addition & 0 deletions dhall_core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl<N, E> From<String> for InterpolatedText<N, E> {
}
}

#[derive(Debug, Clone)]
pub enum InterpolatedTextContents<'a, Note, Embed> {
Text(&'a str),
Expr(SubExpr<Note, Embed>),
Expand Down
1 change: 1 addition & 0 deletions dhall_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(trace_macros)]
#![feature(slice_patterns)]
#![allow(
clippy::many_single_char_names,
clippy::should_implement_trait,
Expand Down
Loading