Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

CARD 32

DEFINITION MODULE CARD32 [ZTYPE];

(* 32-bit Unsigned Integer Numbers *) 


FROM FileIO IMPORT File; (* required for IO *)


(* CARD32 type *)

TYPE
    CARD32 = OPAQUE RECORD
        value : ARRAY 4 OF OCTET; (* 32 bits *)
    END; (* CARD32 *)


(* Z-Type bindings to operators, pervasives and primitives: *)


(* Range *) 

CONST [TMIN] minValue = 0;
(* Smallest value of type CARD32.
   This value is bound to TMIN for type CARD32. *) 

CONST [TMAX] maxValue = 4294967295;
(* Largest value of type CARD32.
   This value is bound to TMAX for type CARD32. *)


(* Literal assignment *)

PROCEDURE [:=] assign ( VAR assignTo : CARD32; literal : ARRAY OF CHAR );
(* Converts string literal to a CARD32 value and assigns it to assignTo.
   This procedure is bound to the := operator for literal assignment. *)


(* Type conversions *)

PROCEDURE [::] toCARD  ( n : CARD32 ) : CARDINAL;
(* Converts CARD32 value n to a CARDINAL value and returns it. This
   function is bound to the :: operator for CARD32 to CARDINAL conversion.*)

PROCEDURE [::] toINT ( n : CARD32 ) : INTEGER; 
(* Converts CARD32 value n to an INTEGER value and returns it. This
   function is bound to the :: operator for CARD32 to INTEGER conversion. *)

PROCEDURE [::] fromCARD  ( n : CARDINAL ) : CARD32;
(* Converts CARDINAL value n to a CARD32 value and returns it. This function
   is bound to the :: operator for CARDINAL to CARD32 conversion. *)

PROCEDURE [::] fromINT  ( n : INTEGER ) : CARD32;
(* Converts INTEGER value n to a CARD32 value and returns it. This function
   is bound to the :: operator for INTEGER to CARD32 conversion. *)


(* Monadic arithmetic operations *) 

PROCEDURE [ABS] abs ( n : CARD32 ) : CARD32;
(* Returns the absolute value of CARD32 value n.
   This function is bound to pervasive function ABS for type CARD32. *)

PROCEDURE [ODD] odd ( n : CARD32 ) : BOOLEAN;
(* Returns TRUE if CARD32 value n is odd, otherwise FALSE.
   This function is bound to pervasive function ODD for type CARD32. *)


(* Dyadic arithmetic operations *) 

PROCEDURE [+] add ( n1, n2 : CARD32 ) : CARD32;
(* Adds CARD32 values n1 and n2 and returns the result.
   This function is bound to the + operator for type CARD32. *)

PROCEDURE [-] sub ( n1, n2 : CARD32 ) : CARD32;
(* Subtracts CARD32 value n2 from n1 and returns the result.
   This function is bound to the - operator for type CARD32. *)

PROCEDURE [*] multiply ( n1, n2 : CARD32 ) : CARD32;
(* Multiplies CARD32 values n1 and n2 and returns the result.
   This function it bound to the * operator for type CARD32. *)

PROCEDURE [/] divide ( n1, n2 : CARD32 ) : CARD32;
(* Divives CARD32 value n1 by n2 and returns the result.
   This function is bound to the / operator for type CARD32. *)


(* Relational operations *) 

PROCEDURE [=] isEqual ( n1, n2 : CARD32 ) : BOOLEAN;
(* Returns TRUE if CARD32 values n1 and n2 are equal, otherwise FALSE.
   This function is bound to operators = and # for type CARD32. *)

PROCEDURE [<] isLess ( n1, n2 : CARD32 ) : BOOLEAN;
(* Returns TRUE if CARD32 value n1 is less than n2, otherwise FASLE.
   This function is bound to operators < and >= for type CARD32. *)

PROCEDURE [>] isGreater ( n1, n2 : CARD32 ) : BOOLEAN;
(* Returns TRUE if CARD32 value n1 is greater than n2, otherwise FALSE.
   This function is bound to operators > and <= for type CARD32. *)


(* Scalar conversion primitives *)

CONST digitCapacity = 8; (* maximum digits in native format *)

PROCEDURE [TO] toSXF ( n : CARD32; VAR s : ARRAY OF CHAR );
(* Converts CARD32 value n to a string in scalar exchange format.
   This procedure is used to synthesise conversions to other scalar types
   when no direct conversion path exists. *)

PROCEDURE [FROM] fromSXF ( VAR n : CARD32; CONST s : ARRAY OF CHAR );
(* Converts a string in scalar exchange format to a CARD32 value.
   This procedure is used to synthesise conversions from other scalar types
   when no direct conversion path exists. *)


(* IO operations *)

PROCEDURE Read( infile : File; VAR n : CARD32 );
(* Reads the textual representation of a CARD32 value from stream infile
   - any leading whitespace is skipped
   - any remaining characters that are part of the numeral being read are
     removed from infile
   - the numeric value of the numeral string read is assigned to the variable
     passed in for n
   - the file status is set to any of:
     success, outOfRange, wrongFormat, endOfLine, or endOfInput. This
   procedure is substituted for invocations of READ with a CARD32 argument. *)

PROCEDURE Write( outfile : File; n : CARD32 );
(* Writes the textual representation of value n to stream outfile. This
   procedure is substituted for invocations of WRITE with a CARD32 argument.*)

PROCEDURE WriteF ( outfile      : File;
                   CONST fmtStr : ARRAY OF CHAR;
                   items        : VARIADIC OF CARD32 );
(* Writes a formatted textual representation of one or more CARD32 values to
   output stream outfile. The output format is determined by fmtStr. This
   procedure is substituted for invocations of WRITEF with one or more
   CARD32 arguments. *)

END CARD32.