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

Implements the make_field system macro #878

Merged
merged 4 commits into from
Dec 11, 2024
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
7 changes: 5 additions & 2 deletions src/lazy/expanded/e_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::lazy::expanded::compiler::{ExpansionAnalysis, ExpansionSingleton};
use crate::lazy::expanded::macro_evaluator::{
AnnotateExpansion, ConditionalExpansion, EExpressionArgGroup, ExprGroupExpansion,
FlattenExpansion, IsExhaustedIterator, MacroExpansion, MacroExpansionKind, MacroExpr,
MacroExprArgsIterator, MakeStructExpansion, MakeTextExpansion, RawEExpression,
TemplateExpansion, ValueExpr,
MacroExprArgsIterator, MakeFieldExpansion, MakeStructExpansion, MakeTextExpansion,
RawEExpression, TemplateExpansion, ValueExpr,
};
use crate::lazy::expanded::macro_table::{MacroKind, MacroRef};
use crate::lazy::expanded::template::TemplateMacroRef;
Expand Down Expand Up @@ -127,6 +127,9 @@ impl<'top, D: Decoder> EExpression<'top, D> {
MacroKind::MakeStruct => {
MacroExpansionKind::MakeStruct(MakeStructExpansion::new(arguments))
}
MacroKind::MakeField => {
MacroExpansionKind::MakeField(MakeFieldExpansion::new(arguments))
}
MacroKind::Annotate => MacroExpansionKind::Annotate(AnnotateExpansion::new(arguments)),
MacroKind::Flatten => MacroExpansionKind::Flatten(FlattenExpansion::new(
self.context,
Expand Down
150 changes: 147 additions & 3 deletions src/lazy/expanded/macro_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use crate::lazy::text::raw::v1_1::arg_group::EExpArg;
use crate::lazy::text::raw::v1_1::reader::MacroIdRef;
use crate::result::IonFailure;
use crate::{
ExpandedValueRef, ExpandedValueSource, IonError, IonResult, LazyExpandedStruct, LazyStruct,
LazyValue, Span, SymbolRef, ValueRef,
ExpandedValueRef, ExpandedValueSource, IonError, IonResult, LazyExpandedField,
LazyExpandedFieldName, LazyExpandedStruct, LazyStruct, LazyValue, Span, SymbolRef, ValueRef,
};

pub trait IsExhaustedIterator<'top, D: Decoder>:
Expand Down Expand Up @@ -389,6 +389,47 @@ impl<D: Decoder> Debug for ValueExpr<'_, D> {
}

impl<'top, D: Decoder> ValueExpr<'top, D> {
/// Like [`evaluate_singleton_in`](Self::evaluate_singleton_in), but uses an empty environment.
pub fn evaluate_singleton(&self) -> IonResult<LazyExpandedValue<'top, D>> {
self.evaluate_singleton_in(Environment::empty())
}

/// Evaluates this `ValueExpr` in the context of the provided `environment`, producing either
/// an `Ok` containing the expansion's sole `LazyExpandedValue`, or an `Err` indicating that
/// this expression expanded to zero or two+ values.
pub fn evaluate_singleton_in(
&self,
environment: Environment<'top, D>,
) -> IonResult<LazyExpandedValue<'top, D>> {
let invocation = match self {
// If it's a single value, we're already done.
ValueExpr::ValueLiteral(v) => return Ok(*v),
ValueExpr::MacroInvocation(i) => *i,
};
let expansion = invocation.expand()?;
// If it's a macro that we know will produce exactly one value, evaluate it without a stack.
if invocation
.expansion_analysis()
.must_produce_exactly_one_value()
{
return expansion.expand_singleton();
}
// Otherwise, use the general case macro evaluator.
let mut evaluator = MacroEvaluator::new_with_environment(environment);
evaluator.push(expansion);
let Some(value) = evaluator.next()? else {
return IonResult::decoding_error(
"expected macro to produce exactly one value but it produced none",
);
};
if evaluator.next()?.is_some() {
return IonResult::decoding_error(
"expected macro to produce exactly one value but it produced more than one",
);
}
Ok(value)
}

pub fn expect_value_literal(&self) -> IonResult<LazyExpandedValue<'top, D>> {
match self {
ValueExpr::ValueLiteral(value) => Ok(*value),
Expand Down Expand Up @@ -460,6 +501,7 @@ pub enum MacroExpansionKind<'top, D: Decoder> {
MakeString(MakeTextExpansion<'top, D>),
MakeSymbol(MakeTextExpansion<'top, D>),
MakeStruct(MakeStructExpansion<'top, D>),
MakeField(MakeFieldExpansion<'top, D>),
Annotate(AnnotateExpansion<'top, D>),
Flatten(FlattenExpansion<'top, D>),
Template(TemplateExpansion<'top>),
Expand Down Expand Up @@ -541,6 +583,7 @@ impl<'top, D: Decoder> MacroExpansion<'top, D> {
ExprGroup(expr_group_expansion) => expr_group_expansion.next(context, environment),
MakeString(make_string_expansion) => make_string_expansion.make_text_value(context),
MakeSymbol(make_symbol_expansion) => make_symbol_expansion.make_text_value(context),
MakeField(make_field_expansion) => make_field_expansion.next(context, environment),
MakeStruct(make_struct_expansion) => make_struct_expansion.next(context, environment),
Annotate(annotate_expansion) => annotate_expansion.next(context, environment),
Flatten(flatten_expansion) => flatten_expansion.next(),
Expand All @@ -558,6 +601,7 @@ impl<D: Decoder> Debug for MacroExpansion<'_, D> {
MacroExpansionKind::ExprGroup(_) => "[internal] expr_group",
MacroExpansionKind::MakeString(_) => "make_string",
MacroExpansionKind::MakeSymbol(_) => "make_symbol",
MacroExpansionKind::MakeField(_) => "make_field",
MacroExpansionKind::MakeStruct(_) => "make_struct",
MacroExpansionKind::Annotate(_) => "annotate",
MacroExpansionKind::Flatten(_) => "flatten",
Expand Down Expand Up @@ -1115,6 +1159,50 @@ impl<'top, D: Decoder> ConditionalExpansion<'top, D> {
}
}

// ===== Implementation of the `make_field` macro =====

#[derive(Copy, Clone, Debug)]
pub struct MakeFieldExpansion<'top, D: Decoder> {
arguments: MacroExprArgsIterator<'top, D>,
}

impl<'top, D: Decoder> MakeFieldExpansion<'top, D> {
pub fn new(arguments: MacroExprArgsIterator<'top, D>) -> Self {
Self { arguments }
}

fn next(
&mut self,
context: EncodingContextRef<'top>,
environment: Environment<'top, D>,
) -> IonResult<MacroExpansionStep<'top, D>> {
// The parser will have confirmed that two argument expressions
// were passed in: a field name and value.
let name_expr = self.arguments.next().unwrap()?;
let value_expr = self.arguments.next().unwrap()?;

let name = name_expr
.evaluate_singleton_in(environment)?
.read_resolved()?
.expect_text()
.map_err(|_| {
IonError::decoding_error("`make_field`'s first argument must be a text value")
})
.map(SymbolRef::with_text)?;
let value = value_expr.evaluate_singleton_in(environment)?;
let field = LazyExpandedField::new(LazyExpandedFieldName::MakeField(name), value);
let lazy_expanded_struct = LazyExpandedStruct::from_make_field(context, field);
let lazy_struct = LazyStruct::new(lazy_expanded_struct);
let value_ref = context
.allocator()
.alloc_with(|| ValueRef::Struct(lazy_struct));
let lazy_expanded_value = LazyExpandedValue::from_constructed(context, &[], value_ref);
Ok(MacroExpansionStep::FinalStep(Some(
ValueExpr::ValueLiteral(lazy_expanded_value),
)))
}
}

// ===== Implementation of the `make_struct` macro =====
#[derive(Copy, Clone, Debug)]
pub struct MakeStructExpansion<'top, D: Decoder> {
Expand All @@ -1136,7 +1224,7 @@ impl<'top, D: Decoder> MakeStructExpansion<'top, D> {
// computed) struct. If/when the application tries to iterate over its fields,
// the iterator will evaluate the field expressions incrementally.
let lazy_expanded_struct =
LazyExpandedStruct::from_constructed(context, environment, self.arguments);
LazyExpandedStruct::from_make_struct(context, environment, self.arguments);
let lazy_struct = LazyStruct::new(lazy_expanded_struct);
// Store the `Struct` in the bump so it's guaranteed to be around as long as the reader is
// positioned on this top-level value.
Expand Down Expand Up @@ -2847,6 +2935,62 @@ mod tests {
)
}

#[test]
fn make_field_eexp() -> IonResult<()> {
stream_eq(
r#"
(:make_field a 1)
(:make_field "a" 1)
(:make_field "foo" [1, 2, 3])
(:make_field "bar" (:make_field baz 4) )
"#,
r#"
{a: 1}
{a: 1}
{foo: [1, 2, 3]}
{bar: {baz: 4}}
"#,
)
}

#[test]
fn combine_make_struct_with_make_field() -> IonResult<()> {
stream_eq(
r#"
(:add_macros
(macro new_yorker (name occupation)
(.make_struct
(.make_field "name" (%name))
(.make_field "occupation" (%occupation))
{city: "New York", state: NY})))
(:new_yorker "Grace" "Author")
(:new_yorker "Ravi" "Painter")
(:new_yorker "Otis" "Musician")

"#,
r#"
{
name: "Grace",
occupation: "Author",
city: "New York",
state: NY,
}
{
name: "Ravi",
occupation: "Painter",
city: "New York",
state: NY,
}
{
name: "Otis",
occupation: "Musician",
city: "New York",
state: NY,
}
"#,
)
}

#[test]
fn make_string_tdl_macro_invocation() -> IonResult<()> {
let invocation = r#"
Expand Down
3 changes: 2 additions & 1 deletion src/lazy/expanded/macro_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub enum MacroKind {
ExprGroup,
MakeString,
MakeSymbol,
MakeField,
MakeStruct,
Annotate,
Flatten,
Expand Down Expand Up @@ -408,7 +409,7 @@ impl MacroTable {
builtin(
"make_field",
"(name value)",
MacroKind::ToDo,
MacroKind::MakeField,
ExpansionAnalysis::single_application_value(IonType::Struct),
),
template(
Expand Down
Loading
Loading