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.QIdwith the same identifier/index value asv. Thehintandfrozeninformation invis 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.QIdfor<mod>.<id>or<id>, respectively. The newQIdhas defaulttypeandindex.
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.QIdid. The newQIdhas defaulttypeandindex.
PROCEDURE QIdFromText(t: TEXT): JunoAST.QId;
Equivalent to QIdFromId(Atom.FromText(t)). ====================== IdList Conversions ===============================
PROCEDURE IdListToNearVarList(l: JunoAST.IdList): JunoAST.NearVarList;
Return a newJunoAST.NearVarListcontaining the same identifier/index pairs as the listl. Thehintin 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.QIdListcontaining 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.ExprListcontainingewith back pointerbp.
PROCEDURE NewQIdList(qid: JunoAST.QId; bp: JunoAST.T := NIL): JunoAST.QIdList;
Return a new, 1-itemJunoAST.QIdListcontainingqidwith back pointerbp.
PROCEDURE NewIdList(id: JunoAST.Id; index: INTEGER := 0): JunoAST.IdList;
Return a new, 1-itemJunoAST.IdListcontainingid, with indexindex.
======================== Membership Tests ===============================
PROCEDURE MemIdList(id: JunoAST.Id; l: JunoAST.IdList): BOOLEAN;
Return TRUE iffidis a member of the listl.
PROCEDURE MemNearVarList(id: JunoAST.Id; l: JunoAST.NearVarList): JunoAST.NearVarLink;
Ifidis the name of one of the variables inl, return the correspondingNearVarLinkofl. 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 listsl1andl2. This procedure is non-destructive.
===================== Operations on NearVarList's =======================
PROCEDURE NearVarListUnion(l1, l2: JunoAST.NearVarList): JunoAST.NearVarList;
Return a newJunoAST.NearVarListcontaining the union of the elements inl1andl2. The order of the resulting list is the reverse ofl1concatenated withl2.
PROCEDURE NearVarListCopy(l: JunoAST.NearVarList): JunoAST.NearVarList;
Return a copy ofl. Requiresl # NIL.
PROCEDURE ExtractHints(vars: JunoAST.NearVarList): JunoAST.Formula;
Returns a conjunction ofEqualsandNearpredicates containing one such predicate for each frozen or hinted variable invars, respectively. ReturnsTRUEif all thevarsare unhinted. Requires thatvarsis 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. Theevarfields 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;
Ifexpris of the formf(t1,...,tn), wherefis one of the predicates or functions described below, and thet_iare terms, then return a new expressionf(p(t1),...,p(tn)). IfexprisTRUEorFALSE(i.e., aLitPred), returnexpr. Signal a run-time error otherwise.The legal forms for
fare: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 causeMapArgsto 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.qidappears invarsif there is aJunoAST.NearVarLinkinvarswith the sameindexvalue.
==================== Create New Special-Purpose AST's ===================
PROCEDURE NewNumber(x: JunoValue.Real): JunoAST.Expr;
Return a new expression that is a newJunoAST.Numberifxis non-negative, or a newJunoAST.UMinuscontaining a newJunoAST.Numberifxis negative.
PROCEDURE NewPoint(x, y: JunoValue.Real): JunoAST.Pair;
Return a new AST that unparses to the expression(xVal, yVal), wherexValandyValare the expressions with valuesxandy, respectively, as created byNewNumberabove.
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 iffqid1andqid2have 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 namedqidin the commandcmd, or NIL if such a procedure call does not occur incmd.
PROCEDURE AlwaysDefined(e: JunoAST.Expr): BOOLEAN;
Return TRUE iffeis 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-definedCLOSEandAPPLYprocedures).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.