G:/ScriptBasic/source/commands/function.c File Reference

#include <stdlib.h>
#include <stdio.h>
#include "../command.h"

Go to the source code of this file.

Data Structures

struct  _GosubStack

Defines

#define NewFunLocalVariables   CallerLocalVariables
#define SWAP_LOCAL_VARIABLES
#define GosubStackRoot   ((pGosubStack)PARAMPTR(CMD_GOSUB))

Typedefs

typedef _GosubStack GosubStack
typedef _GosubStackpGosubStack

Functions

END void COMMAND_FUNCTIONARG (pExecuteObject pEo)
 if (GosubStackRoot=pGSS==NULL)
 if (ItemResult &&TYPE(ItemResult)==VTYPE_ARRAY) ERROR(COMMAND_ERROR_NOARRAY)
 if (pEo->pFunctionResult) memory_ReleaseVariable(pEo->pMo
 if (pEo->pFunctionResult=ItemResult)
 if (CommandOpCode!=CMD_FUNCTION &&CommandOpCode!=CMD_FUNCTIONARG &&CommandOpCode!=CMD_SUB &&CommandOpCode!=CMD_SUBARG)
 if (pEo->pHookers->HOOK_ExecCall &&(iErrorCode=pEo->pHookers->HOOK_ExecCall(pEo)))

Variables

pGosubStack pGSS
 pGSS = ALLOC(sizeof(GosubStack))
pEo fStop = fStopRETURN
END END VARIABLE ItemResult
 ItemResult = _EVALUATEEXPRESSION_A(PARAMETERNODE)
 ASSERTOKE
 ItemResult = memory_DupMortalize(pEo->pMo,ItemResult,_pThisCommandMortals,&iErrorCode)
pEo pFunctionResult
pEo FunctionArgumentsNode = CDR(nItem)
 SaveFunctionResultPointer = pEo->pFunctionResult
pEo pFunctionResult = NULL
 SaveStepCounter = pEo->lStepCounter
pEo lStepCounter = 0
pEo fWeAreCallingFuction = 1
 SaveErrorGoto = pEo->ErrorGoto
pEo ErrorGoto = 0
 SaveErrorResume = pEo->ErrorResume
pEo ErrorResume = 0
 SavefErrorGoto = pEo->fErrorGoto
pEo fErrorGoto = ONERROR_NOTHING
pEo FunctionArgumentsNode = CDR(PARAMETERNODE)
 SaveFunctionResultPointer = pEo->pFunctionResult
pEo pFunctionResult = NULL
 SaveStepCounter = pEo->lStepCounter
pEo lStepCounter = 0
pEo fWeAreCallingFuction = 1
 SaveErrorGoto = pEo->ErrorGoto
pEo ErrorGoto = 0
 SaveErrorResume = pEo->ErrorResume
pEo ErrorResume = 0
 SavefErrorGoto = pEo->fErrorGoto
pEo fErrorGoto = ONERROR_NOTHING


Define Documentation

#define GosubStackRoot   ((pGosubStack)PARAMPTR(CMD_GOSUB))
 

Definition at line 276 of file function.c.

#define NewFunLocalVariables   CallerLocalVariables
 

Referenced by COMMAND_FUNCTIONARG().

#define SWAP_LOCAL_VARIABLES
 

Value:

SwapLVP = CallerLocalVariables; \
                             CallerLocalVariables = pEo->LocalVariables;\
                             pEo->LocalVariables = SwapLVP;

Referenced by COMMAND_FUNCTIONARG().


Typedef Documentation

typedef struct _GosubStack GosubStack
 

typedef struct _GosubStack * pGosubStack
 


Function Documentation

END void COMMAND_FUNCTIONARG pExecuteObject  pEo  ) 
 

FUNCTION =section misc =title FUNCTION fun()

This command should be used to define a function. A function is a piece of code that can be called by the BASIC program from the main part or from a function or subroutine.

=verbatim FUNCTION fun(a,b,c) ... fun = returnvalue ... END FUNCTION =noverbatim

The end of the function is defined by the line containing the keywords T<END FUNCTION>.

=details

