Repository navigation
Description
Goal
- Concise syntax for SCM functions
- Make SCM functions easier to use by hiding some low-level details
Considerations for function syntax
Pascal-style:
function a12(name: string): string
- easier to find using a "function" keyword
- param declaration is consistent with the "var" block syntax
#### C-style:
string a12(string name)
* consistent with inline var declaration
Pascal-style is more consistent with the rest of the language.
Rules
-
Function must be declared before first usageFunction are available anywhere within current scope. Functions defined inside other functions are available anywhere in that function body -
Function body starts with the "function" keyword followed by the function signature.
-
Function name
prefixed with @is a valid label -
The signature includes input parameters (if any) and their types, comma-separated.
-
Input parameters, when present, must be enclosed in "()". For zero-input functions "()" is optional
-
Input parameters are followed by a return type if the function returns anything
-
Multiple return types are comma-separated,
and parenthesized. -
Return type(s) can be prefixed with the
optionalkeyword. Function with an optional result may return nothing. -
Function body ends with the
endkeyword. -
returnkeyword immediately exits the function and returns control to the calling code. See Return Semantics- functions with optional return type(s) can use blank
returnto bail out immediately while returning nothing - There is special
logicalreturn type. This result can only be validated in IF..THEN and can not be stored in a variable. Logical function returns true or false. - functions that define return type(s) must return same number of values. logical function must return a condition flag. Optional functions may return nothing.
- When you return with some value(s), it is always a true condition for IF..THEN. Empty return is always a false condition. Value returned from a logical function defines the condition.
- functions with optional return type(s) can use blank
-
Function can return one or more values using the following syntax:return <condition flag> <value1> <value2> .... -
returnkeyword in functions should not be confused withreturnkeyword in gosubs. Function's return is always followed by some values, ortrue, orfalseusing CLEO5's CLEO_RETURN_WITH -
endkeywords serves as an implicitreturnwith the default values matching the function signatureusing RETURN (CLEO5 is required)using CLEO_RETURN -
return falseis a special case that can be used in any function. It sets the condition result to false and exits the function ignoring all output variables -
return trueis a special case that can be used in function with no return type. It sets the condition result to true.
Examples
Declaration
- Function name serves as a label. it can not duplicate an existing label.
- Functions can be declared upfront to allow calls be located before the function body (forward declarations, see below)
- "end" represents
CLEO_RETURN_FAILRETURNCLEO_RETURN 0if there no an explicitcleo_return_*on the preceding line
{$CLEO}
function a0
end // implicit CLEO_RETURN 0
function a1(x: int)
end // implicit CLEO_RETURN 0
function a2(x: int, y: int)
end // implicit CLEO_RETURN 0
function a3(): int
end // implicit CLEO_RETURN 0
function a4(): int, int
end // implicit CLEO_RETURN 0
function a5(): int
return 42 // explicit cleo_return_with true 42
end // implicit CLEO_RETURN 0
function a6(): int, int
return 42 84 // explicit cleo_return_with true 42 84
end // implicit CLEO_RETURN 0
function a7(): int
if 0@>0
then
return 42 // explicit cleo_return_with true 42
end
end // implicit CLEO_RETURN 0
function a8()
if 0@>0
then
return // explicit 0051: return
end
end // implicit CLEO_RETURN 0
function a9(): logical
return false // explicit cleo_return_with false
end // implicit CLEO_RETURN 0
function a10(): float
if 0@>0
then
return 42.0 // explicit cleo_return_with true 42.0
end
end // implicit CLEO_RETURN 0
function a11(): string
if 0@>0
then
return 'test' // explicit cleo_return_with true 'test'
end
end // implicit CLEO_RETURN 0
function a12(name: string): string
return name // explicit cleo_return_with true 0@ 1@
end // implicit CLEO_RETURN 0
function a14(): int
if 0@>0
then return 1 // cleo_return_with true 1
else return 0 // cleo_return_with true 0
end
end // implicit CLEO_RETURN 0
function a15(): int, float
if 0@>0
then return 1 2.0 // cleo_return_with true 1 2.0
else return 0 0 // cleo_return_with true 0 0
end
end // implicit CLEO_RETURN 0
Examples of logical/optional return types
function no_ret_ok
return
end
function no_ret_error
// return 1 // error, should not return a value
end
function ret_logical_ok: logical
return 1
return 0
return 0@ == 0
end
function ret_logical_error: logical
// return // error, should return 1 value
// return 1 2 // error, should return 1 value
end
function ret_1_ok: int
return 0
end
function ret_1_error: int
// return // error, should return 1 value
// return 1 2 // error, should return 1 value
end
function ret_2_ok: int, int
return 0 0
end
function ret_2_error: int, int
// return // error, should return 2 integer values
// return 1 // error, should return 2 integer values
end
function opt_1_ok: optional int
return
return 0
end
function opt_1_error: optional int
// return 1 2 // error, should return 1 integer value
end
function opt_2_ok: optional int, int
return
return 1 2
end
function opt_2_error: optional int, int
// return 1 // error, should return 2 integer values
// return 1 2 3 // error, should return 2 integer values
end
if and
ret_logical_ok()
0@ = ret_1_ok()
0@, 1@ = ret_2_ok()
0@ = opt_1_ok()
0@, 1@ = opt_2_ok()
then
// ok
else
// error
end
Calling functions
a0() // ambiguous: gosub or call?, need lookahead
a1(5) // cleo_call a1 1 5
a2(5,6) // cleo_call a2 2 5 6
// single result
0@ = a3() // cleo_call a3 0 0@
// multiple results
0@, 1@ = a4() // cleo_call a4 0 0@ 1@
// use functions in initialization position
int x = a3() // cleo_call a3 0 0@
string name = a12("test") // cleo_call a12 1 "test" 0@ 1@
// logical functions
if a9()
then
...
end
Grammar
function := ["export" whitespace] "function" whitespace identifier "(" params ")" ( return_type1 | return_type2)
params := [ identifier ":" type ] [ "," params ]
return_type1 := [ "(" ] type [ ")" ]
return_type2 := "(" type "," types ")"
types := type [ "," types ]
var1 := [ "(" ] var [ ")" ]
var2 := "(" var "," vars ")"
vars := var [ "," vars ]
function_call := [ ( var1 | var2 ) "=" ] identifier "(" args ")"
args := ( var | const ) [ "," args ]
Activity
x87 commentedon Sep 14, 2023
TODO: make it compatible with #45
MiranDMC commentedon Sep 14, 2023
What about calling function reference stored in variable (like passing callback function via parameter, or functions stored in array)?
Currently in 0AB1 function label can be variable.
MiranDMC commentedon Sep 14, 2023
What about functions that can take/return multiple types of variables?
Like numToText accepting first param int or float depending on second boolean param.
Same situation with return type, where depending on logic return type might be int or float.
MiranDMC commentedon Sep 14, 2023
How condition result will be handled? Your earlier idea with
returnandnot returnseems nice. Possibility to use variable as condition result should also be a thing. For situations where function always have to perform some clean-up, then exit with condition result.x87 commentedon Sep 14, 2023
union types are out of scope for this RFC. There should be a separate function for each combination of types
x87 commentedon Sep 14, 2023
any idea for complete syntax?
MiranDMC commentedon Sep 14, 2023
Though case. For sure condition result can not be on the right side of the return keyword. It leads to idea having it on the left:
return 5@ - condition result true
not return 5@ - condition result false
5@ return 5@ - condition result based on value of 5@
x87 commentedon Sep 15, 2023
this syntax is complicated and confusing. I think the last case should be handled outside of the function, or the boolean value should be returned as a regular value.
A function should have the condition result set to true at the start of the function. It would allow to use empty functions or functions without ifs in conditions.
If the function wants to explicitly change the condition result to false, we can use
return false. Note thatreturn 0would be considered as a 'true' result, soreturn falseandreturn 0are not the same.Then you are allowed to use regular tricks with conditional opcodes to alter the condition result:
so to summarize:
If you return any value, it is always a success. Condition result is true (unless altered by the last conditional opcode)
If you
return falseit is always a failure. No output variables are modified.We can use
8AB2forreturn falsecase. It collects all returned values, then skips the output variables in the caller, then sets the condition result to false.MiranDMC commentedon Sep 15, 2023
Um I really don't like fact that
return falsewould behave differently than other cases, and what is situation where function is supposed to return one bool param? Syntax is getting ambiguous.Feature of leaving the function sounds nice, but should not be performed with
returnkeyword. This seems be ideal case for 'break'.Inexplicit carrying on condition state of last executed opcode also seems not right. Instead of easy functionality we get hidden convoluted logic without clear rules enforcement. Function returns true by default, but seemingly not related change will make behave it differently.
Generally in my practice I always use variable to carry function ok\failed state, as it can not be trusted to set in in middle of the function and hope it is still valid while exiting. In my opinion end user should not be even aware that condition result exists in the background all the time.
Maybe instead of return we should only allow return_true and return_false as function ending keywords?
x87 commentedon Sep 15, 2023
If function returns one bool param, it suits the proposed logic.
Boolresultnote that
boolreturn type does not require a variable to store the resultIF and SET
MiranDMC commentedon Sep 15, 2023
Currently there is no such thing as bool type. Will it be reserved for condition result only? It might be good idea do declare if function sets condition state or else it will be true by default. Then condition result is just one of the args in return call statement.
Hm, doesn't it just boils down to treating first return param as condition result? Where value different than 0 is considered as true?
That seems reasonable. Cuts extra complications.
`func test() : (int, string, float)
(...)
return(true, "result", 3.0)
end
test() // allowed, discard return
if test() // allowed, check first return <> 0
if (0@, 1@, 2@) = test() // allowed, first as condition result
if (0@, 1@) = test() // error, not all return values used
if (_, 0@, _) = test() // allowed, first returned value as condition (as usual). Store just second argument`
Idea to consider is to allow '_' in return calls like
return(false, _, _, _, -1)I see one problem with
// 0@ is not setexample:0@ = true (...) 0@ = isThingEnabled()x87 commentedon Sep 15, 2023
Not sure we need to overengineer it. Many functions are just calculations and they don't need to work with condition at all.
As you mentioned, there is no bool type which is correct. It means you can't store a result of a bool function into a variable. It only works as a condition.
MiranDMC commentedon Sep 15, 2023
Then it simply just returns single argument (for example float). If used in condition statement then in typical fashion return[0] is tested for <> 0.
Introducing Boolean type will make everyone question why it is not possible to use it for var declarations or as input argument type.
Some corner cases:
func test() : (int, int) // 2 return values
func test() : (bool, int) // 1 return value now? Bit confusing
func test() : (int, bool) // hm, what now?
func test() : (bool, int, bool) // ???
Over engineering is when you have multiple rules to describe simple thing.
I propose single rule: condition result of function call is
return param[0] <> 0, true if no returns61 remaining items
x87 commentedon Mar 21, 2024
Yes, all or nothing. I crossed out the other part, that suggested that only some params can be returned.
MiranDMC commentedon Mar 21, 2024
so,
optionalis required to exit function with nakedreturnor via function'sendHow then exit from
logicalfunction returning some args and set the result to false?x87 commentedon Mar 21, 2024
logical doesn't return any values, only sets the condition flag.
returning a value from a non-optional function is always a true condition
naked return is always a false condition
returning a value AND setting result to false is not supported with
returnsyntax. use 2002 (cleo_return_with) in this case.MiranDMC commentedon Mar 21, 2024
I thought it will be able to mix logical and args. Ok then.
x87 commentedon Mar 21, 2024
Proposal updated
MiranDMC commentedon Apr 13, 2024
How about allowing using logical and return values in function declaration, like:
function foo() : logical, int, intThen each
returnin the function would be translated tocleo_return_with. Using expression in return line would not be possible in that scenario.Logical keyword would be allowed only as first argument (but technically it wouldn't be problem to make it any).
x87 commentedon Apr 14, 2024
future extension: variable arguments
MiranDMC commentedon Apr 14, 2024
Nice, but how to know provided args count inside the function?
x87 commentedon Apr 14, 2024
up to you.
you can pass arg count as a separate argument, or check for a zero value
MiranDMC commentedon Apr 14, 2024
Hm information like this can possibly be accessed in future via VirtualVariables feature.
x87 commentedon Apr 16, 2024
future extension: any type
x87 commentedon May 3, 2024
RFC Update:
CLEO_RETURN 0instead ofRETURNfor function'sendto have better support where CLEO5 is not availableMiranDMC commentedon May 4, 2024
So for logical functions without return the last set condition result will be passed through? Seems like feature.
x87 commentedon Aug 7, 2024
released in 4.0.0