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 NATURAL

WiP.TypeNATURAL History

Hide minor edits - Show changes to output

2010-03-27 16:58 by benjk -
Added lines 1-88:
[@DEFINITION MODULE NATURAL;

(* Natural Numbers *)


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


(* NATURAL type *)

TYPE
    NATURAL = OPAQUE RECORD
        value : INTEGER; (* subset of integer *)
    END;
 

(* number system *)

CONST [N] isWholeNumberType = TRUE;


(* range *)

CONST [TMIN] minValue = 0;

CONST [TMAX] maxValue = TMAX(INTEGER);


(* assignment *)
 
CONST [.] decimalPointLiterals = FALSE;

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


(* type conversions *)

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

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

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


(* unary arithmetic operations *)

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

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


(* binary arithmetic operations *)

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

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

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

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


(* relational operations *)

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

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

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


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

PROCEDURE Read( infile : File; VAR n : NATURAL );
(* Reads the textual representation of a NATURAL 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 n
  - the file status is set to any of success, outOfRange, wrongFormat, endOfLine, or endOfInput *)

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

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

END NATURAL.@]