## A PEMiniState is one of:
A PEMiniR with env Map of Text to CVal and funcs Map of Text to CFunc and depth Int and staticEnv Map of Text to CExpr and memoCache Map of Text to CExpr and onStackM Seq of Text.
## To makePeState (e: Map of Text to CVal) and (f: Map of Text to CFunc) and (d: Int) -> PEMiniState:
Return a new PEMiniR with env e and funcs f and depth d and staticEnv (a new Map of Text to CExpr) and memoCache (a new Map of Text to CExpr) and onStackM (a new Seq of Text).
## To peEnvM (st: PEMiniState) -> Map of Text to CVal:
Inspect st:
When PEMiniR (e, f, d, se, mc, os):
Return e.
## To peFuncsM (st: PEMiniState) -> Map of Text to CFunc:
Inspect st:
When PEMiniR (e, f, d, se, mc, os):
Return f.
## To peDepthM (st: PEMiniState) -> Int:
Inspect st:
When PEMiniR (e, f, d, se, mc, os):
Return d.
## To peMemoCacheM (st: PEMiniState) -> Map of Text to CExpr:
Inspect st:
When PEMiniR (e, f, d, se, mc, os):
Return mc.
## To peStaticEnvM (st: PEMiniState) -> Map of Text to CExpr:
Inspect st:
When PEMiniR (e, f, d, se, mc, os):
Return se.
## To peOnStackM (st: PEMiniState) -> Seq of Text:
Inspect st:
When PEMiniR (e, f, d, se, mc, os):
Return os.
## To peStateWithEnvDepthM (st: PEMiniState) and (newEnv: Map of Text to CVal) and (d: Int) -> PEMiniState:
Inspect st:
When PEMiniR (e, f, oldD, se, mc, os):
Return a new PEMiniR with env newEnv and funcs f and depth d and staticEnv (a new Map of Text to CExpr) and memoCache mc and onStackM os.
## To peStateWithEnvDepthStaticM (st: PEMiniState) and (newEnv: Map of Text to CVal) and (d: Int) and (newSe: Map of Text to CExpr) -> PEMiniState:
Inspect st:
When PEMiniR (e, f, oldD, se, mc, os):
Return a new PEMiniR with env newEnv and funcs f and depth d and staticEnv newSe and memoCache mc and onStackM os.
## To miniEnvBind (m: mutable Map of Text to CVal) and (k: Text) and (v: CVal):
Set item k of m to v.
## To miniEnvDrop (m: mutable Map of Text to CVal) and (k: Text):
Remove k from m.
## To miniSeBind (m: mutable Map of Text to CExpr) and (k: Text) and (v: CExpr):
Set item k of m to v.
## To miniSeDrop (m: mutable Map of Text to CExpr) and (k: Text):
Remove k from m.
## To miniOsPush (s: mutable Seq of Text) and (v: Text):
Push v to s.
## To isCopyPropSafeM (e: CExpr) -> Bool:
Inspect e:
When CNewSeq:
Return false.
When CNewSet:
Return false.
Otherwise:
Return true.
## To isPeValueForCopyPropM (e: CExpr) -> Bool:
Let sv be isStaticValueM(e).
If sv:
Return true.
Inspect e:
When CList (cpvItems):
Return true.
When CTuple (cpvTup):
Return true.
When CNew (cpvTy, cpvNames, cpvVals):
Return true.
When CNewVariant (cpvTag, cpvNms, cpvVls):
Return true.
When COptionSome (cpvInner):
Return true.
When CRange (cpvS, cpvE):
Return true.
Otherwise:
Return false.
## To exprRefsAnyMutatedM (e: CExpr) and (muts: Seq of Text) -> Bool:
Inspect e:
When CVar (ravVn):
Repeat for ravM in muts:
If ravVn is equal to ravM:
Return true.
Return false.
When CBinOp (ravOp, ravL, ravR):
Let ravA be exprRefsAnyMutatedM(ravL, muts).
If ravA:
Return true.
Return exprRefsAnyMutatedM(ravR, muts).
When CNot (ravNi):
Return exprRefsAnyMutatedM(ravNi, muts).
When CIndex (ravIc, ravIi):
Let ravB be exprRefsAnyMutatedM(ravIc, muts).
If ravB:
Return true.
Return exprRefsAnyMutatedM(ravIi, muts).
When CLen (ravLt):
Return exprRefsAnyMutatedM(ravLt, muts).
When CFieldAccess (ravFt, ravFf):
Return exprRefsAnyMutatedM(ravFt, muts).
When CMapGet (ravMt, ravMk):
Let ravC be exprRefsAnyMutatedM(ravMt, muts).
If ravC:
Return true.
Return exprRefsAnyMutatedM(ravMk, muts).
When CContains (ravCc, ravCe):
Let ravD be exprRefsAnyMutatedM(ravCc, muts).
If ravD:
Return true.
Return exprRefsAnyMutatedM(ravCe, muts).
When CSlice (ravSc, ravSs, ravSe):
Let ravE be exprRefsAnyMutatedM(ravSc, muts).
If ravE:
Return true.
Let ravF be exprRefsAnyMutatedM(ravSs, muts).
If ravF:
Return true.
Return exprRefsAnyMutatedM(ravSe, muts).
When CUnion (ravUl, ravUr):
Let ravG be exprRefsAnyMutatedM(ravUl, muts).
If ravG:
Return true.
Return exprRefsAnyMutatedM(ravUr, muts).
When CIntersection (ravIl, ravIr):
Let ravH be exprRefsAnyMutatedM(ravIl, muts).
If ravH:
Return true.
Return exprRefsAnyMutatedM(ravIr, muts).
When CCopy (ravCt):
Return exprRefsAnyMutatedM(ravCt, muts).
When CInt (ravVi):
Return false.
When CBool (ravVb):
Return false.
When CText (ravVt):
Return false.
When CFloat (ravVf):
Return false.
Otherwise:
Return true.
## To shouldCopyPropM (e: CExpr) and (muts: Seq of Text) -> Bool:
Let scpSv be isPeValueForCopyPropM(e).
If scpSv:
Return true.
Let scpRm be exprRefsAnyMutatedM(e, muts).
If scpRm:
Return false.
Return true.
## To isStaticM (e: CExpr) -> Bool:
Inspect e:
When CInt (v):
Return true.
When CBool (v):
Return true.
When CText (v):
Return true.
When CFloat (v):
Return true.
When CList (items):
Repeat for elem in items:
Let s be isStaticM(elem).
If not s:
Return false.
Return true.
When CNewVariant (tag, names, vals):
Repeat for v in vals:
Let s be isStaticM(v).
If not s:
Return false.
Return true.
When CTuple (items):
Repeat for elem in items:
Let s be isStaticM(elem).
If not s:
Return false.
Return true.
When COptionSome (inner):
Return isStaticM(inner).
When COptionNone:
Return true.
When CNewSeq:
Return true.
When CNewSet:
Return true.
When CNew (typeName, fieldNames, fieldExprs):
Repeat for fe in fieldExprs:
Let s be isStaticM(fe).
If not s:
Return false.
Return true.
When CRange (start, end):
Let s1 be isStaticM(start).
Let s2 be isStaticM(end).
Return (s1 and s2).
When CCopy (target):
Return isStaticM(target).
When CClosure (clp, clb, clc):
Return true.
Otherwise:
Return false.
## To isStaticValueM (e: CExpr) -> Bool:
Inspect e:
When CNewSeq:
Return false.
When CNewSet:
Return false.
Otherwise:
Return isStaticM(e).
## To allStaticM (args: Seq of CExpr) -> Bool:
Repeat for a in args:
Let s be isStaticM(a).
If not s:
Return false.
Return true.
## To makeCycleKeyM (fnName: Text) and (args: Seq of CExpr) -> Text:
Let key be "spec_{fnName}".
Repeat for a in args:
If isStaticM(a):
Let part be exprToKeyPartM(a).
Set key to "{key}_s{part}".
Otherwise:
Set key to "{key}_d".
Return key.
## To checkVNothingM (v: CVal) -> Bool:
Inspect v:
When VNothing:
Return true.
Otherwise:
Return false.
## To exprToValM (e: CExpr) -> CVal:
Inspect e:
When CInt (v):
Return a new VInt with value v.
When CBool (v):
Return a new VBool with value v.
When CText (v):
Return a new VText with value v.
When CFloat (v):
Return a new VFloat with value v.
When CList (items):
Let vals be a new Seq of CVal.
Repeat for elem in items:
Push exprToValM(elem) to vals.
Return a new VSeq with items vals.
When CNewVariant (tag, names, vals):
Let vvals be a new Seq of CVal.
Repeat for v in vals:
Push exprToValM(v) to vvals.
Return a new VVariant with typeName tag and variantName tag and fields vvals.
When CNew (typeName, fieldNames, fieldExprs):
If typeName equals "Map":
Return a new VMap with entries a new Map of Text to CVal.
Let vvals be a new Seq of CVal.
Repeat for fe in fieldExprs:
Push exprToValM(fe) to vvals.
Return a new VVariant with typeName typeName and variantName typeName and fields vvals.
When CCopy (target):
Return exprToValM(target).
Otherwise:
Return a new VNothing.
## To valToExprM (v: CVal) -> CExpr:
Inspect v:
When VInt (n):
Return a new CInt with value n.
When VBool (b):
Return a new CBool with value b.
When VText (s):
Return a new CText with value s.
When VFloat (f):
Return a new CFloat with value f.
When VSeq (items):
Let exprs be a new Seq of CExpr.
Repeat for elem in items:
Push valToExprM(elem) to exprs.
Return a new CList with items exprs.
When VOption (inner, present):
If present:
Return a new COptionSome with inner (valToExprM(inner)).
Return a new COptionNone.
When VMap (entries):
Return a new CNew with typeName "Map" and fieldNames (a new Seq of Text) and fields (a new Seq of CExpr).
Otherwise:
Return a new CVar with name "__unresolvable".
## To evalBinOpM (binOp: Text) and (lv: CVal) and (rv: CVal) -> CVal:
Inspect lv:
When VInt (a):
Inspect rv:
When VInt (b):
If binOp equals "+":
Return a new VInt with value (a + b).
If binOp equals "-":
Return a new VInt with value (a - b).
If binOp equals "*":
Return a new VInt with value (a * b).
If binOp equals "/":
If b equals 0:
Return a new VNothing.
Return a new VInt with value (a / b).
If binOp equals "%":
If b equals 0:
Return a new VNothing.
Return a new VInt with value (a % b).
If binOp equals "<":
Return a new VBool with value (a is less than b).
If binOp equals ">":
Return a new VBool with value (a is greater than b).
If binOp equals "<=":
Return a new VBool with value (a is at most b).
If binOp equals ">=":
Return a new VBool with value (a is at least b).
If binOp equals "==":
Return a new VBool with value (a equals b).
If binOp equals "!=":
Return a new VBool with value (a is not b).
Return a new VNothing.
Otherwise:
Return a new VNothing.
When VBool (a):
Inspect rv:
When VBool (b):
If binOp equals "&&":
Return a new VBool with value (a and b).
If binOp equals "||":
Return a new VBool with value (a or b).
If binOp equals "==":
Return a new VBool with value (a equals b).
If binOp equals "!=":
Return a new VBool with value (a is not b).
Return a new VNothing.
Otherwise:
Return a new VNothing.
When VText (a):
Inspect rv:
When VText (b):
If binOp equals "+":
Let joined be "{a}{b}".
Return a new VText with value joined.
If binOp equals "==":
Return a new VBool with value (a equals b).
If binOp equals "!=":
Return a new VBool with value (a is not b).
Return a new VNothing.
Otherwise:
Return a new VNothing.
Otherwise:
Return a new VNothing.
## To checkLiteralM (e: CExpr) -> Bool:
Inspect e:
When CInt (v):
Return true.
When CBool (v):
Return true.
When CText (v):
Return true.
Otherwise:
Return false.
## To exprToKeyPartM (e: CExpr) -> Text:
Inspect e:
When CInt (v):
Return "i{v}".
When CBool (v):
If v:
Return "btrue".
Return "bfalse".
When CText (v):
Return "t{v}".
When CFloat (v):
Return "f{v}".
When CList (items):
Let mutable parts be "l".
Repeat for elem in items:
Let ep be exprToKeyPartM(elem).
Set parts to "{parts}_{ep}".
Return parts.
When CNewVariant (tag, names, vals):
Let mutable parts be "v{tag}".
Repeat for fv in vals:
Let vp be exprToKeyPartM(fv).
Set parts to "{parts}_{vp}".
Return parts.
When CTuple (items):
Let mutable parts be "tp".
Repeat for elem in items:
Let ep be exprToKeyPartM(elem).
Set parts to "{parts}_{ep}".
Return parts.
When COptionSome (inner):
Let ip be exprToKeyPartM(inner).
Return "s{ip}".
When COptionNone:
Return "none".
When CVar (varName):
Return "v{varName}".
When CBinOp (op, left, right):
Let lp be exprToKeyPartM(left).
Let rp be exprToKeyPartM(right).
Return "op{op}_{lp}_{rp}".
When CNot (inner):
Let ip be exprToKeyPartM(inner).
Return "not_{ip}".
When CCall (fnName, args):
Let mutable parts be "call{fnName}".
Repeat for a in args:
Let ap be exprToKeyPartM(a).
Set parts to "{parts}_{ap}".
Return parts.
When CIndex (coll, idx):
Let cp be exprToKeyPartM(coll).
Let ip be exprToKeyPartM(idx).
Return "idx_{cp}_{ip}".
When CLen (target):
Let tp be exprToKeyPartM(target).
Return "len_{tp}".
When CMapGet (mapExpr, key):
Let mp be exprToKeyPartM(mapExpr).
Let kp be exprToKeyPartM(key).
Return "mg_{mp}_{kp}".
When CNewSeq:
Return "seq".
When CNewSet:
Return "set".
When CFieldAccess (target, field):
Let tp be exprToKeyPartM(target).
Return "fa{field}_{tp}".
When CNew (typeName, fieldNames, fieldExprs):
Let mutable parts be "new{typeName}".
Repeat for fv in fieldExprs:
Let vp be exprToKeyPartM(fv).
Set parts to "{parts}_{vp}".
Return parts.
When CRange (rangeStart, rangeEnd):
Let sp be exprToKeyPartM(rangeStart).
Let ep be exprToKeyPartM(rangeEnd).
Return "r_{sp}_{ep}".
When CSlice (sliceColl, sliceStart, sliceEnd):
Let cp be exprToKeyPartM(sliceColl).
Let sp be exprToKeyPartM(sliceStart).
Let ep be exprToKeyPartM(sliceEnd).
Return "sl_{cp}_{sp}_{ep}".
When CCopy (copyTarget):
Let tp be exprToKeyPartM(copyTarget).
Return "cp_{tp}".
When CContains (containsColl, containsElem):
Let cp be exprToKeyPartM(containsColl).
Let ep be exprToKeyPartM(containsElem).
Return "ct_{cp}_{ep}".
When CUnion (unionLeft, unionRight):
Let lp be exprToKeyPartM(unionLeft).
Let rp be exprToKeyPartM(unionRight).
Return "un_{lp}_{rp}".
When CIntersection (interLeft, interRight):
Let lp be exprToKeyPartM(interLeft).
Let rp be exprToKeyPartM(interRight).
Return "ix_{lp}_{rp}".
When CClosure (clParams, clBody, clCaptured):
Return "cl".
When CCallExpr (ceTarget, ceArgs):
Let tp be exprToKeyPartM(ceTarget).
Return "ce_{tp}".
When CInterpolatedString (isParts):
Let mutable ps be "is".
Repeat for isPart in isParts:
Inspect isPart:
When CLiteralPart (litVal):
Set ps to "{ps}_lt{litVal}".
When CExprPart (epExpr):
Let pp be exprToKeyPartM(epExpr).
Set ps to "{ps}_{pp}".
Return ps.
When CDuration (durAmount, durUnit):
Let ap be exprToKeyPartM(durAmount).
Return "dur_{ap}_{durUnit}".
When CTimeNow:
Return "tnow".
When CDateToday:
Return "today".
When CEscExpr (escCode):
Return "esc".
Otherwise:
Return "unk".
## To makeKeyM (fnName: Text) and (args: Seq of CExpr) -> Text:
Let key be fnName.
Repeat for a in args:
Let part be exprToKeyPartM(a).
Set key to "{key}_{part}".
Return key.
## To peExprM (e: CExpr) and (state: PEMiniState) -> CExpr:
Let env be peEnvM(state).
Let funcs be peFuncsM(state).
Let depth be peDepthM(state).
Inspect e:
When CInt (v):
Return a new CInt with value v.
When CBool (v):
Return a new CBool with value v.
When CText (v):
Return a new CText with value v.
When CFloat (fv):
Return a new CFloat with value fv.
When CVar (varName):
Let sEnv be peStaticEnvM(state).
Let svn be "{varName}".
If sEnv contains svn:
Let staticVal be item svn of sEnv.
Return staticVal.
Let vn be "{varName}".
If env contains vn:
Let val be item vn of env.
Let dyn be checkVNothingM(val).
If dyn:
Return a new CVar with name varName.
Return valToExprM(val).
Return a new CVar with name varName.
When CBinOp (binOp, left, right):
Let peLeft be peExprM(left, state).
Let peRight be peExprM(right, state).
Let leftLit be checkLiteralM(peLeft).
Let rightLit be checkLiteralM(peRight).
If leftLit and rightLit:
Let lv be exprToValM(peLeft).
Let rv be exprToValM(peRight).
Let computed be evalBinOpM(binOp, lv, rv).
Return valToExprM(computed).
Return a new CBinOp with op binOp and left peLeft and right peRight.
When CNot (inner):
Let peInner be peExprM(inner, state).
Let innerLit be checkLiteralM(peInner).
If innerLit:
Inspect peInner:
When CBool (b):
Return a new CBool with value (not b).
Otherwise:
Return a new CNot with inner peInner.
Return a new CNot with inner peInner.
When CCall (fnName, argExprs):
If depth equals 0:
Let residArgs be a new Seq of CExpr.
Repeat for a in argExprs:
Push peExprM(a, state) to residArgs.
Return a new CCall with name fnName and args residArgs.
Let peArgs be a new Seq of CExpr.
Repeat for a in argExprs:
Push peExprM(a, state) to peArgs.
Let fnClKeyM be "{fnName}".
Let sEnvFCM be peStaticEnvM(state).
If sEnvFCM contains fnClKeyM:
Let fcValM be item fnClKeyM of sEnvFCM.
Inspect fcValM:
When CClosure (clParams, clBody, clCaptured):
Let clChildSeM be copy of sEnvFCM.
Let clCallEnvM be copy of env.
Let mutable cpiM be 1.
Repeat for clp in clParams:
Let clArgM be item cpiM of peArgs.
Let clpkM be "{clp}".
Let clArgCM be copy of clArgM.
Set item clpkM of clChildSeM to clArgCM.
Let clpk2M be "{clp}".
If isStaticM(clArgM):
Set item clpk2M of clCallEnvM to exprToValM(clArgM).
Otherwise:
Set item clpk2M of clCallEnvM to a new VNothing.
Set cpiM to cpiM + 1.
Let clStateM be peStateWithEnvDepthStaticM(state, clCallEnvM, depth - 1, clChildSeM).
Let clBodyResM be peBlockM(clBody, clStateM).
Let clRawM be extractReturnM(clBodyResM).
Let clResultM be validateExtractReturnM(clRawM, clBodyResM).
Inspect clResultM:
When CVar (clrnM):
If clrnM equals "__no_return__":
Let clResidArgsM be copy of peArgs.
Let clTargetExprM be a new CVar with name fnName.
Return a new CCallExpr with target clTargetExprM and args clResidArgsM.
Otherwise:
Let skipClRM be true.
Return clResultM.
Otherwise:
Let skipFCvM be true.
Let allStat be allStaticM(peArgs).
If allStat:
Let fn1 be "{fnName}".
Let memoKey be makeKeyM(fn1, peArgs).
Let memoKeyStr be "{memoKey}".
Let mc be peMemoCacheM(state).
Let mk0 be "{memoKeyStr}".
If mc contains mk0:
Let cached be item mk0 of mc.
Return cached.
Let osLocal be peOnStackM(state).
Let mutable osHitM be false.
Repeat for osKey in osLocal:
If osKey equals memoKeyStr:
Set osHitM to true.
If osHitM:
Return a new CCall with name fnName and args peArgs.
If funcs contains fn1:
Let func be item fn1 of funcs.
Inspect func:
When CFuncDef (fname, params, fnParamTypes, fnReturnType, body):
Let callEnv be a new Map of Text to CVal.
Let childSEnv be a new Map of Text to CExpr.
Let mutable pidx be 1.
Repeat for p in params:
Let argExpr be item pidx of peArgs.
Let argVal be exprToValM(argExpr).
Let pk1 be "{p}".
Set item pk1 of callEnv to argVal.
Let cpSafeArg be isCopyPropSafeM(argExpr).
If cpSafeArg:
Let pk2 be "{p}".
Set item pk2 of childSEnv to (copy of argExpr).
Set pidx to pidx + 1.
Let childOsM be copy of osLocal.
Let memoKeyStrCopy be "{memoKeyStr}".
Push memoKeyStrCopy to childOsM.
Let childState be peStateWithEnvDepthStaticM(state, callEnv, depth - 1, childSEnv).
Inspect childState:
When PEMiniR (ce, cf, cd, cse, cmc, cos):
Set childState to a new PEMiniR with env ce and funcs cf and depth cd and staticEnv cse and memoCache cmc and onStackM childOsM.
Let peBody be peBlockM(body, childState).
Let rawResultM be extractReturnM(peBody).
Let bodyResult be validateExtractReturnM(rawResultM, peBody).
Inspect bodyResult:
When CVar (brName2):
If brName2 equals "__no_return__":
Return a new CCall with name fnName and args peArgs.
Otherwise:
Let skip be true.
Let mk3 be "{memoKeyStr}".
Let bodyResultCopy be copy of bodyResult.
miniSeBind(mc, mk3, bodyResultCopy).
Return bodyResult.
Otherwise:
Let fn2 be "{fnName}".
Return a new CCall with name fn2 and args peArgs.
Let mutable hasSomeStatic be false.
Let mutable hasSomeDynamic be false.
Repeat for pa in peArgs:
If isStaticM(pa):
Set hasSomeStatic to true.
Otherwise:
Set hasSomeDynamic to true.
If hasSomeStatic:
If hasSomeDynamic:
Let specKeyStr be makeCycleKeyM(fnName, peArgs).
Let specCache be peMemoCacheM(state).
Let sk1 be "{specKeyStr}".
If specCache contains sk1:
Let dynArgs be a new Seq of CExpr.
Repeat for pa in peArgs:
If not isStaticM(pa):
Push pa to dynArgs.
Let sk2 be "{specKeyStr}".
Return a new CCall with name sk2 and args dynArgs.
Let fn4 be "{fnName}".
If funcs contains fn4:
Let func2 be item fn4 of funcs.
Inspect func2:
When CFuncDef (fname, params, fnParamTypes, fnReturnType, body):
Let callEnv be a new Map of Text to CVal.
Let childSEnv be a new Map of Text to CExpr.
Let dynParams be a new Seq of Text.
Let mutable pidx be 1.
Repeat for p in params:
Let argExpr be item pidx of peArgs.
If isStaticM(argExpr):
Let argVal be exprToValM(argExpr).
Let pk1 be "{p}".
Set item pk1 of callEnv to argVal.
Let pk2 be "{p}".
Let argExprCopy be copy of argExpr.
Set item pk2 of childSEnv to argExprCopy.
Otherwise:
Let pk3 be "{p}".
Push pk3 to dynParams.
Let pk4 be "{p}".
Set item pk4 of callEnv to a new VNothing.
Set pidx to pidx + 1.
Let sk3 be "{specKeyStr}".
miniSeBind(specCache, sk3, (a new CInt with value 0)).
Let placeholderBody be a new Seq of CStmt.
Let sk4 be "{specKeyStr}".
Let phDynParams be a new Seq of Text.
Repeat for dp in dynParams:
Let dpCopy be "{dp}".
Push dpCopy to phDynParams.
Let phPT be a new Seq of Text.
Let placeholderFunc be a new CFuncDef with name sk4 and params phDynParams and paramTypes phPT and returnType "Any" and body placeholderBody.
Let sk5 be "{specKeyStr}".
Set item sk5 of funcs to placeholderFunc.
Let childState be peStateWithEnvDepthStaticM(state, callEnv, depth - 1, childSEnv).
Let peBody be peBlockM(body, childState).
Let rawResultM2 be extractReturnM(peBody).
Let bodyResult be validateExtractReturnM(rawResultM2, peBody).
Let sk6 be "{specKeyStr}".
Let spDynParams be a new Seq of Text.
Repeat for dp in dynParams:
Let dpCopy be "{dp}".
Push dpCopy to spDynParams.
Let spPT be a new Seq of Text.
Let specFunc be a new CFuncDef with name sk6 and params spDynParams and paramTypes spPT and returnType "Any" and body peBody.
Let sk7 be "{specKeyStr}".
Set item sk7 of funcs to specFunc.
Inspect bodyResult:
When CVar (brName):
If brName equals "__no_return__":
Let fallbackDynArgs be a new Seq of CExpr.
Repeat for pa in peArgs:
If not isStaticM(pa):
Push pa to fallbackDynArgs.
Let sk8 be "{specKeyStr}".
Return a new CCall with name sk8 and args fallbackDynArgs.
Otherwise:
Let skip2 be true.
Return bodyResult.
Otherwise:
Let fn5 be "{fnName}".
Return a new CCall with name fn5 and args peArgs.
Return a new CCall with name fnName and args peArgs.
When CIndex (collExpr, idxExpr):
Let peColl be peExprM(collExpr, state).
Let peIdx be peExprM(idxExpr, state).
Return a new CIndex with coll peColl and idx peIdx.
When CLen (lenTarget):
Let peTarget be peExprM(lenTarget, state).
Return a new CLen with target peTarget.
When CMapGet (mapTarget, mapKey):
Let peTarget be peExprM(mapTarget, state).
Let peKey be peExprM(mapKey, state).
Inspect peTarget:
When CNewVariant (nvTag, nvNames, nvVals):
Inspect peKey:
When CText (keyStr):
If keyStr equals "__tag":
Return a new CText with value nvTag.
Let mutable fidx be 1.
Repeat for fname in nvNames:
If fname equals keyStr:
Return item fidx of nvVals.
Set fidx to fidx + 1.
Otherwise:
Let skip be true.
When CNew (cnTypeName, cnFieldNames, cnFieldVals):
Inspect peKey:
When CText (keyStr):
If keyStr equals "__tag":
Return a new CText with value cnTypeName.
Let mutable fidx2 be 1.
Repeat for fn2 in cnFieldNames:
If fn2 equals keyStr:
Return item fidx2 of cnFieldVals.
Set fidx2 to fidx2 + 1.
Otherwise:
Let skip be true.
Otherwise:
Let skip be true.
Return a new CMapGet with target peTarget and key peKey.
When CNewSeq:
Return a new CNewSeq.
When CNewSet:
Return a new CNewSet.
When CNewVariant (nvTag, nvNames, nvVals):
Let peVals be a new Seq of CExpr.
Repeat for v in nvVals:
Push peExprM(v, state) to peVals.
Return a new CNewVariant with tag nvTag and fnames nvNames and fvals peVals.
When CList (listItems):
Let peItems be a new Seq of CExpr.
Repeat for li in listItems:
Push peExprM(li, state) to peItems.
Return a new CList with items peItems.
When CRange (rangeStart, rangeEnd):
Let peStart be peExprM(rangeStart, state).
Let peEnd be peExprM(rangeEnd, state).
Return a new CRange with start peStart and end peEnd.
When CSlice (sliceColl, sliceStart, sliceEnd):
Let peColl be peExprM(sliceColl, state).
Let peStart be peExprM(sliceStart, state).
Let peEnd be peExprM(sliceEnd, state).
Return a new CSlice with coll peColl and startIdx peStart and endIdx peEnd.
When CCopy (copyTarget):
Let peTarget be peExprM(copyTarget, state).
Return a new CCopy with target peTarget.
When CContains (containsColl, containsElem):
Let peColl be peExprM(containsColl, state).
Let peElem be peExprM(containsElem, state).
Inspect peColl:
When CNew (cnTypeName, cnFieldNames, cnFields):
If length of cnFields equals 0:
Return a new CBool with value false.
When CNewSeq:
Return a new CBool with value false.
When CNewSet:
Return a new CBool with value false.
When CList (litems):
If length of litems equals 0:
Return a new CBool with value false.
Otherwise:
Let skipContains be true.
Return a new CContains with coll peColl and elem peElem.
When CUnion (unionLeft, unionRight):
Let peLeft be peExprM(unionLeft, state).
Let peRight be peExprM(unionRight, state).
Return a new CUnion with left peLeft and right peRight.
When CIntersection (interLeft, interRight):
Let peLeft be peExprM(interLeft, state).
Let peRight be peExprM(interRight, state).
Return a new CIntersection with left peLeft and right peRight.
When COptionSome (optInner):
Let peInner be peExprM(optInner, state).
Return a new COptionSome with inner peInner.
When COptionNone:
Return a new COptionNone.
When CTuple (tupleItems):
Let peItems be a new Seq of CExpr.
Repeat for ti in tupleItems:
Push peExprM(ti, state) to peItems.
Return a new CTuple with items peItems.
When CNew (newTypeName, newFieldNames, newFieldExprs):
Let peNewFields be a new Seq of CExpr.
Repeat for nfe in newFieldExprs:
Push peExprM(nfe, state) to peNewFields.
Return a new CNew with typeName newTypeName and fieldNames newFieldNames and fields peNewFields.
When CFieldAccess (faTarget, faField):
Let peFaTarget be peExprM(faTarget, state).
Return a new CFieldAccess with target peFaTarget and field faField.
When CClosure (clParams, clBody, clCaptured):
Let peClBody be peBlockM(clBody, state).
Return a new CClosure with params clParams and body peClBody and captured clCaptured.
When CCallExpr (ceTarget, ceArgs):
Let peCeTarget be peExprM(ceTarget, state).
Let peCeArgs be a new Seq of CExpr.
Repeat for ceArg in ceArgs:
Push peExprM(ceArg, state) to peCeArgs.
Return a new CCallExpr with target peCeTarget and args peCeArgs.
When CInterpolatedString (isParts):
Let peIsParts be a new Seq of CStringPart.
Repeat for isPart in isParts:
Inspect isPart:
When CLiteralPart (litVal):
Push a new CLiteralPart with value litVal to peIsParts.
When CExprPart (epExpr):
Let peEpExpr be peExprM(epExpr, state).
Push a new CExprPart with expr peEpExpr to peIsParts.
Return a new CInterpolatedString with parts peIsParts.
When CDuration (durAmount, durUnit):
Let peDurAmount be peExprM(durAmount, state).
Return a new CDuration with amount peDurAmount and unit durUnit.
When CTimeNow:
Return a new CTimeNow.
When CDateToday:
Return a new CDateToday.
When CEscExpr (escCode):
Return a new CEscExpr with code escCode.
When CManifestOf (zn):
Let peZone be peExprM(zn, state).
Return a new CManifestOf with zn peZone.
When CChunkAt (idx, zn):
Let peIdx be peExprM(idx, state).
Let peZone be peExprM(zn, state).
Return a new CChunkAt with idx peIdx and zn peZone.
## To hasReturnM (stmts: Seq of CStmt) -> Bool:
Repeat for s in stmts:
Inspect s:
When CReturn (retExpr):
Return true.
Otherwise:
Let skip be true.
Return false.
## To collectSetVarsM (stmts: Seq of CStmt) -> Seq of Text:
Let result be a new Seq of Text.
Repeat for s in stmts:
Inspect s:
When CSet (setName, setExpr):
Push setName to result.
When CIf (ifCond, ifThen, ifElse):
Let thenVars be collectSetVarsM(ifThen).
Repeat for tv in thenVars:
Push tv to result.
Let elseVars be collectSetVarsM(ifElse).
Repeat for ev in elseVars:
Push ev to result.
When CWhile (wCond, wBody):
Let wVars be collectSetVarsM(wBody).
Repeat for wv in wVars:
Push wv to result.
When CRepeat (rVar, rColl, rBody):
Let rVars be collectSetVarsM(rBody).
Repeat for rv in rVars:
Push rv to result.
When CRepeatRange (rrRVar, rrRStart, rrREnd, rrRBody):
Let rrRVars be collectSetVarsM(rrRBody).
Repeat for rrRv in rrRVars:
Push rrRv to result.
When CInspect (inspTarget, inspArms):
Repeat for cinsArm in inspArms:
Inspect cinsArm:
When CWhen (cwName, cwBindings, cwBody):
Let cwVars be collectSetVarsM(cwBody).
Repeat for cwv in cwVars:
Push cwv to result.
Otherwise:
Let skipCinsArm be true.
When CSelect (selBranches):
Repeat for cselBr in selBranches:
Inspect cselBr:
When CSelectRecv (csrChan, csrVar, csrBody):
Let csrVars be collectSetVarsM(csrBody).
Repeat for csrv in csrVars:
Push csrv to result.
When CSelectTimeout (cstDur, cstBody):
Let cstVars be collectSetVarsM(cstBody).
Repeat for cstv in cstVars:
Push cstv to result.
Otherwise:
Let skipCselBr be true.
When CZone (zoneName, zoneKind, zoneBody):
Let zVars be collectSetVarsM(zoneBody).
Repeat for zv in zVars:
Push zv to result.
When CConcurrent (concBranches):
Repeat for concBranch in concBranches:
Let concVars be collectSetVarsM(concBranch).
Repeat for ccv in concVars:
Push ccv to result.
When CParallel (parBranches):
Repeat for parBranch in parBranches:
Let parVars be collectSetVarsM(parBranch).
Repeat for pcv in parVars:
Push pcv to result.
When CLaunchTask (ltBody, ltHandle):
Let ltVars be collectSetVarsM(ltBody).
Repeat for ltv in ltVars:
Push ltv to result.
Otherwise:
Let skip be true.
Return result.
## To isDeadTargetM (targetName: Text) and (env: Map of Text to CVal) -> Bool:
Let tk be "{targetName}".
If env contains tk:
Let tv be item tk of env.
Let tvn be checkVNothingM(tv).
If tvn:
Return false.
Return true.
Return false.
## To peBlockM (stmts: Seq of CStmt) and (state: PEMiniState) -> Seq of CStmt:
Let env be peEnvM(state).
Let funcs be peFuncsM(state).
Let depth be peDepthM(state).
Let mutable blockMutatedDone be false.
Let mutable blockMutated be a new Seq of Text.
Let blockResult be a new Seq of CStmt.
Repeat for s in stmts:
Let alreadyReturned be hasReturnM(blockResult).
If alreadyReturned:
Return blockResult.
Inspect s:
When CLet (letName, letExpr):
Let peVal be peExprM(letExpr, state).
Let litV be isStaticValueM(peVal).
Let cpvL be isPeValueForCopyPropM(peVal).
Let mutable cpOk be cpvL.
If not cpvL:
If not blockMutatedDone:
Set blockMutated to collectSetVarsM(stmts).
Set blockMutatedDone to true.
Let rmmL be exprRefsAnyMutatedM(peVal, blockMutated).
If not rmmL:
Set cpOk to true.
If cpOk:
Let letNameS be "{letName}".
Let peValS be copy of peVal.
Let sEnvL be peStaticEnvM(state).
miniSeBind(sEnvL, letNameS, peValS).
Otherwise:
Let sEnvL2 be peStaticEnvM(state).
Let letNameR be "{letName}".
If sEnvL2 contains letNameR:
miniSeDrop(sEnvL2, letNameR).
If litV:
miniEnvBind(env, letName, exprToValM(peVal)).
Otherwise:
Let letKey be "{letName}".
miniEnvBind(env, letKey, (a new VNothing)).
Push (a new CLet with name letName and expr peVal) to blockResult.
When CSet (setName, setExpr):
Let peVal be peExprM(setExpr, state).
Let litV be isStaticValueM(peVal).
Let cpvSL be isPeValueForCopyPropM(peVal).
Let mutable cpOkS be cpvSL.
If not cpvSL:
If not blockMutatedDone:
Set blockMutated to collectSetVarsM(stmts).
Set blockMutatedDone to true.
Let rmmSL be exprRefsAnyMutatedM(peVal, blockMutated).
If not rmmSL:
Set cpOkS to true.
Let sEnvCS be peStaticEnvM(state).
Let setNameChk be "{setName}".
Let mutable priorWasStatic be false.
Let mutable priorStaticVal be (a new CInt with value 0).
If sEnvCS contains setNameChk:
Let setNameItm be "{setName}".
Let priorCV be item setNameItm of sEnvCS.
Let pcvStatic be isStaticValueM(priorCV).
If pcvStatic:
Set priorWasStatic to true.
Set priorStaticVal to copy of priorCV.
If cpOkS:
Let setNameS be "{setName}".
Let peValS be copy of peVal.
miniSeBind(sEnvCS, setNameS, peValS).
Otherwise:
Let setNameR be "{setName}".
If sEnvCS contains setNameR:
Let setNameD be "{setName}".
miniSeDrop(sEnvCS, setNameD).
If litV:
miniEnvBind(env, setName, exprToValM(peVal)).
Otherwise:
If priorWasStatic:
Let setNameL be "{setName}".
Push (a new CLet with name setNameL and expr priorStaticVal) to blockResult.
Let setKey be "{setName}".
miniEnvBind(env, setKey, (a new VNothing)).
Push (a new CSet with name setName and expr peVal) to blockResult.
When CIf (ifCond, thenBlock, elseBlock):
Let peCond be peExprM(ifCond, state).
Let condLit be isStaticM(peCond).
If condLit:
Inspect peCond:
When CBool (b):
If b:
Let thenRes be peBlockM(thenBlock, state).
Repeat for ts in thenRes:
Push ts to blockResult.
Otherwise:
Let elseRes be peBlockM(elseBlock, state).
Repeat for es in elseRes:
Push es to blockResult.
Otherwise:
Let thenRes2 be peBlockM(thenBlock, state).
Let elseRes2 be peBlockM(elseBlock, state).
Push (a new CIf with cond peCond and thenBlock thenRes2 and elseBlock elseRes2) to blockResult.
Otherwise:
Let thenModsIf be collectSetVarsM(thenBlock).
Let elseModsIf be collectSetVarsM(elseBlock).
Let allModsIf be a new Seq of Text.
Repeat for tmI in thenModsIf:
Push tmI to allModsIf.
Repeat for emI in elseModsIf:
Push emI to allModsIf.
Let sEnvIf be peStaticEnvM(state).
Repeat for pmI in allModsIf:
Let pmK1 be "{pmI}".
If sEnvIf contains pmK1:
Let pmK2 be "{pmI}".
Let pmV be item pmK2 of sEnvIf.
Let pmK3 be "{pmI}".
Push (a new CLet with name pmK3 and expr pmV) to blockResult.
Let pmK4 be "{pmI}".
miniSeDrop(sEnvIf, pmK4).
Let pmK5 be "{pmI}".
miniEnvBind(env, pmK5, (a new VNothing)).
Let thenRes3 be peBlockM(thenBlock, state).
Let thenFinal be a new Seq of CStmt.
Repeat for tstmt in thenRes3:
Push tstmt to thenFinal.
Let sEnvThenIf be peStaticEnvM(state).
Repeat for tmv in thenModsIf:
Let tmvK1 be "{tmv}".
If sEnvThenIf contains tmvK1:
Let tmvK2 be "{tmv}".
Let tmvV be item tmvK2 of sEnvThenIf.
Let tmvK3 be "{tmv}".
Push (a new CSet with name tmvK3 and expr tmvV) to thenFinal.
Repeat for rmv in allModsIf:
Let rmvK1 be "{rmv}".
If sEnvThenIf contains rmvK1:
Let rmvK2 be "{rmv}".
miniSeDrop(sEnvThenIf, rmvK2).
Let rmvK3 be "{rmv}".
miniEnvBind(env, rmvK3, (a new VNothing)).
Let elseRes3 be peBlockM(elseBlock, state).
Let elseFinal be a new Seq of CStmt.
Repeat for estmt in elseRes3:
Push estmt to elseFinal.
Let sEnvElseIf be peStaticEnvM(state).
Repeat for emv in elseModsIf:
Let emvK1 be "{emv}".
If sEnvElseIf contains emvK1:
Let emvK2 be "{emv}".
Let emvV be item emvK2 of sEnvElseIf.
Let emvK3 be "{emv}".
Push (a new CSet with name emvK3 and expr emvV) to elseFinal.
Repeat for dmv in allModsIf:
Let dmvK1 be "{dmv}".
If sEnvElseIf contains dmvK1:
Let dmvK2 be "{dmv}".
miniSeDrop(sEnvElseIf, dmvK2).
Let dmvK3 be "{dmv}".
miniEnvBind(env, dmvK3, (a new VNothing)).
Push (a new CIf with cond peCond and thenBlock thenFinal and elseBlock elseFinal) to blockResult.
When CWhile (whileCond, whileBody):
Let modVars be collectSetVarsM(whileBody).
Let sEnvW be peStaticEnvM(state).
Repeat for mv in modVars:
Let mvK1 be "{mv}".
If sEnvW contains mvK1:
Let mvK2 be "{mv}".
Let prevVal be item mvK2 of sEnvW.
Let mvK3 be "{mv}".
Push (a new CLet with name mvK3 and expr prevVal) to blockResult.
Let mvK4 be "{mv}".
miniSeDrop(sEnvW, mvK4).
Let mvK5 be "{mv}".
miniEnvBind(env, mvK5, (a new VNothing)).
Let peCond be peExprM(whileCond, state).
Let condLitW be isStaticM(peCond).
If condLitW:
Inspect peCond:
When CBool (bW):
If not bW:
Let skipW be true.
Otherwise:
Let peBody be peBlockM(whileBody, state).
Let sEnvPost1 be peStaticEnvM(state).
Repeat for mv1 in modVars:
Let mv1K be "{mv1}".
If sEnvPost1 contains mv1K:
Let mv1K2 be "{mv1}".
miniSeDrop(sEnvPost1, mv1K2).
Let mv1K3 be "{mv1}".
miniEnvBind(env, mv1K3, (a new VNothing)).
Push (a new CWhile with cond peCond and body peBody) to blockResult.
Otherwise:
Let peBody2 be peBlockM(whileBody, state).
Let sEnvPost2 be peStaticEnvM(state).
Repeat for mv2 in modVars:
Let mv2K be "{mv2}".
If sEnvPost2 contains mv2K:
Let mv2K2 be "{mv2}".
miniSeDrop(sEnvPost2, mv2K2).
Let mv2K3 be "{mv2}".
miniEnvBind(env, mv2K3, (a new VNothing)).
Push (a new CWhile with cond peCond and body peBody2) to blockResult.
Otherwise:
Let peBody3 be peBlockM(whileBody, state).
Let sEnvPost3 be peStaticEnvM(state).
Repeat for mv3 in modVars:
Let mv3K be "{mv3}".
If sEnvPost3 contains mv3K:
Let mv3K2 be "{mv3}".
miniSeDrop(sEnvPost3, mv3K2).
Let mv3K3 be "{mv3}".
miniEnvBind(env, mv3K3, (a new VNothing)).
Push (a new CWhile with cond peCond and body peBody3) to blockResult.
When CReturn (retExpr):
Let peVal be peExprM(retExpr, state).
Push (a new CReturn with expr peVal) to blockResult.
When CShow (showExpr):
Let peVal be peExprM(showExpr, state).
Push (a new CShow with expr peVal) to blockResult.
When CCallS (callName, callArgs):
Let peArgs be a new Seq of CExpr.
Repeat for a in callArgs:
Push peExprM(a, state) to peArgs.
Push (a new CCallS with name callName and args peArgs) to blockResult.
When CPush (pushExpr, pushTarget):
Let sEnvPush be peStaticEnvM(state).
Let ptk1 be "{pushTarget}".
If sEnvPush contains ptk1:
Let ptk2 be "{pushTarget}".
Let ptPrev be item ptk2 of sEnvPush.
Let ptk3 be "{pushTarget}".
Push (a new CLet with name ptk3 and expr ptPrev) to blockResult.
Let ptk4 be "{pushTarget}".
miniSeDrop(sEnvPush, ptk4).
Let ptk5 be "{pushTarget}".
miniEnvBind(env, ptk5, (a new VNothing)).
Let peVal be peExprM(pushExpr, state).
Push (a new CPush with expr peVal and target pushTarget) to blockResult.
When CSetIdx (siTarget, siIdx, siVal):
Let sEnvSI be peStaticEnvM(state).
Let sik1 be "{siTarget}".
If sEnvSI contains sik1:
Let sik2 be "{siTarget}".
Let siPrev be item sik2 of sEnvSI.
Let sik3 be "{siTarget}".
Push (a new CLet with name sik3 and expr siPrev) to blockResult.
Let sik4 be "{siTarget}".
miniSeDrop(sEnvSI, sik4).
Let sik5 be "{siTarget}".
miniEnvBind(env, sik5, (a new VNothing)).
Let peIdx be peExprM(siIdx, state).
Let peVal be peExprM(siVal, state).
Push (a new CSetIdx with target siTarget and idx peIdx and val peVal) to blockResult.
When CMapSet (msTarget, msKey, msVal):
Let sEnvMS be peStaticEnvM(state).
Let msk1 be "{msTarget}".
If sEnvMS contains msk1:
Let msk2 be "{msTarget}".
Let msPrev be item msk2 of sEnvMS.
Let msk3 be "{msTarget}".
Push (a new CLet with name msk3 and expr msPrev) to blockResult.
Let msk4 be "{msTarget}".
miniSeDrop(sEnvMS, msk4).
Let msk5 be "{msTarget}".
miniEnvBind(env, msk5, (a new VNothing)).
Let peKey be peExprM(msKey, state).
Let peVal be peExprM(msVal, state).
Push (a new CMapSet with target msTarget and key peKey and val peVal) to blockResult.
When CPop (popTarget):
Let sEnvPop be peStaticEnvM(state).
Let ppk1 be "{popTarget}".
If sEnvPop contains ppk1:
Let ppk2 be "{popTarget}".
Let ppPrev be item ppk2 of sEnvPop.
Let ppk3 be "{popTarget}".
Push (a new CLet with name ppk3 and expr ppPrev) to blockResult.
Let ppk4 be "{popTarget}".
miniSeDrop(sEnvPop, ppk4).
Let ppk5 be "{popTarget}".
miniEnvBind(env, ppk5, (a new VNothing)).
Push (a new CPop with target popTarget) to blockResult.
When CRepeat (repVar, repColl, repBody):
Let peColl be peExprM(repColl, state).
Let mutable didUnroll be false.
Inspect peColl:
When CList (listItems):
Set didUnroll to true.
Repeat for listItem in listItems:
Let rv be "{repVar}".
miniEnvBind(env, rv, exprToValM(listItem)).
Let unrolledBody be peBlockM(repBody, state).
Let mutable hasBreakOrReturn be false.
Repeat for us in unrolledBody:
Inspect us:
When CBreak:
Set hasBreakOrReturn to true.
When CReturn (retExpr):
Set hasBreakOrReturn to true.
Otherwise:
Let skip be true.
Repeat for us in unrolledBody:
Push us to blockResult.
If hasBreakOrReturn:
Return blockResult.
Otherwise:
Let skip be true.
If not didUnroll:
Let sEnvF be peStaticEnvM(state).
If sEnvF contains repVar:
miniSeDrop(sEnvF, repVar).
Let repVarEnvKey be "{repVar}".
miniEnvBind(env, repVarEnvKey, (a new VNothing)).
Let repModVars be collectSetVarsM(repBody).
Repeat for rmv in repModVars:
Let rmvK be "{rmv}".
If sEnvF contains rmvK:
Let rmvK2 be "{rmv}".
Let rmvPrev be item rmvK2 of sEnvF.
Let rmvK3 be "{rmv}".
Push (a new CLet with name rmvK3 and expr rmvPrev) to blockResult.
Let rmvK4 be "{rmv}".
miniSeDrop(sEnvF, rmvK4).
Let rmvK5 be "{rmv}".
miniEnvBind(env, rmvK5, (a new VNothing)).
Let peBody be peBlockM(repBody, state).
Push (a new CRepeat with var repVar and coll peColl and body peBody) to blockResult.
When CRepeatRange (rrVar, rrStart, rrEnd, rrBody):
Let peStart be peExprM(rrStart, state).
Let peEnd be peExprM(rrEnd, state).
Let sEnvRR be peStaticEnvM(state).
If sEnvRR contains rrVar:
miniSeDrop(sEnvRR, rrVar).
Let rrVarEnvKey be "{rrVar}".
miniEnvBind(env, rrVarEnvKey, (a new VNothing)).
Let rrModVars be collectSetVarsM(rrBody).
Repeat for rrmv in rrModVars:
Let rrmvK be "{rrmv}".
If sEnvRR contains rrmvK:
Let rrmvK2 be "{rrmv}".
Let rrmvPrev be item rrmvK2 of sEnvRR.
Let rrmvK3 be "{rrmv}".
Push (a new CLet with name rrmvK3 and expr rrmvPrev) to blockResult.
Let rrmvK4 be "{rrmv}".
miniSeDrop(sEnvRR, rrmvK4).
Let rrmvK5 be "{rrmv}".
miniEnvBind(env, rrmvK5, (a new VNothing)).
Let peBody be peBlockM(rrBody, state).
Let sEnvRR3 be peStaticEnvM(state).
Repeat for rrmv2 in rrModVars:
Let rrmv2K be "{rrmv2}".
If sEnvRR3 contains rrmv2K:
miniSeDrop(sEnvRR3, rrmv2K).
Push (a new CRepeatRange with var rrVar and start peStart and end peEnd and body peBody) to blockResult.
When CBreak:
Push a new CBreak to blockResult.
When CForceDynamic (fdName):
Let sEnvFd be peStaticEnvM(state).
Let fdk be "{fdName}".
If sEnvFd contains fdk:
Let fdk2 be "{fdName}".
Let fdPrev be item fdk of sEnvFd.
Push (a new CLet with name fdName and expr fdPrev) to blockResult.
miniSeDrop(sEnvFd, fdk).
miniEnvBind(env, fdk2, (a new VNothing)).
When CAdd (addElem, addTarget):
Let sEnvAdd be peStaticEnvM(state).
Let adk1 be "{addTarget}".
If sEnvAdd contains adk1:
Let adk2 be "{addTarget}".
Let adPrev be item adk2 of sEnvAdd.
Let adk3 be "{addTarget}".
Push (a new CLet with name adk3 and expr adPrev) to blockResult.
Let adk4 be "{addTarget}".
miniSeDrop(sEnvAdd, adk4).
Let adk5 be "{addTarget}".
miniEnvBind(env, adk5, (a new VNothing)).
Let peVal be peExprM(addElem, state).
Push (a new CAdd with elem peVal and target addTarget) to blockResult.
When CRemove (remElem, remTarget):
Let sEnvRem be peStaticEnvM(state).
Let rmk1 be "{remTarget}".
If sEnvRem contains rmk1:
Let rmk2 be "{remTarget}".
Let rmPrev be item rmk2 of sEnvRem.
Let rmk3 be "{remTarget}".
Push (a new CLet with name rmk3 and expr rmPrev) to blockResult.
Let rmk4 be "{remTarget}".
miniSeDrop(sEnvRem, rmk4).
Let rmk5 be "{remTarget}".
miniEnvBind(env, rmk5, (a new VNothing)).
Let peVal be peExprM(remElem, state).
Push (a new CRemove with elem peVal and target remTarget) to blockResult.
When CSetField (sfTarget, sfField, sfValExpr):
Let sEnvSF be peStaticEnvM(state).
Let sfk1 be "{sfTarget}".
If sEnvSF contains sfk1:
Let sfk2 be "{sfTarget}".
Let sfPrev be item sfk2 of sEnvSF.
Let sfk3 be "{sfTarget}".
Push (a new CLet with name sfk3 and expr sfPrev) to blockResult.
Let sfk4 be "{sfTarget}".
miniSeDrop(sEnvSF, sfk4).
Let sfk5 be "{sfTarget}".
miniEnvBind(env, sfk5, (a new VNothing)).
Let peVal be peExprM(sfValExpr, state).
Push (a new CSetField with target sfTarget and field sfField and val peVal) to blockResult.
When CStructDef (sdName, sdFieldNames):
Push (a new CStructDef with name sdName and fieldNames sdFieldNames) to blockResult.
When CEnumDef (edName, edVariants):
Push (a new CEnumDef with name edName and variants edVariants) to blockResult.
When CInspect (inspTarget, inspArms):
Let peTarget be peExprM(inspTarget, state).
Let mutable didDispatch be false.
Let targetStatic be isStaticM(peTarget).
Inspect peTarget:
When CNewVariant (nvTag, nvNames, nvVals):
Repeat for arm in inspArms:
Inspect arm:
When CWhen (wName, wBindings, wBody):
If wName equals nvTag:
Let mutable bidx be 1.
Let sEnvI be peStaticEnvM(state).
Repeat for b in wBindings:
Let bval be item bidx of nvVals.
Let bkey1 be "{b}".
miniEnvBind(env, bkey1, exprToValM(bval)).
Let bkey2 be "{b}".
miniSeBind(sEnvI, bkey2, (copy of bval)).
Set bidx to bidx + 1.
Let matchedBody be peBlockM(wBody, state).
Repeat for ms in matchedBody:
Push ms to blockResult.
Set didDispatch to true.
Return blockResult.
Otherwise:
Let skip be true.
If not didDispatch:
Repeat for arm in inspArms:
Inspect arm:
When COtherwise (oBody):
Let otherwiseBody be peBlockM(oBody, state).
Repeat for os in otherwiseBody:
Push os to blockResult.
Set didDispatch to true.
Return blockResult.
Otherwise:
Let skip be true.
Otherwise:
Let skip be true.
If not didDispatch:
Let peArms be a new Seq of CMatchArm.
Repeat for arm in inspArms:
Inspect arm:
When CWhen (wName, wBindings, wBody):
Let peBody be peBlockM(wBody, state).
Push (a new CWhen with variantName wName and bindings wBindings and body peBody) to peArms.
When COtherwise (oBody):
Let peBody be peBlockM(oBody, state).
Push (a new COtherwise with body peBody) to peArms.
Push (a new CInspect with target peTarget and arms peArms) to blockResult.
When CRuntimeAssert (raCond, raMsg):
Let peRaCond be peExprM(raCond, state).
Let peRaMsg be peExprM(raMsg, state).
Push (a new CRuntimeAssert with cond peRaCond and msg peRaMsg) to blockResult.
When CHardAssert (raCond, raMsg):
Let peRaCond be peExprM(raCond, state).
Let peRaMsg be peExprM(raMsg, state).
Push (a new CHardAssert with cond peRaCond and msg peRaMsg) to blockResult.
When CGive (giveExpr, giveTarget):
Let peGiveExpr be peExprM(giveExpr, state).
Push (a new CGive with expr peGiveExpr and target giveTarget) to blockResult.
When CEscStmt (escCode):
Push (a new CEscStmt with code escCode) to blockResult.
When CSleep (sleepDur):
Let peSleepDur be peExprM(sleepDur, state).
Push (a new CSleep with duration peSleepDur) to blockResult.
When CReadConsole (rcTarget):
Push (a new CReadConsole with target rcTarget) to blockResult.
When CReadFile (rfPath, rfTarget):
Let peRfPath be peExprM(rfPath, state).
Push (a new CReadFile with path peRfPath and target rfTarget) to blockResult.
When CWriteFile (wfPath, wfContent):
Let peWfPath be peExprM(wfPath, state).
Let peWfContent be peExprM(wfContent, state).
Push (a new CWriteFile with path peWfPath and content peWfContent) to blockResult.
When CCheck (chkPred, chkMsg):
Let peChkPred be peExprM(chkPred, state).
Let peChkMsg be peExprM(chkMsg, state).
Push (a new CCheck with predicate peChkPred and msg peChkMsg) to blockResult.
When CAssert (assertProp):
Let peAssertProp be peExprM(assertProp, state).
Push (a new CAssert with proposition peAssertProp) to blockResult.
When CTrust (trustProp, trustJust):
Let peTrustProp be peExprM(trustProp, state).
Push (a new CTrust with proposition peTrustProp and justification trustJust) to blockResult.
When CRequire (reqDep):
Push (a new CRequire with dependency reqDep) to blockResult.
When CMerge (mergeTarget, mergeOther):
Let peMergeOther be peExprM(mergeOther, state).
Push (a new CMerge with target mergeTarget and other peMergeOther) to blockResult.
When CIncrease (incTarget, incAmount):
Let peIncAmount be peExprM(incAmount, state).
Push (a new CIncrease with target incTarget and amount peIncAmount) to blockResult.
When CDecrease (decTarget, decAmount):
Let peDecAmount be peExprM(decAmount, state).
Push (a new CDecrease with target decTarget and amount peDecAmount) to blockResult.
When CAppendToSeq (asTarget, asValue):
Let peAsValue be peExprM(asValue, state).
Push (a new CAppendToSeq with target asTarget and value peAsValue) to blockResult.
When CResolve (resTarget):
Push (a new CResolve with target resTarget) to blockResult.
When CSync (syncTarget, syncChannel):
Let peSyncChannel be peExprM(syncChannel, state).
Push (a new CSync with target syncTarget and channel peSyncChannel) to blockResult.
When CMount (mountTarget, mountPath):
Let peMountPath be peExprM(mountPath, state).
Push (a new CMount with target mountTarget and path peMountPath) to blockResult.
When CConcurrent (concBranches):
Let peConcBranches be a new Seq of Seq of CStmt.
Repeat for concBranch in concBranches:
Push peBlockM(concBranch, state) to peConcBranches.
Push (a new CConcurrent with branches peConcBranches) to blockResult.
When CParallel (parBranches):
Let peParBranches be a new Seq of Seq of CStmt.
Repeat for parBranch in parBranches:
Push peBlockM(parBranch, state) to peParBranches.
Push (a new CParallel with branches peParBranches) to blockResult.
When CLaunchTask (ltBody, ltHandle):
Let peLtBody be peBlockM(ltBody, state).
Push (a new CLaunchTask with body peLtBody and handle ltHandle) to blockResult.
When CStopTask (stHandle):
Let peStHandle be peExprM(stHandle, state).
Push (a new CStopTask with handle peStHandle) to blockResult.
When CSelect (selBranches):
Let peSelBranches be a new Seq of CSelectBranch.
Repeat for selBr in selBranches:
Inspect selBr:
When CSelectRecv (selChan, selVar, selBody):
Let peSelBody be peBlockM(selBody, state).
Push (a new CSelectRecv with chan selChan and var selVar and body peSelBody) to peSelBranches.
When CSelectTimeout (selDur, selBody):
Let peSelDur be peExprM(selDur, state).
Let peSelBody be peBlockM(selBody, state).
Push (a new CSelectTimeout with duration peSelDur and body peSelBody) to peSelBranches.
Push (a new CSelect with branches peSelBranches) to blockResult.
When CCreatePipe (cpName, cpCapacity):
Let peCpCapacity be peExprM(cpCapacity, state).
Push (a new CCreatePipe with name cpName and capacity peCpCapacity) to blockResult.
When CSendPipe (spPipe, spValue):
Let peSpValue be peExprM(spValue, state).
Push (a new CSendPipe with chan spPipe and value peSpValue) to blockResult.
When CReceivePipe (rpPipe, rpTarget):
Push (a new CReceivePipe with chan rpPipe and target rpTarget) to blockResult.
When CTrySendPipe (tspPipe, tspValue):
Let peTspValue be peExprM(tspValue, state).
Push (a new CTrySendPipe with chan tspPipe and value peTspValue) to blockResult.
When CTryReceivePipe (trpPipe, trpTarget):
Push (a new CTryReceivePipe with chan trpPipe and target trpTarget) to blockResult.
When CSpawn (spawnType, spawnTarget):
Push (a new CSpawn with agentType spawnType and target spawnTarget) to blockResult.
When CSendMessage (smTarget, smMsg):
Let peSmTarget be peExprM(smTarget, state).
Let peSmMsg be peExprM(smMsg, state).
Push (a new CSendMessage with target peSmTarget and msg peSmMsg) to blockResult.
When CStreamMessage (stTarget, stVals):
Let peStTarget be peExprM(stTarget, state).
Let peStVals be peExprM(stVals, state).
Push (a new CStreamMessage with target peStTarget and values peStVals) to blockResult.
When CAwaitMessage (amTarget):
Push (a new CAwaitMessage with target amTarget) to blockResult.
When CListen (listenAddr, listenHandler):
Let peListenAddr be peExprM(listenAddr, state).
Push (a new CListen with addr peListenAddr and handler listenHandler) to blockResult.
When CConnectTo (connAddr, connTarget):
Let peConnAddr be peExprM(connAddr, state).
Push (a new CConnectTo with addr peConnAddr and target connTarget) to blockResult.
When CZone (zoneName, zoneKind, zoneBody):
Let peZoneBody be peBlockM(zoneBody, state).
Push (a new CZone with name zoneName and kind zoneKind and body peZoneBody) to blockResult.
Return blockResult.
## To validateExtractReturnM (result: CExpr) and (bodyStmts: Seq of CStmt) -> CExpr:
Inspect result:
When CVar (retName):
If retName equals "__no_return__":
Return result.
If retName equals "__unresolvable":
Return result.
Repeat for s in bodyStmts:
Inspect s:
When CLet (letName, letExpr):
If letName equals retName:
Return a new CVar with name "__no_return__".
Otherwise:
Let skip be true.
Return result.
Otherwise:
Return result.
## To extractReturnM (stmts: Seq of CStmt) -> CExpr:
Let mutable idx be length of stmts.
While idx is at least 1:
Let s be item idx of stmts.
Inspect s:
When CReturn (retExpr):
Return retExpr.
Otherwise:
Set idx to idx - 1.
Return a new CVar with name "__no_return__".