PART EIGHT - A D4D PRIMER - CONVERTING DCAL to D4D CODE
Converting older DCAL macros into D4D macros is reasonably straight forward,but somewhat tedious process. The first step is to load your old DCAL code inin a simple text editor such as Notepad++ and run several search and replace cycles to convert from DCAL to the Delphi language. Changes include:
IF Then statements.
A Delphi if statement reads:
while Delphi If statements reads:
ELSE statements
In Delphi an ELSE statement follows this format:
ELSIF statements
The 'elsif' statement does not exist in Delphi. There are two alternatives to correct it.
1. Search for 'elsif' and replace it with 'end else if '.
2. Delphi includes the 'case condition of' statement structure which reads simply in your code. You can easily assemble 'case' statements individually to replace 'elsif' cascades in your code.
The 'RETURN;' keyword alone
The return keyword used alone is used to end processing a method before reaching the method end. Delphi does not recognize the keyword 'return' and uses the keyword 'Exit' instead. Search for 'Return;' and replace with 'Exit' (without a semicolon).
The 'Return value;' keyword
Delphi uses a built in function variable called 'result' for function return values. Replace the term by searching for 'Return' and replacing it with 'result := '.
For/Do and While/Do statements
These multiple line statement structures must be enclosed with 'begin, and 'end;' keywords. For 'For/Do' and 'While/do' statements Search for the word ' do' (include a leading space to avoid 'do' contained in words) and replace with ' do begin'.
End of method code line
Datacad methods conclude with a last line reading 'end <methodname>; while Delphi methods do not include the method name. To correct this manually move to the end of each method and insert '; //' after 'end' converting the method name to a comment.
! Comments
The '!' is not recognized by Delphi for a line comment. Search for '!' and replace it with '//'.
Method argument types
A typical DCAL method declaration might read as 'function doWork (a: IN OUT integer; b: IN Integer; c: OUT integer, d: integer): integer;'. Delphi does not recognizer the key words IN & OUT and instead uses 'const' for IN and 'var' for OUT. The revised method will read 'function doWork (var a: integer; const b: Integer; var c: integer, d: integer): integer;'.
Global Unit Variables
DCAL units include provisions for persistent global variables declared in units outside of methods. Whenever a macro is closed, the current value for these variables is saved in the macro file '*.dcx'. Delphi does not include these persistent variables so another strategy must be used.
Initialization methods
Many DCAL macros include a method such as 'procedure InitData;' to set initial values for a macro. The call for this procedure is usually placed in the DCAL Macro program starting method for the macro and checks a global variable such as 'Used: boolean;' to determine if the macro variables have been given initial values. Boolean variables are initiated as false so 'Used' will equal false on its first use meaning that InitData should be called. 'InitData' will then set 'Used' to true, preventing reinitialization in later macro runs. Unfortunately, Delphi unit variables are not stored between uses so this initiation strategy will not work. Delphi provides a unit section called 'Initialization' that serves the same first time purpose. However, to preserve these variables between uses of the macro they will have to be saved and retrieved using a file. One way of doing so is discussed in PART SIX using 'ini' files.
Accessing Datacad Variables
The DCAL manual lists numerous program variables that are accessible in DCAL. D4D declares these varaibles in a in a packed record called 'savevar' in uvariables.pas,although many have modified names. requiring all variables to be checked and renamed in your revised code. To access these variables you must use the function PGsavevars; to read or write the specific field as in the following code line:.
Method Public Declarations
DCAL requires two files for each unit file used in a macro. 'UnitA.inc' and 'UnitA.dcs'. The '.inc' file contains declarations including a list of all methods which are to be accessible to other units in the program. The '.dcs' file contains the actual methods including a 'PUBLIC;' declaration ending each methods declaration line. Delphi moves all unit declarations into the same unit file under the section heading 'interface'. Copy the contents of your '.inc'. file into the unit under the 'interface' section keyword. Move your '.dcs' functions into the unit file under the 'implementation' section keyword and remove the 'public;' term from methods.
Character Literals
DCAL uses double quotes " to bracket character literals (eg. "c"). Delphi uses single quotes. A simple search and replace should cover all code line occurrences.
Converting older DCAL macros into D4D macros is reasonably straight forward,but somewhat tedious process. The first step is to load your old DCAL code inin a simple text editor such as Notepad++ and run several search and replace cycles to convert from DCAL to the Delphi language. Changes include:
IF Then statements.
A Delphi if statement reads:
Code: Select all
if condition then
dosomething;
end;
while Delphi If statements reads:
Code: Select all
Although the the final 'end;' can be dropped if the 'doSomething; code is only one line long, it is simpler to keep the 'end' statement in place both for search and replace and for future additions. Therefore search for 'then' and replace it with "then begin'. if condition then begin
doSomething;
end;
ELSE statements
In Delphi an ELSE statement follows this format:
Code: Select all
Search for 'Else' and replace it with 'end else begin'. If condition then begin
doSomething;
end
else begin
doSomethingElse;
end;
ELSIF statements
The 'elsif' statement does not exist in Delphi. There are two alternatives to correct it.
1. Search for 'elsif' and replace it with 'end else if '.
2. Delphi includes the 'case condition of' statement structure which reads simply in your code. You can easily assemble 'case' statements individually to replace 'elsif' cascades in your code.
The 'RETURN;' keyword alone
The return keyword used alone is used to end processing a method before reaching the method end. Delphi does not recognize the keyword 'return' and uses the keyword 'Exit' instead. Search for 'Return;' and replace with 'Exit' (without a semicolon).
The 'Return value;' keyword
Delphi uses a built in function variable called 'result' for function return values. Replace the term by searching for 'Return' and replacing it with 'result := '.
For/Do and While/Do statements
These multiple line statement structures must be enclosed with 'begin, and 'end;' keywords. For 'For/Do' and 'While/do' statements Search for the word ' do' (include a leading space to avoid 'do' contained in words) and replace with ' do begin'.
End of method code line
Datacad methods conclude with a last line reading 'end <methodname>; while Delphi methods do not include the method name. To correct this manually move to the end of each method and insert '; //' after 'end' converting the method name to a comment.
! Comments
The '!' is not recognized by Delphi for a line comment. Search for '!' and replace it with '//'.
Method argument types
A typical DCAL method declaration might read as 'function doWork (a: IN OUT integer; b: IN Integer; c: OUT integer, d: integer): integer;'. Delphi does not recognizer the key words IN & OUT and instead uses 'const' for IN and 'var' for OUT. The revised method will read 'function doWork (var a: integer; const b: Integer; var c: integer, d: integer): integer;'.
Global Unit Variables
DCAL units include provisions for persistent global variables declared in units outside of methods. Whenever a macro is closed, the current value for these variables is saved in the macro file '*.dcx'. Delphi does not include these persistent variables so another strategy must be used.
Initialization methods
Many DCAL macros include a method such as 'procedure InitData;' to set initial values for a macro. The call for this procedure is usually placed in the DCAL Macro program starting method for the macro and checks a global variable such as 'Used: boolean;' to determine if the macro variables have been given initial values. Boolean variables are initiated as false so 'Used' will equal false on its first use meaning that InitData should be called. 'InitData' will then set 'Used' to true, preventing reinitialization in later macro runs. Unfortunately, Delphi unit variables are not stored between uses so this initiation strategy will not work. Delphi provides a unit section called 'Initialization' that serves the same first time purpose. However, to preserve these variables between uses of the macro they will have to be saved and retrieved using a file. One way of doing so is discussed in PART SIX using 'ini' files.
Accessing Datacad Variables
The DCAL manual lists numerous program variables that are accessible in DCAL. D4D declares these varaibles in a in a packed record called 'savevar' in uvariables.pas,although many have modified names. requiring all variables to be checked and renamed in your revised code. To access these variables you must use the function PGsavevars; to read or write the specific field as in the following code line:.
Code: Select all
CurrentSz := PGsavevars.txtsiz;
Method Public Declarations
DCAL requires two files for each unit file used in a macro. 'UnitA.inc' and 'UnitA.dcs'. The '.inc' file contains declarations including a list of all methods which are to be accessible to other units in the program. The '.dcs' file contains the actual methods including a 'PUBLIC;' declaration ending each methods declaration line. Delphi moves all unit declarations into the same unit file under the section heading 'interface'. Copy the contents of your '.inc'. file into the unit under the 'interface' section keyword. Move your '.dcs' functions into the unit file under the 'implementation' section keyword and remove the 'public;' term from methods.
Character Literals
DCAL uses double quotes " to bracket character literals (eg. "c"). Delphi uses single quotes. A simple search and replace should cover all code line occurrences.
Joe
User since V2.4
User since V2.4