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

From den4b Wiki
Jump to navigation Jump to search
(Replaced 'prettytable' with 'wikitable' style.)
 
(32 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=== Basic control flow in a pascal script  ===
+
{{Up|ReNamer:Pascal Script}}
  
<tt>'''PROGRAM'''</tt>
+
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'''.
  
<tt>''&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ProgramName'' (''FileList'')''';'''</tt>  
+
The following is a short overview of Pascal Script.<br>  
  
<tt>'''CONST''' </tt> <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </tt>
+
== Basic pascal script ==
  
<tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;''Constant declarations&gt;''</tt>
+
The structure of a basic script is as follows (keywords are shown in ALLCAPS bold):
  
'''TYPE''' <tt>&nbsp;&nbsp;&nbsp;</tt>
+
<div style="border:1px dashed gray; background-color:#eff; display:inline-block; padding: 1em 2em">
 +
'''CONST'''
  
<tt>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &lt;''Type declarations&gt;''</tt>
+
:''&lt;Constant declarations&gt;''
  
<tt>'''VAR'''</tt>
+
'''TYPE'''
  
<tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;''Variable declarations&gt;''</tt>
+
:''&lt;Type declarations&gt;''
  
<tt></tt> <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;''definitions of subprogram&gt;''</tt>
+
'''VAR'''
 +
 
 +
:''&lt;Variable declarations&gt;''
  
 
'''BEGIN'''
 
'''BEGIN'''
<tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;''Executable statements&gt;''</tt>
 
  
<tt>'''END.'''</tt>
+
:''&lt;Executable statements&gt;''  
  
=== Control Structures  ===
+
'''END.'''
 +
</div>
  
All the typical control structures (building blocks) occurring in Pascal Script are described in the following table.  
+
Note that:
 +
* The main code must be within the '''begin''' and '''end.''' keywords.
 +
* All statements in the script use the semicolon "''';'''" as terminator. Only the last statement ('''END.''') uses a dot as terminator.
  
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.  
+
== 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 [[ReNamer:Rules:PascalScript|PascalScript rule]], you can simply 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:  
  
*Replace '''''&lt;condition&gt;''''' with an actual Pascal statement that tests for a condition.  
+
*Replace '''''&lt;Condition&gt;''''' with an actual Pascal statement that tests for a condition.  
*Replace '''''&lt;Action&gt;''''' with code block that takes action relevant to the condition.<br>
+
*Replace '''''&lt;Action&gt;''''' with code block that takes action relevant to the condition. There may be several statements.<br>
  
{| class="prettytable"
+
=== Branching ===
 +
 
 +
These structures are used to execute different blocks of code depending upon a condition.
 +
 
 +
{| class="wikitable"
 
|-
 
|-
! <center>Control structure</center>
+
! Branching structure
! <center>Pascal script</center>
+
! Pascal script
! <center>Flowchart</center>
+
! Flowchart (Logic)
 +
! Remarks
 
|-
 
|-
| <center>'''If-then'''</center>  
+
! if-then
| if &lt;condition&gt; then  
+
|
 +
<pre>if &lt;Condition&gt; then  
 
begin  
 
begin  
 +
  &lt;Action&gt;
 +
end;</pre>
  
&lt;Action&gt;  
+
| [[Image:PascalScriptIfThen.png|center]]
 +
|
 +
Execute the '''&lt;Action&gt;''' statement only if the '''&lt;Condition&gt;''' is met. Otherwise pass on the control to the next statement that follows the '''&lt;Action&gt;'''.<br>
  
end;
 
 
| [[Image:PascalScriptIfThen.png|center]]
 
 
|-
 
|-
| <center>'''If-then-else'''</center>  
+
! if-then-else
| if &lt;condition&gt; then  
+
|
 +
<pre>if &lt;Condition&gt; then  
 
begin  
 
begin  
 
+
  &lt;Action-1&gt;  
&lt;Action-1&gt;  
 
 
 
 
end else  
 
end else  
 
 
begin  
 
begin  
 +
  &lt;Action-2&gt;
 +
end;</pre>
  
&lt;Action-2&gt;
+
| [[Image:PascalScriptIfThenElse.png|center]]
 +
|
 +
Two alternative actions are provided.
  
end;  
+
If '''&lt;Condition&gt;''' is met, execute '''&lt;Action-1&gt;'''. Otherwise execute '''&lt;Action-2&gt;'''.
  
| [[Image:PascalScriptIfThenElse.png|center]]
+
Thus one of these two '''&lt;Actions&gt;''' are definitely executed.
|-
 
| <center>'''for'''</center>
 
| for I:=x to y do
 
begin
 
 
 
&lt;Action&gt;  
 
  
end;
+
After execution of the action, pass on the control to the next statement.
  
| [[Image:PascalScriptForLoop.png|center]]
 
 
|-
 
|-
| <center>'''while'''</center>  
+
! case/switch
| while &lt;condition&gt; do
+
|  
begin  
+
<pre>case X of
 +
  1:
 +
    begin
 +
      &lt;Action-1&gt;
 +
    end;
 +
  2:
 +
    begin
 +
      &lt;Action-2&gt;  
 +
    end;
 +
  else
 +
    begin  
 +
      &lt;Default Action&gt;
 +
    end;
 +
end;</pre>
  
&lt;Action&gt;  
+
| [[Image:PascalScriptCase.png|center]]
 +
|
 +
<br> This code structure has several '''&lt;Action&gt;''' blocks, each with its own condition.
  
end;  
+
*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 code structure can optionally have a '''&lt;Default Action&gt;'''. It is executed if (and only if-) none of the conditions are met.
  
| [[Image:PascalScriptWhileLoop.png|center]]
+
This is a generalized version of the '''if-then-else''' block (above).
|-
 
| <center>'''case/switch'''</center> <center>'''(exclusive)'''</center>
 
| case x of
 
<br> 1: begin
 
  
&lt;Action-1&gt; Break
+
|}
  
end;
+
=== Loops  ===
  
<br> 2: begin
+
Loops are used to execute a block of code iteratively till a certain condition is met.
  
&lt;Action-2&gt; Break
+
{| class="wikitable"
 +
|-
 +
! Loops
 +
! Pascal script
 +
! Flowchart (Logic)
 +
! Remarks
 +
|-
 +
! For To Do
 +
| <pre>for I&nbsp;:= X to Y do
 +
begin
 +
  &lt;Action&gt;
 +
end;</pre>
 +
| [[Image:PascalScriptForLoop.png|center]]
 +
| Execute the &lt;Action&gt; 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.
  
end;
+
Similarly, the decision block can have any logical expression with the counter.
  
<br> //repeat for other cases
+
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. <br>  
  
<br> else begin  
+
|-
 +
! While Do
 +
| <pre>while &lt;Condition&gt; do
 +
begin  
 +
  &lt;Action&gt;
 +
end;</pre>
 +
| [[Image:PascalScriptWhileLoop.png|center]]
 +
| Check for a condition and if it is met, execute the '''&lt;Action&gt;'''.
 +
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 '''&lt;Action&gt;''' may not be executed at all.
  
&lt;Default Action&gt;  
+
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.
  
end;  
+
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.(See the '''break '''command below)<br>
  
<br> end;
 
 
| [[Image:PascalScriptCase.png|center]]
 
 
|-
 
|-
| <center>'''case/switch'''</center> <center>'''(fall-through)'''</center>  
+
! Repeat Until
| case x of  
+
| <pre>repeat
<br> 1: begin
+
  &lt;Action&gt;
 +
until &lt;Condition&gt;;</pre>
 +
| [[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. <br>
 +
|-
 +
! Break
 +
| <pre>Break;</pre>  
 +
<u>'''OR'''</u><br>
 +
<pre>if &lt;Condition&gt; then Break;</pre>  
 +
| colspan="2" |
 +
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 '''&lt;Action&gt;''' statement in a '''if-then''' block. This block is then embedded (nested) inside the other code block that is to be contionally terminated.
  
&lt;Action-1&gt;
+
See the '''Case''' block [[ReNamer:Pascal_Script:Quick_guide#Branching|above]], which uses the '''break '''statement as integral part of its structure.
  
end;
+
|-
 +
! Continue
 +
| <pre>Continue;</pre>
 +
<u>'''OR'''</u>
 +
<pre>if &lt;Condition&gt; then Continue;</pre>
 +
| colspan="2" |
 +
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).
  
<br> 2: begin
+
Typically, it is used as the '''&lt;Action&gt;''' 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.
 
+
|}
&lt;Action-2&gt;  
 
 
 
end;
 
 
 
<br> //repeat for other cases
 
 
 
<br> else begin
 
 
 
&lt;Default Action&gt;
 
 
 
end;
 
  
<br> end;
+
=== Control  ===
  
| [[Image:PascalScriptCaseWithFallThrough.png|center]]
+
{| class="wikitable"
|-
+
! Exit
| <center>'''Repeat until'''</center>  
+
| <pre>Exit;</pre>
| repeat
+
<u>'''OR'''</u>  
&lt;Action&gt;  
+
<pre>if &lt;Condition&gt; then Exit;</pre>
 +
|
 +
The '''Exit''' procedure abruptly terminates the current function or procedure. If exiting a function, then ''Result'' contains the last set value.
  
until &lt;condition&gt;;
+
'''Warning:''' use with caution - jumping is a concept at odds with structured coding - it makes code maintenance difficult.<br>
  
| [[Image:PascalScriptRepeatUntilLoop.png|center]]
 
|-
 
| <center>'''Break'''</center>
 
| <br>
 
| <br>
 
|-
 
| <center>'''Continue'''</center>
 
| <br>
 
| <br>
 
|-
 
| <center>'''Exit'''</center>
 
| <br>
 
| <br>
 
 
|}
 
|}
 +
[[Category:ReNamer]]

Latest revision as of 15:31, 11 November 2015

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 (keywords are shown in ALLCAPS bold):

CONST

<Constant declarations>

TYPE

<Type declarations>

VAR

<Variable declarations>

BEGIN

<Executable statements>

END.

Note that:

  • The main code must be within the begin and end. keywords.
  • All statements in the script use the semicolon ";" as terminator. Only the last statement (END.) uses a dot as terminator.

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 simply 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.

Branching

These structures are used to execute different blocks of code depending upon a condition.

Branching structure Pascal script Flowchart (Logic) Remarks
if-then
if <Condition> then 
begin 
  <Action> 
end;
PascalScriptIfThen.png

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

if-then-else
if <Condition> then 
begin 
  <Action-1> 
end else 
begin 
  <Action-2> 
end;
PascalScriptIfThenElse.png

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.

case/switch
case X of 
  1:
    begin 
      <Action-1> 
    end; 
  2:
    begin 
      <Action-2> 
    end; 
  else
    begin 
      <Default Action> 
    end; 
end;
PascalScriptCase.png


This code structure has several <Action> blocks, each with its own condition.

  • 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 code structure can optionally have a <Default 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 (above).

Loops

Loops are used to execute a block of code iteratively till a certain condition is met.

Loops Pascal script Flowchart (Logic) Remarks
For To Do
for I := X to Y do 
begin 
  <Action> 
end;
PascalScriptForLoop.png
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 Do
while <Condition> do 
begin 
  <Action> 
end;
PascalScriptWhileLoop.png
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>;
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.
Break
Break;

OR

if <Condition> then Break;

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.

Continue
Continue;

OR

if <Condition> then Continue;

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.

Control

Exit
Exit;

OR

if <Condition> then Exit;

The Exit procedure abruptly terminates the current function or procedure. If exiting a function, then Result contains the last set value.

Warning: use with caution - jumping is a concept at odds with structured coding - it makes code maintenance difficult.