IPTypesImpl.mesa
Copyright © 1984, 1985 by Xerox Corporation. All rights reserved.
Doug Wyatt, November 21, 1985 3:31:08 pm PST
DIRECTORY
Imager USING [Color, Font, Outline, PixelArray, Trajectory, Transformation],
ImagerBackdoor USING [Clipper],
IPInterpreter USING [Any, BoundsCheckCardinal, Bug, Cardinal, Get, Identifier, MasterError, MasterWarning, maxCardinal, Number, NumberRep, Operator, Shape, TypeCode, Vector, VectorShape],
Real USING [Fix],
Rope USING [Equal];
IPTypesImpl: CEDAR PROGRAM
IMPORTS IPInterpreter, Real, Rope
EXPORTS IPInterpreter
= BEGIN OPEN IPInterpreter;
zero: Number ~ NEW[NumberRep ← [zero[]]];
number0: Number ~ NEW[NumberRep ← [int[0]]];
number1: Number ~ NEW[NumberRep ← [int[1]]];
CardinalFromReal: PUBLIC PROC [real: REAL] RETURNS [Cardinal] ~ {
int: INT ← 0;
IF real IN[0..maxCardinal] THEN int ← Real.Fix[real];
IF int=real THEN RETURN[BoundsCheckCardinal[int]];
MasterError[$notCardinal, "Number is not a Cardinal"];
RETURN[0];
};
CardinalFromNum: PUBLIC PROC [n: NumberRep] RETURNS [Cardinal] ~ {
WITH n: n SELECT FROM
zero => { MasterWarning[$nullValue, "Used an uninitialized value"]; RETURN[0] };
int => IF n.int IN[0..maxCardinal] THEN RETURN[n.int];
real => RETURN[CardinalFromReal[n.real]];
rational => RETURN[CardinalFromReal[REAL[n.n]/REAL[n.d]]];
ENDCASE => ERROR Bug;
MasterError[$notCardinal, "Number is not a Cardinal"];
RETURN[0];
};
RealFromNum: PUBLIC PROC [n: NumberRep] RETURNS [REAL] ~ {
WITH n: n SELECT FROM
zero => { MasterWarning[$nullValue, "Used an uninitialized value"]; RETURN[0] };
int => RETURN[REAL[n.int]];
real => RETURN[n.real];
rational => RETURN[REAL[n.n]/REAL[n.d]];
ENDCASE => ERROR Bug;
};
NumberFromCardinal: PUBLIC PROC [x: Cardinal] RETURNS [Number] ~ {
SELECT x FROM 0 => RETURN[number0]; 1 => RETURN[number1]; ENDCASE;
RETURN[NEW[NumberRep ← [int[x]]]];
};
NumberFromReal: PUBLIC PROC [x: REAL] RETURNS [Number] ~ {
IF x=0 THEN RETURN[number0];
RETURN[NEW[NumberRep ← [real[x]]]];
};
NumberFromNum: PUBLIC PROC [x: NumberRep] RETURNS [Number] ~ {
RETURN[NEW[NumberRep ← x]];
};
CardinalFromAny: PUBLIC PROC [x: Any] RETURNS [Cardinal] ~ {
IF x=NIL THEN x ← zero;
WITH x SELECT FROM
x: Number => {
n: NumberRep ~ x^;
WITH n: n SELECT FROM
int => IF n.int IN[0..maxCardinal] THEN RETURN[n.int];
ENDCASE;
RETURN[CardinalFromNum[n]];
};
ENDCASE;
MasterError[$wrongType, "Wrong type (expected Cardinal)"];
RETURN[0];
};
RealFromAny: PUBLIC PROC [x: Any] RETURNS [REAL] ~ {
IF x=NIL THEN x ← zero;
WITH x SELECT FROM
x: Number => {
n: NumberRep ~ x^;
WITH n: n SELECT FROM
int => RETURN[REAL[n.int]];
real => RETURN[n.real];
ENDCASE;
RETURN[RealFromNum[n]];
};
ENDCASE;
MasterError[$wrongType, "Wrong type (expected Number)"];
RETURN[0];
};
NumberFromAny: PUBLIC PROC [x: Any] RETURNS [Number] ~ {
IF x=NIL THEN x ← zero;
WITH x SELECT FROM x: Number => RETURN[x]; ENDCASE;
MasterError[$wrongType, "Wrong type (expected Number)"];
RETURN[NIL];
};
IdentifierFromAny: PUBLIC PROC [x: Any] RETURNS [Identifier] ~ {
WITH x SELECT FROM x: Identifier => RETURN[x]; ENDCASE;
MasterError[$wrongType, "Wrong type (expected Identifier)"];
RETURN[NIL];
};
VectorFromAny: PUBLIC PROC [x: Any] RETURNS [Vector] ~ {
WITH x SELECT FROM x: Vector => RETURN[x]; ENDCASE;
MasterError[$wrongType, "Wrong type (expected Vector)"];
RETURN[NIL];
};
OperatorFromAny: PUBLIC PROC [x: Any] RETURNS [Operator] ~ {
WITH x SELECT FROM x: Operator => RETURN[x]; ENDCASE;
MasterError[$wrongType, "Wrong type (expected Operator)"];
RETURN[NIL];
};
Eq: PUBLIC PROC [a, b: Any] RETURNS [BOOL] ~ {
IF a=NIL THEN a ← zero;
IF b=NIL THEN b ← zero;
IF a=b THEN RETURN[TRUE];
WITH a SELECT FROM
a: Number => WITH b SELECT FROM
b: Number => RETURN[RealFromNum[a^]=RealFromNum[b^]];
ENDCASE;
a: Identifier => WITH b SELECT FROM
b: Identifier => RETURN[Rope.Equal[s1: a, s2: b, case: FALSE]];
ENDCASE;
ENDCASE;
RETURN[FALSE];
};
EqVector: PROC [a, b: Vector] RETURNS [BOOL] ~ {
shape: VectorShape ~ Shape[a];
bShape: VectorShape ~ Shape[b];
IF NOT bShape.lowerBound=shape.lowerBound THEN RETURN[FALSE];
IF NOT bShape.size=shape.size THEN RETURN[FALSE];
FOR i: Cardinal IN[shape.lowerBound..shape.lowerBound+shape.size) DO
IF NOT Eq[Get[a, i], Get[b, i]] THEN RETURN[FALSE];
ENDLOOP;
RETURN[TRUE];
};
EqName: PUBLIC PROC [a, b: Any] RETURNS [BOOL] ~ {
IF Eq[a, b] THEN RETURN[TRUE];
WITH a SELECT FROM
a: Vector => WITH b SELECT FROM
b: Vector => RETURN[EqVector[a, b]];
ENDCASE;
ENDCASE;
RETURN[FALSE];
};
Type: PUBLIC PROC [a: Any] RETURNS [TypeCode] ~ {
IF a=NIL THEN a ← zero;
WITH a SELECT FROM
x: Number => RETURN[number];
x: Identifier => RETURN[identifier];
x: Vector => RETURN[vector];
x: Operator => RETURN[operator];
x: Imager.Transformation => RETURN[transformation];
x: Imager.PixelArray => RETURN[pixelArray];
x: Imager.Color => RETURN[color];
x: Imager.Trajectory => RETURN[trajectory];
x: Imager.Outline => RETURN[outline];
x: Imager.Font => RETURN[font];
x: ImagerBackdoor.Clipper => RETURN[clipper];
ENDCASE;
RETURN[TypeCode.LAST];
};
END.