mirth-lang / mirth

Compiler for the Mirth programming language.
BSD Zero Clause License
447 stars 14 forks source link

Allow inout arguments in externals via labels. #293

Closed typeswitch-dev closed 6 months ago

typeswitch-dev commented 6 months ago

This PR changes external slightly so that any labelled outputs get treated as "inout" arguments. They are pushed back on the stack after the external call. This makes it possible to call functions that follow the extremely common pattern (in C, C++, Rust) of data that is passed by reference and modified in place.

E.g.

data( +Res, +Res -> CPtr(CInt) )
external(
    foo [ +x:+Res -- +x:+Res CInt ]
    "int foo(int* x) { return *x++; }" 
)

Note how foo is declared to have two outputs -- a labelled resource output +x:+Res and an unlabelled value output CInt -- but the corresponding C function only has the int output. That's because the labelled resource output +x is taken from the inputs and pushed back on the stack.

If an output label is not present in the input, the compiler raises an error.