diff --git a/doc/Expr.adoc b/doc/Expr.adoc index 462f5cb..d5700bb 100644 --- a/doc/Expr.adoc +++ b/doc/Expr.adoc @@ -58,7 +58,7 @@ The expression context is analogous to the stack-frame of other programming lang Function contexts are created by cloning the calling context. More details on this topic are given later in this document. -_Expr_ creates and keeps a inner _global context_ where it stores imported functions, either from builtin or plugin modules. To perform calculations, the calling program must provide its own context; this is the _main context_. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The created context can be called _function context_. +_Expr_ creates and keeps a inner _global context_ where it stores imported functions, either from builtin or plugin modules. To perform calculations, the user program must provide its own context; this is the _main context_. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The created context can be called _function context_. Imported functions are registerd in the _global context_. When an expression first calls an imported function, that function is linked to the current context; this can be the _main context_ or a _function context_. @@ -913,8 +913,33 @@ _param-name_ = _identifier_ [green]`15` -Functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the reference operator [blue]`@` it is possibile to export local definition to the calling context. +=== Function context +Functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the _clone_ modifier [blue]`@` it is possibile to export local definition to the calling context. The clone modifier must be used as prefix to variable names and it is part of the name. E.g. [blue]`@x` is not the same as [blue]`x`; they are two different and un related variables. +Clone variables are normal local variables. The only diffence will appear when the defining function terminate, just before the destruction of its local context. At that point, all local clone variables are cloned in the calling context with the same names but the [blue]`@` symbol. + +.Example +`>>>` [blue]`f = func() { @x = 3; x = 5 }` [gray]_// f() declares two *different* local variables: ``@x`` and ``x``_ + +[green]`f():any{}` + +`>>>` [blue]`f()` [gray]_// The multi-expression (two) in f() is calculated and the last result is returned_ + +[green]`5` + +`>>>` [blue]`x` [gray]_// The `x` variable was not defined in the main context before the f() invocation. It appears in the main context by cloning the `@x` variable, local to f() after its termnation._ + +[green]`3` + +NOTE: The clone modifier [blue]`@` does not make a variable a reference variable, as the ampersand character `&` does in languages such as C and C++. + +[IMPORTANT] +==== +The clone modifier can also be used to declare the formal parameters of functions, because they are local variables too. + +.Example +`>>>` [blue]`g = func(@p) {2+@p}` +g(@p):any{}` +`>>>` [blue]`g(9)` +11` +`>>>` [blue]`p +9 +==== == Iterators #TODO: function calls operations# diff --git a/doc/Expr.html b/doc/Expr.html index 331be86..9df2ac6 100644 --- a/doc/Expr.html +++ b/doc/Expr.html @@ -576,6 +576,7 @@ pre.rouge .ss {
  • 6.1. Expr function definition
  • 6.2. Golang function definition
  • 6.3. Function calls
  • +
  • 6.4. Function context
  • 7. Iterators
  • @@ -657,7 +658,7 @@ pre.rouge .ss {

    Function contexts are created by cloning the calling context. More details on this topic are given later in this document.

    -

    Expr creates and keeps a inner global context where it stores imported functions, either from builtin or plugin modules. To perform calculations, the calling program must provide its own context; this is the main context. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The created context can be called function context.

    +

    Expr creates and keeps a inner global context where it stores imported functions, either from builtin or plugin modules. To perform calculations, the user program must provide its own context; this is the main context. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The created context can be called function context.

    Imported functions are registerd in the global context. When an expression first calls an imported function, that function is linked to the current context; this can be the main context or a function context.

    @@ -2338,8 +2339,58 @@ These operators have a high priority, in particular higher than the operator >>> triple(5)
    15

    + +
    +

    6.4. Function context

    -

    Functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the reference operator @ it is possibile to export local definition to the calling context.

    +

    Functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the clone modifier @ it is possibile to export local definition to the calling context. The clone modifier must be used as prefix to variable names and it is part of the name. E.g. @x is not the same as x; they are two different and un related variables.

    +
    +
    +

    Clone variables are normal local variables. The only diffence will appear when the defining function terminate, just before the destruction of its local context. At that point, all local clone variables are cloned in the calling context with the same names but the @ symbol.

    +
    +
    +
    Example
    +

    >>> f = func() { @x = 3; x = 5 } // f() declares two different local variables: @x and x
    +f():any{}
    +>>> f() // The multi-expression (two) in f() is calculated and the last result is returned
    +5
    +>>> x // The x variable was not defined in the main context before the f() invocation. It appears in the main context by cloning the @x variable, local to f() after its termnation.
    +3

    +
    +
    + + + + + +
    + + +The clone modifier @ does not make a variable a reference variable, as the ampersand character & does in languages such as C and C++. +
    +
    +
    + + + + + +
    + + +
    +

    The clone modifier can also be used to declare the formal parameters of functions, because they are local variables too.

    +
    +
    +
    Example
    +

    >>> g = func(@p) {2+@p} +g(@p):any{}` +>>> g(9) +11` +>>> [blue]`p +9

    +
    +
    @@ -2381,7 +2432,7 @@ These operators have a high priority, in particular higher than the operator