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

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

Go to the source code of this file.

Functions

 if (memory_IsUndef(ItemResult))
 switch (TYPE(ItemResult))
END SETPROGRAMCOUNTER (PARAMETERNODE)
 if (nToStatement==0)
 if (nStepStatement)
 if (nLoopEnd)
 if (nStepStatement &&OPCODE(nStepStatement)==CMD_FORSTEP)
 if (TYPE(vStartExpression)==VTYPE_LONG &&TYPE(vEndExpression)==VTYPE_LONG)
 SETPROGRAMCOUNTER (nLoopEnd)
 if (memory_IsUndef(*LetThisVariable)||(TYPE(*LetThisVariable)!=VTYPE_LONG &&TYPE(*LetThisVariable)!=VTYPE_DOUBLE))

Variables

NODE nItem
NODE nGoForward
VARIABLE ItemResult
 nItem = PARAMETERNODE
 ItemResult = EVALUATEEXPRESSION(nItem)
 ASSERTOKE
 NEXTPARAMETER
 nGoForward = CDR(PARAMETERNODE)
END NODE nItem
NODE nGoForward
VARIABLE ItemResult
 nItem = PARAMETERNODE
 ItemResult = EVALUATEEXPRESSION(nItem)
 ASSERTOKE
 NEXTPARAMETER
 nGoForward = CDR(PARAMETERNODE)
END END END END END END END
END VARIABLE 
vStartExpression
END END END END END END END
END VARIABLE 
vEndExpression
END END END END END END END
END VARIABLE 
vStepExpression
LEFTVALUE LetThisVariable
NODE nToStatement
NODE nStepStatement
NODE nLoopStart
NODE nLoopEnd
int iGoingUp
long refcount
int bEnterTheLoop
 nToStatement = CDR(pEo->ProgramCounter)
 nStepStatement = CDR(nToStatement)
 nToStatement = CAR(nToStatement)
 nLoopEnd = pEo->CommandArray[nLoopEnd -1].Parameter.CommandArgument.next
 nLoopEnd = pEo->CommandArray[nLoopEnd-1].Parameter.CommandArgument.Argument.pNode
 vStartExpression = CONVERT2NUMERIC(vStartExpression)
 vEndExpression = EVALUATEEXPRESSION(pEo->CommandArray[nToStatement-1].Parameter.CommandArgument.Argument.pNode)
 ASSERTOKE
 vEndExpression = CONVERT2NUMERIC(vEndExpression)
END LEFTVALUE LetThisVariable
VARIABLE vEndExpression
VARIABLE vStepExpression
VARIABLE vNewLoopValue
NODE nStepStatement
NODE nForStatement
NODE nLoopStart
NODE nLoopEnd
int iGoingUp
int bContinueLoop
long refcount
int iError
 vEndExpression = EVALUATEEXPRESSION(PARAMETERNODE)
 ASSERTOKE
 vEndExpression = CONVERT2NUMERIC(vEndExpression)
 NEXTPARAMETER
 nForStatement = CAR(PARAMETERNODE)
 NEXTPARAMETER
 nLoopEnd = CDR(PARAMETERNODE)
 LetThisVariable = EVALUATELEFTVALUE( pEo->CommandArray[nForStatement-1].Parameter.CommandArgument.Argument.pNode )
 ASSERTOKE
 nStepStatement = CDR(pEo->ProgramCounter)


Function Documentation

if memory_IsUndef *LetThisVariable||  (TYPE(*LetThisVariable)!=VTYPE_LONG &&TYPE(*LetThisVariable)!=VTYPE_DOUBLE)  ) 
 

Definition at line 702 of file while.c.

References COMMAND_ERROR_MEMORY_LOW, ERROR(), memory_ReplaceVariable(), NULL, and pEo.

if TYPE(vStartExpression = = VTYPE_LONG && TYPE(vEndExpression) == VTYPE_LONG  ) 
 

Definition at line 644 of file while.c.

References memory_ReplaceVariable(), pEo, RETURN, and SETPROGRAMCOUNTER().

if nStepStatement &&  OPCODE(nStepStatement) = = CMD_FORSTEP  ) 
 

Definition at line 623 of file while.c.

References ASSERTOKE, LONGVALUE(), OPCODE, and pEo.

if nLoopEnd   ) 
 

Definition at line 603 of file while.c.

if nStepStatement   ) 
 

Definition at line 583 of file while.c.

References pEo.

if nToStatement  = = 0  ) 
 

Definition at line 574 of file while.c.

References RETURN, and SETPROGRAMCOUNTER().

if memory_IsUndef(ItemResult  ) 
 

Definition at line 94 of file while.c.

References RETURN, and SETPROGRAMCOUNTER().

SETPROGRAMCOUNTER nLoopEnd   ) 
 

END SETPROGRAMCOUNTER PARAMETERNODE   ) 
 

switch TYPE(ItemResult  ) 
 

Definition at line 99 of file while.c.

References LONGVALUE(), RETURN, SETPROGRAMCOUNTER(), and STRINGVALUE().


