Migrate PEx expressions to the shared expression-visitor base#953
Merged
Conversation
PExCodeGenerator now extends ExpressionGenerator<CompilationContext>; its WriteExpr switch is replaced by the 31 Write*Expr overrides (bodies moved verbatim). PEx's Nondet/FairNondet shared a switch arm, now routed through a small WriteRandomBool helper to keep the emission identical. With both PChecker and PEx on the base, adding a new IPExpr node forces both to handle it at compile time. Verified: CompilerCore builds clean; PEx-generated Java for tutorials 1-6 is byte-for-byte identical to master (EspressoMachine fails PEx codegen identically on both, due to the pre-existing 'receive in loop' limitation, unrelated to this change).
Contributor
There was a problem hiding this comment.
Pull request overview
Migrates the PEx backend’s expression emission to the shared ExpressionGenerator<TContext> visitor base so expression dispatch is centralized and backend implementations become compile-time exhaustive when new IPExpr nodes are added.
Changes:
- Updated
PExCodeGeneratorto inherit fromExpressionGenerator<CompilationContext>while still implementingICodeGenerator. - Replaced the monolithic
WriteExprswitch with per-nodeWrite*Exproverrides (moved verbatim). - Split
NondetExprandFairNondetExprhandling into separate overrides routed through a sharedWriteRandomBoolhelper.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| protected override void WriteTupleAccessExpr(CompilationContext context, StringWriter output, TupleAccessExpr tupleAccessExpr) | ||
| { | ||
| context.Write(output, $"({GetPExType(tupleAccessExpr.Type)})("); | ||
| var tupleType = tupleAccessExpr.SubExpr.Type.Canonicalize() as TupleType; |
Comment on lines
+1775
to
+1784
| PLanguageType elementType; | ||
| var isMap = PLanguageType.TypeIsOfKind(containsExpr.Collection.Type, TypeKind.Map); | ||
| var isSet = PLanguageType.TypeIsOfKind(containsExpr.Collection.Type, TypeKind.Set); | ||
| if (isMap) | ||
| elementType = ((MapType)containsExpr.Collection.Type.Canonicalize()).KeyType; | ||
| else if (isSet) | ||
| elementType = ((SetType)containsExpr.Collection.Type.Canonicalize()).ElementType; | ||
| else | ||
| elementType = ((SequenceType)containsExpr.Collection.Type.Canonicalize()).ElementType; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Migrates PEx onto
ExpressionGenerator<TContext>(the base merged to master in #950). Phase 2 of consolidating the imperative backends' expression handling.PExCodeGeneratornow extendsExpressionGenerator<CompilationContext>, and itsWriteExprswitch is replaced by the 31Write*Exproverrides, bodies moved verbatim. PEx'sNondetExpr/FairNondetExprpreviously shared one switch arm; they're now two overrides routed through a small privateWriteRandomBoolhelper, so emitted code is unchanged.With PChecker (already on master) and PEx both on the base, adding a new
IPExprnode forces both backends to handle it or fail to compile.Verification (local, .NET 8)
CompilerCorebuilds clean (only the unrelated pre-existingTransformASTPass.cs:796CS0162 warning).diff -rqclean). EspressoMachine fails PEx codegen identically on old and new — the pre-existing "receive in a loop body is not supported" limitation (TransformASTPass.cs:793), unrelated to this change.Note on the old
defaultarmPEx's previous
WriteExprdefaultemitted a/* Skipping expr ... */comment; the shared base throws instead. Since all three imperative backends handle the identical 31-node set, that arm was dead for real input — confirmed by the byte-identical tutorial output.Follow-ups
StatementGenerator<TContext> : ExpressionGenerator<TContext>+ migrate PChecker/PEx statements.WriteExprtakes nocontext/outputparams — writes to instance fields — so it needs a signature change).Generated by Claude Code