Difference between revisions of "ReNamer:Pascal Script:Quick guide"

From den4b Wiki
Jump to navigation Jump to search
Line 1: Line 1:
=== Basic control flow in a pascal script  ===
+
If you are not familiar with Pascal Scripting, first go through the excellent [http://www.taoyue.com/tutorials/pascal/contents.html tutorial] written by '''Tao Yue''':
  
The typical structure of a script is as follows:
+
The following is a short overview of Pascal Script.<br>
 +
 
 +
=== Basic pascal script  ===
 +
 
 +
The structure of a basic script is as follows:  
  
 
<tt>'''PROGRAM'''</tt>  
 
<tt>'''PROGRAM'''</tt>  
Line 21: Line 25:
 
<tt></tt> <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;''definitions of subprogram&gt;''</tt>  
 
<tt></tt> <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;''definitions of subprogram&gt;''</tt>  
  
'''BEGIN'''
+
'''BEGIN'''  
  
 +
<br> <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;''Executable statements&gt;''</tt>
  
 
+
<tt>'''END.'''</tt>  
<tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;''Executable statements&gt;''</tt>
 
 
 
<tt>'''END.'''</tt>
 
  
 
=== Control Structures  ===
 
=== Control Structures  ===
Line 44: Line 46:
 
! <center>Control structure</center>  
 
! <center>Control structure</center>  
 
! <center>Pascal script</center>  
 
! <center>Pascal script</center>  
! <center>Flowchart</center>
+
! <center>Flowchart</center>
 +
! Comments<br>
 
|-
 
|-
 
| <center>'''If-then'''</center>  
 
| <center>'''If-then'''</center>  
Line 54: Line 57:
 
end;  
 
end;  
  
| [[Image:PascalScriptIfThen.png|center]]
+
| [[Image:PascalScriptIfThen.png|center]]  
 +
|
 +
Executes the &lt;Action&gt; statement only if the &lt;Condition&gt; is met. Otherwise pass on the control to the next statement.
 +
 
 +
The '''&lt;Action&gt;''' may be a block of several statements.<br>
 +
 
 
|-
 
|-
 
| <center>'''If-then-else'''</center>  
 
| <center>'''If-then-else'''</center>  
Line 70: Line 78:
 
end;  
 
end;  
  
| [[Image:PascalScriptIfThenElse.png|center]]
+
| [[Image:PascalScriptIfThenElse.png|center]]  
 +
|
 +
Two alternative actions are provided. Each '''&lt;Action&gt;''' may be a block of several statements.<br>
 +
 
 +
If &lt;Condition&gt; is met, execute &lt;Action-1&gt;. Otherwise execute &lt;Action-2&gt;.
 +
 
 +
Thus one of these two actions are definitely executed. <br>
 +
 
 
|-
 
|-
 
| <center>'''for'''</center>  
 
| <center>'''for'''</center>  
Line 80: Line 95:
 
end;  
 
end;  
  
| [[Image:PascalScriptForLoop.png|center]]
+
| [[Image:PascalScriptForLoop.png|center]]  
 +
|
 +
To execute the &lt;Action&gt; a certain number of times. (The '''&lt;Action&gt;''' may be a block of several statements.)
 +
 
 +
This example shows that the counter is incremented by 1 only, but it can be any statement that changes the value of counter towards the target value.<br>
 +
 
 
|-
 
|-
 
| <center>'''while'''</center>  
 
| <center>'''while'''</center>  
Line 90: Line 110:
 
end;  
 
end;  
  
| [[Image:PascalScriptWhileLoop.png|center]]
+
| [[Image:PascalScriptWhileLoop.png|center]]  
 +
|
 +
Checks for a condition and if it is met, executes an &lt;Action&gt;. (The '''&lt;Action&gt;''' may be a block of several statements.)
 +
 
 +
The loop is repeated till the condition is met. When the condition is not met the control passes to the next statement. Note that if the condition fails in the first-ever check, the '''&lt;Action&gt;''' may not be executed at all.<br>
 +
 
 +
Make sure that the condition will fail at some point of time; otherwise the loop will execute endlessly, and ReNamer will appear to be hung.
 +
 
 +
Sometimes the condition is set to be always TRUE, and then a statement inside the '''&lt;Action&gt;''' block breaks the loop based on a different condition.
 +
 
 +
|-
 +
| '''Repeat until'''
 +
| repeat
 +
&lt;Action&gt;
 +
 
 +
until &lt;condition&gt;;
 +
 
 +
| [[Image:PascalScriptRepeatUntilLoop.png|center]]
 +
| This structure is similar to the '''While loop''' (see above). However, the only difference is that the &lt;Action&gt; is taken first and then the condition is checked. As a result, the &lt;Action&gt; is executed ''at least once''.
 
|-
 
|-
 
| <center>'''case/switch'''</center> <center>'''(exclusive)'''</center>  
 
| <center>'''case/switch'''</center> <center>'''(exclusive)'''</center>  
Line 116: Line 154:
 
<br> end;  
 
<br> end;  
  
| [[Image:PascalScriptCase.png|center]]
+
| [[Image:PascalScriptCase.png|center]]  
 +
|
 +
<br> This code structure has several '''&lt;Action&gt;''' blocks, each with its own condition. <br>(The '''&lt;Action&gt;''' may be a block of several statements.)
 +
 
 +
*Any given '''&lt;Action&gt;''' block is executed only if its condition is met.
 +
*One and only one '''&lt;Action&gt;''' is executed. After that, the control passes on to the next statement. (It does <u>not</u> check for the next condition.)
 +
*The conditions are checked in the "top down" order. So even if the other conditions are also met, their '''&lt;Actions&gt;''' will never be executed.
 +
*The '''"Else" Action''' is executed if (and only if-) none of the conditions are met.
 +
 
 +
This is a generalized version of the '''if-then-else'''&nbsp;block (see second row above).
 +
 
 
|-
 
|-
 
| <center>'''case/switch'''</center> <center>'''(fall-through)'''</center>  
 
| <center>'''case/switch'''</center> <center>'''(fall-through)'''</center>  
Line 142: Line 190:
 
<br> end;  
 
<br> end;  
  
| [[Image:PascalScriptCaseWithFallThrough.png|center]]
+
| [[Image:PascalScriptCaseWithFallThrough.png|center]]  
|-
+
|  
| <center>'''Repeat until'''</center>  
+
This is similar to the case structure above, but here, <u>all</u> the conditions are checked, and if any condition is met, the corresponding '''&lt;Action&gt;''' is execurted. The code structure has a default action that is <u>always</u> executed, regardless of whether any of the conditions are met. After that, the control is apassed to the next statement.
| repeat
+
 
&lt;Action&gt;  
+
*Execution of one '''&lt;action&gt;''' may change whether a subsequent condition is met.
 +
 
 +
<br>
  
until &lt;condition&gt;;
+
This structure is equivalent to a series of '''if-then''' blocks (see the first row), PLUS a default &lt;Action&gt; block.
  
| [[Image:PascalScriptRepeatUntilLoop.png|center]]
 
 
|-
 
|-
 
| <center>'''Break'''</center>  
 
| <center>'''Break'''</center>  
 
| <br>  
 
| <br>  
| <br>
+
| <br>  
 +
| Used to break any of the loops.
 
|-
 
|-
 
| <center>'''Continue'''</center>  
 
| <center>'''Continue'''</center>  
 +
| <br>
 
| <br>  
 
| <br>  
 
| <br>
 
| <br>
 
|-
 
|-
 
| <center>'''Exit'''</center>  
 
| <center>'''Exit'''</center>  
 +
| <br>
 
| <br>  
 
| <br>  
 
| <br>
 
| <br>
 
|}
 
