IPMathImpl.mesa
Last edited by:
Doug Wyatt, March 2, 1984 11:13:29 am PST
DIRECTORY
IP USING [Any, CheckAny, Color, Discard, Get, Identifier, Integer, Number, Operator, Outline, PixelArray, Pop, PopBool, PopReal, PushBool, PushInteger, PushReal, Shape, State, TopType, Trajectory, Transformation, TypeCode, Vector, VectorShape],
IPBase USING [],
IPReal USING [Ceiling, Floor, Mod, Rem, Round, Trunc],
Real USING [SqRt],
RealFns USING [ArcTanDeg, CosDeg, Log, Power, SinDeg];
IPMathImpl: CEDAR PROGRAM
IMPORTS IP, IPReal, Real, RealFns
EXPORTS IP, IPBase
~ BEGIN OPEN IP;
EqNumber: PROC[a, b: Number] RETURNS[BOOL] ~ INLINE { RETURN[a.value=b.value] };
EqIdentifier: PROC[a, b: ATOM] RETURNS[BOOL] ~ INLINE { RETURN[a=b] };
This takes ATOM arguments: want to be sure to change it if the Identifier type ever changes!
Eq: PUBLIC PROC[a, b: Any] RETURNS[BOOL] ~ {
a ← CheckAny[a]; b ← CheckAny[b]; -- ensure that neither is NIL
WITH a SELECT FROM
a: Number => WITH b SELECT FROM
b: Number => RETURN[EqNumber[a, b]];
ENDCASE;
a: Identifier => WITH b SELECT FROM
b: Identifier => RETURN[EqIdentifier[a, b]];
ENDCASE;
ENDCASE;
RETURN[FALSE];
};
EqVector: PROC[a, b: Vector] RETURNS[BOOL] ~ {
shape: VectorShape ~ Shape[a];
IF Shape[b]#shape THEN RETURN[FALSE];
FOR i: Integer IN[shape.l..shape.l+shape.n) DO
IF NOT Eq[Get[a, i], Get[b, i]] THEN RETURN[FALSE];
ENDLOOP;
RETURN[TRUE];
};
EqName: PUBLIC PROC[a, b: Any] RETURNS[BOOL] ~ {
a ← CheckAny[a]; b ← CheckAny[b]; -- ensure that neither is NIL
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] ~ {
RETURN[WITH CheckAny[a] SELECT FROM
x: Number => $Number,
x: Identifier => $Identifier,
x: Vector => $Vector,
x: Operator => $Operator,
x: Transformation => $Transformation,
x: PixelArray => $PixelArray,
x: Color => $Color,
x: Trajectory => $Trajectory,
x: Outline => $Outline,
ENDCASE => $Other];
};
ApplyEQ: PUBLIC PROC[self: State] ~ {
b: Any ~ Pop[self];
a: Any ~ Pop[self];
PushBool[self, Eq[a, b]];
};
ApplyEQNAME: PUBLIC PROC[self: State] ~ {
b: Any ~ Pop[self];
a: Any ~ Pop[self];
PushBool[self, EqName[a, b]];
};
ApplyGT: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushBool[self, a>b];
};
ApplyGE: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushBool[self, a>=b];
};
ApplyAND: PUBLIC PROC[self: State] ~ {
b: BOOL ~ PopBool[self];
a: BOOL ~ PopBool[self];
PushBool[self, a AND b];
};
ApplyOR: PUBLIC PROC[self: State] ~ {
b: BOOL ~ PopBool[self];
a: BOOL ~ PopBool[self];
PushBool[self, a OR b];
};
ApplyNOT: PUBLIC PROC[self: State] ~ {
b: BOOL ~ PopBool[self];
PushBool[self, NOT b];
};
ApplyTYPE: PUBLIC PROC[self: State] ~ {
code: TypeCode ~ TopType[self]; Discard[self];
PushInteger[self, code.ORD];
};
ApplyADD: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushReal[self, a+b];
};
ApplySUB: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushReal[self, a-b];
};
ApplyNEG: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, -a];
};
ApplyABS: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, ABS[a]];
};
ApplyFLOOR: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, IPReal.Floor[a]];
};
ApplyCEILING: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, IPReal.Ceiling[a]];
};
ApplyTRUNC: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, IPReal.Trunc[a]];
};
ApplyROUND: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, IPReal.Round[a]];
};
ApplyMUL: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushReal[self, a*b];
};
ApplyDIV: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushReal[self, a/b];
};
ApplyMOD: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushReal[self, IPReal.Mod[a, b]];
};
ApplyREM: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushReal[self, IPReal.Rem[a, b]];
};
ApplyMAX: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushReal[self, MAX[a, b]];
};
ApplyMIN: PUBLIC PROC[self: State] ~ {
b: REAL ~ PopReal[self];
a: REAL ~ PopReal[self];
PushReal[self, MIN[a, b]];
};
ApplySQRT: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, Real.SqRt[a]];
};
ApplyEXP: PUBLIC PROC[self: State] ~ {
e: REAL ~ PopReal[self];
b: REAL ~ PopReal[self];
PushReal[self, RealFns.Power[b, e]];
};
ApplyLOG: PUBLIC PROC[self: State] ~ {
v: REAL ~ PopReal[self];
b: REAL ~ PopReal[self];
PushReal[self, RealFns.Log[b, v]];
};
ApplySIN: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, RealFns.SinDeg[a]];
};
ApplyCOS: PUBLIC PROC[self: State] ~ {
a: REAL ~ PopReal[self];
PushReal[self, RealFns.CosDeg[a]];
};
ApplyATAN: PUBLIC PROC[self: State] ~ {
x: REAL ~ PopReal[self];
y: REAL ~ PopReal[self];
PushReal[self, RealFns.ArcTanDeg[y, x]];
};
END.