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

Spec BCD

BCD

DEFINITION MODULE BCD [RTYPE];

(* Single Precision Binary Coded Decimals *) 


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


(* BCD type *)

TYPE
    BCD = OPAQUE RECORD
        value : ARRAY 8 OF OCTET; (* 64 bits *)
    END; (* BCD *)


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


(* Range *) 

CONST [TMIN] minValue = -9.9999999999999E+99;
(* Smallest value of type BCD. This value is bound to TMIN for type BCD. *) 

CONST [TMAX] maxValue = +9.9999999999999E+99;
(* Largest value of type BCD. This value is bound to TMAX for type BCD. *)


(* Literal assignment *)

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


(* Type conversions *)

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

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

PROCEDURE [::] toREAL ( a : BCD ) : REAL;
(* Converts BCD value a to a REAL value and returns it.
   This function is bound to the :: operator for BCD to REAL conversion. *)


(* Monadic arithmetic operations *) 

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

PROCEDURE [NEG] neg ( a : BCD ) : BCD;
(* Returns the sign reversed value of BCD value a.
   This function is bound to the unary minus operator for type BCD. *)


(* Dyadic arithmetic operations *) 

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

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

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

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


(* Relational operations *) 

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

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

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


(* Scalar conversion primitives *)

CONST digitCapacity = 15; (* maximum digits *)

PROCEDURE [TO] toSXF ( b : BCD; VAR s : ARRAY OF CHAR );
(* Converts a BCD value 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 b : BCD; CONST s : ARRAY OF CHAR );
(* Converts a string in scalar exchange format to a BCD 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 b : BCD );
(* Reads the textual representation of a BCD value 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. This
     procedure is substituted for invocations of READ with a BCD argument. *)

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

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

END BCD.