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

Type Sample UINT

DEFINITION MODULE SampleUINT;

(* Sample definition of an unsigned integer type *) 


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


(* SampleUINT type *)

TYPE
    SampleUINT = OPAQUE RECORD ("Z-Type")
        value : ARRAY 2 OF OCTET; (* 16 bits *)
    END; 


(* range *) 

CONST [TMIN] minValue = 0; 

CONST [TMAX] maxValue = 65535;


(* assignment *)

PROCEDURE [:=] assign ( VAR assignTo : SampleUINT; literal : ARRAY OF CHAR ); 


(* type conversions *)

PROCEDURE [::] toCARD  ( a : SampleUINT ) : CARDINAL; 

PROCEDURE [::] toINT ( a : SampleUINT ) : INTEGER; 

PROCEDURE [::] toREAL ( a : SampleUINT ) : REAL;


(* experimental *)

PROCEDURE [::] fromCARD  ( n : CARDINAL ) : SampleUINT; 

PROCEDURE [::] fromINT  ( n : INTEGER ) : SampleUINT;


(* monadic arithmetic operations *) 

PROCEDURE [ABS] abs ( a : SampleUINT ) : SampleUINT; 

PROCEDURE [ODD] odd ( a : SampleUINT ) : BOOLEAN; 


(* dyadic arithmetic operations *) 

PROCEDURE [+] add ( a, b : SampleUINT ) : SampleUINT; 

PROCEDURE [-] sub ( a, b : SampleUINT ) : SampleUINT; 

PROCEDURE [*] multiply ( a, b : SampleUINT ) : SampleUINT; 

PROCEDURE [DIV] divide ( a, b : SampleUINT ) : SampleUINT; 

PROCEDURE [MOD] modulo ( a, b : SampleUINT ) : SampleUINT; 


(* relational operations *) 

PROCEDURE [=] isEqual ( a, b : SampleUINT ) : BOOLEAN; 

PROCEDURE [<] isLess ( a, b : SampleUINT ) : BOOLEAN; 

PROCEDURE [>] isGreater ( a, b : SampleUINT ) : BOOLEAN; 


(* scalar conversion primitives *)

CONST stdCARD = TRUE; (* experimental *)

CONST digitCapacity = 4; (* total digits in native radix *)

PROCEDURE [TO] toSXF ( n : SampleUINT; VAR s : ARRAY OF CHAR );
(* convert a SampleUINT value to a string in scalar exchange format *)

PROCEDURE [FROM] fromSXF ( VAR n : SampleUINT; s : ARRAY OF CHAR );
(* convert a string in scalar exchange format to a SampleUINT value *)


(* IO operations, bound to READ, WRITE and WRITEF *)

PROCEDURE Read( infile : File; VAR b : SampleUINT );
(* Reads the textual representation of a SampleUINT value in simple format from input 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 b
   - the file status is set to any of success, outOfRange, wrongFormat, endOfLine, or endOfInput *)

PROCEDURE Write( outfile : File; b : SampleUINT );
(* Writes the textual representation of value b in simple format to output stream outfile *)

PROCEDURE WriteF( outfile : File; fmtStr : ARRAY OF CHAR;
                  items : CARDINAL; VARIADIC v[items] OF b : SampleUINT );
(* Writes a formatted textual representation of one or more SampleUINT values to output stream outfile. The
   value of parameter items is calculated and inserted automatically. The output format is determined by fmtStr. *)

END SampleUINT.