CDExpr.mesa
Copyright © 1984, 1985, 1986 by Xerox Corporation. All rights reversed.
Created by: Mayo, July 23, 1984 3:50:47 pm PDT
Last Edited by: Serlet, January 4, 1985 7:03:15 pm PST
Bertrand Serlet June 16, 1986 4:37:44 pm PDT
This module allows reading expressions of the form 'var ← expr' or 'var ~ expr'. If the operator is '← then the expr is evaluated using symbols in a symbol table, or things in the global naming context. See Interpreter.mesa for more details. If the operator is '~ then expr is simply treated as a rope which is then assigned to var.
A way of using this interface is to create a text file assigning values to parameters, and in the client program to read this file (with ReadFile) and then fetch the necessary parameters (with the appropriate Fetch proc). Another way is just to Create a SymTab and then do Stores and Fetches.
The difference with ordinary SymTabs is that the values are TV, so to be compatible with the interpreter. That means that Procs from the SymTab interface that do not deal with values are still directly applicable, for example: Create, Delete, and GetSize.
This module uses the slot named '&' in the passed symbol tables as a temporary placeholder. Anything that the caller places there might be destroyed.
DIRECTORY
IO USING [STREAM],
Rope USING [ROPE],
SymTab USING [EachPairAction, Ref];
CDExpr: CEDAR DEFINITIONS = BEGIN
Utilities
ROPE: TYPE = Rope.ROPE;
Error: ERROR [ec: ErrorCode, msg: ROPE];
ErrorCode:
TYPE = {
CouldNotEval, -- could not evaluate expression
NoValue, -- expression didn't return a value
Syntax, -- bad expression syntax
FileError, -- could not read from file
BadType, -- raised by the FetchXXX procs.
NotTVTable -- the passed SymTab had something other than a TV!
};
Reading and storing parameters
Assign:
PROC [symTab: SymTab.Ref, line:
ROPE]
RETURNS [
BOOL];
Evaluates one assignment statement of the form 'var ← expr', and updates var in the symbol table. Returns TRUE if new value, FALSE if previous value overwritten.
ReadStream:
PROC [stream:
IO.
STREAM]
RETURNS [symTab: SymTab.Ref];
Reads a stream that contains one assignment statement per line, returning a symbol table.
ReadFile:
PROC [fileName:
ROPE, wDir:
ROPE ←
NIL]
RETURNS [symTab: SymTab.Ref];
For conveniency only. Reads a stream or a file that contains one assignment statement per line, returning a symbol table.
Storing parameters
Base types (add more if you wish!). For other types, create a pointer to it and do StoreRef. Same for lists. The boolean returned value has the same meaning as in SymTab.Store, i.e. is TRUE if new value, FALSE if previous value overwritten.
StoreBool: PROC [symTab: SymTab.Ref, name: ROPE, val: BOOL] RETURNS [BOOL];
StoreInt: PROC [symTab: SymTab.Ref, name: ROPE, val: INT] RETURNS [BOOL];
StoreAtom: PROC [symTab: SymTab.Ref, name: ROPE, val: ATOM] RETURNS [BOOL];
StoreRope: PROC [symTab: SymTab.Ref, name: ROPE, val: ROPE] RETURNS [BOOL];
StoreRef:
PROC [symTab: SymTab.Ref, name:
ROPE, val:
REF]
RETURNS [
BOOL];
General hook for other types.
Fetching parameters
Base types (add more if you wish!). Non-ref values are stored as refs to the value. (That is, INT is stored as REF INT). These procs may raise Error[NotTVTable] if the table contains non-TV values. If the parameter is not of the expected type then we raise Error[BadType] with a message like: "Parameter XYZ is of the wrong type, it should be a rope."
FetchBool: PROC [symTab: SymTab.Ref, name: ROPE] RETURNS [found: BOOL, val: BOOL];
FetchInt: PROC [symTab: SymTab.Ref, name: ROPE] RETURNS [found: BOOL, val: INT];
FetchAtom: PROC [symTab: SymTab.Ref, name: ROPE] RETURNS [found: BOOL, val: ATOM];
FetchRope: PROC [symTab: SymTab.Ref, name: ROPE] RETURNS [found: BOOL, val: ROPE];
FetchRef:
PROC [symTab: SymTab.Ref, name:
ROPE]
RETURNS [found:
BOOL, val:
REF];
General hook for other types.
Operations on SymTab of TV
CombineTabs:
PROC [winner: SymTab.Ref, loser: SymTab.Ref ←
NIL]
RETURNS [SymTab.Ref];
Combines 2 tables of TVs to yield a third. Values in the winner table override values in the loser table if there is a conflict. With only first argument, this proc copies the table. Arguments are not changed.
Pairs:
PROC [symTab: SymTab.Ref, action: SymTab.EachPairAction]
RETURNS [
BOOL];
Enumerates pairs currently in symbol table in unspecified order. Pairs inserted/deleted during enumeration may or may not be seen. Applies action to each pair until action returns TRUE or no more pairs. Returns TRUE if some action returns TRUE
END.