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

Add a fieldsOf operator to get record field names #162

Merged
merged 4 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ UOp: UnaryOp<RichTerm> = {
"head" => UnaryOp::ListHead(),
"tail" => UnaryOp::ListTail(),
"length" => UnaryOp::ListLength(),
"fieldsOf" => UnaryOp::FieldsOf(),
};

switch_case: (Ident, RichTerm) = {
Expand Down Expand Up @@ -354,6 +355,7 @@ extern {
"head" => Token::Head,
"tail" => Token::Tail,
"length" => Token::Length,
"fieldsOf" => Token::FieldsOf,

"hasField" => Token::HasField,
"map" => Token::Map,
Expand Down
15 changes: 15 additions & 0 deletions src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,21 @@ fn process_unary_operation(
))
}
}
UnaryOp::FieldsOf() => {
if let Term::Record(map) = *t {
let mut fields: Vec<String> = map.keys().map(|Ident(id)| id.clone()).collect();
fields.sort();
aspiwack marked this conversation as resolved.
Show resolved Hide resolved
let terms = fields.into_iter().map(|id| Term::Str(id).into()).collect();
Ok(Closure::atomic_closure(Term::List(terms).into()))
} else {
Err(EvalError::TypeError(
String::from("Record"),
String::from("fieldsOf"),
arg_pos,
RichTerm { term: t, pos },
))
}
}
UnaryOp::MapRec(f) => {
if let Term::Record(rec) = *t {
let f_as_var = f.body.closurize(&mut env, f.env);
Expand Down
3 changes: 3 additions & 0 deletions src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub enum Token<'input> {
Head,
Tail,
Length,
FieldsOf,

Unwrap,
HasField,
Expand Down Expand Up @@ -219,6 +220,7 @@ impl<'input> fmt::Display for Token<'input> {
Token::Head => "head",
Token::Tail => "tail",
Token::Length => "length",
Token::FieldsOf => "fieldsOf",

Token::HasField => "hasField",
Token::Map => "map",
Expand Down Expand Up @@ -559,6 +561,7 @@ impl<'input> Lexer<'input> {
"map" => Token::Map,
"elemAt" => Token::ElemAt,
"merge" => Token::Merge,
"fieldsOf" => Token::FieldsOf,
ty @ "Dyn" | ty @ "Num" | ty @ "Bool" | ty @ "Str" | ty @ "List" => Token::Type(ty),
id => Token::Identifier(id),
};
Expand Down
11 changes: 11 additions & 0 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,4 +1253,15 @@ Assume(#alwaysTrue -> #alwaysFalse, not ) true
assert_npeq!("{ a = \"a\"; b = true }", "{ a = true; b = \"a\"}");
assert_npeq!("{ a = { a = true } }", "{a = { a = { a = true } } }");
}

#[test]
fn fields_of() {
assert_peq!("fieldsOf {}", "[]");
assert_peq!("fieldsOf {a = 1; b = 2; c = 3}", "[\"a\", \"b\", \"c\"]");
assert_peq!("fieldsOf {aAa = 1; Zzz = 2;}", "[\"Zzz\", \"aAa\"]");
assert_peq!(
"fieldsOf {foo = {bar = 0}; baz = Default(true)}",
"[\"baz\", \"foo\"]"
);
}
}
5 changes: 5 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ pub enum UnaryOp<CapturedTerm> {
/// string accumulator, the remaining chunks to be evaluated, and is applied to the current
/// chunk being evaluated.
ChunksConcat(String, Vec<StrChunk<CapturedTerm>>),

/// Return the names of the fields of a record as a string list.
FieldsOf(),
}

impl<Ty> UnaryOp<Ty> {
Expand Down Expand Up @@ -559,6 +562,8 @@ impl<Ty> UnaryOp<Ty> {
})
.collect(),
),

FieldsOf() => FieldsOf(),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,13 @@ pub fn get_uop_type(
)),
// This should not happen, as ChunksConcat() is only produced during evaluation.
UnaryOp::ChunksConcat(_, _) => panic!("cannot type ChunksConcat()"),
// forall rows. { rows } -> List
UnaryOp::FieldsOf() => TypeWrapper::Concrete(AbsType::arrow(
Box::new(TypeWrapper::Concrete(AbsType::StaticRecord(Box::new(
TypeWrapper::Ptr(new_var(state.table)),
)))),
Box::new(TypeWrapper::Concrete(AbsType::List())),
)),
})
}

Expand Down