[Allwinner ClassA20 class library Analysis] 2. free pascal syntax and structure analysis, allwinnerclassa20

Source: Internet
Author: User
Tags array to string

[Allwinner ClassA20 class library Analysis] 2. free pascal syntax and structure analysis, allwinnerclassa20
The previous section introduced the general development procedure of Lazarus. If you are not familiar with the pascal Language, you may not understand what the pascal Code should contain or what role it plays, this will briefly introduce the syntax and the structure of the code file. Of course, I just want to describe what we usually use. If you want to learn more about pascal, please refer to the book recommended at the end of this section.

There are only two types of free pascal code files:. lpr and. pas (or. pp). lpr files are engineering files, and pas or pp files are unit files. A complete pascal project must contain an lpr file. Let's take a look at what projects created by Lazarus by default include. Open Lazarus, click Save all, select the path, and click OK to save it as the default file name.


Project1.lpr-project file, the entry of the entire program, which generally does not need to be manually modified;

Project1.res-project resource file, save version, String, etc., do not need to be manually modified;

Project1.lpi-project management file, which stores Project information. Lazarus manages all files in this file as a project and does not need to be manually modified;

Project1.lps-information about the closure of the IDE environment, including the last position of various editing forms, without manual modification;

Project1.ico-icon file, which is compiled as the icon displayed after the executable file and does not need to be manually modified;

Unit1.pas-form source code file, which can be manually added;

Unit1.dita-form attribute code file, which does not need to be manually modified;

As you can see, there is only one file where you really need to write code, unit1.pas. Next we will focus on analyzing the structure of this file.

Note: When saving the unit file, you can also select the extension. pp .. The relationship between pp and. pas files:. pp is the. pas file. Delphi's default source code file is. pas, while Lazarus's source code file is. pp. Lazarus supports these two extensions to be compatible with Delphi, so there is no difference except different extensions.

The following is the code of an pas file. I have made some comments to clearly understand the functions of each part.


Pascal's code files, regardless of. h or. c, are included in the. pas file, making the file structure more concise. Functions or definitions that need to be opened to the outside world can be written on top of implementation. The code editor will automatically prompt visible items after writing. Invisible functions will not be displayed.

The structure is almost the same. Let's take a look at pascal's syntax. The syntax is similar to the C language, except that {,} in C is replaced by begin and end. Changing the name and type of the variable definition can even be pascal. Well, such a statement is too irresponsible and will be despised. But there is really little difference in syntax. The biggest difference is that pascal has a class. The class description is easier to accept. This section continues with the basic East

West.

Pascal is case-insensitive. The following is my preferred method for reference only.

1. Data Type:

C Pascal
Int Integer
Short ShortInt
Long LongInt
Unsigned char Byte
Unsigned short Word
Unsigned long LongWord or DWORD
Float Single
Double Double or Real
  Boolean
* P Pointer
& P @ P or Pointer (p );

There may be errors when you think about how much to write. Please add and correct them.

2. operators:

C Pascal
= : =
+ +
- -
* *
/ Div
% Mod
< Shl
> Shr
& And
| Or
! Not
^ Xor

There may be errors when you think about how much to write. Please add and correct them.

3. Loop statement:

C Pascal
For (I = 0; I <10; I ++) For I: = 0 to 9 do
While (1 )...; While (1) do ...;
Do... while (1 ); Repeat... until (1 );

There may be errors when you think about how much to write. Please add and correct them.

4. Comparison statement:

C Pascal // There Are No parentheses.
If (A = B )...; If A = B then ...;
If (A | B )...; If A or B then ...;
If (A & B )...; If A and B then ...;
Switch ()
{
Case 1:
Break;
Case 2:
Break;
Default:
Break;
}
Case A
1 :;
2 :;
Else

End;

There may be errors when you think about how much to write. Please add and correct them.

5. Function Definition:

C Pascal
Void fun (); Procedure fun;
Int fun (); Function fun: Integer;

There may be errors when you think about how much to write. Please add and correct them.

6. Focus on String:

C does not have the string type. Only the character array char [] ends with '\ 0.

Pascal is much more powerful in string operations, and can define arrays array of Char or array of Byte;

There is also a String type. String can be used as an error array or as a String. You can change the length of SetLength (String, 10) at any time ), you do not need to manually release the function. When the function lifecycle ends, it is automatically released (except in struct ).

Array to String: String (Byte array or Char array)

String to array: String = @ Byte array [0];

Subscript: Char: = String [1]; or Byte: = Ord (String [1]);

String addition: String: = String1 + String2;

String insertion: Insert (String, SubString, Index );

String deletion: Delete (String, Index, Count );

...

Therefore, I generally use the String type as the Buffer in the program for transmission or processing, and the operation is very convenient.

There may be errors when you think about how much to write. Please add and correct them.

7. Focus on Exception Handling:

C does not handle exceptions.

Pascal has try, and try has two types:

Try

// Executed code

Except

// Exception Handling

End;

When any execution failure occurs in the execution part of the code, try can not cause the entire program to crash and stop running, but can jump to the exception handling part for error handling, or prompt the user friendly.

Try

// Execute the code

Finally

// Force Execution part

End;

Whether executing any interrupt or exit statement in the code execution part, the finally part will be executed, which is generally used to release resources or other necessary processing.

The two can be used in combination:

Try

Try

// Code

Except

// Exception Handling

End;

// Code

Finally

// Force Execution

End;

This is basically the common things. The ClassA20 class library will be officially introduced in the next section, so stay tuned.

To download the ClassA20 encapsulation class library file, visit: https://github.com/tjcfeng/classa20.
 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.