Oberon (на английском языке), страница 4
Описание файла
PDF-файл из архива "Oberon (на английском языке)", который расположен в категории "". Всё это находится в предмете "языки программирования" из 7 семестр, которые можно найти в файловом архиве МГУ им. Ломоносова. Не смотря на прямую связь этого архива с МГУ им. Ломоносова, его также можно найти и в других разделах. .
Просмотр PDF-файла онлайн
Текст 4 страницы из PDF
Itcannot be a predefined procedure. The result type of a procedure can be neither a record nor anarray.Examples of procedure declarations:PROCEDURE ReadInt(VAR x: INTEGER);VAR i : INTEGER; ch: CHAR;BEGIN i := 0; Read(ch);WHILE ("0" <= ch) & (ch <= "9") DOi := 10*i + (ORD(ch)-ORD("0")); Read(ch)END ;x := iEND ReadInt14PROCEDURE WriteInt(x: INTEGER); (* 0 <= x < 10^5 *)VAR i: INTEGER;buf: ARRAY 5 OF INTEGER;BEGIN i := 0;REPEAT buf[i] := x MOD 10; x := x DIV 10; INC(i) UNTIL x = 0;REPEAT DEC(i); Write(CHR(buf[i] + ORD("0"))) UNTIL i = 0END WriteIntPROCEDURE log2(x: INTEGER): INTEGER;VAR y: INTEGER; (*assume x>0*)BEGIN y := 0;WHILE x > 1 DO x := x DIV 2; INC(y) END ;RETURN yEND log210.2. Predefined proceduresThe following table lists the predefined procedures.
Some are generic procedures, i.e. they applyto several types of operands. v stands for a variable, x and n for expressions, and T for a type.Function procedures:NameArgument typeResult typeFunctionABS(x)numeric typetype of xabsolute valueODD(x)integer typeBOOLEANx MOD 2 = 1CAP(x)CHARCHARcorresponding capital letterASH(x, n)x, n: integer typeLONGINTx * 2^n, arithmetic shiftLEN(v, n)v: arrayn: integer typeLONGINTthe length of v in dimension nLEN(v)is equivalent with LEN(v, 0)MAX(T)T = basic typeT = SETTINTEGERmaximum value of type Tmaximum element of setsMIN(T)T = basic typeT = SETTINTEGERminimum value of type T0SIZE(T)T = any typeinteger typeno.
of bytes required by TType conversion procedures:NameArgument typeResult typeFunctionORD(x)CHARINTEGERordinal number of xCHR(x)integer typeCHARcharacter with ordinal number xSHORT(x)LONGINTINTEGERidentityINTEGERSHORTINTLONGREALREAL(truncation possible)SHORTINTINTEGERidentityINTEGERLONGINTREALLONGREALreal typeLONGINTLONG(x)ENTIER(x)largest integer not greater than x15Proper procedures:NameArgument typesFunctionINC(v)integer typev := v+1INC(v, x)integer typev := v+xDEC(v)integer typev := v-1DEC(v, x)integer typev := v-xINCL(v, x)v: SET; x: integer type v := v + {x}EXCL(v, x)v: SET; x: integer type v := v - {x}COPY(x, v)x: character array, stringv := xv: character arrayNEW(v)pointer typeallocate v^HALT(x)integer constantterminate program executionThe second parameter of INC and DEC may be omitted, in which case its default value is 1.
InHALT(x), x is a parameter whose interpretation is left to the underlying system implementation.11. ModulesA module is a collection of declarations of constants, types, variables, and procedures, and asequence of statements for the purpose of assigning initial values to the variables. A moduletypically constitutes a text that is compilable as a unit.ModuleImportListImport= MODULE ident ";" [ImportList] DeclarationSequence[BEGIN StatementSequence] END ident "." .= IMPORT import {"," import} ";" .= ident [":=" ident].The import list specifies the modules of which the module is a client. If an identifier x is exportedfrom a module M, and if M is listed in a module's import list, then x is referred to as M.x.
If theform "M := M1" is used in the import list, that object declared within M1 is referenced as M.x .Identifiers that are to be visible in client modules, i.e. outside the declaring module, must bemarked by an export mark in their declaration.The statement sequence following the symbol BEGIN is executed when the module is added to asystem (loaded). Individual (parameterless) procedures can thereafter be activated from thesystem, and these procedures serve as commands.Example:MODULE Out;(*exported procedures: Write, WriteInt, WriteLn*)IMPORT Texts, Oberon;VAR W: Texts.Writer;PROCEDURE Write*(ch: CHAR);BEGIN Texts.Write(W, ch)END ;PROCEDURE WriteInt*(x, n: LONGINT);VAR i: INTEGER; a: ARRAY 16 OF CHAR;BEGIN i := 0;IF x < 0 THEN Texts.Write(W, "-"); x := -x END ;REPEAT a[i] := CHR(x MOD 10 + ORD("0")); x := x DIV 10; INC(i) UNTIL x = 0;REPEAT Texts.Write(W, " "); DEC(n) UNTIL n <= i;16REPEAT DEC(i); Texts.Write(W, a[i]) UNTIL i = 0END WriteInt;PROCEDURE WriteLn*;BEGIN Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)END WriteLn;BEGIN Texts.OpenWriter(W)END Out.12.
The Module SYSTEMThe module SYSTEM contains definitions that are necessary to program low-level operationsreferring directly to resources particular to a given computer and/or implementation. Theseinclude for example facilities for accessing devices that are controlled by the computer, andfacilities to break the data type compatibility rules otherwise imposed by the language definition. Itis recommended to restrict their use to specific low-level modules. Such modules are inherentlynon-portable, but easily recognized due to the identifier SYSTEM appearing in their import lists.The subsequent definitions are applicable to most modern computers; however, individualimplementations may include in this module definitions that are particular to the specific,underlying computer.Module SYSTEM exports the data type BYTE.
No representation of values is specified. Instead,certain compatibility rules with other types are given:1. The type BYTE is compatible with CHAR and SHORTINT.2. If a formal parameter is of type ARRAY OF BYTE, then the corresponding actualparameter may be of any type.The procedures contained in module SYSTEM are listed in the following tables. They correspondto single instructions compiled as in-line code. v stands for a variable, x, y, a, and n forexpressions, and T for a type.Function procedures:NameArgument typesResult typeFunctionADR(v)anyLONGINTaddress of variable vBIT(a, n)a: LONGINTn: integer typeBOOLEANbit n of Mem[a]LSH(x, n)x: integer type or SETtype of xlogical shiftrotationn: integer typeROT(x, n)x: integer type or SETn: integer typetype of xVAL(T, x)T, x: any typex interpreted as of type TTProper procedures:NameArgument typesFunctionGET(a, v)a: LONGINT; v: any basic typev := Mem[a]PUT(a, x)a: LONGINT; x: any basic typeMem[a] := xNEW(v, n)v: any pointer typen: integer typeallocate storage block of n bytesassign its address to v.