Algol (562407), страница 6

Файл №562407 Algol (Несколько текстов для зачёта) 6 страницаAlgol (562407) страница 62015-12-04СтудИзба
Просмтор этого файла доступен только зарегистрированным пользователям. Но у нас супер быстрая регистрация: достаточно только электронной почты!

Текст из файла (страница 6)

PROCEDURE (a: ANYPTR) FINALIZE-, NEW, EMPTY;

The FINALIZE procedure can be implemented for any pointer type. The method is called at some unspecified time after an object of that type (or a base type of it) has become unreachable via other pointers (not globally anchored anymore) and before the object is deallocated.

It is not recommended to reanchor an object in its finalizer and the finalizer is not called again when the object repeatedly becomes unreachable. Multiple unreachable objects are finalized in an unspecified order.

11. Modules

A module is a collection of declarations of constants, types, variables, and procedures, together with a sequence of statements for the purpose of assigning initial values to the variables. A module constitutes a text that is compilable as a unit.

Module

=

MODULE ident ";"
[ImportList] DeclarationSequence
[BEGIN StatementSequence]
[CLOSE StatementSequence]
END ident ".".

ImportList

=

IMPORT Import {"," Import} ";".

Import

=

[ident ":="] ident.

The import list specifies the names of the imported modules. If a module A is imported by a module M and A exports an identifier x, then x is referred to as A.x within M. If A is imported as B := A, the object x must be referenced as B.x. This allows short alias names in qualified identifiers. A module must not import itself. Identifiers that are to be exported (i.e. that are to be visible in client modules) must be marked by an export mark in their declaration (see Chapter 4).

The statement sequence following the symbol BEGIN is executed when the module is added to a system (loaded), which is done after the imported modules have been loaded. It follows that cyclic import of modules is illegal. Individual exported procedures can be activated from the system, and these procedures serve as commands .

Variables declared in a module are cleared prior to the execution of the module body. This implies that all pointer or procedure typed variables are initialized to NIL.

The statement sequence following the symbol CLOSE is executed when the module is removed from the system.

Example:

MODULE Trees;

(* exports: Tree, Node, Insert, Search, Write, Init *)

IMPORT StdLog;

TYPE

Tree* = POINTER TO Node;

Node* = RECORD (* exports read-only: Node.name *)

name-: POINTER TO ARRAY OF CHAR;

left, right: Tree

END;

PROCEDURE (t: Tree) Insert* (name: ARRAY OF CHAR);

VAR p, father: Tree;

BEGIN

p := t;

REPEAT father := p;

IF name = p.name THEN RETURN END;

IF name < p.name THEN p := p.left ELSE p := p.right END

UNTIL p = NIL;

NEW(p); p.left := NIL; p.right := NIL;

NEW(p.name, LEN(name$)+1); p.name := name$;

IF name < father.name THEN father.left := p ELSE father.right := p END

END Insert;

PROCEDURE (t: Tree) Search* (name: ARRAY OF CHAR): Tree;

VAR p: Tree;

BEGIN

p := t;