|}

Revision as of 06:17, 11 July 2009

If you are not familiar with Pascal Scripting, first go through the excellent tutorial written by Tao Yue:

The following is a short overview of Pascal Script.

Basic pascal script

The structure of a basic script is as follows:

PROGRAM

        ProgramName (FileList);

CONST        

        <Constant declarations>

TYPE    

        <Type declarations>

VAR

        <Variable declarations>

        <definitions of subprogram>

BEGIN


        <Executable statements>

END.

Control Structures

All the typical control structures (building blocks) occurring in Pascal Script are described in the following table.

The table shows a flow chart and Pascal Script code required to implement that logic. You can simnply copy and paste these blocks and then edit them to finish your script.

In actual implementation, just substitute the following:

  • Replace <condition> with an actual Pascal statement that tests for a condition.
  • Replace <Action> with code block that takes action relevant to the condition.
Control structure
Pascal script
Flowchart
Comments
If-then
if <condition> then

begin

<Action>

end;

PascalScriptIfThen.png

Executes the <Action> statement only if the <Condition> is met. Otherwise pass on the control to the next statement.

The <Action> may be a block of several statements.

If-then-else
if <condition> then

begin

<Action-1>

end else

begin

<Action-2>

end;

PascalScriptIfThenElse.png

Two alternative actions are provided. Each <Action> may be a block of several statements.