The function declaration can be placed anywhere in the code before or after the function is used. Because the functions just as well as the variables are typeless there is no such thing as function prototype and thus functions can not be declared beforehand.

You can call a function in your BASIC code that is not defined yet. The syntax analyzer seeing the T<function_name()> construct will see that this is a function call and will generate the appropriate code to call the function that is defined later. If the function is not defined until the end of the code then the interpreter will generate error before the code is started.

Functions just as well as subroutines can have argument and local variables. When the function or subroutine is called the actual values are evaluated and placed in the argument variables. It is not an error when calling a function to specify less or more number of actual arguments than the declared arguments.

If the actual arguments are too few the rest of the arguments will become T<undef> before the function starts. If there are too many arguments when calling a function or subroutine the extra arguments are evaluated and then dropped (ignored).

ScriptBasic allows recursive functions thus a function can call itself, but a function or subroutine is not allowed to be declared inside of another subroutine or function.

Functions differ from subroutines in the fact functions return value while subroutines do not. However in ScriptBasic this is not a strict rule. Subroutines declared with T instead of T<FUNCTION> are allowed to return a value and been used just as if it was declared with the keyword T<FUNCTION>.

To return a value from a function the name of the function can be used just like a local variable. The assignment assigning a value to the function will be returned from the function. If there are more than one assignments to the function name then the last assignment executed will prevail. On the other hand you can not use the function name as a local variable and retrieve the last assigned value from the function name. Using the function name inside the function in an expression will result recursive call or will be used as a global or local variable.

If the actual value of an argument is left value then the reference to the actual argument is passed to the function or the subroutine instead of the value. In other cases the value of the expression standing in the position of the argument when calling the function is passed.

Passing a variable by reference means altering the argument variable also alters the variable that was passed to the function.

To force a variable passed by value rather than reference the operator T<ByVal> can be used. When T<ByVal> is used as a numeric operator in an expression acts as an identity operator. It means that the value of the expression is the same as the value standing on the right of the operator T<ByVal>. However this is already an expression and not a variable and as such it can not be passed by reference only the value.

The keyword T<ByVal> can also be used as a command listing all the argument variables after the keyword on a line:

=verbatim function myfunc(a,b,c,d) ByVal a,b,c ... =noverbatim

In this case T<ByVal> acts as a command and replaces the references by copies of the values. After executing this command the argument values can be altered without affecting the variables that stand in the argument's place where the function is called.

Although the command T<ByVal> looks like a declaration of arguments to be passed by value instead of reference; this is not a declaration but it rather a command that has to be executed.

Definition at line 148 of file function.c.

References ASSERTOKE, execute_LeftValueArray(), execute_LeftValueSarray(), i, ItemResult, memory_Immortalize(), memory_NewArray(), memory_NewRef(), memory_ReleaseMortals(), memory_SetRef(), NewFunLocalVariables, nItem, NULL, OPCODE, SETPROGRAMCOUNTER(), and SWAP_LOCAL_VARIABLES.

if pEo->pHookers->HOOK_ExecCall &&  (iErrorCode=pEo->pHookers->HOOK_ExecCall(pEo))  ) 
 

Definition at line 597 of file function.c.

if CommandOpCode!  = CMD_FUNCTION && CommandOpCode != CMD_FUNCTIONARG && CommandOpCode != CMD_SUB && CommandOpCode != CMD_SUBARG  ) 
 

Definition at line 578 of file function.c.

References ERROR().

if pEo->pFunctionResult ItemResult  ) 
 

Definition at line 416 of file function.c.

References OPCODE.

if pEo->  pFunctionResult  ) 
 

if ItemResult &&  TYPE(ItemResult) = =VTYPE_ARRAY  ) 
 

if GosubStackRoot =  pGSS = NULL  ) 
 

Definition at line 303 of file function.c.


Variable Documentation

ASSERTOKE
 

Definition at line 408 of file function.c.

pEo ErrorGoto = 0
 

Definition at line 669 of file function.c.

pEo ErrorGoto = 0
 

Definition at line 592 of file function.c.

pEo ErrorResume = 0
 

Definition at line 671 of file function.c.

pEo ErrorResume = 0
 

Definition at line 594 of file function.c.

pEo fErrorGoto = ONERROR_NOTHING
 