WHILE (p # NIL) & (name # p.name) DO

IF name < p.name THEN p := p.left ELSE p := p.right END

END;

RETURN p

END Search;

PROCEDURE (t: Tree) Write*;

BEGIN

IF t.left # NIL THEN t.left.Write END;

StdLog.String(t.name); StdLog.Ln;

IF t.right # NIL THEN t.right.Write END

END Write;

PROCEDURE Init* (t: Tree);

BEGIN

NEW(t.name, 1);

t.name[0] := 0X; t.left := NIL; t.right := NIL

END Init;

BEGIN

StdLog.String("Trees loaded"); StdLog.Ln

CLOSE

StdLog.String("Trees removed"); StdLog.Ln

END Trees.

Appendix A: Definition of Terms

Character types

SHORTCHAR, CHAR

Integer types

BYTE, SHORTINT, INTEGER, LONGINT

Real types

SHORTREAL, REAL

Numeric types

integer types, real types

String types

Shortstring, String

Basic types

BOOLEAN, SET, character types, numeric types

Same types

Two variables a and b with types Ta and Tb are of the same type if

  1. Ta and Tb are both denoted by the same type identifier, or

  2. Ta is declared in a type declaration of the form Ta = Tb, or

  3. a and b appear in the same identifier list in a variable, record field, or formal parameter declaration.

Equal types

Two types Ta and Tb are equal if

  1. Ta and Tb are the same type, or

  2. Ta and Tb are open array types with equal element types, or

  3. Ta and Tb are procedure types whose formal parameter lists match.

  4. Ta and Tb are pointer types with equal base types.

Matching formal parameter lists

Two formal parameter lists match if

  1. they have the same number of parameters, and

  2. they have either equal function result types or none, and

  3. parameters at corresponding positions have equal types, and

  4. parameters at corresponding positions are both either value, IN, OUT, or VAR parameters.

Type inclusion

Numeric and character types include (the values of) smaller types of the same class according to the following hierarchies:

REAL >= SHORTREAL >= LONGINT >= INTEGER >= SHORTINT >= BYTE

CHAR >= SHORTCHAR

Type extension (base type)

Given a type declaration Tb = RECORD (Ta) ... END, Tb is a direct extension of Ta, and Ta is a direct base type of Tb. A type Tb is an extension of a type Ta (Ta is a base type of Tb) if

  1. Ta and Tb are the same types, or

  2. Tb is a direct extension of an extension of Ta, or

  3. Ta is of type ANYREC.

If Pa = POINTER TO Ta and Pb = POINTER TO Tb, Pb is an extension of Pa (Pa is a base type of Pb) if Tb is an extension of Ta.

Assignment compatible

An expression e of type Te is assignment compatible with a variable v of type Tv if one of the following conditions hold:

  1. Te and Tv are equal and neither abstract or extensible record nor open array types;

  2. Te and Tv are numeric or character types and Tv includes Te;

  3. Te and Tv are pointer types and Te is an extension of Tv;

  4. Tv is a pointer or a procedure type and e is NIL;

  5. Tv is an integer type and e is a constant expression whose value is contained in Tv;

  6. Tv is an array of CHAR, Te is String or Shortstring, and LEN(e) < LEN(v);

  7. Tv is an array of SHORTCHAR, Te is Shortstring, and LEN(e) < LEN(v);

  8. Tv is a procedure type and e is the name of a procedure whose formal parameters match those of Tv.

Array compatible

An actual parameter a of type Ta is array compatible with a formal parameter f of type Tf if

  1. Tf and Ta are equal types, or

  2. Tf is an open array, Ta is any array, and their element types are array compatible, or

  3. Tf is an open array of CHAR and Ta is String, or

  4. Tf is an open array of SHORTCHAR and Ta is Shortstring.

Parameter compatible

An actual parameter a of type Ta is parameter compatible with a formal parameter f of type Tf if

  1. Tf and Ta are equal types, or

  2. f is a value parameter and Ta is assignment compatible with Tf, or

  3. f is an IN or VAR parameter and Tf and Ta are record types and Ta is an extension of Tf, or

  4. f is an OUT parameter and Tf and Ta are pointer types and Tf is an extension of Ta.

Expression compatible

For a given operator, the types of its operands are expression compatible if they conform to the following table. The first matching line gives the correct result type. Type T1 must be an extension of type T0:

Operator

First operand

Second operand

Result type

+ - * DIV MOD

<= INTEGER

<= INTEGER

INTEGER

integer type

integer type

LONGINT

+ - * /

numeric type

numeric type

REAL

SET

SET

SET

+

Shortstring

Shortstring

Shortstring

string type

string type

String

OR & ~

BOOLEAN

BOOLEAN

BOOLEAN

= # < <= > >=

numeric type

numeric type

BOOLEAN

character type

character type

BOOLEAN

string type

string type

BOOLEAN

= #

BOOLEAN

BOOLEAN

BOOLEAN

SET

SET

BOOLEAN

NIL, pointer type T0 or T1

NIL, pointer type T0 or T1

BOOLEAN

procedure type T, NIL

procedure type T, NIL

BOOLEAN

IN

integer type

SET

BOOLEAN

IS

T0 type

T1

BOOLEAN

Appendix B: Syntax of Component Pascal

Module

=

MODULE ident ";" [ImportList] DeclSeq [BEGIN StatementSeq] [CLOSE StatementSequence] END ident ".".

ImportList

=

IMPORT [ident ":="] ident {"," [ident ":="] ident} ";".

DeclSeq

=

{ CONST {ConstDecl ";" } | TYPE {TypeDecl ";"} | VAR {VarDecl ";"}}
{ProcDecl ";" | ForwardDecl ";"}.

ConstDecl

=

IdentDef "=" ConstExpr.

TypeDecl

=

IdentDef "=" Type.

VarDecl

=

IdentList ":" Type.

ProcDecl

=

PROCEDURE [Receiver] IdentDef [FormalPars]
["," NEW] ["," (ABSTRACT | EMPTY | EXTENSIBLE)]
[";" DeclSeq [BEGIN StatementSeq] END ident].

ForwardDecl

=

PROCEDURE "^" [Receiver] IdentDef [FormalPars].

FormalPars

=

"(" [FPSection {";" FPSection}] ")" [":" Type].

FPSection

=

[VAR | IN | OUT] ident {"," ident} ":" Type.

Receiver

=

"(" [VAR | IN] ident ":" ident ")".

Type

=

Qualident
| ARRAY [ConstExpr {"," ConstExpr}] OF Type
| [ABSTRACT | EXTENSIBLE | LIMITED] RECORD ["("Qualident")"] FieldList {";" FieldList} END
| POINTER TO Type
| PROCEDURE [FormalPars].

FieldList

=

[IdentList ":" Type].

StatementSeq

=

Statement {";" Statement}.

Statement

=

[ Designator ":=" Expr
| Designator ["(" [ExprList] ")"]
| IF Expr THEN StatementSeq {ELSIF Expr THEN StatementSeq} [ELSE StatementSeq] END
| CASE Expr OF Case {"|" Case} [ELSE StatementSeq] END
| WHILE Expr DO StatementSeq END
| REPEAT StatementSeq UNTIL Expr
| FOR ident ":=" Expr TO Expr [BY ConstExpr] DO StatementSeq END
| LOOP StatementSeq END
| WITH Guard DO StatementSeq {"|" Guard DO StatementSeq} [ELSE StatementSeq] END
| EXIT | RETURN [Expr] ].

Case

=

[CaseLabels {"," CaseLabels} ":" StatementSeq].

CaseLabels

=

ConstExpr [".." ConstExpr].

Guard

=

Qualident ":" Qualident.

ConstExpr

=

Expr.

Expr

=

SimpleExpr [Relation SimpleExpr].

SimpleExpr

=

["+" | "-"] Term {AddOp Term}.

Term

=

Factor {MulOp Factor}.

Factor

=

Designator | number | character | string | NIL | Set | "(" Expr ")" | " ~ " Factor.

Set

=

"{" [Element {"," Element}] "}".

Element

=

Expr [".." Expr].

Relation

=

"=" | "#" | "<" | "<=" | ">" | ">=" | IN | IS.

AddOp

=

"+" | "-" | OR.

MulOp

=

" * " | "/" | DIV | MOD | "&".

Designator

=

Qualident {"." ident | "[" ExprList "]" | " ^ " | "$" | "(" Qualident ")" | "(" [ExprList] ")"}.

ExprList

=

Expr {"," Expr}.

IdentList

=

IdentDef {"," IdentDef}.

Qualident

=

[ident "."] ident.

IdentDef

=

ident [" * " | "-"].

Appendix C: Domains of Basic Types

Type

Domain

BOOLEAN

FALSE, TRUE

SHORTCHAR

0X .. 0FFX

CHAR

0X .. 0FFFFX

BYTE

-128 .. 127

SHORTINT

-32768 .. 32767

INTEGER

-2147483648 .. 2147483647

LONGINT

-9223372036854775808 .. 9223372036854775807

SHORTREAL

-3.4E38 .. 3.4E38,
INF (32-bit IEEE format)

REAL

-1.8E308 .. 1.8E308,
INF (64-bit IEEE format)

SET

set of 0 .. 31

Appendix D: Mandatory Requirements for Environment

The Component Pascal definition implicitly relies on three fundamental assumptions.

  1. There exists some run-time type information that allows to check the dynamic type of an object. This is necessary to implement type tests and type guards.

  2. There is no DISPOSE procedure. Memory cannot be deallocated manually, since this would introduce the safety problem of dangling pointers, i.e. premature deallocation. Except for those embedded systems where no dynamic memory is used or where it can be allocated once and never needs to be released, an automatic garbage collector is required.

  3. Modules and at least their exported procedures (commands) and exported types must be retrievable dynamically. If necessary, this may cause modules to be loaded. The programming interface used to load modules or to access the mentioned meta information is not defined by the language, but the language compiler needs to preserve this information when generating code.

Except for fully linked applications where no modules will ever be added at run-time, a linking loader for modules is required. Embedded systems are important examples of applications that can be fully linked.

Характеристики

Тип файла
Документ
Размер
815,5 Kb
Тип материала
Высшее учебное заведение

Список файлов учебной работы

Свежие статьи
Популярно сейчас
Как Вы думаете, сколько людей до Вас делали точно такое же задание? 99% студентов выполняют точно такие же задания, как и их предшественники год назад. Найдите нужный учебный материал на СтудИзбе!
Ответы на популярные вопросы
Да! Наши авторы собирают и выкладывают те работы, которые сдаются в Вашем учебном заведении ежегодно и уже проверены преподавателями.
Да! У нас любой человек может выложить любую учебную работу и зарабатывать на её продажах! Но каждый учебный материал публикуется только после тщательной проверки администрацией.
Вернём деньги! А если быть более точными, то автору даётся немного времени на исправление, а если не исправит или выйдет время, то вернём деньги в полном объёме!
Да! На равне с готовыми студенческими работами у нас продаются услуги. Цены на услуги видны сразу, то есть Вам нужно только указать параметры и сразу можно оплачивать.
Отзывы студентов
Ставлю 10/10
Все нравится, очень удобный сайт, помогает в учебе. Кроме этого, можно заработать самому, выставляя готовые учебные материалы на продажу здесь. Рейтинги и отзывы на преподавателей очень помогают сориентироваться в начале нового семестра. Спасибо за такую функцию. Ставлю максимальную оценку.
Лучшая платформа для успешной сдачи сессии
Познакомился со СтудИзбой благодаря своему другу, очень нравится интерфейс, количество доступных файлов, цена, в общем, все прекрасно. Даже сам продаю какие-то свои работы.
Студизба ван лав ❤
Очень офигенный сайт для студентов. Много полезных учебных материалов. Пользуюсь студизбой с октября 2021 года. Серьёзных нареканий нет. Хотелось бы, что бы ввели подписочную модель и сделали материалы дешевле 300 рублей в рамках подписки бесплатными.
Отличный сайт
Лично меня всё устраивает - и покупка, и продажа; и цены, и возможность предпросмотра куска файла, и обилие бесплатных файлов (в подборках по авторам, читай, ВУЗам и факультетам). Есть определённые баги, но всё решаемо, да и администраторы реагируют в течение суток.
Маленький отзыв о большом помощнике!
Студизба спасает в те моменты, когда сроки горят, а работ накопилось достаточно. Довольно удобный сайт с простой навигацией и огромным количеством материалов.
Студ. Изба как крупнейший сборник работ для студентов
Тут дофига бывает всего полезного. Печально, что бывают предметы по которым даже одного бесплатного решения нет, но это скорее вопрос к студентам. В остальном всё здорово.
Спасательный островок
Если уже не успеваешь разобраться или застрял на каком-то задание поможет тебе быстро и недорого решить твою проблему.
Всё и так отлично
Всё очень удобно. Особенно круто, что есть система бонусов и можно выводить остатки денег. Очень много качественных бесплатных файлов.
Отзыв о системе "Студизба"
Отличная платформа для распространения работ, востребованных студентами. Хорошо налаженная и качественная работа сайта, огромная база заданий и аудитория.
Отличный помощник
Отличный сайт с кучей полезных файлов, позволяющий найти много методичек / учебников / отзывов о вузах и преподователях.
Отлично помогает студентам в любой момент для решения трудных и незамедлительных задач
Хотелось бы больше конкретной информации о преподавателях. А так в принципе хороший сайт, всегда им пользуюсь и ни разу не было желания прекратить. Хороший сайт для помощи студентам, удобный и приятный интерфейс. Из недостатков можно выделить только отсутствия небольшого количества файлов.
Спасибо за шикарный сайт
Великолепный сайт на котором студент за не большие деньги может найти помощь с дз, проектами курсовыми, лабораторными, а также узнать отзывы на преподавателей и бесплатно скачать пособия.
Популярные преподаватели
Добавляйте материалы
и зарабатывайте!
Продажи идут автоматически
6473
Авторов
на СтудИзбе
304
Средний доход
с одного платного файла
Обучение Подробнее