Skip to content

root_scopes

BlidDev edited this page Jan 9, 2024 · 4 revisions

Overview:

Functions are an inseparable part of any language and Niv is no diffrent. Niv's alternative to functions is are called root scopes.

// Every program must have a `MAIN` root scope
#MAIN |
   // Root scopes can be called by the `run` command
   run hello_there 
    
    // Or either be just called like a built in command
    set sum (add 1 2)

#MAIN

// Each root scope is defined by a `#` followed by a name
// and a `|` character that marks the start of the arugument list
#hello_there |
    print "Hi! :D\n"
#hello_there
// ^^^ A corresponding lable is required in order to mark the end of the root scope 

#add | num1 num2
    // By default, a root scope returns `VOID` 
    // but it can be specified otherwise using `return`
    return (cal $num1 + $num2)
#add

Arguments

Root scopes' aruguments, as any variable in Niv, can be of any type. When passing values into aruguments, the program pushes new variables given the arugument names with the corresponding values, and releases them after the root scope has finished running. So, calling the add root scope from the example would look like this behind the scenes:

#MAIN |
    set sum (add 1 2)
#MAIN

#add | num1 num2
    set num1 1
    set num2 2
    
    return (cal $num1 + $num2)

    release num1
    release num2
#add

Important: because of how aruguments are handled and how Niv only has one variable stack for the entirety of the program, root scopes can use/set/release variables outside of their scope, meaning calling a variable the same name as one of the aruguments will cause it to be released after calling the root scope. This will (hopefully) change in the future.


run

usage example:

// template
run name Args...

// specific use
run add 1 2

Args:

  • name: STR
  • Args: List of arguments of any value

Desc:

Runs the root scope named name and passes Args as the root scope's arguments

Return value: The return value of the root scope.

For more complex examples check root_scopes.nst


return

usage example:

// template
return val

// specific use
set s "hello there"
return (llen s)

Args:

  • name: STR
  • val : Any value

Desc:

Exits the current root scope and sets its return value to val.

Return value: RETURN.

For more complex examples check root_scopes.nst

Clone this wiki locally