Miscellaneous transformations on JunoAST's.
INTERFACE====================== Id/QId/NearVar Conversions =======================JunoASTUtils ; IMPORT JunoValue, JunoAST;
PROCEDURE QIdFromNearVar(v: JunoAST.NearVarLink): JunoAST.QId;
Returns a new unqualifiedJunoAST.QId
with the same identifier/index value asv
. Thehint
andfrozen
information inv
is lost. Requiresv # NIL
.
PROCEDURE QIdFromIds(mod, id: JunoAST.Id): JunoAST.QId;
Create and return a qualified (mod # JunoAST.NilId
) or unqualified (mod = JunoAST.NilId
)JunoAST.QId
for<mod>.<id>
or<id>
, respectively. The newQId
has defaulttype
andindex
.
PROCEDURE QIdFromTexts(mod, id: TEXT): JunoAST.QId;
Equivalent to QIdFromIds(Atom.FromText(mod), Atom.FromText(id))
.
PROCEDURE QIdFromId(id: JunoAST.Id): JunoAST.QId;
Create and return an unqualifiedJunoAST.QId
id
. The newQId
has defaulttype
andindex
.
PROCEDURE QIdFromText(t: TEXT): JunoAST.QId;
Equivalent to QIdFromId(Atom.FromText(t))
.
====================== IdList Conversions ===============================
PROCEDURE IdListToNearVarList(l: JunoAST.IdList): JunoAST.NearVarList;
Return a newJunoAST.NearVarList
containing the same identifier/index pairs as the listl
. Thehint
in each element of the result is set toJunoAST.NilExpr
. The order of the elements in the resulting list is the reverse of those inl
.
PROCEDURE IdListToQIdList(l: JunoAST.IdList): JunoAST.QIdList;
Return a newJunoAST.QIdList
containing the same (unqualified) names inl
(in the same order).
==================== Create New 1-Item Lists ============================
PROCEDURE NewExprList(e: JunoAST.Expr; bp: JunoAST.T := NIL): JunoAST.ExprList;
Return a new, 1-itemJunoAST.ExprList
containinge
with back pointerbp
.
PROCEDURE NewQIdList(qid: JunoAST.QId; bp: JunoAST.T := NIL): JunoAST.QIdList;
Return a new, 1-itemJunoAST.QIdList
containingqid
with back pointerbp
.
PROCEDURE NewIdList(id: JunoAST.Id; index: INTEGER := 0): JunoAST.IdList;
Return a new, 1-itemJunoAST.IdList
containingid
, with indexindex
.
======================== Membership Tests ===============================
PROCEDURE MemIdList(id: JunoAST.Id; l: JunoAST.IdList): BOOLEAN;
Return TRUE iffid
is a member of the listl
.
PROCEDURE MemNearVarList(id: JunoAST.Id; l: JunoAST.NearVarList): JunoAST.NearVarLink;
Ifid
is the name of one of the variables inl
, return the correspondingNearVarLink
ofl
. Otherwise, return NIL.
======================= Operations on IdList's ==========================
PROCEDURE CopyIdList(l: JunoAST.IdList): JunoAST.IdList;
Return a copy of the list l
. This procedure is non-destructive.
PROCEDURE ConcatIdLists(l1, l2: JunoAST.IdList): JunoAST.IdList;
Return the result of concatenating the listsl1
andl2
. This procedure is non-destructive.
===================== Operations on NearVarList's =======================
PROCEDURE NearVarListUnion(l1, l2: JunoAST.NearVarList): JunoAST.NearVarList;
Return a newJunoAST.NearVarList
containing the union of the elements inl1
andl2
. The order of the resulting list is the reverse ofl1
concatenated withl2
.
PROCEDURE NearVarListCopy(l: JunoAST.NearVarList): JunoAST.NearVarList;
Return a copy ofl
. Requiresl # NIL
.
PROCEDURE ExtractHints(vars: JunoAST.NearVarList): JunoAST.Formula;
Returns a conjunction ofEquals
andNear
predicates containing one such predicate for each frozen or hinted variable invars
, respectively. ReturnsTRUE
if all thevars
are unhinted. Requires thatvars
is non-empty.
PROCEDURE StripHints(vars: JunoAST.NearVarList): JunoAST.NearVarList;
Returns a list containing the same variables asvars
, but all variables in the resulting list are unhinted. Theevar
fields of the result variables are all set toFALSE
.
============================= MapArgs ===================================
TYPE Mappee = PROCEDURE(e: JunoAST.Expr): JunoAST.Expr; PROCEDURE MapArgs(expr: JunoAST.Expr; p: Mappee): JunoAST.Expr;
Ifexpr
is of the formf(t1,...,tn)
, wheref
is one of the predicates or functions described below, and thet_i
are terms, then return a new expressionf(p(t1),...,p(tn))
. Ifexpr
isTRUE
orFALSE
(i.e., aLitPred
), returnexpr
. Signal a run-time error otherwise.The legal forms for
f
are:o a user-defined predicate or function (i.e., a "JunoAST.Call"); o predicates "JunoAST.BuiltInUnaryPred" or "JunoAST.Relation" (i.e., all "JunoAST.BuiltInPred"'s except "And", "Or", "Not", and "Exists"); or o functions "JunoAST.BuiltInFunc"Hence, of allJunoAST.Expr
's, the only types that causeMapArgs
to signal a run-time error are:o the compound formulas ("And", "Or", "Not", "Exists") o a grouped expression (i.e., a "JunoAST.GroupedExpr"); o "AtomicExpr" (i.e., "LitValue" and "QId") o "NormalForm"
======================= Operations on JunoAST.Vars ======================
PROCEDURE MemVars(qid: JunoAST.QId; READONLY vars: JunoAST.Vars): INTEGER;
Return the index ofqid
-- which must be an unqualified local variable -- invars
, or -1 if it does not occur.qid
appears invars
if there is aJunoAST.NearVarLink
invars
with the sameindex
value.
==================== Create New Special-Purpose AST's ===================
PROCEDURE NewNumber(x: JunoValue.Real): JunoAST.Expr;
Return a new expression that is a newJunoAST.Number
ifx
is non-negative, or a newJunoAST.UMinus
containing a newJunoAST.Number
ifx
is negative.
PROCEDURE NewPoint(x, y: JunoValue.Real): JunoAST.Pair;
Return a new AST that unparses to the expression(xVal, yVal)
, wherexVal
andyVal
are the expressions with valuesx
andy
, respectively, as created byNewNumber
above.
PROCEDURE NewASTFromValue(v: JunoValue.T): JunoAST.T;
Return a new AST that unparses to the run-time value v
.
PROCEDURE NewAssign(v: JunoAST.QId; e: JunoAST.Expr): JunoAST.Assign;
Return the assignment v := e
.
============================ Miscellaneous ==============================
PROCEDURE Ungroup(ast: JunoAST.T): JunoAST.T;
Return the largest subtree of ast
that is not a grouped command or
grouped expression.
PROCEDURE EqualQIds(qid1, qid2: JunoAST.QId): BOOLEAN;
Return TRUE iffqid1
andqid2
have the same module name and identifier name.
PROCEDURE FirstProcCall(cmd: JunoAST.Cmd; qid: JunoAST.QId): JunoAST.ProcCall;
Return the first procedure call command to the procedure namedqid
in the commandcmd
, or NIL if such a procedure call does not occur incmd
.
PROCEDURE AlwaysDefined(e: JunoAST.Expr): BOOLEAN;
Return TRUE iffe
is an expression that is always defined. The basic expressions that are always defined are literals and qualified identifiers. In addition to these are: grouped expressions whose (body) expression is always defined, list expressions all of whose elements are always defined, pair expressions both of whose values are always defined, and call expressions to user-defined or external procedures all of whose IN parameters are always defined (except for the built-in user-definedCLOSE
andAPPLY
procedures).Note: If this procedure is called before
e
's atoms have been annotated byJunoCompile.AnnotateAtoms
, it will return FALSE in some cases where it would return TRUE once the atoms had been annotated.
END JunoASTUtils.