If <Condition> is met, execute <Action-1>. Otherwise execute <Action-2>.

Thus one of these two actions are definitely executed.

for
for I:=x to y do

begin

<Action>

end;

PascalScriptForLoop.png

To execute the <Action> a certain number of times. (The <Action> may be a block of several statements.)

This example shows that the counter is incremented by 1 only, but it can be any statement that changes the value of counter towards the target value.

while
while <condition> do

begin

<Action>

end;

PascalScriptWhileLoop.png

Checks for a condition and if it is met, executes an <Action>. (The <Action> may be a block of several statements.)

The loop is repeated till the condition is met. When the condition is not met the control passes to the next statement. Note that if the condition fails in the first-ever check, the <Action> may not be executed at all.

Make sure that the condition will fail at some point of time; otherwise the loop will execute endlessly, and ReNamer will appear to be hung.

Sometimes the condition is set to be always TRUE, and then a statement inside the <Action> block breaks the loop based on a different condition.

Repeat until repeat

<Action>

until <condition>;

PascalScriptRepeatUntilLoop.png
This structure is similar to the While loop (see above). However, the only difference is that the <Action> is taken first and then the condition is checked. As a result, the <Action> is executed at least once.
case/switch
(exclusive)
case x of


1: begin

<Action-1> Break

end;


2: begin

<Action-2> Break

end;


//repeat for other cases


else begin

<Default Action>

end;


end;

PascalScriptCase.png


This code structure has several <Action> blocks, each with its own condition.
(The <Action> may be a block of several statements.)

  • Any given <Action> block is executed only if its condition is met.
  • One and only one <Action> is executed. After that, the control passes on to the next statement. (It does not check for the next condition.)
  • The conditions are checked in the "top down" order. So even if the other conditions are also met, their <Actions> will never be executed.
  • The "Else" Action is executed if (and only if-) none of the conditions are met.

This is a generalized version of the if-then-else block (see second row above).

case/switch
(fall-through)
case x of


1: begin

<Action-1>

end;


2: begin

<Action-2>

end;


//repeat for other cases


else begin

<Default Action>

end;


end;

PascalScriptCaseWithFallThrough.png

This is similar to the case structure above, but here, all the conditions are checked, and if any condition is met, the corresponding <Action> is execurted. The code structure has a default action that is always executed, regardless of whether any of the conditions are met. After that, the control is apassed to the next statement.

  • Execution of one <action> may change whether a subsequent condition is met.


This structure is equivalent to a series of if-then blocks (see the first row), PLUS a default <Action> block.

Break


Used to break any of the loops.
Continue



Exit