IPConvertImpl.mesa
Last edited by:
Doug Wyatt, April 29, 1983 2:17 pm
DIRECTORY
IPBasic USING [Any, Identifier, Integer, maxInteger, Number, Operator, Vector, zero],
IPConvert USING [IntegerToReal, NumberToInteger, NumberToReal],
IPErrors USING [MasterError, MasterWarning],
Real USING [Fix, RealException];
IPConvertImpl: CEDAR PROGRAM
IMPORTS IPConvert, IPErrors, Real
EXPORTS IPConvert
= BEGIN OPEN IPConvert, IPBasic;
RealToInteger: PUBLIC PROC[r: REAL] RETURNS[Integer] = {
Fix: PROC[r: REAL] RETURNS[INT] = { RETURN[Real.Fix[r]] };
Wrap a procedure call around Real.Fix so we can catch RealException
i: INT = Fix[r ! Real.RealException => GOTO Fail];
IF i IN[0..maxInteger] AND i=r THEN RETURN[i] ELSE GOTO Fail;
EXITS Fail => ERROR IPErrors.MasterError[NarrowFailed];
};
AnyToNumber: PUBLIC PROC[x: Any] RETURNS[Number] = {
IF x.type=number THEN RETURN[x.number]
ELSE IF x.type=nil THEN { SIGNAL IPErrors.MasterWarning[NullValue]; RETURN[zero] }
ELSE ERROR IPErrors.MasterError[WrongType];
};
AnyToReal: PUBLIC PROC[x: Any] RETURNS[REAL] = {
IF x.type=number THEN RETURN[NumberToReal[x.number]]
ELSE IF x.type=nil THEN { SIGNAL IPErrors.MasterWarning[NullValue]; RETURN[0] }
ELSE ERROR IPErrors.MasterError[WrongType];
};
AnyToInteger: PUBLIC PROC[x: Any] RETURNS[Integer] = {
IF x.type=number THEN RETURN[NumberToInteger[x.number]]
ELSE IF x.type=nil THEN { SIGNAL IPErrors.MasterWarning[$NullValue]; RETURN[0] }
ELSE ERROR IPErrors.MasterError[WrongType];
};
AnyToIdentifier: PUBLIC PROC[x: Any] RETURNS[Identifier] = {
IF x.type=identifier THEN RETURN[NARROW[x.ref]]
ELSE ERROR IPErrors.MasterError[WrongType];
};
AnyToVector: PUBLIC PROC[x: Any] RETURNS[Vector] = {
IF x.type=vector THEN RETURN[x.ref]
ELSE ERROR IPErrors.MasterError[WrongType];
};
AnyToOperator: PUBLIC PROC[x: Any] RETURNS[Operator] = {
IF x.type=operator THEN RETURN[NARROW[x.ref]]
ELSE ERROR IPErrors.MasterError[WrongType];
};
NumberToAny: PUBLIC PROC[x: Number] RETURNS[Any] = {
RETURN[[type: number, number: x]];
};
RealToAny: PUBLIC PROC[x: REAL] RETURNS[Any] = {
RETURN[[type: number, number: [real[x]]]];
};
IntegerToAny: PUBLIC PROC[x: Integer] RETURNS[Any] = {
RETURN[[type: number, number: [int[x]]]];
};
IdentifierToAny: PUBLIC PROC[x: Identifier] RETURNS[Any] = {
RETURN[[type: identifier, ref: x]];
};
VectorToAny: PUBLIC PROC[x: Vector] RETURNS[Any] = {
RETURN[[type: vector, ref: x]];
};
OperatorToAny: PUBLIC PROC[x: Operator] RETURNS[Any] = {
RETURN[[type: operator, ref: x]];
};
END.