ExpressionsImpl.mesa
Last Edited by: Arnon, July 19, 1985 2:54:46 pm PDT
DIRECTORY
Rope,
IO,
Basics,
Atom,
Convert,
AlgebraClasses,
MathConstructors,
Expressions;
ExpressionsImpl: CEDAR PROGRAM
IMPORTS IO, Convert, AlgebraClasses, MathConstructors
EXPORTS Expressions
= BEGIN OPEN Expressions, Convert, AC: AlgebraClasses;
Types
ExpressionsError: PUBLIC SIGNAL [reason: ATOM ← $Unspecified] = CODE;
bitsPerWord: CARDINAL = Basics.bitsPerWord;
CARD: TYPE = LONG CARDINAL;
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
Variables
ClassPrintName: AC.PrintNameProc = {
RETURN["Expressions"];
};
ClassLegalFirstChar: AC.LegalFirstCharOp = {
SELECT char FROM
= '., IN ['0..'9] => RETURN[TRUE];
ENDCASE;
RETURN[TRUE];
};
ClassToExpr: AC.ToExprOp = {
data: ExpressionData ← NARROW[in.data];
RETURN[data];
};
ClassZero: AC.NullaryOp = {
RETURN[ MathConstructors.MakeInt["0"] ]
};
ClassOne: AC.NullaryOp = {
RETURN[ MathConstructors.MakeInt["1"] ]
};
ExpressionClass: AC.StructureClass ← NEW[AC.StructureClassRec ← [
category: field,
printName: ClassPrintName,
structureEqual: AC.defaultStructureEqualityTest,
characteristic: ClassCharacteristic,
isElementOf: AC.defaultElementOfProc,
legalFirstChar: ClassLegalFirstChar,
read: Read,
fromRope: FromRope,
toRope: ToRope,
write: Write,
toExpr: ClassToExpr,
add: Add,
negate: Negate,
subtract: Subtract,
zero: ClassZero,
multiply: Multiply,
commutative: TRUE,
invert: Invert,
divide: Divide,
one: ClassOne,
equal: Equal,
ordered: TRUE,
sign: Sign,
abs: Abs,
compare: Compare,
completeField: TRUE,
realField: TRUE,
realClosedField: TRUE,
algebraicallyClosedField: FALSE,
propList: NIL
] ];
Expressions: PUBLIC AC.Structure ← NEW[AC.StructureRec ← [
class: ExpressionClass,
instanceData: NIL
] ];
ExpressionOne: Expression ← FromREAL[1.0]; -- do after Expressions set
ExpressionZero: Expression ← FromREAL[0.0];
I/O and Conversion
Read: PUBLIC AC.ReadOp ~ {
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REALIO.GetExpression[in] ]
] ] ];
};
FromRope: PUBLIC AC.FromRopeOp = {
stream: IO.STREAMIO.RIS[in];
RETURN[ Read[stream, structure] ];
};
ToRope: PUBLIC AC.ToRopeOp = {
data: ExpressionData ← NARROW[in.data];
RETURN[ Convert.RopeFromExpression[data^] ];
};
Write: PUBLIC AC.WriteOp = {
IO.PutRope[ stream, ToRope[in] ]
};
FromREAL: PUBLIC PROC [real: REAL] RETURNS [Expression] = {
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REAL ← real]
] ] ];
};
ToREAL: PUBLIC PROC [real: Expression] RETURNS [REAL] = {
data: ExpressionData ← NARROW[real.data];
RETURN[data^];
};
Arithmetic
Add: PUBLIC AC.BinaryOp ~ {
firstData: ExpressionData ← NARROW[firstArg.data];
secondData: ExpressionData ← NARROW[secondArg.data];
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REAL ← firstData^ + secondData^ ]
] ] ];
};
Negate: PUBLIC AC.UnaryOp ~ {
data: ExpressionData ← NARROW[arg.data];
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REAL- data^ ]
] ] ];
};
Subtract: PUBLIC AC.BinaryOp ~ {
firstData: ExpressionData ← NARROW[firstArg.data];
secondData: ExpressionData ← NARROW[secondArg.data];
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REAL ← firstData^ - secondData^ ]
] ] ];
};
Multiply: PUBLIC AC.BinaryOp ~ {
firstData: ExpressionData ← NARROW[firstArg.data];
secondData: ExpressionData ← NARROW[secondArg.data];
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REAL ← firstData^ * secondData^ ]
] ] ];
};
Invert: PUBLIC AC.UnaryOp ~ {
data: ExpressionData ← NARROW[arg.data];
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REAL ← 1.0 / data^ ]
] ] ];
};
Divide: PUBLIC AC.BinaryOp ~ {
firstData: ExpressionData ← NARROW[firstArg.data];
secondData: ExpressionData ← NARROW[secondArg.data];
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REAL ← firstData^ / secondData^ ]
] ] ];
};
Comparison
Sign: PUBLIC AC.CompareToZeroOp = {
data: ExpressionData ← NARROW[arg.data];
SELECT data^ FROM
< 0.0 => RETURN[less];
= 0.0 => RETURN[equal];
ENDCASE => RETURN[greater];
};
Abs: PUBLIC AC.UnaryOp ~ {
data: ExpressionData ← NARROW[arg.data];
RETURN[NEW[AC.ObjectRec ← [
structure: Expressions,
data: NEW[REAL ← ABS[data^] ]
] ] ];
};
Compare: PUBLIC AC.BinaryCompareOp ~ {
firstData: ExpressionData ← NARROW[firstArg.data];
secondData: ExpressionData ← NARROW[secondArg.data];
SELECT firstData^ FROM
< secondData^ => RETURN[less];
= secondData^ => RETURN[equal];
ENDCASE => RETURN[greater];
};
Equal: PUBLIC AC.EqualityOp ~ {
firstData: ExpressionData ← NARROW[firstArg.data];
secondData: ExpressionData ← NARROW[secondArg.data];
RETURN[ firstData^ = secondData^]
};
END.