pascal

pascal

Intro

Pascal由瑞士苏黎世联邦工业大学的Niklaus Wirth教授于六十年代末设计并创立。1971年,以电脑先驱帕斯卡pascal的名字为之命名。

Pascal语言语法严谨,层次分明,程序易写,可读性强,是第一个结构化编程语言。

Pascal有6个主要的版本,分别是Action Pascal、Unextended Pascal、Extended Pascal、Object-Oriented Extensions to Pascal、Borland Pascal和Delphi Object Pascal。

Feature

  • strong-type
  • case-insensitive
  • functional
  • object pascal provide Object-Oriented

Annotation

{comment}
(*comment*)
//coment

Basic-Syntax

主程序

program     程序名;
uses        单元;
begin
        ...statement
end.

单元

unit        单元名;
interface
...
implementation
...
initialization
...
finalization
...
end.
label       标号;
type        类型;
const       常量;
var         变量;
function    函数;
procedure   过程;

Data-Type

boolean

  • Boolean
  • ByteBool
  • WordBool
  • LongBool

integer

  • Shortint
  • Smallint
  • Integer
  • Longint
  • Int64
  • Byte
  • Word
  • Longword
  • QWord

real

  • real
  • single
  • double
  • extended
  • comp

character

  • char
  • ansichar
  • widechar

string

  • string
  • ansistring

array

  • array[from...to] of <Data-Type>

pointer

  • Pointer
  • PChar
  • PInteger
// address-of, evaluate
type PInteger = ^integer;
var a,b:integer;
var pa:;PInteger;
a := 10;
pa := @a;
b := pa^

Expression

<unary> <rvalue>;
<lvalue> <binary> <rvalue>;

operator

arithmetic

  • +
  • -
  • *
  • /
  • div整除
  • mod取余

logical

  • and
  • or
  • not
  • xor

relational

  • =
  • <>不等于
  • <
  • >
  • <=
  • >=

bitwise

  • shl
  • shr

Statement

const
<identifier>[:Data-Type]=<value>;
var
<identifier1,identifier2...>:<Data-Type>;
type
<identifier>=<Data-Type>;

assign

<var>:=<value>;

if

if <exp> then <statement>;
if <exp> then <statement1>
                else <statement2>;
case <exp> of
        con1: statement1;
        con2: statement2;
        ...
        else statement
end;

loop

for <i>:=<init-value> to <end-value> do <statement>;
for <i>:=<init-value> downto <end-value> do <statement>;
while <exp> do <statement>;
repeat <statement> until <exp>;

jump

break;
continue;
goto <label>;

function

function func-name(param1;param2...):<return Data-Type>;
var
    <var1,var2...>:<Data-Type>;
begin
        statement;
        ...
        func-name := value;
end;

Input

read<var1,var2...>;
readIn<var1,var2...>;

Output

write<con1,val1...>
wirteIn<con1,var1...>

pascal VS c

data-type

pascal c
boolean bool
shortint char
smallint short
integer int
longint long
int64 long long
single float
double double
pointer void*
pchar char*
pinteger int*

var-def and func-def

// c
int min(int a, int b)
{
    if (a < b)
    {
        return a;
    }
    return b;
}

void swap(int* a, int* b)
{
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}

int main()
{
    int a,b,c;
    a = 1;
    b = 2;
    c = min(a,b);
    printf("%d\n", c);
    return 0;
}

// pascal
function min(a: integer; b: integer): integer;
begin
    if a < b then
    begin
        min := a;
        exit;
    end;
    min := b;
end;

procedure swap(a: pinteger; b: pinteger);
var
    tmp: integer;
begin
    tmp := a^;
    a^ := b^;
    b^ := tmp; 
end;

var
    a,b,c: integer;
begin
    a := 1;
    b := 2;
    c := min(a,b);
    writeln(c);
end.

dll

function func-name(params): <return-type>; <cdecl,stdcall>; <external 'some.dll' [name 'func-name']>;

diff

pascal c
var-def var identifier: data-type;
func-def function func(params): return-type;
params delim param1;param2
address-of @a
evaluate a^
发布了130 篇原创文章 · 获赞 147 · 访问量 29万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览