Definition at line 673 of file function.c.

pEo fErrorGoto = ONERROR_NOTHING
 

Definition at line 596 of file function.c.

pEo fStop = fStopRETURN
 

Definition at line 380 of file function.c.

pEo FunctionArgumentsNode = CDR(PARAMETERNODE)
 

Definition at line 662 of file function.c.

pEo FunctionArgumentsNode = CDR(nItem)
 

Definition at line 585 of file function.c.

pEo fWeAreCallingFuction = 1
 

Definition at line 667 of file function.c.

pEo fWeAreCallingFuction = 1
 

Definition at line 590 of file function.c.

ItemResult = memory_DupMortalize(pEo->pMo,ItemResult,_pThisCommandMortals,&iErrorCode)
 

Definition at line 412 of file function.c.

ItemResult = _EVALUATEEXPRESSION_A(PARAMETERNODE)
 

Definition at line 407 of file function.c.

END END VARIABLE ItemResult
 

Definition at line 401 of file function.c.

pEo lStepCounter = 0
 

Definition at line 666 of file function.c.

pEo lStepCounter = 0
 

Definition at line 589 of file function.c.

pEo pFunctionResult = NULL
 

Definition at line 664 of file function.c.

pEo pFunctionResult = NULL
 

Definition at line 587 of file function.c.

pEo pFunctionResult
 

Definition at line 415 of file function.c.

pGSS = ALLOC(sizeof(GosubStack))
 

Definition at line 302 of file function.c.

pGosubStack pGSS
 

GOSUB =section misc =H Gosub commands =title GOSUB label

This is the good old way implementation of the BASIC T<GOSUB> command. The command T<GOSUB> works similar to the command T<GOTO> with the exception that the next return command will drive the interpreter to the line following the line with the T<GOSUB>.

You can only call a code segment that is inside the actual code environment. In other words if the T<GOSUB> is in a function or subroutine then the label referenced by the T<GOSUB> should also be in the same function or subroutine. Similarly any T<GOSUB> in the main code should reference a label, which is also in the main code.

To return from the code fragment called by the command T<GOSUB> the command T<RETURN> should be used. Note that this will not break the execution of a function or a subroutine. The execution will continue on the command line following the T<GOSUB> line.

T<GOSUB> commands can follow each other, ScriptBasic will build up a stack of T<GOSUB> calls and will return to the appropriate command line following the matching T<GOSUB> command.

When a subroutine or function contains T<GOSUB> commands and the function or subroutine is finished so that one or more executed T<GOSUB> command remains without executed T<RETURN> then the T<GOSUB>/T<RATURN> stack is cleared. This is not an error.

See also R<RETURN>.

Definition at line 300 of file function.c.

SaveErrorGoto = pEo->ErrorGoto
 

Definition at line 668 of file function.c.

SaveErrorGoto = pEo->ErrorGoto
 

Definition at line 591 of file function.c.

Referenced by execute_Evaluate(), and hook_CallScribaFunction().

SaveErrorResume = pEo->ErrorResume
 

Definition at line 670 of file function.c.

SaveErrorResume = pEo->ErrorResume
 

Definition at line 593 of file function.c.

Referenced by execute_Evaluate(), and hook_CallScribaFunction().

SavefErrorGoto = pEo->fErrorGoto
 

Definition at line 672 of file function.c.

SavefErrorGoto = pEo->fErrorGoto
 

Definition at line 595 of file function.c.

Referenced by execute_Evaluate(), and hook_CallScribaFunction().

SaveFunctionResultPointer = pEo->pFunctionResult
 

Definition at line 663 of file function.c.

SaveFunctionResultPointer = pEo->pFunctionResult
 

Definition at line 586 of file function.c.

Referenced by execute_Evaluate(), and hook_CallScribaFunction().

SaveStepCounter = pEo->lStepCounter
 

Definition at line 665 of file function.c.

SaveStepCounter = pEo->lStepCounter
 

Definition at line 588 of file function.c.

Referenced by execute_Evaluate(), and hook_CallScribaFunction().


Generated on Sun Mar 12 23:56:33 2006 for ScriptBasic by  doxygen 1.4.6-NO