Difference between revisions of "ReNamer:Pascal Script:Quick guide"
Line 35: | Line 35: | ||
All the typical control structures (building blocks) occurring in Pascal Script are described in the following table. | 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. | + | The table shows a flow chart and Pascal Script code required to implement that logic. To compose your own [[ReNamer:Rules:PascalScript|'''PascalScript''' rule]], you can simnply copy and paste the code and then edit it to finish your script. |
In actual implementation, just substitute the following: | In actual implementation, just substitute the following: | ||
Line 59: | Line 59: | ||
| [[Image:PascalScriptIfThen.png|center]] | | [[Image:PascalScriptIfThen.png|center]] | ||
| | | | ||
− | + | Execute the '''<Action>''' statement only if the '''<Condition>''' is met. Otherwise terminate the loop and pass on the control to the next statement.<br> | |
|- | |- | ||
Line 82: | Line 82: | ||
If '''<Condition>''' is met, execute '''<Action-1>'''. Otherwise execute '''<Action-2>'''. | If '''<Condition>''' is met, execute '''<Action-1>'''. Otherwise execute '''<Action-2>'''. | ||
− | Thus one of these two '''<Actions>''' are definitely executed. <br> | + | Thus one of these two '''<Actions>''' are definitely executed.<br> |
+ | |||
+ | After execution of the action, pass on the control to the next statement.<br> | ||
|- | |- | ||
Line 95: | Line 97: | ||
| [[Image:PascalScriptForLoop.png|center]] | | [[Image:PascalScriptForLoop.png|center]] | ||
| | | | ||
− | + | Execute the '''<Action>''' a certain number of times. | |
− | This example shows that the counter is incremented by 1 | + | This example shows that the counter is incremented by 1, but it can be any statement that changes the value of the counter vaiable towards the target value.<br> |
+ | |||
+ | Similarly, the decision block can have any logical expression with the counter.<br> | ||
+ | |||
+ | Make sure that the exit condition is reached at some point of time; otherwise the loop will execute endlessly, and ReNamer will appear to be hung. | ||
|- | |- | ||
Line 110: | Line 116: | ||
| [[Image:PascalScriptWhileLoop.png|center]] | | [[Image:PascalScriptWhileLoop.png|center]] | ||
| | | | ||
− | + | Check for a condition and if it is met, execute the '''<Action>'''. | |
− | 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.<br> | + | The loop is repeated till the condition is met. When the condition is not met, the loop is terminated and 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.<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. | 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. | + | 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.(See the '''break''' command below)<br> |
|- | |- | ||
Line 126: | Line 132: | ||
| [[Image:PascalScriptRepeatUntilLoop.png|center]] | | [[Image:PascalScriptRepeatUntilLoop.png|center]] | ||
− | | 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''. | + | | 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''. |
|- | |- | ||
| <center>'''case/switch'''</center> <center>'''(exclusive)'''</center> | | <center>'''case/switch'''</center> <center>'''(exclusive)'''</center> | ||
Line 159: | Line 165: | ||
*One and only one '''<Action>''' is executed. After that, the control passes on to the next statement. (It does <u>not</u> check for the next condition.) | *One and only one '''<Action>''' 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 '''<Actions>''' will never be executed. | *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. | + | *The code structure can optionally have a '''"Else" Action'''. It 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). | This is a generalized version of the '''if-then-else''' block (see second row above). | ||
Line 190: | Line 196: | ||
| [[Image:PascalScriptCaseWithFallThrough.png|center]] | | [[Image:PascalScriptCaseWithFallThrough.png|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 '''<Action>''' is execurted. The code structure | + | 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 '''<Action>''' is execurted. <br> |
+ | |||
+ | The code structure can optionally have a '''default''' action. If defined, this action is <u>always</u> 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. | *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 an optional '''<Default''' '''Action>''' block. | |
− | |||
− | This structure is equivalent to a series of '''if-then''' blocks (see the first row), PLUS | ||
|- | |- | ||
Line 203: | Line 209: | ||
| <br> | | <br> | ||
| | | | ||
− | This statement is placed in any of the above | + | This statement is placed in any of the above loops to terminate the loop when a condition is met. Typically, it is used as the '''<Action>''' statement in a '''if-then''' block. This block is then embedded (nested) inside the other code block that is to be contionally terminated. |
See the '''Case''' block above, which uses the '''break '''statement as integral part of its structure. | See the '''Case''' block above, which uses the '''break '''statement as integral part of its structure. | ||
Line 212: | Line 218: | ||
| <br> | | <br> | ||
| | | | ||
− | This statement is placed in any of the above loops to jump to the end of the current iteration, bypassing all the subsequent statements within the loop. However, the execution of the loop continues ( | + | This statement is placed in any of the above loops to jump to the end of the current iteration, bypassing all the subsequent statements within the loop. However, the execution of the loop continues (the next iteration starts). |
− | Typically, it is used in a '''if-then''' block | + | Typically, it is used as the '''<Action>''' statement in a '''if-then''' block. This block is then embedded (nested) inside the other code block, just before the statements that are to be skipped in the current iteration. |
|- | |- | ||
Line 221: | Line 227: | ||
| <br> | | <br> | ||
| | | | ||
− | This statement is placed in any of the above loops to jump to the end of the current iteration, and terminate the loop. The control passes to | + | This statement is placed in any of the above loops to jump to the end of the current iteration, and also terminate the loop. The control passes to the next statement after the loop <br> |
− | Typically, it is used in a '''if-then''' block that is embedded (nested) inside the other code block | + | Typically, it is used as the '''<Action>''' statement in a '''if-then''' block that is embedded (nested) inside the other code block |
|} | |} |
Revision as of 09:26, 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. To compose your own PascalScript rule, you can simnply copy and paste the code and then edit it 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. There may be several statements.
Comments | |||
---|---|---|---|
if <condition> then
begin <Action> end; |
Execute the <Action> statement only if the <Condition> is met. Otherwise terminate the loop and pass on the control to the next statement. | ||
if <condition> then
begin <Action-1> end else begin <Action-2> end; |
Two alternative actions are provided. If <Condition> is met, execute <Action-1>. Otherwise execute <Action-2>. Thus one of these two <Actions> are definitely executed. After execution of the action, pass on the control to the next statement. | ||
for I:=x to y do
begin <Action> end; |
Execute the <Action> a certain number of times. This example shows that the counter is incremented by 1, but it can be any statement that changes the value of the counter vaiable towards the target value. Similarly, the decision block can have any logical expression with the counter. Make sure that the exit condition is reached at some point of time; otherwise the loop will execute endlessly, and ReNamer will appear to be hung. | ||
while <condition> do
begin <Action> end; |
Check for a condition and if it is met, execute the <Action>. The loop is repeated till the condition is met. When the condition is not met, the loop is terminated and 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.(See the break command below) | ||
Repeat until | repeat
<Action> until <condition>; |
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 x of
<Action-1> Break end;
<Action-2> Break end;
<Default Action> end;
|
This is a generalized version of the if-then-else block (see second row above). | ||
case x of
<Action-1> end;
<Action-2> end;
<Default Action> end;
|
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 can optionally have a default action. If defined, this action is always executed, regardless of whether any of the conditions are met. After that, the control is apassed to the next statement.
This structure is equivalent to a series of if-then blocks (see the first row), PLUS an optional <Default Action> block. | ||
This statement is placed in any of the above loops to terminate the loop when a condition is met. Typically, it is used as the <Action> statement in a if-then block. This block is then embedded (nested) inside the other code block that is to be contionally terminated. See the Case block above, which uses the break statement as integral part of its structure. | |||
This statement is placed in any of the above loops to jump to the end of the current iteration, bypassing all the subsequent statements within the loop. However, the execution of the loop continues (the next iteration starts). Typically, it is used as the <Action> statement in a if-then block. This block is then embedded (nested) inside the other code block, just before the statements that are to be skipped in the current iteration. | |||
This statement is placed in any of the above loops to jump to the end of the current iteration, and also terminate the loop. The control passes to the next statement after the loop Typically, it is used as the <Action> statement in a if-then block that is embedded (nested) inside the other code block |