Skip to content

Function Syntax RFC #263

Closed
Closed

Description

@x87

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 usage Function 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 optional keyword. Function with an optional result may return nothing.

  • Function body ends with the end keyword.

  • return keyword immediately exits the function and returns control to the calling code. See Return Semantics

    • functions with optional return type(s) can use blank return to bail out immediately while returning nothing
    • There is special logical return 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.
  • Function can return one or more values using the following syntax: return <condition flag> <value1> <value2> ....

  • return keyword in functions should not be confused with return keyword in gosubs. Function's return is always followed by some values, or true, or false using CLEO5's CLEO_RETURN_WITH

  • end keywords serves as an implicit return with the default values matching the function signature using RETURN (CLEO5 is required) using CLEO_RETURN

  • return false is 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 true is 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_FAIL RETURN CLEO_RETURN 0 if there no an explicit cleo_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

  1. added
    discussion
    on Sep 14, 2023
  2. x87 commented on Sep 14, 2023

    @x87
    CollaboratorAuthor

    TODO: make it compatible with #45

  3. MiranDMC commented on Sep 14, 2023

    @MiranDMC

    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.

  4. MiranDMC commented on Sep 14, 2023

    @MiranDMC

    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.

  5. MiranDMC commented on Sep 14, 2023

    @MiranDMC

    How condition result will be handled? Your earlier idea with return and not return seems 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.

  6. x87 commented on Sep 14, 2023

    @x87
    CollaboratorAuthor

    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.

    union types are out of scope for this RFC. There should be a separate function for each combination of types

  7. x87 commented on Sep 14, 2023

    @x87
    CollaboratorAuthor

    How condition result will be handled? Your earlier idea with return and not return seems 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.

    any idea for complete syntax?

  8. MiranDMC commented on Sep 14, 2023

    @MiranDMC

    How condition result will be handled? Your earlier idea with return and not return seems 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.

    any idea for complete syntax?

    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@

  9. x87 commented on Sep 15, 2023

    @x87
    CollaboratorAuthor

    How condition result will be handled? Your earlier idea with return and not return seems 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.

    any idea for complete syntax?

    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@

    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 fun1()
    then
    //  <---  fun1() is true
    end
    
    function fun1() <--- condition result set to true by cleo_call
    
    end <--- return as is
    

    If the function wants to explicitly change the condition result to false, we can use return false. Note that return 0 would be considered as a 'true' result, so return false and return 0 are not the same.

    Then you are allowed to use regular tricks with conditional opcodes to alter the condition result:

    function fun1()
    
      is_australian_game // changes condition_result to false
      is_pc_game // changes condition result to true
    
    end
    

    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 false it is always a failure. No output variables are modified.

    We can use 8AB2 for return false case. It collects all returned values, then skips the output variables in the caller, then sets the condition result to false.

    0@=100 
    1@=200 
    2@=300
    if  (0@,1@,2@) = fun() // cleo_call 0 0@ 1@ 2@
    then
      print 0@ 1@ 2@ // here it prints 10 20 30
    else
      print 0@ 1@ 2@ // here it prints 100 200 300
    end
    
    function fun(): (int, int, int)
      if not <cond>
      then
        return false // 8AB2: not cleo_return 3 0 0 0
      end
      
      return (10,20,30) // 0AB2: cleo_return 3 10 20 30
    end
    
  10. MiranDMC commented on Sep 15, 2023

    @MiranDMC

    Um I really don't like fact that return false would 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 return keyword. 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?

  11. x87 commented on Sep 15, 2023

    @x87
    CollaboratorAuthor

    If function returns one bool param, it suits the proposed logic.

    Bool result

    if 
      test() // cleo_call @test 0
    then
      // true
    else
      // false
    end
    
    function test(): bool
      if x 
      then
        return true // cleo_return 0
      else
        return false // not cleo_return 0
      end
    end
    

    note that bool return type does not require a variable to store the result

    IF and SET

    if 
      0@ = test() // cleo_call @test 0 0@
    then
      // 0@ is 1
    else
      // 0@ is not set
    end
    
    function test(): int
      if x
      then
        return 1 // cleo_return 1 1
      else
        return false // not cleo_return 1 0  /// last zero is just a placeholder to match the function signature. CLEO does NOT set result to 0
      end
    end
    
    
  12. MiranDMC commented on Sep 15, 2023

    @MiranDMC

    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 set example:
    0@ = true (...) 0@ = isThingEnabled()

  13. x87 commented on Sep 15, 2023

    @x87
    CollaboratorAuthor

    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.

    0@ = true
    if isThingEnabled()
    then
     0@ = true // or 1
    else
     0@ = false // or 0
    end
    
  14. MiranDMC commented on Sep 15, 2023

    @MiranDMC

    Many functions are just calculations and they don't need to work with condition at all.

    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 returns

  15. 61 remaining items

  16. x87 commented on Mar 21, 2024

    @x87
    CollaboratorAuthor

    Yes, all or nothing. I crossed out the other part, that suggested that only some params can be returned.

  17. MiranDMC commented on Mar 21, 2024

    @MiranDMC

    so, optional is required to exit function with naked return or via function's end
    How then exit from logical function returning some args and set the result to false?

  18. x87 commented on Mar 21, 2024

    @x87
    CollaboratorAuthor

    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 return syntax. use 2002 (cleo_return_with) in this case.

  19. MiranDMC commented on Mar 21, 2024

    @MiranDMC

    I thought it will be able to mix logical and args. Ok then.

  20. x87 commented on Mar 21, 2024

    @x87
    CollaboratorAuthor

    Proposal updated

  21. MiranDMC commented on Apr 13, 2024

    @MiranDMC

    How about allowing using logical and return values in function declaration, like:
    function foo() : logical, int, int
    Then each return in the function would be translated to cleo_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).

  22. x87 commented on Apr 14, 2024

    @x87
    CollaboratorAuthor

    future extension: variable arguments

    function foo(a: int, b: int, ...args: int[])
    // args is defined here as an int array with size of 30 (because other 2 available slots occupied by a and b)
    end
    
    
    foo(1, 2) // args can be empty
    foo(5, 6, 1, 2, 3)
    
  23. MiranDMC commented on Apr 14, 2024

    @MiranDMC

    Nice, but how to know provided args count inside the function?

  24. x87 commented on Apr 14, 2024

    @x87
    CollaboratorAuthor

    Nice, but how to know provided args count inside the function?

    up to you.
    you can pass arg count as a separate argument, or check for a zero value

  25. MiranDMC commented on Apr 14, 2024

    @MiranDMC

    Hm information like this can possibly be accessed in future via VirtualVariables feature.

  26. x87 commented on Apr 16, 2024

    @x87
    CollaboratorAuthor

    future extension: any type

    function foo(a: any)
     // a has no type here
     string_format buf "%d" a
     string_format buf "%s" a
     string_format buf "%f" a
    end
    
    foo(5)
    foo("test")
    foo(1.0)
    
  27. x87 commented on May 3, 2024

    @x87
    CollaboratorAuthor

    RFC Update:

    • use CLEO_RETURN 0 instead of RETURN for function's end to have better support where CLEO5 is not available
  28. MiranDMC commented on May 4, 2024

    @MiranDMC

    So for logical functions without return the last set condition result will be passed through? Seems like feature.

  29. x87 commented on Aug 7, 2024

    @x87
    CollaboratorAuthor

    released in 4.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionscope:language

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions