polserver/testsuite/escript/func/var-args.src
Kevin Eady 93645f0770
Add variable arguments to user functions (#679)
* Update grammar

* Update parser and AST generator

* Update semantic analysis

* Executor implementation
- Move Executor stack modification logic to BFunctionRef
- Handle user function calls
- Handle expression function calls

* Tests

* Update existing listfile snapshots
- Adds variadic boolean to instruction stack
- Changes instruction counts and offsets

* Fix test issue

* Better implementation of calling user functions

* Add var-args-captures test

* Refactor continuation handling + handle variadic args
- Move stack modifications from make/withContinuation into the executor's handling of the continuation
- Only trim args in continuation call if function is not variadic

* Add var-args-continuation test (via array.filter)

* update escriptguide + formatting, docs

* Fix CI annotations
- '=': conversion from 'size_t' to 'unsigned int', possible loss of data
- declaration of 'continuation' hides previous local declaration
2024-08-04 17:13:30 +02:00

116 lines
3 KiB
Text

function sum( args ... )
return $"sum of {args}";
endfunction
function sumWithArg( arg0, args ... )
return $"sum of {arg0} + {args}";
endfunction
function sumWithArgs( arg0, arg1, args ... )
return $"sum of {arg0} + {arg1} + {args}";
endfunction
// -----
// Tests via static calls `func(...)`
// -----
// Pass no arg
print( sum() );
print( sumWithArg( 0 ) );
print( sumWithArgs( 0, 1 ) );
print( "-" );
// Pass one arg
print( sum( 0 ) );
print( sumWithArg( 0, 1 ) );
print( sumWithArgs( 0, 1, 2 ) );
print( "-" );
// Pass multiple args
print( sum( 0, 1, 2 ) );
print( sumWithArg( 0, 1, 2, 3 ) );
print( sumWithArgs( 0, 1, 2, 3, 4 ) );
print( "-" );
// -----
// Tests via expression call `@func.call(...)`
// -----
// Pass no arg
print( @sum.call() );
print( @sumWithArg.call( 0 ) );
print( @sumWithArgs.call( 0, 1 ) );
print( "-" );
// Pass one arg
print( @sum.call( 0 ) );
print( @sumWithArg.call( 0, 1 ) );
print( @sumWithArgs.call( 0, 1, 2 ) );
print( "-" );
// Pass multiple args
print( @sum.call( 0, 1, 2 ) );
print( @sumWithArg.call( 0, 1, 2, 3 ) );
print( @sumWithArgs.call( 0, 1, 2, 3, 4 ) );
print( "-" );
// -----
// Tests via function expression call `(@(<args>){})(...)`
// -----
var sum_functor := @( args ... ) {
return $"sum of {args}";
};
var sumWithArg_functor := @( arg0, args ... ) {
return $"sum of {arg0} + {args}";
};
var sumWithArgs_functor := @( arg0, arg1, args ... ) {
return $"sum of {arg0} + {arg1} + {args}";
};
// Pass no arg
print( sum_functor.call() );
print( sumWithArg_functor.call( 0 ) );
print( sumWithArgs_functor.call( 0, 1 ) );
print( "-" );
// Pass one arg
print( sum_functor.call( 0 ) );
print( sumWithArg_functor.call( 0, 1 ) );
print( sumWithArgs_functor.call( 0, 1, 2 ) );
print( "-" );
// Pass multiple args
print( sum_functor.call( 0, 1, 2 ) );
print( sumWithArg_functor.call( 0, 1, 2, 3 ) );
print( sumWithArgs_functor.call( 0, 1, 2, 3, 4 ) );
print( "-" );
// -----
// Simple sanity checks cross `func()` and `@func.call()`
// -----
print( sum() == @sum.call() );
print( sumWithArg( 0 ) == @sumWithArg.call( 0 ) );
print( sumWithArgs( 0, 1 ) == @sumWithArgs.call( 0, 1 ) );
print( sum( 0 ) == @sum.call( 0 ) );
print( sumWithArg( 0, 1 ) == @sumWithArg.call( 0, 1 ) );
print( sumWithArgs( 0, 1, 2 ) == @sumWithArgs.call( 0, 1, 2 ) );
print( sum( 0, 1, 2 ) == @sum.call( 0, 1, 2 ) );
print( sumWithArg( 0, 1, 2, 3 ) == @sumWithArg.call( 0, 1, 2, 3 ) );
print( sumWithArgs( 0, 1, 2, 3, 4 ) == @sumWithArgs.call( 0, 1, 2, 3, 4 ) );
print( "-" );
// -----
// Simple sanity checks cross `func()` and `(@(<args>){})(...)`
// -----
print( sum() == sum_functor() );
print( sumWithArg( 0 ) == sumWithArg_functor( 0 ) );
print( sumWithArgs( 0, 1 ) == sumWithArgs_functor( 0, 1 ) );
print( sum( 0 ) == sum_functor( 0 ) );
print( sumWithArg( 0, 1 ) == sumWithArg_functor( 0, 1 ) );
print( sumWithArgs( 0, 1, 2 ) == sumWithArgs_functor( 0, 1, 2 ) );
print( sum( 0, 1, 2 ) == sum_functor( 0, 1, 2 ) );
print( sumWithArg( 0, 1, 2, 3 ) == sumWithArg_functor( 0, 1, 2, 3 ) );
print( sumWithArgs( 0, 1, 2, 3, 4 ) == sumWithArgs_functor( 0, 1, 2, 3, 4 ) );