use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use crate::diagnostic::{BuiltinDiagnostic, Diagnostic, Result};
use crate::program::{Cardinality, ProgramDescriptor, ProgramRegistry, StackAccess};
use crate::source::{
ArgumentValue, AudioSettings, Invocation, Item, ItemKind, ItemOrigin, Literal, OutputBindings,
ProgramBody, ProjectSettings, Reference, SOURCE_PROGRAM_DEFAULT_STACK_ACCESS,
STACK_BLOCK_DEFAULT_STACK_ACCESS, ScalarBinaryOperator,
ScalarExpression as SourceScalarExpression, ScalarPostfixOperator, ScalarUnaryOperator,
SourceExternalArgument, SourceExternalImplementation, SourceFile, SourceImport, SourceInput,
SourcePackage, SourceParameter, SourceProgram, SourceProgramImplementation, SourceUnit,
SourceUnitId, Spanned, StackBlock, UnlinkedSourceUnit, VideoSettings,
};
use super::syntax::{
Argument, BinaryOperator, Block, Declaration, Expression, ExternalArgumentDeclaration,
OutputBindings as SyntaxOutputBindings, PostfixOperator, Scalar, ScalarExpression,
SourceFileSyntax, Statement, UnaryOperator,
};
use super::{parser, sugar};
pub fn parse_str(path: &Path, text: &str) -> Result<SourcePackage> {
parse_str_with_registry(path, text, &ProgramRegistry::default())
}
pub(crate) fn parse_str_with_registry(
path: &Path,
text: &str,
registry: &ProgramRegistry,
) -> Result<SourcePackage> {
let source = SourceFile::new(path.to_path_buf(), text.to_owned());
let syntax = parser::parse(source.clone())?;
reject_file_backed_declarations(&syntax)?;
let programs = registry_shapes(registry);
let unit = lower_source(source, syntax, &programs)?;
Ok(SourcePackage {
root: SourceUnitId(0),
units: vec![SourceUnit {
source: unit.source,
imports: Vec::new(),
project: unit.project,
program: unit.program,
output: unit.output,
}],
})
}
pub(crate) fn lower_source(
source: SourceFile,
syntax: SourceFileSyntax,
programs: &BTreeMap<String, CallableShape>,
) -> Result<UnlinkedSourceUnit> {
let declarations = lower_declarations(syntax.declarations)?;
let lexical = declarations
.inputs
.iter()
.map(|input| input.name.clone())
.collect();
let parameters = declarations
.parameters
.iter()
.map(|parameter| parameter.name.value.clone())
.collect();
let lowerer = Lowerer {
programs,
parameters,
};
let implementation = match declarations.external {
Some(external) => {
if !syntax.statements.is_empty() {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::ExternalWithBody,
"an external program cannot also contain executable statements",
syntax.statements[0].span.clone(),
));
}
SourceProgramImplementation::External(external)
}
None => SourceProgramImplementation::Body(ProgramBody {
items: lowerer.lower_statements(&syntax.statements, &lexical, &BTreeSet::new())?,
span: syntax.span.clone(),
}),
};
Ok(UnlinkedSourceUnit {
source,
imports: declarations.imports,
project: declarations.project,
program: SourceProgram {
inputs: declarations.inputs,
parameters: declarations.parameters,
implementation,
span: syntax.version.span,
stack_access: SOURCE_PROGRAM_DEFAULT_STACK_ACCESS,
},
output: declarations.output,
})
}
pub(super) fn builtin_shapes() -> BTreeMap<String, CallableShape> {
registry_shapes(&ProgramRegistry::default())
}
fn registry_shapes(registry: &ProgramRegistry) -> BTreeMap<String, CallableShape> {
registry
.definitions()
.iter()
.map(|definition| {
(
definition.descriptor.name.clone(),
CallableShape::from_descriptor(&definition.descriptor),
)
})
.collect()
}
#[derive(Clone, Debug)]
pub(super) struct CallableShape {
inputs: Vec<String>,
parameters: Vec<String>,
}
impl CallableShape {
pub(super) fn from_descriptor(descriptor: &ProgramDescriptor) -> Self {
Self {
inputs: descriptor
.inputs
.iter()
.map(|input| input.name.clone())
.collect(),
parameters: descriptor
.parameters
.iter()
.map(|parameter| parameter.name.clone())
.collect(),
}
}
pub(super) fn from_source(program: &SourceProgram) -> Self {
Self {
inputs: program
.inputs()
.iter()
.map(|input| input.name.clone())
.collect(),
parameters: program
.parameters()
.iter()
.map(|parameter| parameter.name.value.clone())
.collect(),
}
}
fn has_input(&self, name: &str) -> bool {
self.inputs.iter().any(|input| input == name)
}
fn parameter_index(&self, name: &str) -> Option<usize> {
self.parameters
.iter()
.position(|parameter| parameter == name)
}
}
fn reject_file_backed_declarations(syntax: &SourceFileSyntax) -> Result<()> {
for declaration in &syntax.declarations {
match declaration {
Declaration::Import(import) => {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::ImportRequiresFile,
"imports require file-backed package loading",
import.path.span.clone(),
));
}
Declaration::Config(_)
| Declaration::External(_)
| Declaration::Input(_)
| Declaration::Parameter(_) => {}
}
}
Ok(())
}
struct LoweredDeclarations {
imports: Vec<SourceImport>,
external: Option<SourceExternalImplementation>,
project: Option<Spanned<ProjectSettings>>,
output: Option<Spanned<PathBuf>>,
inputs: Vec<SourceInput>,
parameters: Vec<SourceParameter>,
}
fn lower_declarations(declarations: Vec<Declaration>) -> Result<LoweredDeclarations> {
let mut imports = Vec::new();
let mut external = None;
let mut project = None;
let mut output = None;
let mut inputs = Vec::new();
let mut parameters = Vec::new();
let mut config_span = None;
for declaration in declarations {
match declaration {
Declaration::Config(config) => {
if config_span.replace(config.span.clone()).is_some() {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::DuplicateConfig,
"a source file may declare at most one `config` block",
config.span,
));
}
let video = config
.video
.map(|video| {
Ok::<_, Diagnostic>(VideoSettings {
width: video
.width
.map(|value| parse_u32(value, "width"))
.transpose()?,
height: video
.height
.map(|value| parse_u32(value, "height"))
.transpose()?,
fps: video.fps,
})
})
.transpose()?;
let audio = config
.audio
.map(|audio| {
Ok::<_, Diagnostic>(AudioSettings {
sample_rate: audio
.sample_rate
.map(|value| parse_u32(value, "sample_rate"))
.transpose()?,
})
})
.transpose()?;
if video.is_some() || audio.is_some() {
project = Some(Spanned::new(
ProjectSettings {
video: video.unwrap_or_default(),
audio: audio.unwrap_or_default(),
},
config.span.clone(),
));
}
output = config
.output
.map(|value| Spanned::new(PathBuf::from(value.value), value.span));
}
Declaration::Import(import) => imports.push(SourceImport {
alias: import.alias,
}),
Declaration::External(declaration) => {
if external.is_some() {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::DuplicateExternal,
"a source file may declare at most one `external` block",
declaration.span,
));
}
external = Some(lower_external_declaration(declaration)?);
}
Declaration::Input(input) => inputs.push(SourceInput {
name: input.name.value,
value_type: input.value_type.value.into(),
cardinality: Cardinality::One,
declared_at: input.name.span,
}),
Declaration::Parameter(parameter) => parameters.push(SourceParameter {
name: parameter.name,
parameter_type: parameter.parameter_type.value,
default: parameter.default.map(lower_scalar_literal),
}),
}
}
Ok(LoweredDeclarations {
imports,
external,
project,
output,
inputs,
parameters,
})
}
fn lower_external_declaration(
declaration: super::syntax::ExternalDeclaration,
) -> Result<SourceExternalImplementation> {
let executable = declaration.executable.ok_or_else(|| {
Diagnostic::builtin(
BuiltinDiagnostic::MissingExternalField,
"external program requires `executable`",
declaration.span.clone(),
)
})?;
if executable.value.is_empty() {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::InvalidExternalProgram,
"external `executable` must not be empty",
executable.span,
));
}
let arguments = declaration
.arguments
.unwrap_or_default()
.into_iter()
.map(|argument| match argument {
ExternalArgumentDeclaration::Text(value) => SourceExternalArgument::Text(value),
ExternalArgumentDeclaration::File(path) => {
SourceExternalArgument::File(Spanned::new(PathBuf::from(path.value), path.span))
}
})
.collect();
let semantic_version = declaration.semantic_version.ok_or_else(|| {
Diagnostic::builtin(
BuiltinDiagnostic::MissingExternalField,
"external program requires `semantic_version`",
declaration.span.clone(),
)
})?;
let semantic_version_value = semantic_version.value.parse::<u32>().map_err(|_| {
Diagnostic::builtin(
BuiltinDiagnostic::InvalidExternalProgram,
"external `semantic_version` must be a positive unsigned integer",
semantic_version.span.clone(),
)
})?;
if semantic_version_value == 0 {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::InvalidExternalProgram,
"external `semantic_version` must be greater than zero",
semantic_version.span,
));
}
let preserve = declaration.preserve.ok_or_else(|| {
Diagnostic::builtin(
BuiltinDiagnostic::MissingExternalField,
"external program requires `preserve`",
declaration.span,
)
})?;
Ok(SourceExternalImplementation {
executable: Spanned::new(PathBuf::from(executable.value), executable.span),
arguments,
semantic_version: Spanned::new(semantic_version_value, semantic_version.span),
preserve,
})
}
fn parse_u32(value: Spanned<String>, field: &str) -> Result<Spanned<u32>> {
let parsed = value.value.parse::<u32>().map_err(|_| {
Diagnostic::builtin(
BuiltinDiagnostic::InvalidVideoSpec,
format!("`{field}` must be an unsigned integer"),
value.span.clone(),
)
})?;
Ok(Spanned::new(parsed, value.span))
}
fn lower_scalar_literal(value: Scalar) -> SourceScalarExpression {
match value {
Scalar::String(value) => lower_scalar_leaf(value, true),
Scalar::Atom(value) => lower_scalar_leaf(value, false),
Scalar::Expression(expression) => lower_scalar_expression(&expression),
}
}
fn lower_scalar_leaf(value: Spanned<String>, quoted: bool) -> SourceScalarExpression {
SourceScalarExpression::Literal(if quoted {
Literal::String(value.value, value.span)
} else {
match value.value.parse::<i64>() {
Ok(integer) => Literal::Integer(integer, value.span),
Err(_) => Literal::Atom(value.value, value.span),
}
})
}
fn lower_scalar_expression(expression: &ScalarExpression) -> SourceScalarExpression {
match expression {
ScalarExpression::String(value) => lower_scalar_leaf(value.clone(), true),
ScalarExpression::Atom(value) => lower_scalar_leaf(value.clone(), false),
ScalarExpression::Reference(reference) => {
SourceScalarExpression::Reference(reference.clone())
}
ScalarExpression::Selector { root, path, span } => SourceScalarExpression::Selector {
root: root.clone(),
path: path.clone(),
span: span.clone(),
},
ScalarExpression::Unary {
operator,
operand,
span,
} => SourceScalarExpression::Unary {
operator: match operator {
UnaryOperator::Positive => ScalarUnaryOperator::Positive,
UnaryOperator::Negative => ScalarUnaryOperator::Negative,
},
operand: Box::new(lower_scalar_expression(operand)),
span: span.clone(),
},
ScalarExpression::Binary {
operator,
left,
right,
span,
} => SourceScalarExpression::Binary {
operator: match operator {
BinaryOperator::Range => ScalarBinaryOperator::Range,
BinaryOperator::Add => ScalarBinaryOperator::Add,
BinaryOperator::Subtract => ScalarBinaryOperator::Subtract,
BinaryOperator::Multiply => ScalarBinaryOperator::Multiply,
BinaryOperator::Divide => ScalarBinaryOperator::Divide,
},
left: Box::new(lower_scalar_expression(left)),
right: Box::new(lower_scalar_expression(right)),
span: span.clone(),
},
ScalarExpression::Postfix {
operator,
operand,
span,
} => SourceScalarExpression::Postfix {
operator: match operator {
PostfixOperator::Percent => ScalarPostfixOperator::Percent,
PostfixOperator::Milliseconds => ScalarPostfixOperator::Milliseconds,
PostfixOperator::Seconds => ScalarPostfixOperator::Seconds,
},
operand: Box::new(lower_scalar_expression(operand)),
span: span.clone(),
},
}
}
struct Lowerer<'a> {
programs: &'a BTreeMap<String, CallableShape>,
parameters: BTreeSet<String>,
}
impl Lowerer<'_> {
fn lower_statements(
&self,
statements: &[Statement],
lexical: &BTreeSet<String>,
parent_scalars: &BTreeSet<String>,
) -> Result<Vec<Item>> {
let mut scalars = parent_scalars.clone();
collect_body_scalar_alias_names(statements, &mut scalars);
let mut items = Vec::new();
for statement in statements {
items.extend(self.lower_statement(statement, lexical, &scalars)?);
}
Ok(items)
}
fn lower_statement(
&self,
statement: &Statement,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
) -> Result<Vec<Item>> {
let bindings = lower_output_bindings(&statement.output_bindings);
match &statement.expression {
Expression::Reference(reference) => Ok(vec![Item {
kind: ItemKind::Reference(Reference {
name: reference.clone(),
}),
output_bindings: bindings,
origin: ItemOrigin::authored("reference", statement.span.clone()),
}]),
Expression::ScalarBinding { name, value, .. } => Ok(vec![Item {
kind: ItemKind::ScalarBinding(crate::source::ScalarBinding {
name: name.clone(),
value: lower_scalar_expression(value),
}),
output_bindings: OutputBindings::None,
origin: ItemOrigin::authored("scalar binding", statement.span.clone()),
}]),
Expression::Invocation(invocation) => {
self.lower_invocation(invocation, bindings, lexical, scalars)
}
Expression::Block(block) => Ok(vec![self.lower_stack_block(
block,
bindings,
lexical,
scalars,
statement.span.clone(),
)?]),
Expression::String(_) | Expression::Atom(_) | Expression::Scalar(_) => {
Err(Diagnostic::builtin(
BuiltinDiagnostic::InvalidStatement,
"scalar values cannot be used as executable statements",
statement.span.clone(),
))
}
}
}
fn lower_invocation(
&self,
invocation: &super::syntax::Invocation,
output_bindings: OutputBindings,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
) -> Result<Vec<Item>> {
if let Some(sugar) = sugar::resolve(&invocation.name.value) {
return self.lower_sugar(sugar, invocation, output_bindings, lexical, scalars);
}
let shape = self.programs.get(&invocation.name.value).ok_or_else(|| {
Diagnostic::builtin(
BuiltinDiagnostic::UnknownProgram,
format!("unknown program `{}`", invocation.name.value),
invocation.name.span.clone(),
)
})?;
let mut preceding = Vec::new();
let mut arguments = BTreeMap::new();
let mut assigned_parameters = BTreeSet::new();
let mut has_named_graph_input = false;
for argument in &invocation.arguments {
let Argument::Named { name, value } = argument else {
continue;
};
if arguments.contains_key(&name.value) {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::DuplicateArgument,
format!("duplicate argument `{}`", name.value),
name.span.clone(),
));
}
let lowered = if shape.has_input(&name.value) {
has_named_graph_input = true;
self.lower_explicit_input(value, lexical, scalars)?
} else if let Some(slot) = shape.parameter_index(&name.value) {
assigned_parameters.insert(slot);
Self::lower_scalar_argument(value, &invocation.name.value, &name.value)?
} else {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::UnknownProgramArgument,
format!(
"unknown argument `{}` for program `{}`",
name.value, invocation.name.value
),
name.span.clone(),
));
};
arguments.insert(name.value.clone(), lowered);
}
let mut next_parameter = 0;
for argument in &invocation.arguments {
let Argument::Positional(value) = argument else {
continue;
};
if self.is_scalar_expression(value, lexical, scalars) {
while assigned_parameters.contains(&next_parameter) {
next_parameter += 1;
}
let Some(parameter) = shape.parameters.get(next_parameter) else {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::TooManyPositionalArguments,
format!(
"program `{}` has no remaining scalar parameter for this argument",
invocation.name.value
),
value.span().clone(),
));
};
arguments.insert(
parameter.clone(),
Self::lower_scalar_argument(value, &invocation.name.value, parameter)?,
);
assigned_parameters.insert(next_parameter);
next_parameter += 1;
} else {
if has_named_graph_input {
return Err(Diagnostic::builtin(
BuiltinDiagnostic::MixedGraphArgumentStyles,
"positional graph expressions cannot be mixed with named graph inputs",
value.span().clone(),
));
}
preceding.extend(self.lower_graph_expression(value, lexical, scalars)?);
}
}
let mut body_lexical = lexical.clone();
body_lexical.extend(shape.inputs.iter().cloned());
let body = invocation
.body
.as_ref()
.map(|block| self.lower_program_body(block, &body_lexical, scalars))
.transpose()?;
preceding.push(Item {
kind: ItemKind::Invocation(Invocation {
program: invocation.name.clone(),
type_argument: invocation.type_argument.clone(),
stack_access: invocation.access.clone(),
arguments,
body,
}),
output_bindings,
origin: ItemOrigin::authored(invocation.name.value.clone(), invocation.span.clone()),
});
Ok(preceding)
}
fn lower_sugar(
&self,
sugar: sugar::Sugar,
invocation: &super::syntax::Invocation,
output_bindings: OutputBindings,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
) -> Result<Vec<Item>> {
match sugar {
sugar::Sugar::Clip => self.lower_clip(invocation, output_bindings, lexical, scalars),
}
}
fn lower_clip(
&self,
invocation: &super::syntax::Invocation,
output_bindings: OutputBindings,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
) -> Result<Vec<Item>> {
if let Some(argument) = invocation.arguments.first() {
let span = match argument {
Argument::Positional(value) => value.span(),
Argument::Named { name, .. } => &name.span,
};
return Err(Diagnostic::builtin(
BuiltinDiagnostic::UnexpectedSugarArgument,
"`clip` does not accept arguments; put operations inside its body",
span.clone(),
));
}
let mut body = invocation
.body
.as_ref()
.map(|body| self.lower_program_body(body, lexical, scalars))
.transpose()?
.unwrap_or_else(|| ProgramBody {
items: Vec::new(),
span: invocation.name.span.clone(),
});
let expansion = sugar::Expansion::new(sugar::Sugar::Clip, invocation.span.clone());
let access = invocation
.access
.as_ref()
.map_or(StackAccess::Owned, |access| access.value);
body.items.push(expansion.hidden(
"concat",
ItemKind::Invocation(Invocation {
program: Spanned::new("concat".to_owned(), invocation.name.span.clone()),
type_argument: invocation.type_argument.clone(),
stack_access: Some(Spanned::new(
StackAccess::Owned,
invocation.name.span.clone(),
)),
arguments: BTreeMap::new(),
body: None,
}),
));
Ok(vec![
expansion.visible(
"result",
ItemKind::StackBlock(StackBlock {
stack_access: access,
body,
}),
output_bindings,
),
expansion.hidden(
"cleanup",
ItemKind::Invocation(Invocation {
program: Spanned::new("drop".to_owned(), invocation.name.span.clone()),
type_argument: None,
stack_access: Some(Spanned::new(
StackAccess::Owned,
invocation.name.span.clone(),
)),
arguments: BTreeMap::new(),
body: None,
}),
),
])
}
fn lower_scalar_argument(
expression: &Expression,
program: &str,
parameter: &str,
) -> Result<ArgumentValue> {
match expression {
Expression::String(value) => Ok(ArgumentValue::Scalar(lower_scalar_leaf(
value.clone(),
true,
))),
Expression::Atom(value) => Ok(ArgumentValue::Scalar(lower_scalar_leaf(
value.clone(),
false,
))),
Expression::Scalar(expression) => {
Ok(ArgumentValue::Scalar(lower_scalar_expression(expression)))
}
Expression::Reference(reference) => Ok(ArgumentValue::Scalar(
SourceScalarExpression::Reference(reference.clone()),
)),
Expression::ScalarBinding { .. } | Expression::Invocation(_) | Expression::Block(_) => {
Err(Diagnostic::builtin(
BuiltinDiagnostic::InvalidArgumentType,
format!("parameter `{program}.{parameter}` requires a scalar value"),
expression.span().clone(),
))
}
}
}
fn lower_explicit_input(
&self,
expression: &Expression,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
) -> Result<ArgumentValue> {
match expression {
Expression::Reference(reference) => Ok(ArgumentValue::Reference(reference.clone())),
Expression::Invocation(_) | Expression::Block(_) => {
Ok(ArgumentValue::Body(ProgramBody {
items: self.lower_graph_expression(expression, lexical, scalars)?,
span: expression.span().clone(),
}))
}
Expression::ScalarBinding { .. }
| Expression::String(_)
| Expression::Atom(_)
| Expression::Scalar(_) => Err(Diagnostic::builtin(
BuiltinDiagnostic::InvalidArgumentType,
"a graph input requires a reference, invocation, or stack block",
expression.span().clone(),
)),
}
}
fn lower_graph_expression(
&self,
expression: &Expression,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
) -> Result<Vec<Item>> {
match expression {
Expression::Reference(reference) => Ok(vec![Item {
kind: ItemKind::Reference(Reference {
name: reference.clone(),
}),
output_bindings: OutputBindings::None,
origin: ItemOrigin::authored("reference", reference.span.clone()),
}]),
Expression::Invocation(invocation) => {
self.lower_invocation(invocation, OutputBindings::None, lexical, scalars)
}
Expression::Block(block) => Ok(vec![self.lower_stack_block(
block,
OutputBindings::None,
lexical,
scalars,
block.span.clone(),
)?]),
Expression::ScalarBinding { .. }
| Expression::String(_)
| Expression::Atom(_)
| Expression::Scalar(_) => Err(Diagnostic::builtin(
BuiltinDiagnostic::InvalidArgumentType,
"expected a graph-producing expression",
expression.span().clone(),
)),
}
}
fn lower_stack_block(
&self,
block: &Block,
output_bindings: OutputBindings,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
span: crate::source::SourceSpan,
) -> Result<Item> {
Ok(Item {
kind: ItemKind::StackBlock(StackBlock {
stack_access: block
.access
.as_ref()
.map_or(STACK_BLOCK_DEFAULT_STACK_ACCESS, |access| access.value),
body: self.lower_program_body(block, lexical, scalars)?,
}),
output_bindings,
origin: ItemOrigin::authored("stack block", span),
})
}
fn lower_program_body(
&self,
block: &Block,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
) -> Result<ProgramBody> {
Ok(ProgramBody {
items: self.lower_statements(&block.statements, lexical, scalars)?,
span: block.span.clone(),
})
}
fn is_scalar_expression(
&self,
expression: &Expression,
lexical: &BTreeSet<String>,
scalars: &BTreeSet<String>,
) -> bool {
match expression {
Expression::String(_) | Expression::Atom(_) | Expression::Scalar(_) => true,
Expression::Reference(reference) => {
!lexical.contains(&reference.value)
&& (self.parameters.contains(&reference.value)
|| scalars.contains(&reference.value))
}
Expression::ScalarBinding { .. } | Expression::Invocation(_) | Expression::Block(_) => {
false
}
}
}
}
fn collect_body_scalar_alias_names(statements: &[Statement], names: &mut BTreeSet<String>) {
for statement in statements {
if let Expression::ScalarBinding { name, .. } = &statement.expression {
names.insert(name.value.clone());
}
}
}
fn lower_output_bindings(bindings: &SyntaxOutputBindings) -> OutputBindings {
match bindings {
SyntaxOutputBindings::None => OutputBindings::None,
SyntaxOutputBindings::One(name) => OutputBindings::One(name.clone()),
SyntaxOutputBindings::Many(names, span) => {
OutputBindings::Many(names.clone(), span.clone())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compiler;
use crate::model::ValueType;
use crate::source::SurfaceVisibility;
fn body(package: &SourcePackage) -> &ProgramBody {
let SourceProgramImplementation::Body(body) = package.root().program().implementation()
else {
panic!("expected ClipAsm body");
};
body
}
#[test]
fn lowers_and_compiles_native_positional_graph_expressions() {
let package = parse_str(
Path::new("program.clipasm"),
"clipasm 1\nconfig {\n video {\n width = 1280\n height = 720\n fps = 30\n }\n}\nflash_cut(image(\"before.png\", 1s), image(\"after.png\", 1s), 100ms)\n",
)
.expect("native source");
let compiled = compiler::compile(&package).expect("compiled source");
assert_eq!(compiled.outputs().len(), 1);
assert_eq!(compiled.outputs()[0].value_type(), ValueType::Video);
}
#[test]
fn lowers_parameters_named_inputs_and_stack_blocks() {
let package = parse_str(
Path::new("program.clipasm"),
"clipasm 1\nparam duration: Duration = 1s\n{\n set_audio(video=video(\"source.mp4\"), audio=audio(\"sound.wav\"))\n image(\"card.png\", $duration, contain)\n} as (video, card)\ndrop<Video>\ndrop<Video>\n",
)
.expect("native source");
let ItemKind::StackBlock(block) = &body(&package).items[0].kind else {
panic!("stack block");
};
assert_eq!(block.stack_access, StackAccess::Visible);
let compiled = compiler::compile(&package).expect("compiled source");
assert!(compiled.outputs().is_empty());
}
#[test]
fn explicit_owned_stack_block_overrides_the_visible_default() {
let package = parse_str(
Path::new("program.clipasm"),
"clipasm 1\n@owned { image(\"card.png\", 1s) }\n",
)
.expect("native source");
let ItemKind::StackBlock(block) = &body(&package).items[0].kind else {
panic!("stack block");
};
assert_eq!(block.stack_access, StackAccess::Owned);
}
#[test]
fn rejects_mixed_positional_and_named_graph_inputs() {
let error = parse_str(
Path::new("program.clipasm"),
"clipasm 1\nflash_cut(image(\"after.png\", 1s), before=image(\"before.png\", 1s), duration=100ms)\n",
)
.expect_err("mixed graph argument styles");
assert_eq!(error.code, "E_MIXED_GRAPH_ARGUMENT_STYLES");
}
#[test]
fn string_parsing_rejects_imports_that_require_package_loading() {
let import = parse_str(
Path::new("program.clipasm"),
"clipasm 1\nimport \"effect.clipasm\" as effect\n",
)
.expect_err("file-backed import");
assert_eq!(import.code, "E_IMPORT_REQUIRES_FILE");
}
#[test]
fn clip_expands_with_surface_provenance_and_hidden_cleanup() {
let package = parse_str(
Path::new("program.clipasm"),
"clipasm 1\n@visible clip<Audio> {\n audio(\"sound.wav\")\n} as soundtrack\n$soundtrack\n",
)
.expect("native clip");
let items = &body(&package).items;
assert_eq!(items.len(), 3);
let ItemKind::StackBlock(block) = &items[0].kind else {
panic!("generated stack block");
};
assert_eq!(block.stack_access, StackAccess::Visible);
let ItemKind::Invocation(concat) = &block.body.items.last().expect("generated concat").kind
else {
panic!("generated concat invocation");
};
assert_eq!(concat.program.value, "concat");
assert_eq!(
concat.type_argument.as_ref().map(|argument| argument.value),
Some(ValueType::Audio)
);
assert_eq!(
concat.stack_access.as_ref().map(|access| access.value),
Some(StackAccess::Owned)
);
assert_eq!(
block
.body
.items
.last()
.expect("generated concat")
.origin
.visibility,
SurfaceVisibility::Hidden
);
assert_eq!(items[0].origin.construct, "clip");
assert_eq!(items[0].origin.visibility, SurfaceVisibility::Visible);
assert_eq!(items[0].origin.expansion.len(), 1);
assert_eq!(items[0].origin.expansion[0].sugar, "clip");
assert_eq!(items[0].origin.expansion[0].role, "result");
let ItemKind::Invocation(drop) = &items[1].kind else {
panic!("generated drop");
};
assert_eq!(drop.program.value, "drop");
assert_eq!(
drop.stack_access.as_ref().map(|access| access.value),
Some(StackAccess::Owned)
);
assert_eq!(items[1].origin.construct, "clip");
assert_eq!(items[1].origin.visibility, SurfaceVisibility::Hidden);
assert_eq!(items[1].origin.expansion[0].role, "cleanup");
let compiled = compiler::compile(&package).expect("compiled clip");
assert_eq!(compiled.outputs().len(), 1);
assert_eq!(compiled.outputs()[0].value_type(), ValueType::Audio);
let constructs = compiled
.explain()
.iter()
.map(crate::compiler::ExplainEntry::construct)
.collect::<Vec<_>>();
assert_eq!(
constructs
.iter()
.filter(|construct| **construct == "clip")
.count(),
1
);
assert!(!constructs.contains(&"drop"));
}
#[test]
fn clip_is_semantically_equal_to_explicit_stack_block_concat_and_drop() {
let sugar = parse_str(
Path::new("program.clipasm"),
"clipasm 1\nclip {\n image(\"card.png\", 1s)\n} as opening\n$opening\n",
)
.expect("sugar source");
let explicit = parse_str(
Path::new("program.clipasm"),
"clipasm 1\n@owned {\n image(\"card.png\", 1s)\n @owned concat\n} as opening\n@owned drop\n$opening\n",
)
.expect("explicit source");
assert_eq!(
compiler::compile(&sugar)
.expect("compiled sugar")
.structure_hash(),
compiler::compile(&explicit)
.expect("compiled explicit form")
.structure_hash()
);
}
#[test]
fn clip_reports_generated_body_errors_as_clip() {
let package = parse_str(
Path::new("program.clipasm"),
"clipasm 1\nclip {\n image(\"card.png\", 1s)\n audio(\"sound.wav\")\n}\n",
)
.expect("native clip");
let error = compiler::compile(&package).expect_err("mixed clip body");
assert!(error.message.contains("`clip`"), "{}", error.message);
assert!(!error.message.contains("`concat`"), "{}", error.message);
}
#[test]
fn clip_treats_an_omitted_body_as_empty_and_rejects_arguments() {
let mut empty_errors = Vec::new();
for invocation in ["clip", "clip()", "clip {}", "clip() {}"] {
let package = parse_str(
Path::new("program.clipasm"),
&format!("clipasm 1\n{invocation}\n"),
)
.expect("empty clip lowers");
let error = compiler::compile(&package).expect_err("empty clip cannot concatenate");
empty_errors.push((error.code, error.message));
}
assert!(empty_errors.windows(2).all(|pair| pair[0] == pair[1]));
let argument = parse_str(
Path::new("program.clipasm"),
"clipasm 1\nclip(1) {\n image(\"card.png\", 1s)\n}\n",
)
.expect_err("clip argument");
assert_eq!(argument.code, "E_UNEXPECTED_SUGAR_ARGUMENT");
}
}