ack.7

来自「minix操作系统最新版本(3.1.1)的源代码」· 7 代码 · 共 2,091 行 · 第 1/5 页

7
2,091
字号
.MXPascal deviates from the standard in the following ways:.IP 1.Standard procedures and functions are not allowed as parameters in .MXPascal.You can obtain the same result with negligible loss of performanceby declaring some user routines like:.XS.CWfunction sine(x:real):real;begin    sine:=sin(x)end;.ft R.XE.IP 2.The standard procedures read, readln, write and writeln are implemented asword-symbols, and can therefore not be redeclared..SS "Compiler options".PPSome options of the compiler may be controlled by using '{$....}'.Each option consists of a lower case letter followed by +, \(mi or an unsignednumber.Options are separated by commas.The following options exist:.IP a+/\(miThis option switches assertions on and off.If this option is on, then code is included to test these assertionsat run time.Default +..IP c+/\(miThis option, if on, allows you to use C-type string constantssurrounded by double quotes.Moreover, a new type identifier 'string' is predefined.Default \(mi..IP d+/\(miThis option, if on, allows you to use variables of type 'long'.Default \(mi..IP i<num>.brWith this flag the setsize for a set of integers can bemanipulated.The number must be the number of bits per set.The default value is 16..IP l+/\(miIf + then code is inserted to keep track of the source line number.When this flag is switched on and off, an incorrect line number may appearif the error occurs in a part of your program for which this flag is off.Default +..IP r+/\(miIf + then code is inserted to check subrange variables againstlower and upper subrange limits.Default +..IP s+/\(miIf + then the compiler will hunt for places in your programwhere non-standard features are used, and for each place foundit will generate a warning.Default \(mi..IP t+/\(miIf + then each time a procedure is entered, the routine 'procentry' iscalled, and each time a procedure exits, the procedure 'procexit' iscalled.Both 'procentry' and 'procexit' have a 'string' as parameter.This means that when a user specifies his or her own procedures, the c-optionmust be used.Default procedures are present in the run time library.Default \(mi..IP u+/\(miIf + then the character '_' is treated like a letter,so that it may be used in identifiers.Procedure and function identifiers are not allowed to start with anunderscore because they may collide with library routine names.Default \(mi..PPSome of these flags (c, d, i, s, u, C and U) are only effective whenthey appear before the 'program' symbol.The others may be switchedon and off..PPA very powerful debugging tool is the knowledge that inaccessible statementsand useless tests are removed by the optimizer.For instance, a statement like:.XS.CWif debug then    writeln('initialization done');.ft R.XEis completely removed by the optimizer if debug is a constant withvalue false.The first line is removed if debug is a constant with value true.Of course, if debug is a variable nothing can be removed..SS "Library routines".PPThe following library of external routines for Pascal programs is available:.nf.SP.CW.ta 12nconst	bufsize = ?;type	br1 =  1..bufsize;	br2 =  0..bufsize;	br3 = -1..bufsize;	ok = -1..0;	buf = packed array[br1] of char;	alfa = packed array[1..8] of char;	string = ^packed array[1..?] of char;	filetype = file of ?;	long = ?;.SP{all routines must be declared extern}.SPfunction	argc:integer;function	argv(i:integer):string;function	environ(i:integer):string;procedure	argshift;.SPprocedure	buff(var f:filetype);procedure	nobuff(var f:filetype);procedure	notext(var f:text);procedure	diag(var f:text);procedure	pcreat(var f:text; s:string);procedure	popen(var f:text; s:string);procedure	pclose(var f:filetype);.SPprocedure	trap(err:integer);procedure	encaps(procedure p; procedure q(n:integer));.SPfunction	perrno:integer;function	uread(fd:integer; var b:buf; len:br1):br3;function	uwrite(fd:integer; var b:buf; len:br1):br3;.SPfunction	strbuf(var b:buf):string;function	strtobuf(s:string; var b:buf; len:br1):br2;function	strlen(s:string):integer;function	strfetch(s:string; i:integer):char;procedure	strstore(s:string; i:integer; c:char);.SPfunction	clock:integer;.fi.ft R.PPThis library contains some often used external routines for Pascal programs.The routines can be divided into several categories:.PP.ti -2Argument control:.RS.IP argc 10Gives the number of arguments provided when the program is called..IP argvSelects the specified argument from the argument list and returns apointer to it.This pointer is nil if the index is out of bounds (<0 or >=argc)..IP environReturns a pointer to the i-th environment string (i>=0).Returns nilif i is beyond the end of the environment list (UNIX version 7)..IP argshiftEffectively deletes the first argument from the argument list.Its function is equivalent to \fIshift\fR in the UNIX shell: argv[2] becomesargv[1], argv[3] becomes argv[2], etc.It is a useful procedure to skip optional flag arguments.Note that the matching of arguments and filesis done at the time a file is opened by a call to reset or rewrite..PP.ti -2Additional file handling routines:.IP buffTurn on buffering of a file.Not very useful, because allfiles are buffered except standard output to a terminal and diagnostic output.Input files are always buffered..IP nobuffTurn off buffering of an output file.It causes the current contents of thebuffer to be flushed..IP notextOnly useful for input files.End of line characters are not replaced by a space and character codes out ofthe ASCII range (0..127) do not cause an error message..IP diagInitialize a file for output on the diagnostic output stream (fd=2).Output is not buffered..IP pcreatThe same as rewrite(f), except that you must provide the file name yourself.The name must be zero terminated.Only text files are allowed..IP popenThe same as reset(f), except that you must provide the file name yourself.The name must be zero terminated.Only text files are allowed..IP pcloseGives you the opportunity to close files hidden in records or arrays.All other files are closed automatically..PP.ti -2String handling:.IP strbufType conversion from character array to string.It is your own responsibility that the string is zero terminated..IP strtobufCopy string into buffer until the string terminating zero byteis found or until the buffer if full, whatever comes first.The zero byte is also copied.The number of copied characters, excluding the zero byte, is returned.So ifthe result is equal to the buffer length, then the end of buffer is reachedbefore the end of string..IP strlenReturns the string length excluding the terminating zero byte..IP strfetchFetches the i-th character from a string.There is no check against the string length..IP strstoreStores a character in a string.There is no check againststring length, so this is a dangerous procedure..PP.ti -2Trap handling:.PPThese routines allow you to handle almost allthe possible error situations yourself.You may define your own trap handler, replacing thedefault handler that produces an error message and quits.You may also generate traps yourself..IP trapTrap generates the trap passed as argument (0..252).The trap numbers 128..252 may be used freely.The others are reserved..IP encapsEncapsulate the execution of \fIp\fR with the trap handler \fIq\fR.Encaps replaces the previous trap handler by \fIq\fR, calls \fIp\fRand restoresthe previous handler when \fIp\fR returns.If, during the execution of \fIp\fR, a trap occurs,then \fIq\fR is called with the trap number as parameter.For the duration of \fIq\fR the previous trap handler is restored, so thatyou may handle only some of the errors in \fIq\fR.All the other errors mustthen be raised again by a call to \fItrap\fR..brEncapsulations may be nested: you may encapsulate a procedure while executingan encapsulated routine..brJumping out of an encapsulated procedure (non-local goto) is dangerous,because the previous trap handler must be restored.Therefore, you may only jump out of procedure \fIp\fR from inside \fIq\fR andyou may only jump out of one level of encapsulation.If you want to exit several levels of encapsulation, use traps.See pc_prlib(7) for lists of trap numbersfor EM machine errors and Pascal run time system errors.Note that \fIp\fR may not have parameters..PP.ti -2UNIX system calls:.IP ureadEqual to the read system call.Its normal name is blocked by the standard Pascal routine read..IP uwriteAs above but for write(2)..IP perrnoBecause external data references are not possible in Pascal,this routine returns the global variable \fIerrno\fR, indicating the result ofthe last system call..PP.ti -2Miscellaneous:.IP clockReturn the number of ticks of user and system time consumed by the program..PPThe following program presents an example of how these routines can be used.This program is equivalent to the UNIX command cat(1)..nf.SP.CW{$c+}.CWprogram cat(input,inp,output);.CWvar	inp:text;.CW	s:string;.SP.CWfunction argc:integer; extern;.CWfunction argv(i:integer):string; extern;.CWprocedure argshift; extern;.CWfunction strlen(s:string):integer; extern;.CWfunction strfetch(s:string; i:integer):char; extern;.SP.CWprocedure copy(var fi:text);.CWvar c:char;.CWbegin reset(fi);.CW  while not eof(fi) do.CW  begin.CW    while not eoln(fi) do.CW    begin.CW      read(fi,c);.CW      write(c).CW    end;.CW    readln(fi);.CW    writeln.CW  end.CWend;.SP.CWbegin  {main}.CW  if argc = 1 then.CW    	copy(input).CW  else.CW    repeat.CW      s := argv(1);.CW      if (strlen(s) = 1) and (strfetch(s,1) = '-').CW      then copy(input).CW      else copy(inp);.CW      argshift;.CW    until argc <= 1;.CWend..fi.ft R.PPAnother example gives some idea of the way to manage trap handling:.nf.SP.CWprogram bigreal(output);.CWconst EFOVFL=4;.CWvar trapped:boolean;.CW.SP.CWprocedure encaps(procedure p; procedure q(n:integer)); extern;.CWprocedure trap(n:integer); extern;.CW.SP.CWprocedure traphandler(n:integer);.CWbegin if n=EFOVFL then trapped:=true else trap(n) end;.CW.SP.CWprocedure work;.CWvar i,j:real;.CWbegin trapped:=false; i:=1;.CW  while not trapped do.CW    begin j:=i; i:=i*2 end;.CW  writeln('bigreal = ',j);.CWend;.CW.SP.CWbegin.CW  encaps(work,traphandler);.CWend..fi.ft R.PPTwo routines may cause fatal error messages to be generated.These are:.IP pcreatRewrite error (trap 77) if the file cannot be created..IP popenReset error (trap 76) if the file cannot be opened for reading.SS References.IP [1]BSI standard BS 6192: 1982 (ISO 7185)..IP [2]A.S.Tanenbaum, J.W.Stevenson, Hans van Staveren, E.G.Keizer,"Description of a machine architecture for use with block structured languages",Informatica rapport IR-81.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?