polserver/testsuite/escript/funcexpr/complex-nested-struct.src

69 lines
2.3 KiB
Text
Raw Permalink Normal View History

Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
// Has `unused` parameters make sure ordering of used parameters is correct.
//
// The most-inner function will create a local (circumstantially, inside an
// `if`-block) to test returning locals.
var level1 := @( a, b, c ) {
return struct{ fnA1 := @( d, e, f ) {
return struct{ fnB1 := @( g, h, i ) {
return struct{ fnC1 := @( j, unused k, l ) {
var res := a + b + c + d + e + f + g + h + i + j;
if ( res > 0 )
var out_local := res * l;
return out_local;
endif
return res;
}, fnC2 := @( j, unused k, l ) {
var res := a * b * c * d * e * f * g * h * i * j;
if ( res > 0 )
var out_local := res / l;
return out_local;
endif
return res;
}, fnC3 := @( j, unused k, l ) {
var res := a - b - c - d - e - f - g - h - i - j;
if ( res > 0 )
var out_local := res + l;
return out_local;
endif
return res;
} };
}, fnB2 := @( g, h, i ) {
return struct{ fnC1 := @( j, unused k, l ) {
var res := a * b + c + d + e + f + g + h + i + j;
if ( res > 0 )
var out_local := res * l;
return out_local;
endif
return res;
}, fnC2 := @( j, unused k, l ) {
var res := a + b * c * d * e * f * g * h * i * j;
if ( res > 0 )
var out_local := res / l;
return out_local;
endif
return res;
}, fnC3 := @( j, unused k, l ) {
var res := a - b * c - d - e - f - g - h - i - j;
if ( res > 0 )
var out_local := res + l;
return out_local;
endif
return res;
} };
} };
} };
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
};
// (a + b + c + d + e + f + g + h + i + j) * l since inner expression is >0
// (-7 + -6 + -1 + 4 + -4 + 17 + 9 + 3 + 15 + -10) * -7 = -140
print( level1( -7, -6, -1 ).fnA1.call( 4, -4, 17 ).fnB1.call( 9, 3, 15 ).fnC1.call( -10, -7, -7 ) );
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
// a * b + c + d + e + f + g + h + i + j
// -3 * -1 + 2 + -7 + -4 + -5 + -2 + 7 + 1 + -1 = -6
print( level1( -3, -1, 2 ).fnA1.call( -7, -4, -5 ).fnB2.call( -2, 7, 1 ).fnC1.call( -1, 1, 5 ) );
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
// a - b * c - d - e - f - g - h - i - j
// 10 - 2 * 11 - 10 - 2 - 4 - -8 - -1 - -5 - 10 = -24
print( level1( 10, 2, 11 ).fnA1.call( 10, 2, 4 ).fnB2.call( -8, -1, -5 ).fnC3.call( 10, -3, 1 ) );