Variable Documentation

ASSERTOKE
 

Definition at line 700 of file while.c.

ASSERTOKE
 

Definition at line 691 of file while.c.

ASSERTOKE
 

Definition at line 620 of file while.c.

ASSERTOKE
 

Definition at line 167 of file while.c.

ASSERTOKE
 

Definition at line 90 of file while.c.

int bContinueLoop
 

Definition at line 686 of file while.c.

int bEnterTheLoop
 

Definition at line 569 of file while.c.

int iError
 

Definition at line 688 of file while.c.

int iGoingUp
 

Definition at line 685 of file while.c.

int iGoingUp
 

Definition at line 567 of file while.c.

ItemResult = EVALUATEEXPRESSION(nItem)
 

Definition at line 166 of file while.c.

VARIABLE ItemResult
 

Definition at line 163 of file while.c.

ItemResult = EVALUATEEXPRESSION(nItem)
 

Definition at line 89 of file while.c.

VARIABLE ItemResult
 

Definition at line 86 of file while.c.

LetThisVariable = EVALUATELEFTVALUE( pEo->CommandArray[nForStatement-1].Parameter.CommandArgument.Argument.pNode )
 

Definition at line 699 of file while.c.

END LEFTVALUE LetThisVariable
 

Definition at line 678 of file while.c.

LEFTVALUE LetThisVariable
 

Definition at line 563 of file while.c.

NEXTPARAMETER
 

Definition at line 696 of file while.c.

NEXTPARAMETER
 

Definition at line 694 of file while.c.

NEXTPARAMETER
 

Definition at line 168 of file while.c.

NEXTPARAMETER
 

Definition at line 91 of file while.c.

nForStatement = CAR(PARAMETERNODE)
 

Definition at line 695 of file while.c.

NODE nForStatement
 

Definition at line 683 of file while.c.

nGoForward = CDR(PARAMETERNODE)
 

Definition at line 171 of file while.c.

NODE nGoForward
 

Definition at line 162 of file while.c.

nGoForward = CDR(PARAMETERNODE)
 

Definition at line 93 of file while.c.

NODE nGoForward
 

Definition at line 85 of file while.c.

nItem = PARAMETERNODE
 

Definition at line 165 of file while.c.

END NODE nItem
 

DOUNTIL =section loop =title DO UNTIL condition

This command implements a looping construct that loops the code between the line T<DO UNTIL> and T<LOOP> util the expression following the keywords on the loop starting line becomes T<true>.

=verbatim DO UNTIL expression ... commands to repeat ... LOOP =noverbatim

The expression is evaluated when the looping starts and each time the loop is restarted. It means that the code between the T<DO UNTIL> and T<LOOP> lines may be skipped totally if the expression evaluates to some T<TRUE> value during the first evaluation before the execution starts the loop.

This command is practically equivalent to the construct

=verbatim WHILE NOT expression ... commands to repeat ... WEND =noverbatim

You can and you also should use the construct that creates more readable code.

See also R<WHILE>, R<DOUNTIL>, R<DOWHILE>, R<REPEAT>, R<DO> and R<FOR>.

Definition at line 161 of file while.c.

nItem = PARAMETERNODE
 

Definition at line 88 of file while.c.

NODE nItem
 

WHILE =section loop =title WHILE condition

Implements the 'while' loop as it is usually done in most basic implementations. The loop starts with the command T<while> and finished with the line containing the keyword T<wend>. The keyword T<while> is followed by an expression and the loop is executes so long as long the expression is evaluated T<true>.

=verbatim while expression ... commands to repeat ... wend =noverbatim

The expression is evaluated when the looping starts and each time the loop is restarted. It means that the code between the T<while> and T<wend> lines may be skipped totally if the expression evaluates to some T<false> value during the first evaluation before the execution starts the loop.

In case some condition makes it necessary to exit the loop from its middle then the command R<GOTO> can be used.

ScriptBasic implements several looping constructs to be compatible with different BASIC language dialects. Some constructs are even totally interchangeable to let programmers with different BASIC experience use the one that fit they the best. See also R<WHILE>, R<DOUNTIL>, R<DOWHILE>, R<REPEAT>, R<DO> and R<FOR>.

Definition at line 84 of file while.c.

nLoopEnd = CDR(PARAMETERNODE)
 

Definition at line 697 of file while.c.

NODE nLoopEnd
 

Definition at line 684 of file while.c.

nLoopEnd = pEo->CommandArray[nLoopEnd-1].Parameter.CommandArgument.Argument.pNode
 

Definition at line 602 of file while.c.

nLoopEnd = pEo->CommandArray[nLoopEnd -1].Parameter.CommandArgument.next
 

Definition at line 601 of file while.c.

NODE nLoopEnd
 

Definition at line 566 of file while.c.

NODE nLoopStart
 

Definition at line 684 of file while.c.

NODE nLoopStart
 

Definition at line 566 of file while.c.

nStepStatement = CDR(pEo->ProgramCounter)
 

Definition at line 710 of file while.c.

NODE nStepStatement
 

