[Indigo]<Rosemary>®>Rosemary.DF=>SwitchNumConvertImpl.Mesa
Last Edited by: Spreitzer, May 1, 1985 4:44:45 pm PDT
DIRECTORY Basics, NumTypes, RoseTypes, SwitchNumConvert, SwitchTypes;
SwitchNumConvertImpl:
CEDAR
PROGRAM
IMPORTS NumTypes, SwitchTypes
EXPORTS SwitchNumConvert =
BEGIN OPEN NumTypes, SwitchTypes, RoseTypes;
Transduce:
PUBLIC
PROC [fromS: Strength, fromT, toT: NodeType, fromP, toP: Ptr] = {
SELECT
TRUE
FROM
Numeric[fromT] =>
SELECT
TRUE
FROM
Numeric[toT] => ERROR;
IsSwitchType[toT] => NumToSwitch[fromS, fromT, toT, fromP, toP];
ENDCASE => ERROR;
IsSwitchType[fromT] =>
SELECT
TRUE
FROM
Numeric[toT] => SwitchToNum[fromS, fromT, toT, fromP, toP];
IsSwitchType[toT] => ERROR;
ENDCASE => ERROR;
ENDCASE => ERROR;
};
Switches: TYPE = LONG POINTER TO PACKED ARRAY CARDINAL OF SwitchVal;
Nums:
TYPE =
RECORD [
o: [0 .. Basics.bitsPerWord),
p: LONG POINTER TO PACKED ARRAY CARDINAL OF BOOLEAN];
SwitchToNum:
PROC [fromS: Strength, fromT, toT: NodeType, fromP, toP: Ptr] = {
ss: Switches ← PtrToSwitches[fromP];
ns: Nums ← PtrToNums[toP];
WITH fromT
SELECT
FROM
fromA: ArrayNodeType => {
FOR i:
NAT
IN [0 .. fromA.length)
DO
TRUSTED {
SELECT ss[i].val
FROM
L => ns.p[i+ns.o] ← FALSE;
H => ns.p[i+ns.o] ← TRUE;
X => ERROR;
ENDCASE => ERROR};
ENDLOOP;
};
fromA: AtomNodeType => {
TRUSTED {
SELECT ss[0].val
FROM
L => ns.p[ns.o] ← FALSE;
H => ns.p[ns.o] ← TRUE;
X => ERROR;
ENDCASE => ERROR};
};
ENDCASE => ERROR;
};
NumToSwitch:
PROC [fromS: Strength, fromT, toT: NodeType, fromP, toP: Ptr] = {
ns: Nums ← PtrToNums[fromP];
ss: Switches ← PtrToSwitches[toP];
WITH fromT
SELECT
FROM
fromA: ArrayNodeType => {
FOR i:
NAT
IN [0 .. fromA.length)
DO
TRUSTED {
ss[i].s[q] ← fromS;
IF ns.p[i+ns.o]
THEN {ss[i].s[u] ← fromS; ss[i].s[d] ← none; ss[i].val ← H}
ELSE {ss[i].s[u] ← none; ss[i].s[d] ← fromS; ss[i].val ← L};
};
ENDLOOP;
};
fromA: AtomNodeType => {
TRUSTED {
ss[0].s[q] ← fromS;
IF ns.p[ns.o]
THEN {ss[0].s[u] ← fromS; ss[0].s[d] ← none; ss[0].val ← H}
ELSE {ss[0].s[u] ← none; ss[0].s[d] ← fromS; ss[0].val ← L};
};
};
ENDCASE => ERROR;
};
PtrToSwitches:
PROC [p: Ptr]
RETURNS [ss: Switches] = {
IF p.bit # 0 THEN ERROR;
TRUSTED {ss ← LOOPHOLE[p.word]}};
PtrToNums:
PROC [p: Ptr]
RETURNS [ns: Nums] =
TRUSTED
{ns ← [o: p.bit, p: LOOPHOLE[p.word]]};
END.