Definition at line 682 of file while.c.

nStepStatement = CDR(nToStatement)
 

Definition at line 580 of file while.c.

NODE nStepStatement
 

Definition at line 565 of file while.c.

nToStatement = CAR(nToStatement)
 

Definition at line 581 of file while.c.

nToStatement = CDR(pEo->ProgramCounter)
 

Definition at line 572 of file while.c.

NODE nToStatement
 

Definition at line 564 of file while.c.

long refcount
 

Definition at line 687 of file while.c.

long refcount
 

Definition at line 568 of file while.c.

vEndExpression = CONVERT2NUMERIC(vEndExpression)
 

Definition at line 692 of file while.c.

vEndExpression = EVALUATEEXPRESSION(PARAMETERNODE)
 

Definition at line 690 of file while.c.

VARIABLE vEndExpression
 

Definition at line 679 of file while.c.

vEndExpression = CONVERT2NUMERIC(vEndExpression)
 

Definition at line 621 of file while.c.

vEndExpression = EVALUATEEXPRESSION(pEo->CommandArray[nToStatement-1].Parameter.CommandArgument.Argument.pNode)
 

Definition at line 619 of file while.c.

END END END END END END END END VARIABLE vEndExpression
 

Definition at line 560 of file while.c.

VARIABLE vNewLoopValue
 

Definition at line 681 of file while.c.

vStartExpression = CONVERT2NUMERIC(vStartExpression)
 

Definition at line 617 of file while.c.

END END END END END END END END VARIABLE vStartExpression
 

FOR =section loop =title FOR var=exp_start TO exp_stop [ STEP exp_step ]

Implements a FOR loop. The variable T gets the value of the start expression T<exp_start>, and after each execution of the loop body it is incremented or decrement by the value T<exp_step> until it reaches the stop value T<exp_stop>.

=verbatim FOR var= exp_start TO exp_stop [ STEP exp_step] ... commands to repeat ... NEXT var =noverbatim

The T<STEP> part of the command is optional. If this part is missing then the default value to increment the variable is 1.

If =itemize =item the expression T<exp_start> is larger than the expression T<exp_stop> and T<exp_step> is positive or if =item the expression T<exp_start> is smaller than the expression T<exp_stop> and T<exp_step> is negative =noitemize then the loop body is not executed even once and the variable retains its old value.

When the loop is executed at least once the variable gets the values one after the other and after the loop exists the loop variable holds the last value for which the loop already did not execute. Thus

=verbatim for h= 1 to 3 next print h stop =noverbatim

prints T<4>.

The expression T<exp_start> is evaluated only once when the loop starts. The other two expressions T<exp_stop> and T<exp_step> are evaluated before each loop. Thus

=verbatim j = 1 k = 10 for h= 1 to k step j print h,"\n" j += 1 k -= 1 next print k," ",j,"\n" stop =noverbatim

will print

=verbatim 1 3 6 7 4 =noverbatim

To get into more details the following example loop

=verbatim STEP_v = 5 for z= 1 to 10 step STEP_v print z,"\n" STEP_v -= 10 next z =noverbatim

executes only once. This is because the step value changes its sign during the evaluation and the new value being negative commands the loop to terminate as the loop variable altered value is smaller then the end value. In other words the comparison also depends on the actual value of the step expression.

These are not only the expressions that are evaluated before each loop, but the variable as well. If the loop variable is a simple variable then this has not too much effect. However if the loop variable is an array member then this really has to be taken into account. For example:

=verbatim for j=1 to 9 A[j] = 0 next

j = 1 for A[j]= 1 to 9

for k=1 to 9 print A[k] next k print

j += 1 if j > 9 then STOP

next =noverbatim

prints

=verbatim 100000000 110000000 111000000 111100000 111110000 111111000 111111100 111111110 111111111 =noverbatim

so you can see that the loop takes, evaluates, compares and increments the actual array element as the variable T<j> in the sample code above is incremented.

The loop variable or some other left value has to stand between the keyword T<FOR> and the sign T<=> on the start line of the loop but this is optional following the keyword T<NEXT>. ScriptBasic optionally allow you to write the variable name after the keyword T<NEXT> but the interpreter does not check if the symbol is a variable of the loop. The role of this symbol is merely documentation of the BASIC code. However, you can not write an array element following the keyword T<NEXT>, only a simple variable name.

If the expression T<exp_step> is zero then the loop variable is not altered and the loop is re-executed with the same loop variable value. This way you can easily get into infinite loop.

These are fine tuning details of the command T<FOR> that you may need to be aware when you read some tricky code. On the other hand you should never create any code that depends on these features. The loop variable is recommended to be a simple variable and the expressions in the loop head should evaluate the same for each execution of the loop. If you need something more special that may depend on some of the features discussed above then you have to consider using some other looping construct to get more readable code.

Definition at line 560 of file while.c.

VARIABLE vStepExpression
 

Definition at line 680 of file while.c.

END END END END END END END END VARIABLE vStepExpression
 

Definition at line 560 of file while.c.


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